source: main/trunk/greenstone2/perllib/DBDrivers/70HyphenFormat.pm@ 32012

Last change on this file since 32012 was 30431, checked in by davidb, 8 years ago

Changed the way 'append' is handled, to avoid having 'undefined' in @_ later on in optional args array

File size: 11.6 KB
Line 
1###############################################################################
2#
3# 70HyphenFormat.pm -- The parent class of drivers that use the basic GS format
4# of a text obeying these rules:
5#
6# <line> := <uniqueid> <metadata>+ <separator>
7# <uniqueid> := \[[a-z][a-z0-9]*\]\n
8# <metadata> := <[a-z][a-z0-9]*>(^-{70})+\n
9# <separator> := -{70}\n
10#
11# Contains some utility functions useful to any driver
12# that makes use of this format.
13#
14# A component of the Greenstone digital library software from the New Zealand
15# Digital Library Project at the University of Waikato, New Zealand.
16#
17# Copyright (C) 1999-2015 New Zealand Digital Library Project
18#
19# This program is free software; you can redistribute it and/or modify it under
20# the terms of the GNU General Public License as published by the Free Software
21# Foundation; either version 2 of the License, or (at your option) any later
22# version.
23#
24# This program is distributed in the hope that it will be useful, but WITHOUT
25# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
26# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
27# more details.
28#
29# You should have received a copy of the GNU General Public License along with
30# this program; if not, write to the Free Software Foundation, Inc., 675 Mass
31# Ave, Cambridge, MA 02139, USA.
32#
33###############################################################################
34
35# Note: This driver may be a candidate for further splitting, maybe into a
36# PipedExecutableDriver and a 70HyphenFormatDriver... but for now all piped
37# drivers are 70 hyphen format ones, so, yeah.
38
39package DBDrivers::70HyphenFormat;
40
41# Pragma
42use strict;
43
44# Libraries
45use ghtml;
46use util;
47use FileUtils;
48
49use DBDrivers::BaseDBDriver;
50
51BEGIN {
52 @DBDrivers::70HyphenFormat::ISA = ('DBDrivers::BaseDBDriver');
53}
54
55use constant {
56 RWMODE_READ => '-|',
57 RWMODE_WRITE => '|-',
58};
59
60## @function constructor
61#
62sub new
63{
64 my $class = shift(@_);
65 my $self = DBDrivers::BaseDBDriver->new(@_);
66 $self->{'executable_path'} = 'error';
67 $self->{'keyread_executable'} = 'error';
68 $self->{'read_executable'} = 'error';
69 $self->{'write_executable'} = 'error';
70 #
71 $self->{'forced_affinity'} = -1; # Set to processor number for forced affinity
72 bless($self, $class);
73 return $self;
74}
75## new(void) => 70HyphenFormat ##
76
77
78################################## Protected ##################################
79
80
81## @function close_infodb_write_handle(filehandle)
82#
83sub close_infodb_write_handle
84{
85 my $self = shift(@_);
86 $self->debugPrintFunctionHeader(@_);
87 my $handle = shift(@_);
88 my $force_close = shift(@_); # Undefined most of the time
89 my $continue_close = $self->removeConnectionIfPersistent($handle, $force_close);
90 if ($continue_close) {
91 close($handle);
92 }
93 return;
94}
95## close_infodb_write_handle(filehandle) => void ##
96
97
98## @function delete_infodb_entry(filehandle, string)
99#
100sub delete_infodb_entry
101{
102 my $self = shift(@_);
103 $self->debugPrintFunctionHeader(@_);
104 my $infodb_handle = shift(@_);
105 my $infodb_key = shift(@_);
106 # A minus at the end of a key (after the ]) signifies 'delete'
107 print $infodb_handle '[' . $infodb_key . ']-' . "\n";
108 # The 70 minus signs are also needed, to help make the parsing by db2txt simple
109 print $infodb_handle '-' x 70, "\n";
110}
111## delete_infodb_entry(filehandle, string) => void ##
112
113
114## @function open_infodb_write_handle(string, string)
115#
116sub open_infodb_write_handle
117{
118 my $self = shift(@_);
119 $self->debugPrintFunctionHeader(@_);
120 my $path = shift(@_);
121 my $append = shift(@_);
122 my $infodb_file_handle = $self->retrieveConnectionIfPersistent($path, $append);;
123 # No available existing connection
124 if (!defined $infodb_file_handle || !$infodb_file_handle) {
125 push(@_,$append) if defined $append;
126 $infodb_file_handle = $self->openWriteHandle($path, @_);
127 $self->registerConnectionIfPersistent($infodb_file_handle, $path, $append);
128 }
129 return $infodb_file_handle;
130}
131## open_infodb_write_handle(string, string) => filehandle ##
132
133
134## @function openPipedHandle(integer, string, string, string*) => filehandle
135#
136sub openPipedHandle
137{
138 my $self = shift(@_);
139 my $mode = shift(@_);
140 my $executable_and_default_args = shift(@_);
141 my $infodb_file_path = shift(@_);
142 my ($executable, $default_args) = $executable_and_default_args =~ /^([a-z0-9]+)\s*(.*)$/;
143 my $exe = &FileUtils::filenameConcatenate($self->{'executable_path'}, $executable . &util::get_os_exe());
144 if (!-e $exe) {
145 # Hope it's on path
146 $exe = $executable . &util::get_os_exe();
147 }
148 my $infodb_file_handle = undef;
149 my $cmd = '';
150 if ($self->{'forced_affinity'} >= 0)
151 {
152 $cmd = 'taskset -c ' . $self->{'forced_affinity'} . ' ';
153 }
154 $cmd .= '"' . $exe . '" ' . $default_args;
155 foreach my $open_arg (@_) {
156 # Special - append is typically missing a hyphen
157 if ($open_arg eq 'append') {
158 $open_arg = '-append';
159 }
160 $cmd .= ' ' . $open_arg;
161 }
162 $cmd .= ' "' . $infodb_file_path . '"';
163 $self->debugPrint("CMD: '" . $cmd . "'\n");
164 if(!open($infodb_file_handle, $mode . ':utf8', $cmd)) {
165 print STDERR "Error: Failed to open pipe to '$cmd'\n";
166 print STDERR " $!\n";
167 return undef;
168 }
169 #binmode($infodb_file_handle,":utf8");
170 return $infodb_file_handle;
171}
172## openPipedHandle(integer, string, string, string*) => filehandle ##
173
174
175## @function openReadHandle(string, string) => filehandle
176#
177sub openReadHandle
178{
179 my $self = shift(@_);
180 return $self->openPipedHandle(RWMODE_READ, $self->{'read_executable'}, @_);
181}
182## openReadHandle(string, string) => filehandle
183
184
185## @function openWriteHandle(*) => filehandle
186#
187sub openWriteHandle
188{
189 my $self = shift(@_);
190 return $self->openPipedHandle(RWMODE_WRITE, $self->{'write_executable'}, @_);
191}
192## openWriteHandle(*) => filehandle ##
193
194
195## @function read_infodb_entry(string, string) => hashmap
196#
197sub read_infodb_entry
198{
199 my $self = shift(@_);
200 my $raw_string = $self->read_infodb_rawentry(@_);
201 my $infodb_rec = $self->convert_infodb_string_to_hash($raw_string);
202 return $infodb_rec;
203}
204## read_infodb_entry(string, string) => hashmap ##
205
206
207## @function read_infodb_file(string, hashmap) => void
208#
209sub read_infodb_file
210{
211 my $self = shift(@_);
212 my $infodb_file_path = shift(@_);
213 my $infodb_map = shift(@_);
214 $self->debugPrintFunctionHeader($infodb_file_path, $infodb_map);
215 my $infodb_file_handle = $self->openReadHandle($infodb_file_path);
216 my $infodb_line = "";
217 my $infodb_key = "";
218 my $infodb_value = "";
219 while (defined ($infodb_line = <$infodb_file_handle>)) {
220 $infodb_line =~ s/(\r\n)+$//; # more general than chomp
221 if ($infodb_line =~ /^\[([^\]]+)\]$/) {
222 $infodb_key = $1;
223 }
224 elsif ($infodb_line =~ /^-{70}$/) {
225 $infodb_map->{$infodb_key} = $infodb_value;
226 $infodb_key = "";
227 $infodb_value = "";
228 }
229 else {
230 $infodb_value .= $infodb_line;
231 }
232 }
233 $self->close_infodb_write_handle($infodb_file_handle);
234}
235## read_infodb_file(string, hashmap) => void ##
236
237
238## @function read_infodb_keys(string, hashmap) => void
239#
240sub read_infodb_keys
241{
242 my $self = shift(@_);
243 my $infodb_file_path = shift(@_);
244 my $infodb_map = shift(@_);
245 my $infodb_file_handle = $self->openPipedHandle(RWMODE_READ, $self->{'keyread_executable'}, $infodb_file_path);
246 if (!$infodb_file_handle) {
247 die("Couldn't open pipe from gdbmkeys: " . $infodb_file_path . "\n");
248 }
249 my $infodb_line = "";
250 my $infodb_key = "";
251 my $infodb_value = "";
252 # Simple case - dedicated keyread exe, so keys are strings
253 if ($self->{'keyread_executable'} ne $self->{'read_executable'}) {
254 while (defined ($infodb_line = <$infodb_file_handle>)) {
255 $infodb_line =~ s/[\r\n]+$//;
256 $infodb_map->{$infodb_line} = 1;
257 }
258 }
259 # Slightly more difficult - have to parse keys out of 70hyphen format
260 else {
261 while (defined ($infodb_line = <$infodb_file_handle>)) {
262 if ($infodb_line =~ /^\[([^\]]+)\](-)?[\r\n]*$/) {
263 my $key = $1;
264 my $delete_flag = $2;
265 if (defined $delete_flag) {
266 delete $infodb_map->{$key}
267 }
268 else {
269 $infodb_map->{$key} = 1;
270 }
271 }
272 }
273 }
274 $self->close_infodb_write_handle($infodb_file_handle);
275}
276## read_infodb_keys(string, hashmap) => void ##
277
278
279## @function read_infodb_rawentry(string, string) => string
280#
281# !! TEMPORARY: Slow and naive implementation that just reads the entire file
282# and picks out the one value. This should one day be replaced with database-
283# specific versions that will use dbget etc.
284#
285sub read_infodb_rawentry
286{
287 my $self = shift(@_);
288 my $infodb_file_path = shift(@_);
289 my $infodb_key = shift(@_);
290 # temporary hashmap... we're only interested in one entry
291 my $infodb_map = {};
292 $self->read_infodb_file($infodb_file_path, $infodb_map);
293 return $infodb_map->{$infodb_key};
294}
295## read_infodb_rawentry(string, string) => string ##
296
297
298## @function set_infodb_entry(string, string, hashmap)
299#
300sub set_infodb_entry
301{
302 my $self = shift(@_);
303 my $infodb_file_path = shift(@_);
304 my $infodb_key = shift(@_);
305 my $infodb_map = shift(@_);
306
307 # HTML escape anything that is not part of the "contains" metadata value
308 foreach my $k (keys %$infodb_map) {
309 my @escaped_v = ();
310 foreach my $v (@{$infodb_map->{$k}}) {
311 if ($k eq "contains") {
312 push(@escaped_v, $v);
313 }
314 else {
315 my $ev = &ghtml::unescape_html($v);
316 push(@escaped_v, $ev);
317 }
318 }
319 $infodb_map->{$k} = \@escaped_v;
320 }
321
322 # Generate the record string
323 my $serialized_infodb_map = $self->convert_infodb_hash_to_string($infodb_map);
324
325 # Store it into DB using '... -append' which despite its name actually
326 # replaces the record if it already exists
327 my $status = undef;
328 my $infodb_file_handle = $self->openWriteHandle($infodb_file_path, '-append');
329 if (!$infodb_file_handle) {
330 print STDERR "Error: set_infodb_entry() failed to open pipe to: " . $infodb_file_handle ."\n";
331 print STDERR " $!\n";
332 $status = -1;
333 }
334 else {
335 print $infodb_file_handle "[$infodb_key]\n";
336 print $infodb_file_handle "$serialized_infodb_map\n";
337 $self->close_infodb_write_handle($infodb_file_handle);
338 $status = 0; # as in exit status of cmd OK
339 }
340 return $status;
341}
342## set_infodb_entry(string, string, hashmap) => integer ##
343
344
345## @function write_infodb_entry(filehandle, string, hashmap)
346#
347sub write_infodb_entry
348{
349 my $self = shift(@_);
350 my $infodb_handle = shift(@_);
351 my $infodb_key = shift(@_);
352 my $infodb_map = shift(@_);
353
354 print $infodb_handle "[$infodb_key]\n";
355 foreach my $infodb_value_key (sort keys(%$infodb_map)) {
356 foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}}) {
357 if ($infodb_value =~ /-{70,}/) {
358 # if value contains 70 or more hyphens in a row we need to escape them
359 # to prevent txt2db from treating them as a separator
360 $infodb_value =~ s/-/&\#045;/gi;
361 }
362 print $infodb_handle "<$infodb_value_key>" . $infodb_value . "\n";
363 }
364 }
365 print $infodb_handle '-' x 70, "\n";
366}
367## write_infodb_entry(filehandle, string, hashmap) => void ##
368
369
370## @function write_infodb_rawentry(filehandle, string, string)
371#
372sub write_infodb_rawentry
373{
374 my $self = shift(@_);
375 my $infodb_handle = shift(@_);
376 my $infodb_key = shift(@_);
377 my $infodb_val = shift(@_);
378
379 print $infodb_handle "[$infodb_key]\n";
380 print $infodb_handle "$infodb_val\n";
381 print $infodb_handle '-' x 70, "\n";
382}
383## write_infodb_rawentry(filehandle, string, string) ##
384
385
3861;
Note: See TracBrowser for help on using the repository browser.