source: gs2-extensions/tdb/trunk/perllib/DBDrivers/TDB.pm@ 30318

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

Initial checkin of object-oriented rewrite of the dbutils stuff to bring it more into line with plugins and classifiers.

  • Property svn:executable set to *
File size: 7.2 KB
RevLine 
[30318]1###############################################################################
2#
3# DBDrivers/TDB.pm -- 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 from the New Zealand
7# Digital Library Project at the University of Waikato, New Zealand.
8#
9# Copyright (C) 2011-2015 New Zealand Digital Library Project
10#
11# This program is free software; you can redistribute it and/or modify it under
12# the terms of the GNU General Public License as published by the Free Software
13# Foundation; either version 2 of the License, or (at your option) any later
14# version.
15#
16# This program is distributed in the hope that it will be useful, but WITHOUT
17# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19# more details.
20#
21# You should have received a copy of the GNU General Public License along with
22# this program; if not, write to the Free Software Foundation, Inc., 675 Mass
23# Ave, Cambridge, MA 02139, USA.
24#
25###############################################################################
26
27package DBDrivers::TDB;
28
29# Pragma
30use strict;
31
32# Libraries
33use Cwd;
34use ghtml;
35use util;
36use DBDrivers::GDBM;
37
38sub BEGIN
39{
40 if (!defined $ENV{'GSDLHOME'} || !defined $ENV{'GSDLOS'}) {
41 die("Error! Environment not prepared. Have you sourced setup.bash?\n");
42 }
43 if (!defined $ENV{'GEXTTDBEDIT_INSTALLED'}) {
44 die("Error! Path to TDB binaries not found. Have you sourced setup.bash?\n");
45 }
46 @DBDrivers::TDB::ISA = ( 'DBDrivers::GDBM' );
47}
48
49sub new
50{
51 my $class = shift(@_);
52 return bless ($self, $class);
53}
54
55# -----------------------------------------------------------------------------
56# TDB IMPLEMENTATION
57# -----------------------------------------------------------------------------
58
59## Ask TDB executables to display debugging information?
60my $debug = 0; # 1 to enable
61
62## Should the TDB used a specific affinity?
63my $forced_affinity = -1; # zero upwards indicates the affinity
64
65## Default TDB file extension
66my $infodb_file_extension = '.tdb';
67
68## @function _get_tdb_executable(string)
69#
70sub _get_tdb_executable
71{
72 my $program = shift(@_);
73 if (!defined $ENV{GEXTTDBEDIT_INSTALLED} || !-d $ENV{GEXTTDBEDIT_INSTALLED})
74 {
75 die('Fatal Error! Path to TDB binaries not found. Have you sourced setup.bash?');
76 }
77 my $program_exe = &util::filename_cat($ENV{GEXTTDBEDIT_INSTALLED} . '/bin/' . $program . &util::get_os_exe());
78 if (!-x $program_exe)
79 {
80 die('Fatal Error! File doesn\'t exist or isn\'t executable: ' . $program_exe);
81 }
82 return $program_exe;
83}
84## get_tdb_executable(string) => string ##
85
86
87# With infodb_handle already set up, these functions work the same as parent version
88# sub close_infodb_write_handle {}
89# sub delete_infodb_entry {}
90# sub write_infodb_entry {}
91# sub write_infodb_rawentry {}
92
93
94## @function open_infodb_write_handle(string, string)
95#
96sub open_infodb_write_handle
97{
98 my $infodb_file_path = shift(@_);
99 my $opt_append = shift(@_);
100
101 my $txt2tdb_exe = &_get_tdb_executable('txt2tdb');
102
103 my $pool_key = $infodb_file_path;
104 my $cmd = '"' . $txt2tdb_exe . '"';
105 if ((defined $opt_append) && ($opt_append eq "append")) {
106 $cmd .= ' -append';
107 }
108 $cmd .= ' "' . $infodb_file_path . '"';
109 # Optional flags
110 if ($forced_affinity >= 0) {
111 $cmd = 'taskset -c 5 ' . $cmd;
112 }
113 if ($debug) {
114 $cmd .= ' -debug';
115 }
116
117 # we're going to pipe the key value pairs, in the appropriate format, from
118 # within the buildproc, so we create a piped handle here
119 my $infodb_file_handle = undef;
120 print STDERR "tdb::open_infodb_write_handle(" . $infodb_file_path . ")\n";
121 if(!open($infodb_file_handle, "| $cmd")) {
122 print STDERR "Error: Failed to open pipe to $cmd\n";
123 print STDERR " $!\n";
124 return undef;
125 }
126 binmode($infodb_file_handle,":utf8");
127 return $infodb_file_handle;
128}
129## open_infodb_write_handle(string, string) => filehandle ##
130
131
132## @function get_infodb_file_path(string, string)
133#
134sub get_infodb_file_path
135{
136 my $collection_name = shift(@_);
137 my $infodb_directory_path = shift(@_);
138 my $infodb_file_name = &util::get_dirsep_tail($collection_name) . $infodb_file_extension;
139 return &util::filename_cat($infodb_directory_path, $infodb_file_name);
140}
141## get_infodb_file_path(string, string) => string ##
142
143
144## @function read_infodb_file
145#
146sub read_infodb_file
147{
148 my $infodb_file_path = shift(@_);
149 my $infodb_map = shift(@_);
150
151 my $tdb2txt_exe = &_get_tdb_executable('tdb2txt');
152
153 if (!open (PIPEIN, '"' . $tdb2txt_exe . '" "' . $infodb_file_path . '" |')) {
154 print STDERR 'Error: Failed to open pipe to ' . $tdb2txt_exe . "\n";
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 if ($infodb_line =~ /^\[([^\]]+)\]$/) {
166 $infodb_key = $1;
167 }
168 elsif ($infodb_line =~ /^-{70}$/) {
169 $infodb_map->{$infodb_key} = $infodb_value;
170 $infodb_key = "";
171 $infodb_value = "";
172 }
173 else {
174 $infodb_value .= $infodb_line;
175 }
176 }
177 close (PIPEIN);
178}
179## read_infodb_file(string, hashmap) => void ##
180
181
182## @function read_infodb_keys(string, hashmap)
183#
184sub read_infodb_keys
185{
186 my $infodb_file_path = shift(@_);
187 my $infodb_map = shift(@_);
188
189 my $tdbkeys_exe = &_get_tdb_executable('tdbkeys');
190
191 if (!open (PIPEIN, '"' . tdbkeys_exe . '" "' . $infodb_file_path . '" |')) {
192 die("Error! Couldn't open pipe from read_infodb_keys: $infodb_file_path\n$!\n");
193 }
194
195 binmode(PIPEIN,":utf8");
196
197 my $infodb_line = "";
198 my $infodb_key = "";
199 my $infodb_value = "";
200 while (defined ($infodb_line = <PIPEIN>)) {
201 # remove end of line
202 chomp $infodb_line;
203 $infodb_map->{$infodb_line} = 1;
204 }
205
206 close (PIPEIN);
207}
208## read_infodb_keys(string, hashmap) => void ##
209
210
211## @function set_infodb_entry(string, string, hashmap)
212#
213sub set_infodb_entry
214{
215 my $infodb_file_path = shift(@_);
216 my $infodb_key = shift(@_);
217 my $infodb_map = shift(@_);
218
219 # Protect metadata values that go inside quotes for tdbset
220 foreach my $k (keys %$infodb_map) {
221 my @escaped_v = ();
222 foreach my $v (@{$infodb_map->{$k}}) {
223 if ($k eq "contains") {
224 # protect quotes in ".2;".3 etc
225 $v =~ s/\"/\\\"/g;
226 push(@escaped_v, $v);
227 }
228 else {
229 my $ev = &ghtml::unescape_html($v);
230 $ev =~ s/\"/\\\"/g;
231 push(@escaped_v, $ev);
232 }
233 }
234 $infodb_map->{$k} = \@escaped_v;
235 }
236
237 # Generate the record string
238 my $serialized_infodb_map = &_convert_infodb_hash_to_string($infodb_map);
239
240 # Store it into GDBM
241 my $tdbset_exe = &_get_tdb_executable('tdbset');
242 my $cmd = '"' . $tdbset_exe . '" "' . $infodb_file_path . '" "' . $infodb_key . '" "' . $serialized_infodb_map . '"';
243 my $status = system($cmd);
244
245 return $status;
246}
247## set_infodb_entry(string, string, hashmap) => integer ##
248
2491;
Note: See TracBrowser for help on using the repository browser.