source: gs2-extensions/tdb/trunk/perllib/DBDrivers/JDBM.pm@ 30338

Last change on this file since 30338 was 30338, checked in by jmt12, 8 years ago

First versions of these drivers, that should be further refined to move repeated code to a parent class

  • Property svn:executable set to *
File size: 9.3 KB
RevLine 
[30318]1###############################################################################
2#
3# DBDrivers/JDBM.pm -- utility functions for writing to jdbm databases
4#
5# A component of the Greenstone digital library software from the New Zealand
6# Digital Library Project at the University of Waikato, New Zealand.
7#
8# Copyright (C) 1999-2015 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify it under
11# the terms of the GNU General Public License as published by the Free Software
12# Foundation; either version 2 of the License, or (at your option) any later
13# version.
14#
15# This program is distributed in the hope that it will be useful, but WITHOUT
16# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18# more details.
19#
20# You should have received a copy of the GNU General Public License along with
21# this program; if not, write to the Free Software Foundation, Inc., 675 Mass
22# Ave, Cambridge, MA 02139, USA.
23#
24###############################################################################
25
26package DBDrivers::JDBM;
27
28# Pragma
29use strict;
30
31# Libraries
32use util;
33use FileUtils;
[30338]34# - OO inheritence
35use parent 'DBDrivers::BaseDBDriver';
[30318]36
37sub BEGIN
38{
[30338]39 if (!defined $ENV{'GSDLHOME'} || !defined $ENV{'GSDLOS'}) {
40 die("Error! Environment must be prepared by sourcing setup.bash\n");
41 }
[30318]42}
43
[30338]44
45## @function constructor
46#
[30318]47sub new
48{
49 my $class = shift(@_);
[30338]50 my $self = DBDrivers::BaseDBDriver->new();
51 $self->{'default_file_extension'} = 'jdb';
52 bless($self, $class);
53 return $self;
[30318]54}
[30338]55## constructor() ##
[30318]56
[30338]57
[30318]58# -----------------------------------------------------------------------------
59# JDBM IMPLEMENTATION
60# -----------------------------------------------------------------------------
61
62# When DBUtil::* is properly structured with inheritence, then
63# much of this code (along with GDBM and GDBM-TXT-GZ) can be grouped into
64# a shared base class. Really it is only the the command that needs to
65# be constructed that changes between much of the code that is used
66
[30338]67# Handled by BaseDBDriver
68# sub get_infodb_file_path {}
69
70
71
[30318]72sub open_infodb_write_handle
73{
[30338]74 my $self = shift(@_);
75 my $infodb_file_path = shift(@_);
76 my $opt_append = shift(@_);
77 if (!defined $opt_append) {
78 $opt_append = '';
79 }
80 $self->_debugPrint('("' . $infodb_file_path . '","' . $opt_append . '")');
[30318]81
[30338]82 my $jdbmwrap_jar = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'},"bin","java", "JDBMWrapper.jar");
83 my $jdbm_jar = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'},"lib","java", "jdbm.jar");
[30318]84
85 my $classpath = &util::pathname_cat($jdbmwrap_jar,$jdbm_jar);
86
87 if ($^O eq "cygwin") {
88 # Away to run a java program, using a binary that is native to Windows, so need
89 # Windows directory and path separators
90
91 $classpath = `cygpath -wp "$classpath"`;
92 chomp($classpath);
93 $classpath =~ s%\\%\\\\%g;
94 }
95
96 my $infodb_file_handle = undef;
97 my $txt2jdb_cmd = "java -cp \"$classpath\" Txt2Jdb";
98
[30338]99 if ($opt_append eq "append") {
[30318]100 $txt2jdb_cmd .= " -append";
101 print STDERR "Append operation to $infodb_file_path\n";
102 }
103 else {
104 print STDERR "Create database $infodb_file_path\n";
105 }
106
107 # Lop off file extension, as JDBM does not expect this to be present
108 $infodb_file_path =~ s/\.jdb$//;
109
110 if ($^O eq "cygwin") {
111 $infodb_file_path = `cygpath -w "$infodb_file_path"`;
112 chomp($infodb_file_path);
113 $infodb_file_path =~ s%\\%\\\\%g;
114 }
115
116 $txt2jdb_cmd .= " \"$infodb_file_path\"";
117
118 if (!open($infodb_file_handle, "| $txt2jdb_cmd"))
119 {
120 print STDERR "Error: Failed to open pipe to $txt2jdb_cmd";
121 print STDERR " $!\n";
122 return undef;
123 }
124
125 binmode($infodb_file_handle,":utf8");
126 return $infodb_file_handle;
127}
128
129
130
131sub close_infodb_write_handle
132{
133 my $infodb_handle = shift(@_);
134
135 close($infodb_handle);
136}
137
138
139sub read_infodb_file
140{
141 my $infodb_file_path = shift(@_);
142 my $infodb_map = shift(@_);
143
144 my $jdbmwrap_jar = &util::filename_cat($ENV{'GSDLHOME'},"bin","java", "JDBMWrapper.jar");
145 my $jdbm_jar = &util::filename_cat($ENV{'GSDLHOME'},"lib","java", "jdbm.jar");
146
147 my $classpath = &util::pathname_cat($jdbmwrap_jar,$jdbm_jar);
148
149 if ($^O eq "cygwin") {
150 # Away to run a java program, using a binary that is native to Windows, so need
151 # Windows directory and path separators
152
153 $classpath = `cygpath -wp "$classpath"`;
154 chomp($classpath);
155 $classpath =~ s%\\%\\\\%g;
156
157 $infodb_file_path = `cygpath -w "$infodb_file_path"`;
158 chomp($infodb_file_path);
159 $infodb_file_path =~ s%\\%\\\\%g;
160 }
161
162 my $jdb2txt_cmd = "java -cp \"$classpath\" Jdb2Txt";
163
164 open (PIPEIN, "$jdb2txt_cmd \"$infodb_file_path\" |") || die "couldn't open pipe from db2txt \$infodb_file_path\"\n";
165 binmode(PIPEIN,":utf8");
166 my $infodb_line = "";
167 my $infodb_key = "";
168 my $infodb_value = "";
169 while (defined ($infodb_line = <PIPEIN>))
170 {
171 $infodb_line =~ s/(\r\n)+$//; # more general than chomp
172
173 if ($infodb_line =~ /^\[([^\]]+)\]$/)
174 {
175 $infodb_key = $1;
176 }
177 elsif ($infodb_line =~ /^-{70}$/)
178 {
179 $infodb_map->{$infodb_key} = $infodb_value;
180 $infodb_key = "";
181 $infodb_value = "";
182 }
183 else
184 {
185 $infodb_value .= $infodb_line;
186 }
187 }
188
189 close (PIPEIN);
190}
191
192sub read_infodb_keys
193{
194 my $infodb_file_path = shift(@_);
195 my $infodb_map = shift(@_);
196
197 my $jdbmwrap_jar = &util::filename_cat($ENV{'GSDLHOME'},"bin","java", "JDBMWrapper.jar");
198 my $jdbm_jar = &util::filename_cat($ENV{'GSDLHOME'},"lib","java", "jdbm.jar");
199
200 my $classpath = &util::pathname_cat($jdbmwrap_jar,$jdbm_jar);
201
202 my $jdbkeys_cmd = "java -cp \"$classpath\" JdbKeys";
203
204 open (PIPEIN, "$jdbkeys_cmd \"$infodb_file_path\" |") || die "couldn't open pipe from jdbmkeys \$infodb_file_path\"\n";
205 binmode(PIPEIN,":utf8");
206 my $infodb_line = "";
207 my $infodb_key = "";
208 my $infodb_value = "";
209 while (defined ($infodb_line = <PIPEIN>))
210 {
211 # chomp $infodb_line; # remove end of line
212 $infodb_line =~ s/(\r\n)+$//; # more general than chomp
213
214 $infodb_map->{$infodb_line} = 1;
215 }
216
217 close (PIPEIN);
218}
219
220
221
222sub write_infodb_entry
223{
224
225 my $infodb_handle = shift(@_);
226 my $infodb_key = shift(@_);
227 my $infodb_map = shift(@_);
228
229 print $infodb_handle "[$infodb_key]\n";
230 foreach my $infodb_value_key (keys(%$infodb_map))
231 {
232 foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
233 {
234 if ($infodb_value =~ /-{70,}/)
235 {
236 # if value contains 70 or more hyphens in a row we need to escape them
237 # to prevent txt2db from treating them as a separator
238 $infodb_value =~ s/-/&\#045;/gi;
239 }
240 print $infodb_handle "<$infodb_value_key>" . $infodb_value . "\n";
241 }
242 }
243 print $infodb_handle '-' x 70, "\n";
244}
245
246
247sub write_infodb_rawentry
248{
249
250 my $infodb_handle = shift(@_);
251 my $infodb_key = shift(@_);
252 my $infodb_val = shift(@_);
253
254 print $infodb_handle "[$infodb_key]\n";
255 print $infodb_handle "$infodb_val\n";
256 print $infodb_handle '-' x 70, "\n";
257}
258
259sub set_infodb_entry
260{
261 my $infodb_file_path = shift(@_);
262 my $infodb_key = shift(@_);
263 my $infodb_map = shift(@_);
264
265 # HTML escape anything that is not part of the "contains" metadata value
266 foreach my $k (keys %$infodb_map) {
267 my @escaped_v = ();
268 foreach my $v (@{$infodb_map->{$k}}) {
269 if ($k eq "contains") {
270 push(@escaped_v, $v);
271 }
272 else {
273 my $ev = &ghtml::unescape_html($v);
274 push(@escaped_v, $ev);
275 }
276 }
277 $infodb_map->{$k} = \@escaped_v;
278 }
279
280 # Generate the record string
281 my $serialized_infodb_map = &dbutil::convert_infodb_hash_to_string($infodb_map);
282### print STDERR "**** ser dr\n$serialized_infodb_map\n\n\n";
283
284 # Store it into JDBM using 'Txt2Jdb .... -append' which despite its name
285 # actually replaces the record if it already exists
286
287 my $jdbmwrap_jar = &util::filename_cat($ENV{'GSDLHOME'},"bin","java", "JDBMWrapper.jar");
288 my $jdbm_jar = &util::filename_cat($ENV{'GSDLHOME'},"lib","java", "jdbm.jar");
289
290 my $classpath = &util::pathname_cat($jdbmwrap_jar,$jdbm_jar);
291
292 # Lop off file extension, as JDBM does not expect this to be present
293 $infodb_file_path =~ s/\.jdb$//;
294
295 if ($^O eq "cygwin") {
296 # Away to run a java program, using a binary that is native to Windows, so need
297 # Windows directory and path separators
298
299 $classpath = `cygpath -wp "$classpath"`;
300 chomp($classpath);
301 $classpath =~ s%\\%\\\\%g;
302
303 $infodb_file_path = `cygpath -w "$infodb_file_path"`;
304 chomp($infodb_file_path);
305 $infodb_file_path =~ s%\\%\\\\%g;
306 }
307
308 my $cmd = "java -cp \"$classpath\" Txt2Jdb -append \"$infodb_file_path\"";
309
310 my $status = undef;
311 if(!open(GOUT, "| $cmd"))
312 {
313 print STDERR "Error: jdbm::set_infodb_entry() failed to open pipe to: $cmd\n";
314 print STDERR " $!\n";
315 $status = -1;
316 }
317 else {
318 binmode(GOUT,":utf8");
319
320 print GOUT "[$infodb_key]\n";
321 print GOUT "$serialized_infodb_map\n";
322
323 close(GOUT);
324 $status = 0; # as in exit status of cmd OK
325 }
326
327 return $status;
328}
329
330
331
332
333sub delete_infodb_entry
334{
335 my $infodb_handle = shift(@_);
336 my $infodb_key = shift(@_);
337
338 # A minus at the end of a key (after the ]) signifies 'delete'
339 print $infodb_handle "[$infodb_key]-\n";
340
341 # The 70 minus signs are also needed, to help make the parsing by db2txt simple
342 print $infodb_handle '-' x 70, "\n";
343}
344
3451;
Note: See TracBrowser for help on using the repository browser.