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