1 package Text::Tradition::Directory;
7 use KiokuDB::TypeMap::Entry::Naive;
9 extends 'KiokuX::Model';
13 Text::Tradition::Directory - a KiokuDB interface for storing and retrieving traditions
17 use Text::Tradition::Directory;
18 my $d = Text::Tradition::Directory->new(
19 'dsn' => 'dbi:SQLite:mytraditions.db',
20 'extra_args' => { 'create' => 1 },
23 my $tradition = Text::Tradition->new( @args );
24 my $stemma = $tradition->add_stemma( $dotfile );
25 $d->save_tradition( $tradition );
27 foreach my $id ( $d->traditions ) {
28 print $d->tradition( $id )->name;
33 Text::Tradition::Directory is an interface for storing and retrieving text traditions and all their data, including an associated stemma hypothesis. It is an instantiation of a KiokuDB::Model, storing traditions and associated stemmas by UUID.
39 Returns a Directory object.
43 Returns the ID of all traditions in the database.
45 =head2 tradition( $id )
47 Returns the Text::Tradition object of the given ID.
49 =head2 save( $tradition )
51 Writes the given tradition to the database, returning its ID.
58 use_ok 'Text::Tradition::Directory';
60 my $fh = File::Temp->new();
61 my $file = $fh->filename;
63 my $dsn = "dbi:SQLite:dbname=$file";
65 my $d = Text::Tradition::Directory->new( 'dsn' => $dsn,
66 'extra_args' => { 'create' => 1 } );
67 is( ref $d, 'Text::Tradition::Directory', "Got directory object" );
69 my $scope = $d->new_scope;
70 my $t = Text::Tradition->new(
73 'file' => 't/data/simple.txt',
75 my $uuid = $d->save( $t );
76 ok( $uuid, "Saved test tradition" );
78 my $s = $t->add_stemma( 't/data/simple.dot' );
79 ok( $d->save( $t ), "Updated tradition with stemma" );
80 is( $d->tradition( $uuid ), $t, "Correct tradition returned for id" );
81 is( $d->tradition( $uuid )->stemma, $s, "...and it has the correct stemma" );
82 warning_like { $d->save( $s ) } qr/not a Text::Tradition/, "Correctly failed to save stemma directly";
84 my $e = Text::Tradition::Directory->new( 'dsn' => $dsn );
85 $scope = $e->new_scope;
86 is( scalar $e->tradition_ids, 1, "Directory index has our tradition" );
87 my $te = $e->tradition( $uuid );
88 is( $te->name, $t->name, "Retrieved the tradition from a new directory" );
89 my $sid = $e->object_to_id( $te->stemma );
90 warning_like { $e->tradition( $sid ) } qr/not a Text::Tradition/, "Did not retrieve stemma via tradition call";
91 warning_like { $e->delete( $sid ) } qr/Cannot directly delete non-Tradition object/, "Stemma object not deleted from DB";
93 ok( !$e->exists( $uuid ), "Object is deleted from DB" );
94 is( scalar $e->tradition_ids, 0, "Object is deleted from index" );
103 isa => 'KiokuDB::TypeMap',
105 KiokuDB::TypeMap->new(
107 "Graph" => KiokuDB::TypeMap::Entry::Naive->new,
108 "Graph::AdjacencyMap" => KiokuDB::TypeMap::Entry::Naive->new,
114 has tradition_index => (
116 isa => 'HashRef[Str]',
119 del_index => 'delete',
121 tradition_ids => 'keys',
123 default => sub { {} },
126 # Populate the tradition index.
129 my $stream = $self->root_set;
130 until( $stream->is_done ) {
131 foreach my $obj ( $stream->items ) {
132 my $uuid = $self->object_to_id( $obj );
133 if( ref( $obj ) eq 'Text::Tradition' ) {
134 $self->add_index( $uuid => $obj->name );
136 warn "Found root object in DB that is not a Text::Tradition";
143 # If a tradition is deleted, remove it from the index.
144 around delete => sub {
147 warn "Only the first object will be deleted" if @_ > 1;
149 my $obj = ref( $arg ) ? $arg : $self->lookup( $arg );
150 my $id = ref( $arg ) ? $self->object_to_id( $arg ) : $arg;
151 unless( ref $obj eq 'Text::Tradition' ) {
152 warn "Cannot directly delete non-Tradition object $obj";
155 $self->$orig( $arg );
156 $self->del_index( $id );
160 my( $self, $obj ) = @_;
161 unless( ref( $obj ) eq 'Text::Tradition' ) {
162 warn "Object $obj is not a Text::Tradition";
165 my $uuid = $self->store( $obj );
166 $self->add_index( $uuid => $obj->name ) if $uuid;
172 my( $self, $id ) = @_;
173 my $obj = $self->lookup( $id );
174 unless( ref( $obj ) eq 'Text::Tradition' ) {
175 warn "Retrieved object is a " . ref( $obj ) . ", not a Text::Tradition";