1 package Text::Tradition::Directory;
7 use Encode qw/ decode_utf8 /;
8 use KiokuDB::GC::Naive;
10 use KiokuDB::TypeMap::Entry::Naive;
11 use Text::Tradition::Error;
13 extends 'KiokuX::Model';
17 Text::Tradition::Directory - a KiokuDB interface for storing and retrieving traditions
21 use Text::Tradition::Directory;
22 my $d = Text::Tradition::Directory->new(
23 'dsn' => 'dbi:SQLite:mytraditions.db',
24 'extra_args' => { 'create' => 1 },
27 my $tradition = Text::Tradition->new( @args );
28 my $stemma = $tradition->add_stemma( dotfile => $dotfile );
29 $d->save_tradition( $tradition );
31 foreach my $id ( $d->traditions ) {
32 print $d->tradition( $id )->name;
37 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.
43 Returns a Directory object.
47 Returns a hashref mapping of ID => name for all traditions in the directory.
49 =head2 tradition( $id )
51 Returns the Text::Tradition object of the given ID.
53 =head2 save( $tradition )
55 Writes the given tradition to the database, returning its ID.
57 =head2 delete( $tradition )
59 Deletes the given tradition object from the database.
60 WARNING!! Garbage collection does not yet work. Use this sparingly.
67 use_ok 'Text::Tradition::Directory';
69 my $fh = File::Temp->new();
70 my $file = $fh->filename;
72 my $dsn = "dbi:SQLite:dbname=$file";
74 my $t = Text::Tradition->new(
77 'file' => 't/data/simple.txt',
81 my $d = Text::Tradition::Directory->new( 'dsn' => $dsn,
82 'extra_args' => { 'create' => 1 } );
83 is( ref $d, 'Text::Tradition::Directory', "Got directory object" );
85 my $scope = $d->new_scope;
86 $uuid = $d->save( $t );
87 ok( $uuid, "Saved test tradition" );
89 my $s = $t->add_stemma( dotfile => 't/data/simple.dot' );
90 ok( $d->save( $t ), "Updated tradition with stemma" );
91 is( $d->tradition( $uuid ), $t, "Correct tradition returned for id" );
92 is( $d->tradition( $uuid )->stemma(0), $s, "...and it has the correct stemma" );
95 } catch( Text::Tradition::Error $e ) {
96 is( $e->ident, 'database error', "Got exception trying to save stemma directly" );
97 like( $e->message, qr/Cannot directly save non-Tradition object/,
98 "Exception has correct message" );
101 my $nt = Text::Tradition->new(
103 'input' => 'CollateX',
104 'file' => 't/data/Collatex-16.xml',
106 is( ref( $nt ), 'Text::Tradition', "Made new tradition" );
109 my $f = Text::Tradition::Directory->new( 'dsn' => $dsn );
110 my $scope = $f->new_scope;
111 is( scalar $f->traditionlist, 1, "Directory index has our tradition" );
112 my $nuuid = $f->save( $nt );
113 ok( $nuuid, "Stored second tradition" );
114 my @tlist = $f->traditionlist;
115 is( scalar @tlist, 2, "Directory index has both traditions" );
116 my $tf = $f->tradition( $uuid );
117 my( $tlobj ) = grep { $_->{'id'} eq $uuid } @tlist;
118 is( $tlobj->{'name'}, $tf->name, "Directory index has correct tradition name" );
119 is( $tf->name, $t->name, "Retrieved the tradition from a new directory" );
120 my $sid = $f->object_to_id( $tf->stemma(0) );
122 $f->tradition( $sid );
123 } catch( Text::Tradition::Error $e ) {
124 is( $e->ident, 'database error', "Got exception trying to fetch stemma directly" );
125 like( $e->message, qr/not a Text::Tradition/, "Exception has correct message" );
129 } catch( Text::Tradition::Error $e ) {
130 is( $e->ident, 'database error', "Got exception trying to delete stemma directly" );
131 like( $e->message, qr/Cannot directly delete non-Tradition object/,
132 "Exception has correct message" );
135 ok( !$f->exists( $uuid ), "Object is deleted from DB" );
136 ok( !$f->exists( $sid ), "Object stemma also deleted from DB" );
137 is( scalar $f->traditionlist, 1, "Object is deleted from index" );
141 my $g = Text::Tradition::Directory->new( 'dsn' => $dsn );
142 my $scope = $g->new_scope;
143 is( scalar $g->traditionlist, 1, "Now one object in new directory index" );
152 isa => 'KiokuDB::TypeMap',
154 KiokuDB::TypeMap->new(
156 "Graph" => KiokuDB::TypeMap::Entry::Naive->new,
157 "Graph::AdjacencyMap" => KiokuDB::TypeMap::Entry::Naive->new,
163 # Push some columns into the extra_args
164 around BUILDARGS => sub {
173 if( $args->{'dsn'} =~ /^dbi/ ) { # We're using Backend::DBI
174 my @column_args = ( 'columns',
175 [ 'name' => { 'data_type' => 'varchar', 'is_nullable' => 1 } ] );
176 my $ea = $args->{'extra_args'};
177 if( ref( $ea ) eq 'ARRAY' ) {
178 push( @$ea, @column_args );
179 } elsif( ref( $ea ) eq 'HASH' ) {
180 $ea = { %$ea, @column_args };
182 $ea = { @column_args };
184 $args->{'extra_args'} = $ea;
186 return $class->$orig( $args );
189 before [ qw/ store update insert delete / ] => sub {
192 foreach my $obj ( @_ ) {
193 if( ref( $obj ) && ref( $obj ) ne 'Text::Tradition' ) {
194 # Is it an id => Tradition hash?
195 if( ref( $obj ) eq 'HASH' && keys( %$obj ) == 1 ) {
196 my( $k ) = keys %$obj;
197 next if ref( $obj->{$k} ) eq 'Text::Tradition';
199 push( @nontrad, $obj );
203 throw( "Cannot directly save non-Tradition object of type "
204 . ref( $nontrad[0] ) );
208 # TODO Garbage collection doesn't work. Suck it up and live with the
210 # after delete => sub {
212 # my $gc = KiokuDB::GC::Naive->new( backend => $self->directory->backend );
213 # $self->directory->backend->delete( $gc->garbage->members );
218 return $self->store( @_ );
222 my( $self, $id ) = @_;
223 my $obj = $self->lookup( $id );
224 unless( ref( $obj ) eq 'Text::Tradition' ) {
225 throw( "Retrieved object is a " . ref( $obj ) . ", not a Text::Tradition" );
232 # If we are using DBI, we can do it the easy way; if not, the hard way.
233 # Easy way still involves making a separate DBI connection. Ew.
235 if( $self->dsn =~ /^dbi:(\w+):/ ) {
237 my @connection = @{$self->directory->backend->connect_info};
238 # Get rid of KiokuDB-specific arg
239 pop @connection if scalar @connection > 4;
240 $connection[3]->{'sqlite_unicode'} = 1 if $dbtype eq 'SQLite';
241 $connection[3]->{'pg_enable_utf8'} = 1 if $dbtype eq '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 my( $id, $name ) = @row;
247 # Horrible horrible hack
248 $name = decode_utf8( $name ) if $dbtype eq 'mysql';
249 push( @tlist, { 'id' => $row[0], 'name' => $row[1] } );
252 $self->scan( sub { my $o = shift;
253 push( @tlist, { 'id' => $self->object_to_id( $o ),
254 'name' => $o->name } ) } );
260 Text::Tradition::Error->throw(
261 'ident' => 'database error',
270 This package is free software and is provided "as is" without express
271 or implied warranty. You can redistribute it and/or modify it under
272 the same terms as Perl itself.
276 Tara L Andrews E<lt>aurum@cpan.orgE<gt>