source: main/trunk/greenstone2/perllib/dbutil/jdbm.pm@ 22953

Last change on this file since 22953 was 22076, checked in by sjm84, 14 years ago

Added error messages to these files that are printed when a write handle cannot be opened

File size: 5.7 KB
Line 
1###########################################################################
2#
3# dbutil::jdbm -- utility functions for writing to jdbm 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::jdbm;
28
29use strict;
30
31
32# -----------------------------------------------------------------------------
33# JDBM IMPLEMENTATION
34# -----------------------------------------------------------------------------
35
36# When DBUtil::* is properly structured with inheritence, then
37# much of this code (along with GDBM and GDBM-TXT-GZ) can be grouped into
38# a shared base class. Really it is only the the command that needs to
39# be constructed that changes between much of the code that is used
40
41
42sub open_infodb_write_handle
43{
44 my $infodb_file_path = shift(@_);
45 my $opt_append = shift(@_);
46
47 my $jdbmwrap_jar = &util::filename_cat($ENV{'GSDLHOME'},"bin","java", "JDBMWrapper.jar");
48 my $jdbm_jar = &util::filename_cat($ENV{'GSDLHOME'},"lib","java", "jdbm.jar");
49
50 my $classpath = &util::pathname_cat($jdbmwrap_jar,$jdbm_jar);
51
52 my $infodb_file_handle = undef;
53 my $txt2jdb_cmd = "java -cp \"$classpath\" Txt2Jdb";
54
55 if ((defined $opt_append) && ($opt_append eq "append")) {
56 $txt2jdb_cmd .= " -append";
57 }
58
59 # Lop off file extension, as JDBM does not expect this to be present
60 $infodb_file_path =~ s/\.jdb$//;
61
62 $txt2jdb_cmd .= " \"$infodb_file_path\"";
63
64 if (!open($infodb_file_handle, "| $txt2jdb_cmd"))
65 {
66 print STDERR "Error: Failed to open pipe to $txt2jdb_cmd";
67 print STDERR " $!\n";
68 return undef;
69 }
70
71 return $infodb_file_handle;
72}
73
74
75
76sub close_infodb_write_handle
77{
78 my $infodb_handle = shift(@_);
79
80 close($infodb_handle);
81}
82
83
84sub get_infodb_file_path
85{
86 my $collection_name = shift(@_);
87 my $infodb_directory_path = shift(@_);
88
89 my $infodb_file_extension = ".jdb";
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
95sub read_infodb_file
96{
97 my $infodb_file_path = shift(@_);
98 my $infodb_map = shift(@_);
99
100 my $jdbmwrap_jar = &util::filename_cat($ENV{'GSDLHOME'},"bin","java", "JDBMWrapper.jar");
101 my $jdbm_jar = &util::filename_cat($ENV{'GSDLHOME'},"lib","java", "jdbm.jar");
102
103 my $classpath = &util::pathname_cat($jdbmwrap_jar,$jdbm_jar);
104
105 my $jdb2txt_cmd = "java -cp \"$classpath\" Jdb2Txt";
106
107 open (PIPEIN, "$jdb2txt_cmd \"$infodb_file_path\" |") || die "couldn't open pipe from db2txt \$infodb_file_path\"\n";
108 my $infodb_line = "";
109 my $infodb_key = "";
110 my $infodb_value = "";
111 while (defined ($infodb_line = <PIPEIN>))
112 {
113 if ($infodb_line =~ /^\[([^\]]+)\]$/)
114 {
115 $infodb_key = $1;
116 }
117 elsif ($infodb_line =~ /^-{70}$/)
118 {
119 $infodb_map->{$infodb_key} = $infodb_value;
120 $infodb_key = "";
121 $infodb_value = "";
122 }
123 else
124 {
125 $infodb_value .= $infodb_line;
126 }
127 }
128
129 close (PIPEIN);
130}
131
132sub read_infodb_keys
133{
134 my $infodb_file_path = shift(@_);
135 my $infodb_map = shift(@_);
136
137 my $jdbmwrap_jar = &util::filename_cat($ENV{'GSDLHOME'},"bin","java", "JDBMWrapper.jar");
138 my $jdbm_jar = &util::filename_cat($ENV{'GSDLHOME'},"lib","java", "jdbm.jar");
139
140 my $classpath = &util::pathname_cat($jdbmwrap_jar,$jdbm_jar);
141
142 my $jdbkeys_cmd = "java -cp \"$classpath\" JdbKeys";
143
144 open (PIPEIN, "$jdbkeys_cmd \"$infodb_file_path\" |") || die "couldn't open pipe from jdbmkeys \$infodb_file_path\"\n";
145 my $infodb_line = "";
146 my $infodb_key = "";
147 my $infodb_value = "";
148 while (defined ($infodb_line = <PIPEIN>))
149 {
150 chomp $infodb_line; # remove end of line
151
152 $infodb_map->{$infodb_line} = 1;
153 }
154
155 close (PIPEIN);
156}
157
158
159
160sub write_infodb_entry
161{
162
163 my $infodb_handle = shift(@_);
164 my $infodb_key = shift(@_);
165 my $infodb_map = shift(@_);
166
167 print $infodb_handle "[$infodb_key]\n";
168 foreach my $infodb_value_key (keys(%$infodb_map))
169 {
170 foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
171 {
172 if ($infodb_value =~ /-{70,}/)
173 {
174 # if value contains 70 or more hyphens in a row we need to escape them
175 # to prevent txt2db from treating them as a separator
176 $infodb_value =~ s/-/&\#045;/gi;
177 }
178 print $infodb_handle "<$infodb_value_key>" . $infodb_value . "\n";
179 }
180 }
181 print $infodb_handle '-' x 70, "\n";
182}
183
184
185sub write_infodb_rawentry
186{
187
188 my $infodb_handle = shift(@_);
189 my $infodb_key = shift(@_);
190 my $infodb_val = shift(@_);
191
192 print $infodb_handle "[$infodb_key]\n";
193 print $infodb_handle "$infodb_val\n";
194 print $infodb_handle '-' x 70, "\n";
195}
196
197
198sub delete_infodb_entry
199{
200 my $infodb_handle = shift(@_);
201 my $infodb_key = shift(@_);
202
203 # A minus at the end of a key (after the ]) signifies 'delete'
204 print $infodb_handle "[$infodb_key]-\n";
205
206 # The 70 minus signs are also needed, to help make the parsing by db2txt simple
207 print $infodb_handle '-' x 70, "\n";
208}
209
210
211
212
213
2141;
Note: See TracBrowser for help on using the repository browser.