source: gs2-extensions/parallel-building/trunk/src/perllib/dbutil/gdbm.pm@ 24672

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

changed some error messages to be less... sacrilegious

File size: 6.5 KB
Line 
1###########################################################################
2#
3# dbutil::gdbm -- utility functions for writing to gdbm databases
4#
5# A component of the Greenstone digital library software
6# from the New Zealand Digital Library Project at the
7# University of Waikato, New Zealand.
8#
9# Copyright (C) 2009
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24#
25###########################################################################
26
27package dbutil::gdbm;
28
29use strict;
30
31use dbutil::gdbmtxtgz;
32
33# -----------------------------------------------------------------------------
34# GDBM IMPLEMENTATION
35# -----------------------------------------------------------------------------
36
37sub open_infodb_write_handle
38{
39 my $infodb_file_path = shift(@_);
40 my $opt_append = shift(@_);
41
42 # note the use of locking enabled txt2db [jmt12]
43 # also, we assume the path is correct to find the executable [jmt12]
44 my $txt2db_exe = "txt2dbl" . &util::get_os_exe();
45 #my $txt2db_exe = "txt2db" . &util::get_os_exe();
46 my $infodb_file_handle = undef;
47 my $cmd = "\"$txt2db_exe\"";
48 if ((defined $opt_append) && ($opt_append eq "append")) {
49 $cmd .= " -append";
50 }
51 $cmd .= " \"$infodb_file_path\"";
52
53 # Why? Why do you hate me? [jmt12]
54# if (!-e "$txt2db_exe")
55# {
56# print STDERR "Error: Unable to find $txt2db_exe\n";
57# return undef;
58# }
59
60 if(!open($infodb_file_handle, "| $cmd"))
61 {
62
63 print STDERR "Error: Failed to open pipe to $cmd\n";
64 print STDERR " $!\n";
65 return undef;
66 }
67
68 binmode($infodb_file_handle,":utf8");
69
70 return $infodb_file_handle;
71}
72
73
74
75sub close_infodb_write_handle
76{
77 my $infodb_handle = shift(@_);
78 close($infodb_handle);
79}
80
81
82sub get_infodb_file_path
83{
84 my $collection_name = shift(@_);
85 my $infodb_directory_path = shift(@_);
86
87 my $infodb_file_extension = ".gdb";
88 my $infodb_file_name = &util::get_dirsep_tail($collection_name) . $infodb_file_extension;
89 return &util::filename_cat($infodb_directory_path, $infodb_file_name);
90}
91
92
93sub read_infodb_file
94{
95 my $infodb_file_path = shift(@_);
96 my $infodb_map = shift(@_);
97
98 # note the use of locking enabled db2txt [jmt12]
99 open (PIPEIN, "db2txtl \"$infodb_file_path\" |")
100 || die "couldn't open pipe from db2txt \$infodb_file_path\"\n";
101
102 binmode(PIPEIN,":utf8");
103
104 my $infodb_line = "";
105 my $infodb_key = "";
106 my $infodb_value = "";
107 while (defined ($infodb_line = <PIPEIN>))
108 {
109 if ($infodb_line =~ /^\[([^\]]+)\]$/)
110 {
111 $infodb_key = $1;
112 }
113 elsif ($infodb_line =~ /^-{70}$/)
114 {
115 $infodb_map->{$infodb_key} = $infodb_value;
116 $infodb_key = "";
117 $infodb_value = "";
118 }
119 else
120 {
121 $infodb_value .= $infodb_line;
122 }
123 }
124
125 close (PIPEIN);
126}
127
128sub read_infodb_keys
129{
130 my $infodb_file_path = shift(@_);
131 my $infodb_map = shift(@_);
132
133 die("[gdbmkeys] not implemented for parallel building");
134
135 open (PIPEIN, "gdbmkeys \"$infodb_file_path\" |")
136 || die "couldn't open pipe from gdbmkeys \$infodb_file_path\"\n";
137
138 binmode(PIPEIN,":utf8");
139
140 my $infodb_line = "";
141 my $infodb_key = "";
142 my $infodb_value = "";
143 while (defined ($infodb_line = <PIPEIN>))
144 {
145 chomp $infodb_line; # remove end of line
146
147 $infodb_map->{$infodb_line} = 1;
148 }
149
150 close (PIPEIN);
151}
152
153sub write_infodb_entry
154{
155 # With infodb_handle already set up, works the same as _gdbm_txtgz version
156 &dbutil::gdbmtxtgz::write_infodb_entry(@_);
157}
158
159sub write_infodb_rawentry
160{
161 # With infodb_handle already set up, works the same as _gdbm_txtgz version
162 &dbutil::gdbmtxtgz::write_infodb_rawentry(@_);
163}
164
165
166sub set_infodb_entry_OLD
167{
168 my $infodb_file_path = shift(@_);
169 my $infodb_key = shift(@_);
170 my $infodb_map = shift(@_);
171
172 # Protect metadata values that go inside quotes for gdbmset
173 foreach my $k (keys %$infodb_map) {
174 my @escaped_v = ();
175 foreach my $v (@{$infodb_map->{$k}}) {
176 if ($k eq "contains") {
177 # protect quotes in ".2;".3 etc
178 $v =~ s/\"/\\\"/g;
179 push(@escaped_v, $v);
180 }
181 else {
182 my $ev = &ghtml::unescape_html($v);
183 $ev =~ s/\"/\\\"/g;
184 push(@escaped_v, $ev);
185 }
186 }
187 $infodb_map->{$k} = \@escaped_v;
188 }
189
190 # Generate the record string
191 my $serialized_infodb_map = &dbutil::convert_infodb_hash_to_string($infodb_map);
192## print STDERR "**** ser dr\n$serialized_infodb_map\n\n\n";
193
194 die("[gdbmset] not implemented for parallel building");
195
196 # Store it into GDBM
197 my $cmd = "gdbmset \"$infodb_file_path\" \"$infodb_key\" \"$serialized_infodb_map\"";
198 my $status = system($cmd);
199
200 return $status;
201
202}
203
204
205
206sub set_infodb_entry
207{
208 my $infodb_file_path = shift(@_);
209 my $infodb_key = shift(@_);
210 my $infodb_map = shift(@_);
211
212 # HTML escape anything that is not part of the "contains" metadata value
213 foreach my $k (keys %$infodb_map) {
214 my @escaped_v = ();
215 foreach my $v (@{$infodb_map->{$k}}) {
216 if ($k eq "contains") {
217 push(@escaped_v, $v);
218 }
219 else {
220 my $ev = &ghtml::unescape_html($v);
221 push(@escaped_v, $ev);
222 }
223 }
224 $infodb_map->{$k} = \@escaped_v;
225 }
226
227 # Generate the record string
228 my $serialized_infodb_map = &dbutil::convert_infodb_hash_to_string($infodb_map);
229### print STDERR "**** ser dr\n$serialized_infodb_map\n\n\n";
230
231 # Store it into GDBM using 'txt2db -append' which despite its name
232 # actually replaces the record if it already exists
233
234 # note the use of locking enabled txt2db [jmt12]
235 my $cmd = "txt2dbl -append \"$infodb_file_path\"";
236
237 my $status = undef;
238 if(!open(GOUT, "| $cmd"))
239 {
240 print STDERR "Error: gdbm::set_infodb_entry() failed to open pipe to: $cmd\n";
241 print STDERR " $!\n";
242 $status = -1;
243 }
244 else {
245 binmode(GOUT,":utf8");
246
247 print GOUT "[$infodb_key]\n";
248 print GOUT "$serialized_infodb_map\n";
249
250 close(GOUT);
251 $status = 0; # as in exit status of cmd OK
252 }
253
254 return $status;
255}
256
257
258sub delete_infodb_entry
259{
260 # With infodb_handle already set up, works the same as _gdbm_txtgz version
261 &dbutil::gdbmtxtgz::delete_infodb_entry(@_);
262}
263
264
265
2661;
Note: See TracBrowser for help on using the repository browser.