using YAML-based collapser
[scpubgit/stemmatology.git] / lib / Text / Tradition / Directory.pm
CommitLineData
8d9a1cd8 1package Text::Tradition::Directory;
2
3use strict;
4use warnings;
5use Moose;
98a6cab2 6use DBI;
0a900793 7use Encode qw/ decode_utf8 /;
ad1291ee 8use KiokuDB::GC::Naive;
8d9a1cd8 9use KiokuDB::TypeMap;
10use KiokuDB::TypeMap::Entry::Naive;
861c3e27 11use Text::Tradition::Error;
8d9a1cd8 12
13extends 'KiokuX::Model';
14
12523041 15=head1 NAME
16
17Text::Tradition::Directory - a KiokuDB interface for storing and retrieving traditions
18
19=head1 SYNOPSIS
20
21 use Text::Tradition::Directory;
22 my $d = Text::Tradition::Directory->new(
23 'dsn' => 'dbi:SQLite:mytraditions.db',
24 'extra_args' => { 'create' => 1 },
25 );
26
27 my $tradition = Text::Tradition->new( @args );
9ba651b9 28 my $stemma = $tradition->add_stemma( dotfile => $dotfile );
12523041 29 $d->save_tradition( $tradition );
12523041 30
31 foreach my $id ( $d->traditions ) {
32 print $d->tradition( $id )->name;
12523041 33 }
34
35=head1 DESCRIPTION
36
37Text::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.
38
39=head1 METHODS
40
41=head2 new
42
56cf65bd 43Returns a Directory object.
12523041 44
98a6cab2 45=head2 traditionlist
12523041 46
98a6cab2 47Returns a hashref mapping of ID => name for all traditions in the directory.
12523041 48
49=head2 tradition( $id )
50
51Returns the Text::Tradition object of the given ID.
52
56cf65bd 53=head2 save( $tradition )
12523041 54
56cf65bd 55Writes the given tradition to the database, returning its ID.
12523041 56
d7ba60b4 57=head2 delete( $tradition )
58
59Deletes the given tradition object from the database.
60WARNING!! Garbage collection does not yet work. Use this sparingly.
61
12523041 62=begin testing
63
861c3e27 64use TryCatch;
12523041 65use File::Temp;
66use Text::Tradition;
12523041 67use_ok 'Text::Tradition::Directory';
68
69my $fh = File::Temp->new();
70my $file = $fh->filename;
71$fh->close;
72my $dsn = "dbi:SQLite:dbname=$file";
861c3e27 73my $uuid;
12523041 74my $t = Text::Tradition->new(
56cf65bd 75 'name' => 'inline',
76 'input' => 'Tabular',
77 'file' => 't/data/simple.txt',
78 );
56cf65bd 79
861c3e27 80{
81 my $d = Text::Tradition::Directory->new( 'dsn' => $dsn,
82 'extra_args' => { 'create' => 1 } );
83 is( ref $d, 'Text::Tradition::Directory', "Got directory object" );
84
85 my $scope = $d->new_scope;
86 $uuid = $d->save( $t );
87 ok( $uuid, "Saved test tradition" );
88
9ba651b9 89 my $s = $t->add_stemma( dotfile => 't/data/simple.dot' );
861c3e27 90 ok( $d->save( $t ), "Updated tradition with stemma" );
91 is( $d->tradition( $uuid ), $t, "Correct tradition returned for id" );
e0d617e6 92 is( $d->tradition( $uuid )->stemma(0), $s, "...and it has the correct stemma" );
861c3e27 93 try {
94 $d->save( $s );
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" );
99 }
100}
101my $nt = Text::Tradition->new(
102 'name' => 'CX',
103 'input' => 'CollateX',
104 'file' => 't/data/Collatex-16.xml',
105 );
106is( ref( $nt ), 'Text::Tradition', "Made new tradition" );
107
108{
109 my $f = Text::Tradition::Directory->new( 'dsn' => $dsn );
110 my $scope = $f->new_scope;
98a6cab2 111 is( scalar $f->traditionlist, 1, "Directory index has our tradition" );
861c3e27 112 my $nuuid = $f->save( $nt );
113 ok( $nuuid, "Stored second tradition" );
98a6cab2 114 my @tlist = $f->traditionlist;
115 is( scalar @tlist, 2, "Directory index has both traditions" );
861c3e27 116 my $tf = $f->tradition( $uuid );
98a6cab2 117 my( $tlobj ) = grep { $_->{'id'} eq $uuid } @tlist;
118 is( $tlobj->{'name'}, $tf->name, "Directory index has correct tradition name" );
861c3e27 119 is( $tf->name, $t->name, "Retrieved the tradition from a new directory" );
e0d617e6 120 my $sid = $f->object_to_id( $tf->stemma(0) );
861c3e27 121 try {
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" );
126 }
127 try {
128 $f->delete( $sid );
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" );
133 }
ad39942e 134
861c3e27 135 $f->delete( $uuid );
136 ok( !$f->exists( $uuid ), "Object is deleted from DB" );
137 ok( !$f->exists( $sid ), "Object stemma also deleted from DB" );
98a6cab2 138 is( scalar $f->traditionlist, 1, "Object is deleted from index" );
861c3e27 139}
140
d7ba60b4 141{
861c3e27 142 my $g = Text::Tradition::Directory->new( 'dsn' => $dsn );
143 my $scope = $g->new_scope;
98a6cab2 144 is( scalar $g->traditionlist, 1, "Now one object in new directory index" );
ad39942e 145 my $ntobj = $g->tradition( 'CX' );
09909f9d 146 my @w1 = sort { $a->sigil cmp $b->sigil } $ntobj->witnesses;
147 my @w2 = sort{ $a->sigil cmp $b->sigil } $nt->witnesses;
ad39942e 148 is_deeply( \@w1, \@w2, "Looked up remaining tradition by name" );
861c3e27 149}
12523041 150
151=end testing
152
153=cut
f84a7d06 154use Text::Tradition::TypeMap::Entry;
12523041 155
12523041 156has +typemap => (
f84a7d06 157 is => 'rw',
158 isa => 'KiokuDB::TypeMap',
159 default => sub {
160 KiokuDB::TypeMap->new(
161 isa_entries => {
162 "Text::Tradition" =>
163 KiokuDB::TypeMap::Entry::Naive->new(),
164 "Graph" => Text::Tradition::TypeMap::Entry->new(),
165 "Graph::AdjacencyMap" => Text::Tradition::TypeMap::Entry->new()
166 }
167 );
168 },
8d9a1cd8 169);
170
98a6cab2 171# Push some columns into the extra_args
172around BUILDARGS => sub {
173 my $orig = shift;
174 my $class = shift;
175 my $args;
176 if( @_ == 1 ) {
177 $args = $_[0];
178 } else {
179 $args = { @_ };
180 }
181 if( $args->{'dsn'} =~ /^dbi/ ) { # We're using Backend::DBI
182 my @column_args = ( 'columns',
183 [ 'name' => { 'data_type' => 'varchar', 'is_nullable' => 1 } ] );
184 my $ea = $args->{'extra_args'};
185 if( ref( $ea ) eq 'ARRAY' ) {
186 push( @$ea, @column_args );
187 } elsif( ref( $ea ) eq 'HASH' ) {
188 $ea = { %$ea, @column_args };
189 } else {
190 $ea = { @column_args };
191 }
192 $args->{'extra_args'} = $ea;
193 }
194 return $class->$orig( $args );
195};
196
861c3e27 197before [ qw/ store update insert delete / ] => sub {
8d9a1cd8 198 my $self = shift;
861c3e27 199 my @nontrad;
200 foreach my $obj ( @_ ) {
201 if( ref( $obj ) && ref( $obj ) ne 'Text::Tradition' ) {
202 # Is it an id => Tradition hash?
203 if( ref( $obj ) eq 'HASH' && keys( %$obj ) == 1 ) {
204 my( $k ) = keys %$obj;
205 next if ref( $obj->{$k} ) eq 'Text::Tradition';
8d9a1cd8 206 }
861c3e27 207 push( @nontrad, $obj );
8d9a1cd8 208 }
12523041 209 }
861c3e27 210 if( @nontrad ) {
211 throw( "Cannot directly save non-Tradition object of type "
212 . ref( $nontrad[0] ) );
213 }
214};
12523041 215
d7ba60b4 216# TODO Garbage collection doesn't work. Suck it up and live with the
217# inflated DB.
218# after delete => sub {
219# my $self = shift;
220# my $gc = KiokuDB::GC::Naive->new( backend => $self->directory->backend );
221# $self->directory->backend->delete( $gc->garbage->members );
222# };
56cf65bd 223
224sub save {
861c3e27 225 my $self = shift;
226 return $self->store( @_ );
12523041 227}
228
56cf65bd 229sub tradition {
230 my( $self, $id ) = @_;
231 my $obj = $self->lookup( $id );
ad39942e 232 unless( $obj ) {
233 # Try looking up by name.
234 foreach my $item ( $self->traditionlist ) {
235 if( $item->{'name'} eq $id ) {
236 $obj = $self->lookup( $item->{'id'} );
237 last;
238 }
239 }
240 }
241 if( $obj && ref( $obj ) ne 'Text::Tradition' ) {
861c3e27 242 throw( "Retrieved object is a " . ref( $obj ) . ", not a Text::Tradition" );
12523041 243 }
56cf65bd 244 return $obj;
12523041 245}
8d9a1cd8 246
98a6cab2 247sub traditionlist {
861c3e27 248 my $self = shift;
98a6cab2 249 # If we are using DBI, we can do it the easy way; if not, the hard way.
250 # Easy way still involves making a separate DBI connection. Ew.
251 my @tlist;
0a900793 252 if( $self->dsn =~ /^dbi:(\w+):/ ) {
253 my $dbtype = $1;
98a6cab2 254 my @connection = @{$self->directory->backend->connect_info};
255 # Get rid of KiokuDB-specific arg
256 pop @connection if scalar @connection > 4;
0a900793 257 $connection[3]->{'sqlite_unicode'} = 1 if $dbtype eq 'SQLite';
258 $connection[3]->{'pg_enable_utf8'} = 1 if $dbtype eq 'Pg';
98a6cab2 259 my $dbh = DBI->connect( @connection );
260 my $q = $dbh->prepare( 'SELECT id, name from entries WHERE class = "Text::Tradition"' );
261 $q->execute();
262 while( my @row = $q->fetchrow_array ) {
0a900793 263 my( $id, $name ) = @row;
264 # Horrible horrible hack
265 $name = decode_utf8( $name ) if $dbtype eq 'mysql';
98a6cab2 266 push( @tlist, { 'id' => $row[0], 'name' => $row[1] } );
267 }
268 } else {
269 $self->scan( sub { my $o = shift;
270 push( @tlist, { 'id' => $self->object_to_id( $o ),
271 'name' => $o->name } ) } );
272 }
273 return @tlist;
861c3e27 274}
275
276sub throw {
277 Text::Tradition::Error->throw(
278 'ident' => 'database error',
279 'message' => $_[0],
280 );
281}
282
8d9a1cd8 2831;
12523041 284
027d819c 285=head1 LICENSE
286
287This package is free software and is provided "as is" without express
288or implied warranty. You can redistribute it and/or modify it under
289the same terms as Perl itself.
290
291=head1 AUTHOR
292
293Tara L Andrews E<lt>aurum@cpan.orgE<gt>