source: main/trunk/greenstone2/perllib/dbutil/gdbm.pm@ 27306

Last change on this file since 27306 was 27306, checked in by jmt12, 11 years ago

Moving the critical file-related functions (copy, rm, etc) out of util.pm into their own proper class FileUtils. Use of the old functions in util.pm will prompt deprecated warning messages. There may be further functions that could be moved across in the future, but these are the critical ones when considering supporting other filesystems (HTTP, HDFS, WebDav, etc). Updated some key files to use the new functions so now deprecated messages thrown when importing/building demo collection 'out of the box'

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