/[webpac2]/trunk/lib/WebPAC/Store.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/WebPAC/Store.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 218 - (show annotations)
Mon Dec 5 17:48:00 2005 UTC (18 years, 4 months ago) by dpavlin
File size: 5310 byte(s)
 r11539@llin:  dpavlin | 2005-12-05 16:18:58 +0100
 WebPAC::Store now uses prefix in load_ds and save_ds and requires database
 when created with new [0.06]

1 package WebPAC::Store;
2
3 use warnings;
4 use strict;
5
6 use base 'WebPAC::Common';
7 use Storable;
8 use File::Path;
9 use Data::Dumper;
10
11 =head1 NAME
12
13 WebPAC::Store - Store normalized data on disk
14
15 =head1 VERSION
16
17 Version 0.06
18
19 =cut
20
21 our $VERSION = '0.06';
22
23 =head1 SYNOPSIS
24
25 This module provides disk storage for normalised data.
26
27 It is newest component of WebPAC, so it will change quite often or be in
28 flux. However, I will try to keep backward compatiblity by providing
29 multiple back-ends.
30
31 This has additional advantage. I can create single place to plugin other
32 file formats which provide better performance for particular type of data.
33
34 For now, this is a prototype version.
35
36 use WebPAC::Store;
37
38 my $foo = WebPAC::Store->new();
39 ...
40
41 =head1 FUNCTIONS
42
43 =head2 new
44
45 Create new normalised database object
46
47 my $db = new WebPAC::Store(
48 path => '/path/to/cache/ds/',
49 database => 'name',
50 read_only => 1,
51 );
52
53 Optional parameter C<path> defines path to directory
54 in which cache file for C<data_structure> call will be created.
55
56 If called with C<read_only> it will not disable caching if
57 called without write permission (but will die on C<save_ds>).
58
59 Mandatory parametar C<database> is used as subdirectory in database directory.
60
61 =cut
62
63 sub new {
64 my $class = shift;
65 my $self = {@_};
66 bless($self, $class);
67
68 my $log = $self->_get_logger();
69
70 foreach my $p (qw/path database/) {
71 $log->logconfess("need $p") unless ($self->{$p});
72 }
73
74 $self->path( $self->{'path'} );
75
76 $self ? return $self : return undef;
77 }
78
79 =head2 path
80
81 Check if specified cache directory exist, and if not, disable caching.
82
83 $db->path('./cache/ds/');
84
85 If you pass false or zero value to this function, it will disable
86 cacheing.
87
88 You can also example C<< $db->{path} >> to get current cache path.
89
90 =cut
91
92 sub path {
93 my $self = shift;
94
95 my $dir = shift;
96
97 my $log = $self->_get_logger();
98
99 if ($dir) {
100 my $msg;
101 if (! -e $dir) {
102 if ($self->{'read_only'}) {
103 $msg = "doesn't exist";
104 } else {
105 $log->info("creating $dir");
106 mkpath $dir;
107 }
108 } elsif (! -d $dir) {
109 $msg = "is not directory";
110 } elsif (! -w $dir) {
111 $msg = "not writable" unless ($self->{'read_only'});
112 }
113
114 if ($msg) {
115 $log->warn("cache path $dir $msg, disabling...");
116 undef $self->{'path'};
117 } else {
118 $log->debug("using cache dir $dir");
119 $self->{'path'} = $dir;
120 }
121 } else {
122 $log->debug("disabling cache");
123 undef $self->{'path'};
124 }
125 }
126
127 =head2 load_ds
128
129 Retrive from disk one data_structure records usually using field 000 as key
130
131 my $ds = $db->load_ds( id => 42, prefix => 'name' );
132
133 This function will also perform basic sanity checking on returned
134 data and disable caching if data is corrupted (or changed since last
135 update).
136
137 C<prefix> is used to differenciate different source input databases
138 which are indexed in same database.
139
140 Returns hash or undef if cacheing is disabled or unavailable.
141
142 =cut
143
144 sub load_ds {
145 my $self = shift;
146
147 my $log = $self->_get_logger;
148
149 my $cache_path = $self->{'path'};
150
151 if (! $cache_path) {
152 $log->warn("path not set, ignoring load_ds");
153 return;
154 }
155
156 $log->logconfess("arguments for load_ds must be HASH") unless (ref(\@_) eq 'ARRAY' && ($#_ % 2 == 1));
157
158 my $args = {@_};
159 my $id = $args->{id};
160
161 $log->logconfess("got hash, but without id") unless (defined($id));
162 $log->logconfess("got hash, but id [$id] isn't number") unless ($id =~ /^\d+$/);
163
164 my $database = $self->{database};
165 my $prefix = $args->{prefix} || '';
166
167 $log->logconfess("can't find database name") unless ($database);
168
169 my $cache_file = "$cache_path/$prefix/$database#$id";
170 $cache_file =~ s#//#/#go;
171
172 open(my $fh, '>>', '/tmp/foo');
173 print $fh "$cache_file\n";
174 close($fh);
175
176 $log->debug("using cache_file $cache_file");
177
178 if (-r $cache_file) {
179 my $ds_ref = retrieve($cache_file);
180 if ($ds_ref) {
181 $log->debug("cache hit: $cache_file");
182 if ($ds_ref->{'ds'}) {
183 return $ds_ref->{'ds'};
184 } else {
185 $log->warn("cache entry $cache_file corrupt. Use rm $cache_path/* to re-create it on next run!");
186 undef $self->{'path'};
187 }
188 }
189 } else {
190 #$log->warn("cache entry $cache_file doesn't exist");
191 return undef;
192 }
193
194 return undef;
195 }
196
197 =head2 save_ds
198
199 Store data_structure on disk.
200
201 $db->save_ds(
202 id => $ds->{000}->[0],
203 prefix => 'name',
204 ds => $ds,
205 );
206
207 B<Totally broken, but fast.>
208
209 Depends on filename generated by C<load_ds>.
210
211 =cut
212
213 sub save_ds {
214 my $self = shift;
215
216 die "can't write to database in read_only mode!" if ($self->{'read_only'});
217
218 return unless($self->{'path'});
219
220 my $arg = {@_};
221
222 my $log = $self->_get_logger;
223
224 foreach my $f (qw/id ds/) {
225 $log->logconfess("need $f") unless ($arg->{$f});
226 }
227
228 my $database = $self->{database};
229 $log->logconfess("can't find database name") unless ($database);
230
231 my $prefix = $arg->{prefix} || '';
232
233 my $cache_file = $self->{path} . "/$prefix/$database#" . $arg->{id};
234 $cache_file =~ s#//#/#go;
235
236 $log->debug("creating storable cache file $cache_file");
237
238 return store {
239 ds => $arg->{ds},
240 id => $arg->{id},
241 }, $cache_file;
242
243 }
244
245 =head1 AUTHOR
246
247 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
248
249 =head1 COPYRIGHT & LICENSE
250
251 Copyright 2005 Dobrica Pavlinusic, All Rights Reserved.
252
253 This program is free software; you can redistribute it and/or modify it
254 under the same terms as Perl itself.
255
256 =cut
257
258 1; # End of WebPAC::Store

  ViewVC Help
Powered by ViewVC 1.1.26