source: gs2-extensions/tdb-edit/trunk/src/perllib/dbutil/tdb.pm@ 24069

Last change on this file since 24069 was 24069, checked in by jmt12, 13 years ago

Removed environment setting for executable path resolution as this shoulod already be correctly set up on the system PATH. Wrong variable name in one debug comment.

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