[21411] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
| 3 | # dbutil::jdbm -- utility functions for writing to jdbm databases
|
---|
| 4 | #
|
---|
| 5 | # A component of the Greenstone digital library software
|
---|
| 6 | # from the New Zealand Digital Library Project at the
|
---|
| 7 | # University of Waikato, New Zealand.
|
---|
| 8 | #
|
---|
| 9 | # Copyright (C) 2009
|
---|
| 10 | #
|
---|
| 11 | # This program is free software; you can redistribute it and/or modify
|
---|
| 12 | # it under the terms of the GNU General Public License as published by
|
---|
| 13 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 14 | # (at your option) any later version.
|
---|
| 15 | #
|
---|
| 16 | # This program is distributed in the hope that it will be useful,
|
---|
| 17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | # GNU General Public License for more details.
|
---|
| 20 | #
|
---|
| 21 | # You should have received a copy of the GNU General Public License
|
---|
| 22 | # along with this program; if not, write to the Free Software
|
---|
| 23 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 24 | #
|
---|
| 25 | ###########################################################################
|
---|
| 26 |
|
---|
| 27 | package dbutil::jdbm;
|
---|
| 28 |
|
---|
| 29 | use strict;
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | # -----------------------------------------------------------------------------
|
---|
| 33 | # JDBM IMPLEMENTATION
|
---|
| 34 | # -----------------------------------------------------------------------------
|
---|
| 35 |
|
---|
| 36 | # When DBUtil::* is properly structured with inheritence, then
|
---|
| 37 | # much of this code (along with GDBM and GDBM-TXT-GZ) can be grouped into
|
---|
| 38 | # a shared base class. Really it is only the the command that needs to
|
---|
| 39 | # be constructed that changes between much of the code that is used
|
---|
| 40 |
|
---|
| 41 |
|
---|
| 42 | sub open_infodb_write_handle
|
---|
| 43 | {
|
---|
| 44 | my $infodb_file_path = shift(@_);
|
---|
| 45 | my $opt_append = shift(@_);
|
---|
| 46 |
|
---|
| 47 | my $jdbmwrap_jar = &util::filename_cat($ENV{'GSDLHOME'},"bin","java", "JDBMWrapper.jar");
|
---|
| 48 | my $jdbm_jar = &util::filename_cat($ENV{'GSDLHOME'},"lib","java", "jdbm.jar");
|
---|
| 49 |
|
---|
| 50 | my $classpath = &util::pathname_cat($jdbmwrap_jar,$jdbm_jar);
|
---|
| 51 |
|
---|
| 52 | my $infodb_file_handle = undef;
|
---|
| 53 | my $txt2jdb_cmd = "java -cp \"$classpath\" Txt2Jdb";
|
---|
| 54 |
|
---|
| 55 | if ((defined $opt_append) && ($opt_append eq "append")) {
|
---|
| 56 | $txt2jdb_cmd .= " -append";
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | # Lop off file extension, as JDBM does not expect this to be present
|
---|
| 60 | $infodb_file_path =~ s/\.jdb$//;
|
---|
| 61 |
|
---|
| 62 | $txt2jdb_cmd .= " \"$infodb_file_path\"";
|
---|
| 63 |
|
---|
| 64 | if (!open($infodb_file_handle, "| $txt2jdb_cmd"))
|
---|
| 65 | {
|
---|
[22076] | 66 | print STDERR "Error: Failed to open pipe to $txt2jdb_cmd";
|
---|
| 67 | print STDERR " $!\n";
|
---|
| 68 | return undef;
|
---|
[21411] | 69 | }
|
---|
[23166] | 70 |
|
---|
| 71 | binmode($infodb_file_handle,":utf8");
|
---|
[21411] | 72 | return $infodb_file_handle;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 |
|
---|
| 77 | sub close_infodb_write_handle
|
---|
| 78 | {
|
---|
| 79 | my $infodb_handle = shift(@_);
|
---|
| 80 |
|
---|
| 81 | close($infodb_handle);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 |
|
---|
| 85 | sub get_infodb_file_path
|
---|
| 86 | {
|
---|
| 87 | my $collection_name = shift(@_);
|
---|
| 88 | my $infodb_directory_path = shift(@_);
|
---|
| 89 |
|
---|
| 90 | my $infodb_file_extension = ".jdb";
|
---|
| 91 | my $infodb_file_name = &util::get_dirsep_tail($collection_name) . $infodb_file_extension;
|
---|
| 92 | return &util::filename_cat($infodb_directory_path, $infodb_file_name);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | sub read_infodb_file
|
---|
| 97 | {
|
---|
| 98 | my $infodb_file_path = shift(@_);
|
---|
| 99 | my $infodb_map = shift(@_);
|
---|
| 100 |
|
---|
| 101 | my $jdbmwrap_jar = &util::filename_cat($ENV{'GSDLHOME'},"bin","java", "JDBMWrapper.jar");
|
---|
| 102 | my $jdbm_jar = &util::filename_cat($ENV{'GSDLHOME'},"lib","java", "jdbm.jar");
|
---|
| 103 |
|
---|
| 104 | my $classpath = &util::pathname_cat($jdbmwrap_jar,$jdbm_jar);
|
---|
| 105 |
|
---|
| 106 | my $jdb2txt_cmd = "java -cp \"$classpath\" Jdb2Txt";
|
---|
| 107 |
|
---|
| 108 | open (PIPEIN, "$jdb2txt_cmd \"$infodb_file_path\" |") || die "couldn't open pipe from db2txt \$infodb_file_path\"\n";
|
---|
[23166] | 109 | binmode(PIPEIN,":utf8");
|
---|
[21411] | 110 | my $infodb_line = "";
|
---|
| 111 | my $infodb_key = "";
|
---|
| 112 | my $infodb_value = "";
|
---|
| 113 | while (defined ($infodb_line = <PIPEIN>))
|
---|
| 114 | {
|
---|
| 115 | if ($infodb_line =~ /^\[([^\]]+)\]$/)
|
---|
| 116 | {
|
---|
| 117 | $infodb_key = $1;
|
---|
| 118 | }
|
---|
| 119 | elsif ($infodb_line =~ /^-{70}$/)
|
---|
| 120 | {
|
---|
| 121 | $infodb_map->{$infodb_key} = $infodb_value;
|
---|
| 122 | $infodb_key = "";
|
---|
| 123 | $infodb_value = "";
|
---|
| 124 | }
|
---|
| 125 | else
|
---|
| 126 | {
|
---|
| 127 | $infodb_value .= $infodb_line;
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | close (PIPEIN);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | sub read_infodb_keys
|
---|
| 135 | {
|
---|
| 136 | my $infodb_file_path = shift(@_);
|
---|
| 137 | my $infodb_map = shift(@_);
|
---|
| 138 |
|
---|
| 139 | my $jdbmwrap_jar = &util::filename_cat($ENV{'GSDLHOME'},"bin","java", "JDBMWrapper.jar");
|
---|
| 140 | my $jdbm_jar = &util::filename_cat($ENV{'GSDLHOME'},"lib","java", "jdbm.jar");
|
---|
| 141 |
|
---|
| 142 | my $classpath = &util::pathname_cat($jdbmwrap_jar,$jdbm_jar);
|
---|
| 143 |
|
---|
| 144 | my $jdbkeys_cmd = "java -cp \"$classpath\" JdbKeys";
|
---|
| 145 |
|
---|
| 146 | open (PIPEIN, "$jdbkeys_cmd \"$infodb_file_path\" |") || die "couldn't open pipe from jdbmkeys \$infodb_file_path\"\n";
|
---|
[23166] | 147 | binmode(PIPEIN,":utf8");
|
---|
[21411] | 148 | my $infodb_line = "";
|
---|
| 149 | my $infodb_key = "";
|
---|
| 150 | my $infodb_value = "";
|
---|
| 151 | while (defined ($infodb_line = <PIPEIN>))
|
---|
| 152 | {
|
---|
| 153 | chomp $infodb_line; # remove end of line
|
---|
| 154 |
|
---|
| 155 | $infodb_map->{$infodb_line} = 1;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | close (PIPEIN);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 |
|
---|
| 162 |
|
---|
| 163 | sub write_infodb_entry
|
---|
| 164 | {
|
---|
| 165 |
|
---|
| 166 | my $infodb_handle = shift(@_);
|
---|
| 167 | my $infodb_key = shift(@_);
|
---|
| 168 | my $infodb_map = shift(@_);
|
---|
| 169 |
|
---|
| 170 | print $infodb_handle "[$infodb_key]\n";
|
---|
| 171 | foreach my $infodb_value_key (keys(%$infodb_map))
|
---|
| 172 | {
|
---|
| 173 | foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
|
---|
| 174 | {
|
---|
| 175 | if ($infodb_value =~ /-{70,}/)
|
---|
| 176 | {
|
---|
| 177 | # if value contains 70 or more hyphens in a row we need to escape them
|
---|
| 178 | # to prevent txt2db from treating them as a separator
|
---|
| 179 | $infodb_value =~ s/-/&\#045;/gi;
|
---|
| 180 | }
|
---|
| 181 | print $infodb_handle "<$infodb_value_key>" . $infodb_value . "\n";
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 | print $infodb_handle '-' x 70, "\n";
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 |
|
---|
[21856] | 188 | sub write_infodb_rawentry
|
---|
| 189 | {
|
---|
| 190 |
|
---|
| 191 | my $infodb_handle = shift(@_);
|
---|
| 192 | my $infodb_key = shift(@_);
|
---|
| 193 | my $infodb_val = shift(@_);
|
---|
| 194 |
|
---|
| 195 | print $infodb_handle "[$infodb_key]\n";
|
---|
| 196 | print $infodb_handle "$infodb_val\n";
|
---|
| 197 | print $infodb_handle '-' x 70, "\n";
|
---|
| 198 | }
|
---|
| 199 |
|
---|
[23399] | 200 | sub set_infodb_entry
|
---|
| 201 | {
|
---|
[28108] | 202 | my $infodb_file_path = shift(@_);
|
---|
| 203 | my $infodb_key = shift(@_);
|
---|
| 204 | my $infodb_map = shift(@_);
|
---|
[23399] | 205 |
|
---|
[28108] | 206 | # HTML escape anything that is not part of the "contains" metadata value
|
---|
| 207 | foreach my $k (keys %$infodb_map) {
|
---|
| 208 | my @escaped_v = ();
|
---|
| 209 | foreach my $v (@{$infodb_map->{$k}}) {
|
---|
| 210 | if ($k eq "contains") {
|
---|
| 211 | push(@escaped_v, $v);
|
---|
| 212 | }
|
---|
| 213 | else {
|
---|
| 214 | my $ev = &ghtml::unescape_html($v);
|
---|
| 215 | push(@escaped_v, $ev);
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 | $infodb_map->{$k} = \@escaped_v;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | # Generate the record string
|
---|
| 222 | my $serialized_infodb_map = &dbutil::convert_infodb_hash_to_string($infodb_map);
|
---|
| 223 | ### print STDERR "**** ser dr\n$serialized_infodb_map\n\n\n";
|
---|
| 224 |
|
---|
| 225 | # Store it into JDBM using 'Txt2Jdb .... -append' which despite its name
|
---|
| 226 | # actually replaces the record if it already exists
|
---|
| 227 |
|
---|
| 228 | my $jdbmwrap_jar = &util::filename_cat($ENV{'GSDLHOME'},"bin","java", "JDBMWrapper.jar");
|
---|
| 229 | my $jdbm_jar = &util::filename_cat($ENV{'GSDLHOME'},"lib","java", "jdbm.jar");
|
---|
| 230 |
|
---|
| 231 | my $classpath = &util::pathname_cat($jdbmwrap_jar,$jdbm_jar);
|
---|
| 232 |
|
---|
| 233 | # Lop off file extension, as JDBM does not expect this to be present
|
---|
| 234 | $infodb_file_path =~ s/\.jdb$//;
|
---|
| 235 |
|
---|
| 236 | my $cmd = "java -cp \"$classpath\" Txt2Jdb -append \"$infodb_file_path\"";
|
---|
| 237 |
|
---|
| 238 | my $status = undef;
|
---|
| 239 | if(!open(GOUT, "| $cmd"))
|
---|
| 240 | {
|
---|
| 241 | print STDERR "Error: jdbm::set_infodb_entry() failed to open pipe to: $cmd\n";
|
---|
| 242 | print STDERR " $!\n";
|
---|
| 243 | $status = -1;
|
---|
| 244 | }
|
---|
| 245 | else {
|
---|
| 246 | binmode(GOUT,":utf8");
|
---|
| 247 |
|
---|
| 248 | print GOUT "[$infodb_key]\n";
|
---|
| 249 | print GOUT "$serialized_infodb_map\n";
|
---|
| 250 |
|
---|
| 251 | close(GOUT);
|
---|
| 252 | $status = 0; # as in exit status of cmd OK
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | return $status;
|
---|
[23399] | 256 | }
|
---|
| 257 |
|
---|
| 258 |
|
---|
[28108] | 259 |
|
---|
| 260 |
|
---|
[21411] | 261 | sub delete_infodb_entry
|
---|
| 262 | {
|
---|
| 263 | my $infodb_handle = shift(@_);
|
---|
| 264 | my $infodb_key = shift(@_);
|
---|
| 265 |
|
---|
| 266 | # A minus at the end of a key (after the ]) signifies 'delete'
|
---|
| 267 | print $infodb_handle "[$infodb_key]-\n";
|
---|
| 268 |
|
---|
| 269 | # The 70 minus signs are also needed, to help make the parsing by db2txt simple
|
---|
| 270 | print $infodb_handle '-' x 70, "\n";
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 |
|
---|
| 274 |
|
---|
| 275 |
|
---|
| 276 |
|
---|
| 277 | 1;
|
---|