6 use base 'WebPAC::Common';
11 WebPAC::DB - Store normalized data on disk
19 our $VERSION = '0.01';
23 This module provides disk storage for normalised data.
25 It is newest component of WebPAC, so it will change quite often or be in
26 flux. However, I will try to keep backward compatiblity by providing
29 This has additional advantage. I can create single place to plugin other
30 file formats which provide better performance for particular type of data.
32 For now, this is a prototype version.
36 my $foo = WebPAC::DB->new();
43 Create new normalised database object
45 my $db = new WebPAC::DB(
46 path => '/path/to/cache/ds/',
50 Optional parameter C<path> defines path to directory
51 in which cache file for C<data_structure> call will be created.
53 If called with C<read_only> it will not disable caching if
54 called without write permission (but will die on C<save_ds>).
63 $self->path( $self->{'path'} );
65 $self ? return $self : return undef;
70 Check if specified cache directory exist, and if not, disable caching.
72 $db->path('./cache/ds/');
74 If you pass false or zero value to this function, it will disable
84 my $log = $self->_get_logger();
89 $msg = "doesn't exist";
91 $msg = "is not directory";
93 $msg = "not writable" unless ($self->{'read_only'});
97 $log->warn("cache path $dir $msg, disabling...");
98 undef $self->{'path'};
100 $log->debug("using cache dir $dir");
101 $self->{'path'} = $dir;
104 $log->debug("disabling cache");
105 undef $self->{'path'};
111 Retrive from disk one data_structure records using field 000 as key
113 my $ds = $db->load_ds($rec);
115 This function will also perform basic sanity checking on returned
116 data and disable caching if data is corrupted (or changed since last
119 Returns hash or undef if cacheing is disabled or unavailable.
126 return unless $self->{'path'};
128 my $rec = shift || return;
130 my $log = $self->_get_logger;
132 my $cache_path = $self->{'path'};
135 $id = $rec->{'000'} if (ref($id) eq 'HASH');
136 $id = $rec->{'000'}->[0] if (ref($id) eq 'ARRAY');
138 unless (defined($id)) {
139 $log->warn("Can't use cacheing on records without unique identifier in field 000");
140 undef $self->{'path'};
142 my $cache_file = "$cache_path/$id";
143 $self->{'cache_file'} = $cache_file;
144 if (-r $cache_file) {
145 my $ds_ref = retrieve($cache_file);
147 $log->debug("cache hit: $cache_file");
149 foreach my $f (qw(current_filename headline)) {
151 $self->{$f} = $ds_ref->{$f};
156 if ($ok && $ds_ref->{'ds'}) {
157 return $ds_ref->{'ds'};
159 $log->warn("cache entry $cache_path corrupt. Use rm $cache_path/* to re-create it on next run!");
160 undef $self->{'path'};
173 Store data_structure on disk.
177 current_filename => $self->{'current_filename'},
178 headline => $self->{'headline'},
181 B<Totally broken, but fast.>
183 Depends on filename generated by C<load_ds>.
190 die "can't write to database in read_only mode!" if ($self->{'read_only'});
192 return unless($self->{'path'});
197 my $log = $self->_get_logger;
199 $log->logdie("save_ds without cache_file -- did you called gs_get before?") unless ($self->{'cache_file'});
201 $log->logdie("need ds") unless ($arg->{ds});
203 foreach my $e (qw/current_filename headline/) {
204 my $mfn = $arg->{ds}->{000}->[0] || '?';
205 $log->warn("missing $e in record $mfn") unless $arg->{$e};
208 $log->debug("creating storable cache file ",$self->{'cache_file'});
212 current_filename => $arg->{'current_filename'},
213 headline => $arg->{'headline'},
214 }, $self->{'cache_file'};
220 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
222 =head1 COPYRIGHT & LICENSE
224 Copyright 2005 Dobrica Pavlinusic, All Rights Reserved.
226 This program is free software; you can redistribute it and/or modify it
227 under the same terms as Perl itself.
231 1; # End of WebPAC::DB