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::tdbcluster;
|
---|
29 |
|
---|
30 | # Pragma
|
---|
31 | use strict;
|
---|
32 |
|
---|
33 | # Libraries/Modules
|
---|
34 | use Cwd;
|
---|
35 | use dbutil::gdbmtxtgz;
|
---|
36 | use dbutil::tdb;
|
---|
37 |
|
---|
38 | our $debug = 1;
|
---|
39 |
|
---|
40 | # -----------------------------------------------------------------------------
|
---|
41 | # TDB IMPLEMENTATION
|
---|
42 | # -----------------------------------------------------------------------------
|
---|
43 |
|
---|
44 | our %handle_pool;
|
---|
45 |
|
---|
46 | END {
|
---|
47 | # Close any handles left open in the pool
|
---|
48 | foreach my $pool_key (keys %handle_pool)
|
---|
49 | {
|
---|
50 | my $infodb_file_handle = $handle_pool{$pool_key};
|
---|
51 | close_infodb_write_handle($infodb_file_handle, 1);
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | # /**
|
---|
56 | # */
|
---|
57 | sub get_tdb_executable
|
---|
58 | {
|
---|
59 | my $program = shift(@_);
|
---|
60 | my $program_exe = &util::filename_cat($program . &util::get_os_exe());
|
---|
61 | #if (!-x $program_exe)
|
---|
62 | #{
|
---|
63 | # die('Fatal Error! File doesn\'t exist or isn\'t executable: ' . $program_exe);
|
---|
64 | #}
|
---|
65 | return $program_exe;
|
---|
66 | }
|
---|
67 | # /** get_tdb_executable() **/
|
---|
68 |
|
---|
69 |
|
---|
70 |
|
---|
71 | # /**
|
---|
72 | # */
|
---|
73 | sub open_infodb_write_handle
|
---|
74 | {
|
---|
75 | my $infodb_file_path = shift(@_);
|
---|
76 | my $opt_append = shift(@_);
|
---|
77 |
|
---|
78 | my $txt2tdb_exe = &dbutil::tdb::get_tdb_executable('txt2tdb');
|
---|
79 |
|
---|
80 | my $pool_key = $infodb_file_path;
|
---|
81 | #my $cmd = "taskset -c 5 \"$txt2tdb_exe\"";
|
---|
82 | my $cmd = "\"$txt2tdb_exe\"";
|
---|
83 | if ((defined $opt_append) && ($opt_append eq "append"))
|
---|
84 | {
|
---|
85 | $cmd .= " -append";
|
---|
86 | $pool_key .= '-append';
|
---|
87 | }
|
---|
88 | $cmd .= " \"$infodb_file_path\"";
|
---|
89 | #$cmd .= " -debug"; # Uncomment to enable debug timing
|
---|
90 |
|
---|
91 | # we're going to pipe the key value pairs, in the appropriate format, from
|
---|
92 | # within the buildproc, so we create a piped handle here
|
---|
93 | my $infodb_file_handle = undef;
|
---|
94 | if (defined $handle_pool{$pool_key})
|
---|
95 | {
|
---|
96 | $infodb_file_handle = $handle_pool{$pool_key};
|
---|
97 | }
|
---|
98 | else
|
---|
99 | {
|
---|
100 | print STDERR "tdbcluster::open_infodb_write_handle(" . $infodb_file_path . ")\n";
|
---|
101 | if(!open($infodb_file_handle, "| $cmd"))
|
---|
102 | {
|
---|
103 | print STDERR "Error: Failed to open pipe to $cmd\n";
|
---|
104 | print STDERR " $!\n";
|
---|
105 | return undef;
|
---|
106 | }
|
---|
107 | binmode($infodb_file_handle,":utf8");
|
---|
108 | $handle_pool{$pool_key} = $infodb_file_handle;
|
---|
109 | }
|
---|
110 | return $infodb_file_handle;
|
---|
111 | }
|
---|
112 | # /** open_infodb_write_handle() **/
|
---|
113 |
|
---|
114 | # /**
|
---|
115 | # */
|
---|
116 | sub close_infodb_write_handle
|
---|
117 | {
|
---|
118 | my ($infodb_handle, $empty_pool) = @_;
|
---|
119 | if (defined $empty_pool && $empty_pool == 1)
|
---|
120 | {
|
---|
121 | print STDERR "tdbcluster::close_infodb_write_handle()\n";
|
---|
122 | close($infodb_handle);
|
---|
123 | }
|
---|
124 | }
|
---|
125 | # /** close_infodb_write_handle() **/
|
---|
126 |
|
---|
127 | # /**
|
---|
128 | # */
|
---|
129 | sub get_infodb_file_path
|
---|
130 | {
|
---|
131 | my ($collection_name, $infodb_directory_path, $perform_firsttime_initialization, $hostname) = @_;
|
---|
132 | if (!defined $perform_firsttime_initialization)
|
---|
133 | {
|
---|
134 | $perform_firsttime_initialization = 0;
|
---|
135 | }
|
---|
136 | if (!defined $hostname)
|
---|
137 | {
|
---|
138 | $hostname = `hostname -s`;
|
---|
139 | $hostname =~ s/^\s*|\s*$//gi;
|
---|
140 |
|
---|
141 | # Special case: look up the head node (top of list) from mpi.conf and do
|
---|
142 | # not add suffix if a match
|
---|
143 | if ($hostname eq 'medusa')
|
---|
144 | {
|
---|
145 | $hostname = '';
|
---|
146 | }
|
---|
147 | }
|
---|
148 | ###rint STDERR 'tdbcluster::get_infodb_file_path("' . $collection_name . '","' . $infodb_directory_path . '","' . $hostname . '")' . "\n";
|
---|
149 | my $infodb_file_extension = '.tdb';
|
---|
150 | if ($hostname ne '')
|
---|
151 | {
|
---|
152 | $infodb_file_extension = '.' . $hostname . $infodb_file_extension;
|
---|
153 | }
|
---|
154 | my $infodb_file_name = &util::get_dirsep_tail($collection_name) . $infodb_file_extension;
|
---|
155 | my $path = &util::filename_cat($infodb_directory_path, $infodb_file_name);
|
---|
156 | ###rint STDERR ' => ' . $path . "\n";
|
---|
157 | return $path;
|
---|
158 | }
|
---|
159 | # /** get_infodb_file_path() **/
|
---|
160 |
|
---|
161 | # /**
|
---|
162 | # */
|
---|
163 | sub read_infodb_file
|
---|
164 | {
|
---|
165 | my $infodb_file_path = shift(@_);
|
---|
166 | my $infodb_map = shift(@_);
|
---|
167 |
|
---|
168 | my $tdb2txt_exe = &dbutil::tdb::get_tdb_executable('tdb2txt');
|
---|
169 |
|
---|
170 | if (!open (PIPEIN, "\"$tdb2txt_exe\" \"$infodb_file_path\" |"))
|
---|
171 | {
|
---|
172 | print STDERR 'Error: Failed to open pipe to ' . $tdb2txt_exe . "\n";
|
---|
173 | print STDERR " $!\n";
|
---|
174 | return undef;
|
---|
175 | }
|
---|
176 |
|
---|
177 | binmode(PIPEIN,":utf8");
|
---|
178 |
|
---|
179 | my $infodb_line = "";
|
---|
180 | my $infodb_key = "";
|
---|
181 | my $infodb_value = "";
|
---|
182 | while (defined ($infodb_line = <PIPEIN>))
|
---|
183 | {
|
---|
184 | if ($infodb_line =~ /^\[([^\]]+)\]$/)
|
---|
185 | {
|
---|
186 | $infodb_key = $1;
|
---|
187 | }
|
---|
188 | elsif ($infodb_line =~ /^-{70}$/)
|
---|
189 | {
|
---|
190 | $infodb_map->{$infodb_key} = $infodb_value;
|
---|
191 | $infodb_key = "";
|
---|
192 | $infodb_value = "";
|
---|
193 | }
|
---|
194 | else
|
---|
195 | {
|
---|
196 | $infodb_value .= $infodb_line;
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | close (PIPEIN);
|
---|
201 | }
|
---|
202 | # /** read_infodb_file() **/
|
---|
203 |
|
---|
204 | # /**
|
---|
205 | # */
|
---|
206 | sub read_infodb_keys
|
---|
207 | {
|
---|
208 | my $infodb_file_path = shift(@_);
|
---|
209 | my $infodb_map = shift(@_);
|
---|
210 |
|
---|
211 | my $tdbkeys_exe = &dbutil::tdb::get_tdb_executable('tdbkeys');
|
---|
212 |
|
---|
213 | if (!open (PIPEIN, "\"tdbkeys_exe\" \"$infodb_file_path\" |"))
|
---|
214 | {
|
---|
215 | die "couldn't open pipe from gdbmkeys \$infodb_file_path\"\n$!\n";
|
---|
216 | }
|
---|
217 |
|
---|
218 | binmode(PIPEIN,":utf8");
|
---|
219 |
|
---|
220 | my $infodb_line = "";
|
---|
221 | my $infodb_key = "";
|
---|
222 | my $infodb_value = "";
|
---|
223 | while (defined ($infodb_line = <PIPEIN>))
|
---|
224 | {
|
---|
225 | # remove end of line
|
---|
226 | chomp $infodb_line;
|
---|
227 |
|
---|
228 | $infodb_map->{$infodb_line} = 1;
|
---|
229 | }
|
---|
230 |
|
---|
231 | close (PIPEIN);
|
---|
232 | }
|
---|
233 | # /** read_infodb_keys() **/
|
---|
234 |
|
---|
235 | # /**
|
---|
236 | # */
|
---|
237 | sub write_infodb_entry
|
---|
238 | {
|
---|
239 | # With infodb_handle already set up, works the same as gdbm and gdbm_txtgz
|
---|
240 | # versions
|
---|
241 | &dbutil::gdbmtxtgz::write_infodb_entry(@_);
|
---|
242 | }
|
---|
243 | # /** write_infodb_entry() **/
|
---|
244 |
|
---|
245 | # /**
|
---|
246 | # */
|
---|
247 | sub write_infodb_rawentry
|
---|
248 | {
|
---|
249 | # With infodb_handle already set up, works the same as gdbm and gdbm_txtgz
|
---|
250 | # versions
|
---|
251 | &dbutil::gdbmtxtgz::write_infodb_rawentry(@_);
|
---|
252 | }
|
---|
253 | # /** write_infodb_rawentry() **/
|
---|
254 |
|
---|
255 | # /**
|
---|
256 | # */
|
---|
257 | sub set_infodb_entry
|
---|
258 | {
|
---|
259 | my $infodb_file_path = shift(@_);
|
---|
260 | my $infodb_key = shift(@_);
|
---|
261 | my $infodb_map = shift(@_);
|
---|
262 |
|
---|
263 | # Protect metadata values that go inside quotes for tdbset
|
---|
264 | foreach my $k (keys %$infodb_map)
|
---|
265 | {
|
---|
266 | my @escaped_v = ();
|
---|
267 | foreach my $v (@{$infodb_map->{$k}})
|
---|
268 | {
|
---|
269 | if ($k eq "contains")
|
---|
270 | {
|
---|
271 | # protect quotes in ".2;".3 etc
|
---|
272 | $v =~ s/\"/\\\"/g;
|
---|
273 | push(@escaped_v, $v);
|
---|
274 | }
|
---|
275 | else
|
---|
276 | {
|
---|
277 | my $ev = &ghtml::unescape_html($v);
|
---|
278 | $ev =~ s/\"/\\\"/g;
|
---|
279 | push(@escaped_v, $ev);
|
---|
280 | }
|
---|
281 | }
|
---|
282 | $infodb_map->{$k} = \@escaped_v;
|
---|
283 | }
|
---|
284 |
|
---|
285 | # Generate the record string
|
---|
286 | my $serialized_infodb_map = &dbutil::convert_infodb_hash_to_string($infodb_map);
|
---|
287 | ###rint STDERR "**** ser dr\n$serialized_infodb_map\n\n\n";
|
---|
288 |
|
---|
289 | # Store it into GDBM
|
---|
290 | my $tdbset_exe = &dbutil::tdb::get_tdb_executable('tdbset');
|
---|
291 | my $cmd = "\"tdbset_exe\" \"$infodb_file_path\" \"$infodb_key\" \"$serialized_infodb_map\"";
|
---|
292 | my $status = system($cmd);
|
---|
293 |
|
---|
294 | return $status;
|
---|
295 | }
|
---|
296 | # /** set_infodb_entry() **/
|
---|
297 |
|
---|
298 | # /**
|
---|
299 | # */
|
---|
300 | sub delete_infodb_entry
|
---|
301 | {
|
---|
302 | # With infodb_handle already set up, works the same as gdbm and gdbm_txtgz
|
---|
303 | # versions
|
---|
304 | &dbutil::gdbmtxtgz::delete_infodb_entry(@_);
|
---|
305 | }
|
---|
306 | # /** delete_infodb_entry() **/
|
---|
307 |
|
---|
308 |
|
---|
309 | ## @function
|
---|
310 | sub merge_databases
|
---|
311 | {
|
---|
312 | my $source_infodb_file_path = shift(@_);
|
---|
313 | my $target_infodb_file_path = shift(@_);
|
---|
314 | # path specific filenames
|
---|
315 | my $txt2tdb_exe = &dbutil::tdb::get_tdb_executable('txt2tdb');
|
---|
316 | my $tdb2txt_exe = &dbutil::tdb::get_tdb_executable('tdb2txt');
|
---|
317 | my $cmd = $tdb2txt_exe . ' "' . $source_infodb_file_path . '" | ' . $txt2tdb_exe . ' -append "' . $target_infodb_file_path . '"';
|
---|
318 | ###rint STDERR "[DEBUG] cmd: " . $cmd . "\n";
|
---|
319 | my $status = system($cmd);
|
---|
320 | ###rint STDERR "[DEBUG] status: $status\n";
|
---|
321 | if ($status == 0)
|
---|
322 | {
|
---|
323 | &FileUtils::removeFiles($source_infodb_file_path);
|
---|
324 | }
|
---|
325 | return $status;
|
---|
326 | }
|
---|
327 | ##
|
---|
328 |
|
---|
329 |
|
---|
330 | 1;
|
---|