root/gs2-extensions/tdb-edit/trunk/src/perllib/dbutil/tdb.pm @ 24429

Revision 24429, 6.8 KB (checked in by jmt12, 8 years ago)

Added code to support persistence of TDB connections in a pool, and some debug comments

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