1 package Text::Tradition::Directory;
7 use KiokuDB::GC::Naive;
9 use KiokuDB::TypeMap::Entry::Naive;
10 use Text::Tradition::Error;
12 extends 'KiokuX::Model';
16 Text::Tradition::Directory - a KiokuDB interface for storing and retrieving traditions
20 use Text::Tradition::Directory;
21 my $d = Text::Tradition::Directory->new(
22 'dsn' => 'dbi:SQLite:mytraditions.db',
23 'extra_args' => { 'create' => 1 },
26 my $tradition = Text::Tradition->new( @args );
27 my $stemma = $tradition->add_stemma( dotfile => $dotfile );
28 $d->save_tradition( $tradition );
30 foreach my $id ( $d->traditions ) {
31 print $d->tradition( $id )->name;
36 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.
42 Returns a Directory object.
46 Returns a hashref mapping of ID => name for all traditions in the directory.
48 =head2 tradition( $id )
50 Returns the Text::Tradition object of the given ID.
52 =head2 save( $tradition )
54 Writes the given tradition to the database, returning its ID.
56 =head2 delete( $tradition )
58 Deletes the given tradition object from the database.
59 WARNING!! Garbage collection does not yet work. Use this sparingly.
66 use_ok 'Text::Tradition::Directory';
68 my $fh = File::Temp->new();
69 my $file = $fh->filename;
71 my $dsn = "dbi:SQLite:dbname=$file";
73 my $t = Text::Tradition->new(
76 'file' => 't/data/simple.txt',
80 my $d = Text::Tradition::Directory->new( 'dsn' => $dsn,
81 'extra_args' => { 'create' => 1 } );
82 is( ref $d, 'Text::Tradition::Directory', "Got directory object" );
84 my $scope = $d->new_scope;
85 $uuid = $d->save( $t );
86 ok( $uuid, "Saved test tradition" );
88 my $s = $t->add_stemma( dotfile => 't/data/simple.dot' );
89 ok( $d->save( $t ), "Updated tradition with stemma" );
90 is( $d->tradition( $uuid ), $t, "Correct tradition returned for id" );
91 is( $d->tradition( $uuid )->stemma(0), $s, "...and it has the correct stemma" );
94 } catch( Text::Tradition::Error $e ) {
95 is( $e->ident, 'database error', "Got exception trying to save stemma directly" );
96 like( $e->message, qr/Cannot directly save non-Tradition object/,
97 "Exception has correct message" );
100 my $nt = Text::Tradition->new(
102 'input' => 'CollateX',
103 'file' => 't/data/Collatex-16.xml',
105 is( ref( $nt ), 'Text::Tradition', "Made new tradition" );
108 my $f = Text::Tradition::Directory->new( 'dsn' => $dsn );
109 my $scope = $f->new_scope;
110 is( scalar $f->traditionlist, 1, "Directory index has our tradition" );
111 my $nuuid = $f->save( $nt );
112 ok( $nuuid, "Stored second tradition" );
113 my @tlist = $f->traditionlist;
114 is( scalar @tlist, 2, "Directory index has both traditions" );
115 my $tf = $f->tradition( $uuid );
116 my( $tlobj ) = grep { $_->{'id'} eq $uuid } @tlist;
117 is( $tlobj->{'name'}, $tf->name, "Directory index has correct tradition name" );
118 is( $tf->name, $t->name, "Retrieved the tradition from a new directory" );
119 my $sid = $f->object_to_id( $tf->stemma(0) );
121 $f->tradition( $sid );
122 } catch( Text::Tradition::Error $e ) {
123 is( $e->ident, 'database error', "Got exception trying to fetch stemma directly" );
124 like( $e->message, qr/not a Text::Tradition/, "Exception has correct message" );
128 } catch( Text::Tradition::Error $e ) {
129 is( $e->ident, 'database error', "Got exception trying to delete stemma directly" );
130 like( $e->message, qr/Cannot directly delete non-Tradition object/,
131 "Exception has correct message" );
134 ok( !$f->exists( $uuid ), "Object is deleted from DB" );
135 ok( !$f->exists( $sid ), "Object stemma also deleted from DB" );
136 is( scalar $f->traditionlist, 1, "Object is deleted from index" );
140 my $g = Text::Tradition::Directory->new( 'dsn' => $dsn );
141 my $scope = $g->new_scope;
142 is( scalar $g->traditionlist, 1, "Now one object in new directory index" );
151 isa => 'KiokuDB::TypeMap',
153 KiokuDB::TypeMap->new(
155 "Graph" => KiokuDB::TypeMap::Entry::Naive->new,
156 "Graph::AdjacencyMap" => KiokuDB::TypeMap::Entry::Naive->new,
162 # Push some columns into the extra_args
163 around BUILDARGS => sub {
172 if( $args->{'dsn'} =~ /^dbi/ ) { # We're using Backend::DBI
173 my @column_args = ( 'columns',
174 [ 'name' => { 'data_type' => 'varchar', 'is_nullable' => 1 } ] );
175 my $ea = $args->{'extra_args'};
176 if( ref( $ea ) eq 'ARRAY' ) {
177 push( @$ea, @column_args );
178 } elsif( ref( $ea ) eq 'HASH' ) {
179 $ea = { %$ea, @column_args };
181 $ea = { @column_args };
183 $args->{'extra_args'} = $ea;
185 return $class->$orig( $args );
188 before [ qw/ store update insert delete / ] => sub {
191 foreach my $obj ( @_ ) {
192 if( ref( $obj ) && ref( $obj ) ne 'Text::Tradition' ) {
193 # Is it an id => Tradition hash?
194 if( ref( $obj ) eq 'HASH' && keys( %$obj ) == 1 ) {
195 my( $k ) = keys %$obj;
196 next if ref( $obj->{$k} ) eq 'Text::Tradition';
198 push( @nontrad, $obj );
202 throw( "Cannot directly save non-Tradition object of type "
203 . ref( $nontrad[0] ) );
207 # TODO Garbage collection doesn't work. Suck it up and live with the
209 # after delete => sub {
211 # my $gc = KiokuDB::GC::Naive->new( backend => $self->directory->backend );
212 # $self->directory->backend->delete( $gc->garbage->members );
217 return $self->store( @_ );
221 my( $self, $id ) = @_;
222 my $obj = $self->lookup( $id );
223 unless( ref( $obj ) eq 'Text::Tradition' ) {
224 throw( "Retrieved object is a " . ref( $obj ) . ", not a Text::Tradition" );
231 # If we are using DBI, we can do it the easy way; if not, the hard way.
232 # Easy way still involves making a separate DBI connection. Ew.
234 if( $self->dsn =~ /^dbi/ ) {
236 my @connection = @{$self->directory->backend->connect_info};
237 # Get rid of KiokuDB-specific arg
238 pop @connection if scalar @connection > 4;
239 $connection[3]->{'sqlite_unicode'} = 1 if $connection[0] =~ /^dbi:SQLite/;
240 $connection[3]->{'mysql_enable_utf8'} = 1 if $connection[0] =~ /^dbi:mysql/;
241 $connection[3]->{'pg_enable_utf8'} = 1 if $connection[0] =~ /^dbi:Pg/;
242 my $dbh = DBI->connect( @connection );
243 my $q = $dbh->prepare( 'SELECT id, name from entries WHERE class = "Text::Tradition"' );
245 while( my @row = $q->fetchrow_array ) {
246 push( @tlist, { 'id' => $row[0], 'name' => $row[1] } );
249 $self->scan( sub { my $o = shift;
250 push( @tlist, { 'id' => $self->object_to_id( $o ),
251 'name' => $o->name } ) } );
257 Text::Tradition::Error->throw(
258 'ident' => 'database error',
267 This package is free software and is provided "as is" without express
268 or implied warranty. You can redistribute it and/or modify it under
269 the same terms as Perl itself.
273 Tara L Andrews E<lt>aurum@cpan.orgE<gt>