43 |
Create new normalised database object |
Create new normalised database object |
44 |
|
|
45 |
my $db = new WebPAC::DB( |
my $db = new WebPAC::DB( |
46 |
path = '/path/to/cache/ds/', |
path => '/path/to/cache/ds/', |
47 |
|
read_only => 1, |
48 |
); |
); |
49 |
|
|
50 |
Optional parameter C<path> defines path to directory |
Optional parameter C<path> defines path to directory |
51 |
in which cache file for C<data_structure> call will be created. |
in which cache file for C<data_structure> call will be created. |
52 |
|
|
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>). |
55 |
|
|
56 |
=cut |
=cut |
57 |
|
|
58 |
sub new { |
sub new { |
90 |
} elsif (! -d $dir) { |
} elsif (! -d $dir) { |
91 |
$msg = "is not directory"; |
$msg = "is not directory"; |
92 |
} elsif (! -w $dir) { |
} elsif (! -w $dir) { |
93 |
$msg = "not writable"; |
$msg = "not writable" unless ($self->{'read_only'}); |
94 |
} |
} |
95 |
|
|
96 |
if ($msg) { |
if ($msg) { |
106 |
} |
} |
107 |
} |
} |
108 |
|
|
109 |
=head2 load_gs |
=head2 load_ds |
110 |
|
|
111 |
Retrive from disk one data_structure records using field 000 as key |
Retrive from disk one data_structure records using field 000 as key |
112 |
|
|
113 |
my @ds = $db->load_gs($rec); |
my $ds = $db->load_ds($rec); |
114 |
|
|
115 |
This function will also perform basic sanity checking on returned |
This function will also perform basic sanity checking on returned |
116 |
data and disable caching if data is corrupted (or changed since last |
data and disable caching if data is corrupted (or changed since last |
117 |
update). |
update). |
118 |
|
|
119 |
Returns array or undef if cacheing is disabled or unavailable. |
Returns hash or undef if cacheing is disabled or unavailable. |
120 |
|
|
121 |
=cut |
=cut |
122 |
|
|
123 |
sub load_gs { |
sub load_ds { |
124 |
my $self = shift; |
my $self = shift; |
125 |
|
|
126 |
return unless $self->{'path'}; |
return unless $self->{'path'}; |
131 |
|
|
132 |
my $cache_path = $self->{'path'}; |
my $cache_path = $self->{'path'}; |
133 |
|
|
134 |
my $id = $rec->{'000'}; |
my $id = $rec; |
135 |
$id = $rec->{'000'}->[0] if ($id =~ m/^ARRAY/o); |
$id = $rec->{'000'} if (ref($id) eq 'HASH'); |
136 |
|
$id = $rec->{'000'}->[0] if (ref($id) eq 'ARRAY'); |
137 |
|
|
138 |
unless (defined($id)) { |
unless (defined($id)) { |
139 |
$log->warn("Can't use cacheing on records without unique identifier in field 000"); |
$log->warn("Can't use cacheing on records without unique identifier in field 000"); |
154 |
} |
} |
155 |
}; |
}; |
156 |
if ($ok && $ds_ref->{'ds'}) { |
if ($ok && $ds_ref->{'ds'}) { |
157 |
return @{ $ds_ref->{'ds'} }; |
return $ds_ref->{'ds'}; |
158 |
} else { |
} else { |
159 |
$log->warn("cache entry $cache_path corrupt. Use rm $cache_path/* to re-create it on next run!"); |
$log->warn("cache entry $cache_path corrupt. Use rm $cache_path/* to re-create it on next run!"); |
160 |
undef $self->{'path'}; |
undef $self->{'path'}; |
161 |
} |
} |
162 |
} |
} |
163 |
|
} else { |
164 |
|
return undef; |
165 |
} |
} |
166 |
} |
} |
167 |
|
|
168 |
return undef; |
return undef; |
169 |
} |
} |
170 |
|
|
171 |
=head2 save_gs |
=head2 save_ds |
172 |
|
|
173 |
Store data_structure on disk. |
Store data_structure on disk. |
174 |
|
|
175 |
$db->save_gs( |
$db->save_ds( |
176 |
ds => \@ds, |
ds => $ds, |
177 |
current_filename => $self->{'current_filename'}, |
current_filename => $self->{'current_filename'}, |
178 |
headline => $self->{'headline'}, |
headline => $self->{'headline'}, |
179 |
); |
); |
180 |
|
|
181 |
B<Totally broken, but fast.> |
B<Totally broken, but fast.> |
182 |
|
|
183 |
Depends on filename generated by C<load_gs>. |
Depends on filename generated by C<load_ds>. |
184 |
|
|
185 |
=cut |
=cut |
186 |
|
|
187 |
sub save_gs { |
sub save_ds { |
188 |
my $self = shift; |
my $self = shift; |
189 |
|
|
190 |
|
die "can't write to database in read_only mode!" if ($self->{'read_only'}); |
191 |
|
|
192 |
return unless($self->{'path'}); |
return unless($self->{'path'}); |
193 |
return unless (@_); |
return unless (@_); |
194 |
|
|
196 |
|
|
197 |
my $log = $self->_get_logger; |
my $log = $self->_get_logger; |
198 |
|
|
199 |
$log->logdie("save_gs without cache_file -- did you called gs_get before?") unless ($self->{'cache_file'}); |
$log->logdie("save_ds without cache_file -- did you called gs_get before?") unless ($self->{'cache_file'}); |
200 |
|
|
201 |
|
$log->logdie("need ds") unless ($arg->{ds}); |
202 |
|
|
203 |
foreach my $e (qw/ds current_filename headline/) { |
foreach my $e (qw/current_filename headline/) { |
204 |
$log->logdie("missing $e") unless $arg->{$e}; |
my $mfn = $arg->{ds}->{000}->[0] || '?'; |
205 |
|
$log->warn("missing $e in record $mfn") unless $arg->{$e}; |
206 |
} |
} |
207 |
|
|
208 |
$log->debug("creating storable cache file ",$self->{'cache_file'}); |
$log->debug("creating storable cache file ",$self->{'cache_file'}); |