restore Directory deletion, albeit without garbage collection
[scpubgit/stemmatology.git] / lib / Text / Tradition / Directory.pm
CommitLineData
8d9a1cd8 1package Text::Tradition::Directory;
2
3use strict;
4use warnings;
5use Moose;
ad1291ee 6use KiokuDB::GC::Naive;
8d9a1cd8 7use KiokuDB::TypeMap;
8use KiokuDB::TypeMap::Entry::Naive;
861c3e27 9use Text::Tradition::Error;
8d9a1cd8 10
11extends 'KiokuX::Model';
12
12523041 13=head1 NAME
14
15Text::Tradition::Directory - a KiokuDB interface for storing and retrieving traditions
16
17=head1 SYNOPSIS
18
19 use Text::Tradition::Directory;
20 my $d = Text::Tradition::Directory->new(
21 'dsn' => 'dbi:SQLite:mytraditions.db',
22 'extra_args' => { 'create' => 1 },
23 );
24
25 my $tradition = Text::Tradition->new( @args );
9ba651b9 26 my $stemma = $tradition->add_stemma( dotfile => $dotfile );
12523041 27 $d->save_tradition( $tradition );
12523041 28
29 foreach my $id ( $d->traditions ) {
30 print $d->tradition( $id )->name;
12523041 31 }
32
33=head1 DESCRIPTION
34
35Text::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.
36
37=head1 METHODS
38
39=head2 new
40
56cf65bd 41Returns a Directory object.
12523041 42
43=head2 tradition_ids
44
45Returns the ID of all traditions in the database.
46
47=head2 tradition( $id )
48
49Returns the Text::Tradition object of the given ID.
50
56cf65bd 51=head2 save( $tradition )
12523041 52
56cf65bd 53Writes the given tradition to the database, returning its ID.
12523041 54
55=begin testing
56
861c3e27 57use TryCatch;
12523041 58use File::Temp;
59use Text::Tradition;
12523041 60use_ok 'Text::Tradition::Directory';
61
62my $fh = File::Temp->new();
63my $file = $fh->filename;
64$fh->close;
65my $dsn = "dbi:SQLite:dbname=$file";
861c3e27 66my $uuid;
12523041 67my $t = Text::Tradition->new(
56cf65bd 68 'name' => 'inline',
69 'input' => 'Tabular',
70 'file' => 't/data/simple.txt',
71 );
56cf65bd 72
861c3e27 73{
74 my $d = Text::Tradition::Directory->new( 'dsn' => $dsn,
75 'extra_args' => { 'create' => 1 } );
76 is( ref $d, 'Text::Tradition::Directory', "Got directory object" );
77
78 my $scope = $d->new_scope;
79 $uuid = $d->save( $t );
80 ok( $uuid, "Saved test tradition" );
81
9ba651b9 82 my $s = $t->add_stemma( dotfile => 't/data/simple.dot' );
861c3e27 83 ok( $d->save( $t ), "Updated tradition with stemma" );
84 is( $d->tradition( $uuid ), $t, "Correct tradition returned for id" );
e0d617e6 85 is( $d->tradition( $uuid )->stemma(0), $s, "...and it has the correct stemma" );
861c3e27 86 try {
87 $d->save( $s );
88 } catch( Text::Tradition::Error $e ) {
89 is( $e->ident, 'database error', "Got exception trying to save stemma directly" );
90 like( $e->message, qr/Cannot directly save non-Tradition object/,
91 "Exception has correct message" );
92 }
93}
94my $nt = Text::Tradition->new(
95 'name' => 'CX',
96 'input' => 'CollateX',
97 'file' => 't/data/Collatex-16.xml',
98 );
99is( ref( $nt ), 'Text::Tradition', "Made new tradition" );
100
101{
102 my $f = Text::Tradition::Directory->new( 'dsn' => $dsn );
103 my $scope = $f->new_scope;
104 is( scalar $f->tradition_ids, 1, "Directory index has our tradition" );
105 my $nuuid = $f->save( $nt );
106 ok( $nuuid, "Stored second tradition" );
107 is( scalar $f->tradition_ids, 2, "Directory index has both traditions" );
108 my $tf = $f->tradition( $uuid );
109 is( $tf->name, $t->name, "Retrieved the tradition from a new directory" );
e0d617e6 110 my $sid = $f->object_to_id( $tf->stemma(0) );
861c3e27 111 try {
112 $f->tradition( $sid );
113 } catch( Text::Tradition::Error $e ) {
114 is( $e->ident, 'database error', "Got exception trying to fetch stemma directly" );
115 like( $e->message, qr/not a Text::Tradition/, "Exception has correct message" );
116 }
117 try {
118 $f->delete( $sid );
119 } catch( Text::Tradition::Error $e ) {
120 is( $e->ident, 'database error', "Got exception trying to delete stemma directly" );
121 like( $e->message, qr/Cannot directly delete non-Tradition object/,
122 "Exception has correct message" );
123 }
124 $f->delete( $uuid );
125 ok( !$f->exists( $uuid ), "Object is deleted from DB" );
126 ok( !$f->exists( $sid ), "Object stemma also deleted from DB" );
127 is( scalar $f->tradition_ids, 1, "Object is deleted from index" );
128}
129
086f445c 130{
861c3e27 131 my $g = Text::Tradition::Directory->new( 'dsn' => $dsn );
132 my $scope = $g->new_scope;
133 is( scalar $g->tradition_ids, 1, "Now one object in new directory index" );
134}
12523041 135
136=end testing
137
138=cut
139
12523041 140has +typemap => (
8d9a1cd8 141 is => 'rw',
142 isa => 'KiokuDB::TypeMap',
143 default => sub {
144 KiokuDB::TypeMap->new(
145 isa_entries => {
8d9a1cd8 146 "Graph" => KiokuDB::TypeMap::Entry::Naive->new,
147 "Graph::AdjacencyMap" => KiokuDB::TypeMap::Entry::Naive->new,
148 }
149 );
150 },
151);
152
861c3e27 153before [ qw/ store update insert delete / ] => sub {
8d9a1cd8 154 my $self = shift;
861c3e27 155 my @nontrad;
156 foreach my $obj ( @_ ) {
157 if( ref( $obj ) && ref( $obj ) ne 'Text::Tradition' ) {
158 # Is it an id => Tradition hash?
159 if( ref( $obj ) eq 'HASH' && keys( %$obj ) == 1 ) {
160 my( $k ) = keys %$obj;
161 next if ref( $obj->{$k} ) eq 'Text::Tradition';
8d9a1cd8 162 }
861c3e27 163 push( @nontrad, $obj );
8d9a1cd8 164 }
12523041 165 }
861c3e27 166 if( @nontrad ) {
167 throw( "Cannot directly save non-Tradition object of type "
168 . ref( $nontrad[0] ) );
169 }
170};
12523041 171
086f445c 172# TODO Garbage collection doesn't work. Suck it up and live with the
173# inflated DB.
174# after delete => sub {
175# my $self = shift;
176# my $gc = KiokuDB::GC::Naive->new( backend => $self->directory->backend );
177# $self->directory->backend->delete( $gc->garbage->members );
178# };
56cf65bd 179
180sub save {
861c3e27 181 my $self = shift;
182 return $self->store( @_ );
12523041 183}
184
56cf65bd 185sub tradition {
186 my( $self, $id ) = @_;
187 my $obj = $self->lookup( $id );
188 unless( ref( $obj ) eq 'Text::Tradition' ) {
861c3e27 189 throw( "Retrieved object is a " . ref( $obj ) . ", not a Text::Tradition" );
12523041 190 }
56cf65bd 191 return $obj;
12523041 192}
8d9a1cd8 193
861c3e27 194sub tradition_ids {
195 my $self = shift;
196 my @ids;
197 $self->scan( sub { push( @ids, $self->object_to_id( @_ ) ) } );
198 return @ids;
199}
200
201sub throw {
202 Text::Tradition::Error->throw(
203 'ident' => 'database error',
204 'message' => $_[0],
205 );
206}
207
8d9a1cd8 2081;
12523041 209
027d819c 210=head1 LICENSE
211
212This package is free software and is provided "as is" without express
213or implied warranty. You can redistribute it and/or modify it under
214the same terms as Perl itself.
215
216=head1 AUTHOR
217
218Tara L Andrews E<lt>aurum@cpan.orgE<gt>