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

Last change on this file since 21411 was 21411, checked in by davidb, 12 years ago

Separation of different database back-ends into individual files

File size: 5.3 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 return undef;
67 }
68
69 return $infodb_file_handle;
70}
71
72
73
74sub close_infodb_write_handle
75{
76 my $infodb_handle = shift(@_);
77
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 = ".jdb";
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 my $jdbmwrap_jar = &util::filename_cat($ENV{'GSDLHOME'},"bin","java", "JDBMWrapper.jar");
99 my $jdbm_jar = &util::filename_cat($ENV{'GSDLHOME'},"lib","java", "jdbm.jar");
100
101 my $classpath = &util::pathname_cat($jdbmwrap_jar,$jdbm_jar);
102
103 my $jdb2txt_cmd = "java -cp \"$classpath\" Jdb2Txt";
104
105 open (PIPEIN, "$jdb2txt_cmd \"$infodb_file_path\" |") || die "couldn't open pipe from db2txt \$infodb_file_path\"\n";
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
130sub read_infodb_keys
131{
132 my $infodb_file_path = shift(@_);
133 my $infodb_map = shift(@_);
134
135 my $jdbmwrap_jar = &util::filename_cat($ENV{'GSDLHOME'},"bin","java", "JDBMWrapper.jar");
136 my $jdbm_jar = &util::filename_cat($ENV{'GSDLHOME'},"lib","java", "jdbm.jar");
137
138 my $classpath = &util::pathname_cat($jdbmwrap_jar,$jdbm_jar);
139
140 my $jdbkeys_cmd = "java -cp \"$classpath\" JdbKeys";
141
142 open (PIPEIN, "$jdbkeys_cmd \"$infodb_file_path\" |") || die "couldn't open pipe from jdbmkeys \$infodb_file_path\"\n";
143 my $infodb_line = "";
144 my $infodb_key = "";
145 my $infodb_value = "";
146 while (defined ($infodb_line = <PIPEIN>))
147 {
148 chomp $infodb_line; # remove end of line
149
150 $infodb_map->{$infodb_line} = 1;
151 }
152
153 close (PIPEIN);
154}
155
156
157
158sub write_infodb_entry
159{
160
161 my $infodb_handle = shift(@_);
162 my $infodb_key = shift(@_);
163 my $infodb_map = shift(@_);
164
165 print $infodb_handle "[$infodb_key]\n";
166 foreach my $infodb_value_key (keys(%$infodb_map))
167 {
168 foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
169 {
170 if ($infodb_value =~ /-{70,}/)
171 {
172 # if value contains 70 or more hyphens in a row we need to escape them
173 # to prevent txt2db from treating them as a separator
174 $infodb_value =~ s/-/&\#045;/gi;
175 }
176 print $infodb_handle "<$infodb_value_key>" . $infodb_value . "\n";
177 }
178 }
179 print $infodb_handle '-' x 70, "\n";
180}
181
182
183sub delete_infodb_entry
184{
185 my $infodb_handle = shift(@_);
186 my $infodb_key = shift(@_);
187
188 # A minus at the end of a key (after the ]) signifies 'delete'
189 print $infodb_handle "[$infodb_key]-\n";
190
191 # The 70 minus signs are also needed, to help make the parsing by db2txt simple
192 print $infodb_handle '-' x 70, "\n";
193}
194
195
196
197
198
1991;
Note: See TracBrowser for help on using the repository browser.