[24050] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
| 3 | # dbutil::tdb -- utility functions for writing to tdb databases. Should be
|
---|
| 4 | # hauntingly similar to GDBM utility functions.
|
---|
| 5 | #
|
---|
| 6 | # A component of the Greenstone digital library software
|
---|
| 7 | # from the New Zealand Digital Library Project at the
|
---|
| 8 | # University of Waikato, New Zealand.
|
---|
| 9 | #
|
---|
| 10 | # Copyright (C) 2011
|
---|
| 11 | #
|
---|
| 12 | # This program is free software; you can redistribute it and/or modify
|
---|
| 13 | # it under the terms of the GNU General Public License as published by
|
---|
| 14 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 15 | # (at your option) any later version.
|
---|
| 16 | #
|
---|
| 17 | # This program is distributed in the hope that it will be useful,
|
---|
| 18 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 20 | # GNU General Public License for more details.
|
---|
| 21 | #
|
---|
| 22 | # You should have received a copy of the GNU General Public License
|
---|
| 23 | # along with this program; if not, write to the Free Software
|
---|
| 24 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 25 | #
|
---|
| 26 | ###########################################################################
|
---|
| 27 |
|
---|
| 28 | package dbutil::tdb;
|
---|
| 29 |
|
---|
| 30 | # Pragma
|
---|
| 31 | use strict;
|
---|
| 32 |
|
---|
| 33 | # Libraries/Modules
|
---|
[24429] | 34 | use Cwd;
|
---|
[24050] | 35 | use dbutil::gdbmtxtgz;
|
---|
| 36 |
|
---|
| 37 | # -----------------------------------------------------------------------------
|
---|
| 38 | # TDB IMPLEMENTATION
|
---|
| 39 | # -----------------------------------------------------------------------------
|
---|
| 40 |
|
---|
[24429] | 41 | our %handle_pool;
|
---|
| 42 |
|
---|
| 43 | END {
|
---|
| 44 | # Close any handles left open in the pool
|
---|
| 45 | foreach my $pool_key (keys %handle_pool)
|
---|
| 46 | {
|
---|
| 47 | my $infodb_file_handle = $handle_pool{$pool_key};
|
---|
| 48 | close_infodb_write_handle($infodb_file_handle, 1);
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[24050] | 52 | # /**
|
---|
| 53 | # */
|
---|
| 54 | sub get_tdb_executable
|
---|
| 55 | {
|
---|
[30265] | 56 | my $program = shift(@_);
|
---|
| 57 | if (!defined $ENV{GEXTTDBEDIT_INSTALLED} || !-d $ENV{GEXTTDBEDIT_INSTALLED})
|
---|
| 58 | {
|
---|
| 59 | die('Fatal Error! Path to TDB binaries not found. Have you sourced setup.bash?');
|
---|
| 60 | }
|
---|
| 61 | my $program_exe = &util::filename_cat($ENV{GEXTTDBEDIT_INSTALLED} . '/bin/' . $program . &util::get_os_exe());
|
---|
| 62 | if (!-x $program_exe)
|
---|
| 63 | {
|
---|
| 64 | die('Fatal Error! File doesn\'t exist or isn\'t executable: ' . $program_exe);
|
---|
| 65 | }
|
---|
| 66 | return $program_exe;
|
---|
[24050] | 67 | }
|
---|
| 68 | # /** get_tdb_executable() **/
|
---|
| 69 |
|
---|
[24429] | 70 |
|
---|
| 71 |
|
---|
[24050] | 72 | # /**
|
---|
| 73 | # */
|
---|
| 74 | sub open_infodb_write_handle
|
---|
| 75 | {
|
---|
| 76 | my $infodb_file_path = shift(@_);
|
---|
| 77 | my $opt_append = shift(@_);
|
---|
| 78 |
|
---|
| 79 | my $txt2tdb_exe = &dbutil::tdb::get_tdb_executable('txt2tdb');
|
---|
| 80 |
|
---|
[24429] | 81 | my $pool_key = $infodb_file_path;
|
---|
| 82 | #my $cmd = "taskset -c 5 \"$txt2tdb_exe\"";
|
---|
[24050] | 83 | my $cmd = "\"$txt2tdb_exe\"";
|
---|
| 84 | if ((defined $opt_append) && ($opt_append eq "append"))
|
---|
| 85 | {
|
---|
| 86 | $cmd .= " -append";
|
---|
[24429] | 87 | $pool_key .= '-append';
|
---|
[24050] | 88 | }
|
---|
| 89 | $cmd .= " \"$infodb_file_path\"";
|
---|
[24429] | 90 | #$cmd .= " -debug"; # Uncomment to enable debug timing
|
---|
[24050] | 91 |
|
---|
| 92 | # we're going to pipe the key value pairs, in the appropriate format, from
|
---|
| 93 | # within the buildproc, so we create a piped handle here
|
---|
| 94 | my $infodb_file_handle = undef;
|
---|
[24429] | 95 | if (defined $handle_pool{$pool_key})
|
---|
[24050] | 96 | {
|
---|
[24429] | 97 | $infodb_file_handle = $handle_pool{$pool_key};
|
---|
[24050] | 98 | }
|
---|
[24429] | 99 | else
|
---|
| 100 | {
|
---|
[27401] | 101 | print STDERR "tdb::open_infodb_write_handle(" . $infodb_file_path . ")\n";
|
---|
[24429] | 102 | if(!open($infodb_file_handle, "| $cmd"))
|
---|
| 103 | {
|
---|
| 104 | print STDERR "Error: Failed to open pipe to $cmd\n";
|
---|
| 105 | print STDERR " $!\n";
|
---|
| 106 | return undef;
|
---|
| 107 | }
|
---|
| 108 | binmode($infodb_file_handle,":utf8");
|
---|
| 109 | $handle_pool{$pool_key} = $infodb_file_handle;
|
---|
| 110 | }
|
---|
[24050] | 111 | return $infodb_file_handle;
|
---|
| 112 | }
|
---|
| 113 | # /** open_infodb_write_handle() **/
|
---|
| 114 |
|
---|
| 115 | # /**
|
---|
| 116 | # */
|
---|
| 117 | sub close_infodb_write_handle
|
---|
| 118 | {
|
---|
| 119 | my $infodb_handle = shift(@_);
|
---|
[24429] | 120 | my ($empty_pool) = @_;
|
---|
| 121 | if (defined $empty_pool && $empty_pool == 1)
|
---|
| 122 | {
|
---|
| 123 | print STDERR "tdb::close_infodb_write_handle()\n";
|
---|
| 124 | close($infodb_handle);
|
---|
| 125 | }
|
---|
[24050] | 126 | }
|
---|
| 127 | # /** close_infodb_write_handle() **/
|
---|
| 128 |
|
---|
| 129 | # /**
|
---|
| 130 | # */
|
---|
| 131 | sub get_infodb_file_path
|
---|
| 132 | {
|
---|
| 133 | my $collection_name = shift(@_);
|
---|
| 134 | my $infodb_directory_path = shift(@_);
|
---|
| 135 |
|
---|
| 136 | my $infodb_file_extension = ".tdb";
|
---|
| 137 | my $infodb_file_name = &util::get_dirsep_tail($collection_name) . $infodb_file_extension;
|
---|
| 138 |
|
---|
| 139 | return &util::filename_cat($infodb_directory_path, $infodb_file_name);
|
---|
| 140 | }
|
---|
| 141 | # /** get_infodb_file_path() **/
|
---|
| 142 |
|
---|
| 143 | # /**
|
---|
| 144 | # */
|
---|
| 145 | sub read_infodb_file
|
---|
| 146 | {
|
---|
| 147 | my $infodb_file_path = shift(@_);
|
---|
| 148 | my $infodb_map = shift(@_);
|
---|
| 149 |
|
---|
| 150 | my $tdb2txt_exe = &dbutil::tdb::get_tdb_executable('tdb2txt');
|
---|
| 151 |
|
---|
| 152 | if (!open (PIPEIN, "\"$tdb2txt_exe\" \"$infodb_file_path\" |"))
|
---|
| 153 | {
|
---|
[24069] | 154 | print STDERR 'Error: Failed to open pipe to ' . $tdb2txt_exe . "\n";
|
---|
[24050] | 155 | print STDERR " $!\n";
|
---|
| 156 | return undef;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | binmode(PIPEIN,":utf8");
|
---|
| 160 |
|
---|
| 161 | my $infodb_line = "";
|
---|
| 162 | my $infodb_key = "";
|
---|
| 163 | my $infodb_value = "";
|
---|
| 164 | while (defined ($infodb_line = <PIPEIN>))
|
---|
| 165 | {
|
---|
| 166 | if ($infodb_line =~ /^\[([^\]]+)\]$/)
|
---|
| 167 | {
|
---|
| 168 | $infodb_key = $1;
|
---|
| 169 | }
|
---|
| 170 | elsif ($infodb_line =~ /^-{70}$/)
|
---|
| 171 | {
|
---|
| 172 | $infodb_map->{$infodb_key} = $infodb_value;
|
---|
| 173 | $infodb_key = "";
|
---|
| 174 | $infodb_value = "";
|
---|
| 175 | }
|
---|
| 176 | else
|
---|
| 177 | {
|
---|
| 178 | $infodb_value .= $infodb_line;
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | close (PIPEIN);
|
---|
| 183 | }
|
---|
| 184 | # /** read_infodb_file() **/
|
---|
| 185 |
|
---|
| 186 | # /**
|
---|
| 187 | # */
|
---|
| 188 | sub read_infodb_keys
|
---|
| 189 | {
|
---|
| 190 | my $infodb_file_path = shift(@_);
|
---|
| 191 | my $infodb_map = shift(@_);
|
---|
| 192 |
|
---|
| 193 | my $tdbkeys_exe = &dbutil::tdb::get_tdb_executable('tdbkeys');
|
---|
| 194 |
|
---|
| 195 | if (!open (PIPEIN, "\"tdbkeys_exe\" \"$infodb_file_path\" |"))
|
---|
| 196 | {
|
---|
| 197 | die "couldn't open pipe from gdbmkeys \$infodb_file_path\"\n$!\n";
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | binmode(PIPEIN,":utf8");
|
---|
| 201 |
|
---|
| 202 | my $infodb_line = "";
|
---|
| 203 | my $infodb_key = "";
|
---|
| 204 | my $infodb_value = "";
|
---|
| 205 | while (defined ($infodb_line = <PIPEIN>))
|
---|
| 206 | {
|
---|
| 207 | # remove end of line
|
---|
| 208 | chomp $infodb_line;
|
---|
| 209 |
|
---|
| 210 | $infodb_map->{$infodb_line} = 1;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | close (PIPEIN);
|
---|
| 214 | }
|
---|
| 215 | # /** read_infodb_keys() **/
|
---|
| 216 |
|
---|
| 217 | # /**
|
---|
| 218 | # */
|
---|
| 219 | sub write_infodb_entry
|
---|
| 220 | {
|
---|
| 221 | # With infodb_handle already set up, works the same as gdbm and gdbm_txtgz
|
---|
| 222 | # versions
|
---|
| 223 | &dbutil::gdbmtxtgz::write_infodb_entry(@_);
|
---|
| 224 | }
|
---|
| 225 | # /** write_infodb_entry() **/
|
---|
| 226 |
|
---|
| 227 | # /**
|
---|
| 228 | # */
|
---|
| 229 | sub write_infodb_rawentry
|
---|
| 230 | {
|
---|
| 231 | # With infodb_handle already set up, works the same as gdbm and gdbm_txtgz
|
---|
| 232 | # versions
|
---|
| 233 | &dbutil::gdbmtxtgz::write_infodb_rawentry(@_);
|
---|
| 234 | }
|
---|
| 235 | # /** write_infodb_rawentry() **/
|
---|
| 236 |
|
---|
| 237 | # /**
|
---|
| 238 | # */
|
---|
| 239 | sub set_infodb_entry
|
---|
| 240 | {
|
---|
| 241 | my $infodb_file_path = shift(@_);
|
---|
| 242 | my $infodb_key = shift(@_);
|
---|
| 243 | my $infodb_map = shift(@_);
|
---|
| 244 |
|
---|
| 245 | # Protect metadata values that go inside quotes for tdbset
|
---|
| 246 | foreach my $k (keys %$infodb_map)
|
---|
| 247 | {
|
---|
| 248 | my @escaped_v = ();
|
---|
| 249 | foreach my $v (@{$infodb_map->{$k}})
|
---|
| 250 | {
|
---|
| 251 | if ($k eq "contains")
|
---|
| 252 | {
|
---|
| 253 | # protect quotes in ".2;".3 etc
|
---|
| 254 | $v =~ s/\"/\\\"/g;
|
---|
| 255 | push(@escaped_v, $v);
|
---|
| 256 | }
|
---|
| 257 | else
|
---|
| 258 | {
|
---|
| 259 | my $ev = &ghtml::unescape_html($v);
|
---|
| 260 | $ev =~ s/\"/\\\"/g;
|
---|
| 261 | push(@escaped_v, $ev);
|
---|
| 262 | }
|
---|
| 263 | }
|
---|
| 264 | $infodb_map->{$k} = \@escaped_v;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | # Generate the record string
|
---|
| 268 | my $serialized_infodb_map = &dbutil::convert_infodb_hash_to_string($infodb_map);
|
---|
| 269 | ## print STDERR "**** ser dr\n$serialized_infodb_map\n\n\n";
|
---|
| 270 |
|
---|
| 271 | # Store it into GDBM
|
---|
| 272 | my $tdbset_exe = &dbutil::tdb::get_tdb_executable('tdbset');
|
---|
| 273 | my $cmd = "\"tdbset_exe\" \"$infodb_file_path\" \"$infodb_key\" \"$serialized_infodb_map\"";
|
---|
| 274 | my $status = system($cmd);
|
---|
| 275 |
|
---|
| 276 | return $status;
|
---|
| 277 | }
|
---|
| 278 | # /** set_infodb_entry() **/
|
---|
| 279 |
|
---|
| 280 | # /**
|
---|
| 281 | # */
|
---|
| 282 | sub delete_infodb_entry
|
---|
| 283 | {
|
---|
| 284 | # With infodb_handle already set up, works the same as gdbm and gdbm_txtgz
|
---|
| 285 | # versions
|
---|
| 286 | &dbutil::gdbmtxtgz::delete_infodb_entry(@_);
|
---|
| 287 | }
|
---|
| 288 | # /** delete_infodb_entry() **/
|
---|
| 289 |
|
---|
| 290 | 1;
|
---|