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/',
49 Optional parameter C<path> defines path to directory
50 in which cache file for C<data_structure> call will be created.
59 $self->path( $self->{'path'} );
61 $self ? return $self : return undef;
66 Check if specified cache directory exist, and if not, disable caching.
68 $db->path('./cache/ds/');
70 If you pass false or zero value to this function, it will disable
80 my $log = $self->_get_logger();
85 $msg = "doesn't exist";
87 $msg = "is not directory";
89 $msg = "not writable";
93 $log->warn("cache path $dir $msg, disabling...");
94 undef $self->{'path'};
96 $log->debug("using cache dir $dir");
97 $self->{'path'} = $dir;
100 $log->debug("disabling cache");
101 undef $self->{'path'};
107 Retrive from disk one data_structure records using field 000 as key
109 my @ds = $db->load_ds($rec);
111 This function will also perform basic sanity checking on returned
112 data and disable caching if data is corrupted (or changed since last
115 Returns array or undef if cacheing is disabled or unavailable.
122 return unless $self->{'path'};
124 my $rec = shift || return;
126 my $log = $self->_get_logger;
128 my $cache_path = $self->{'path'};
130 my $id = $rec->{'000'};
131 $id = $rec->{'000'}->[0] if ($id =~ m/^ARRAY/o);
133 unless (defined($id)) {
134 $log->warn("Can't use cacheing on records without unique identifier in field 000");
135 undef $self->{'path'};
137 my $cache_file = "$cache_path/$id";
138 $self->{'cache_file'} = $cache_file;
139 if (-r $cache_file) {
140 my $ds_ref = retrieve($cache_file);
142 $log->debug("cache hit: $cache_file");
144 foreach my $f (qw(current_filename headline)) {
146 $self->{$f} = $ds_ref->{$f};
151 if ($ok && $ds_ref->{'ds'}) {
152 return @{ $ds_ref->{'ds'} };
154 $log->warn("cache entry $cache_path corrupt. Use rm $cache_path/* to re-create it on next run!");
155 undef $self->{'path'};
166 Store data_structure on disk.
170 current_filename => $self->{'current_filename'},
171 headline => $self->{'headline'},
174 B<Totally broken, but fast.>
176 Depends on filename generated by C<load_ds>.
183 return unless($self->{'path'});
188 my $log = $self->_get_logger;
190 $log->logdie("save_ds without cache_file -- did you called gs_get before?") unless ($self->{'cache_file'});
192 foreach my $e (qw/ds current_filename headline/) {
193 $log->logconfess("missing $e") unless $arg->{$e};
196 $log->debug("creating storable cache file ",$self->{'cache_file'});
200 current_filename => $arg->{'current_filename'},
201 headline => $arg->{'headline'},
202 }, $self->{'cache_file'};
208 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
210 =head1 COPYRIGHT & LICENSE
212 Copyright 2005 Dobrica Pavlinusic, All Rights Reserved.
214 This program is free software; you can redistribute it and/or modify it
215 under the same terms as Perl itself.
219 1; # End of WebPAC::DB