source: main/trunk/greenstone2/perllib/dbutil/gdbmtxtgz.pm@ 28395

Last change on this file since 28395 was 28395, checked in by davidb, 11 years ago

Support for running under Cygwin added

File size: 5.3 KB
Line 
1###########################################################################
2#
3# dbutil::gdbmtxtgz -- utility functions for writing to gdbm-txtgz 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::gdbmtxtgz;
28
29use strict;
30
31
32
33# -----------------------------------------------------------------------------
34# GDBM TXT-GZ IMPLEMENTATION
35# -----------------------------------------------------------------------------
36
37sub open_infodb_write_handle
38{
39 # Keep infodb in GDBM neutral form => save data as compressed text file,
40 # read for txt2db to be run on it later (i.e. by the runtime system,
41 # first time the collection is ever accessed). This makes it easier
42 # distribute pre-built collections to various architectures.
43 #
44 # NB: even if two architectures are little endian (e.g. Intel and
45 # ARM procesors) GDBM does *not* guarantee that the database generated on
46 # one will work on the other
47
48 my $infodb_file_path = shift(@_);
49
50 # Greenstone ships with gzip for windows, on $PATH
51
52 my $infodb_file_handle = undef;
53 if (!open($infodb_file_handle, "| gzip - > \"$infodb_file_path\""))
54 {
55 print STDERR "Error: Failed to open pipe to gzip - > \"$infodb_file_path\"\n";
56 print STDERR " $!\n";
57 return undef;
58 }
59 binmode($infodb_file_handle,":utf8");
60 return $infodb_file_handle;
61}
62
63
64sub close_infodb_write_handle
65{
66 my $infodb_handle = shift(@_);
67
68 close($infodb_handle);
69}
70
71
72sub get_infodb_file_path
73{
74 my $collection_name = shift(@_);
75 my $infodb_directory_path = shift(@_);
76
77 my $infodb_file_name = &util::get_dirsep_tail($collection_name).".txt.gz";
78 return &util::filename_cat($infodb_directory_path, $infodb_file_name);
79}
80
81
82sub read_infodb_file
83{
84 my $infodb_file_path = shift(@_);
85 my $infodb_map = shift(@_);
86
87 my $cmd = "gzip --decompress --to-stdout \"$infodb_file_path\"";
88
89 open (PIPEIN, "$cmd |")
90 || die "Error: Couldn't open pipe from gzip: $!\n $cmd\n";
91
92 binmode(PIPEIN,":utf8");
93 my $infodb_line = "";
94 my $infodb_key = "";
95 my $infodb_value = "";
96 while (defined ($infodb_line = <PIPEIN>))
97 {
98 $infodb_line =~ s/(\r\n)+$//; # more general than chomp
99
100 if ($infodb_line =~ /^\[([^\]]+)\]$/)
101 {
102 $infodb_key = $1;
103 }
104 elsif ($infodb_line =~ /^-{70}$/)
105 {
106 $infodb_map->{$infodb_key} = $infodb_value;
107 $infodb_key = "";
108 $infodb_value = "";
109 }
110 else
111 {
112 $infodb_value .= $infodb_line;
113 }
114 }
115
116 close (PIPEIN);
117}
118
119
120sub read_infodb_keys
121{
122 my $infodb_file_path = shift(@_);
123 my $infodb_map = shift(@_);
124
125 my $cmd = "gzip --decompress --to-stdout \"$infodb_file_path\"";
126
127 open (PIPEIN, "$cmd |")
128 || die "Error: Couldn't open pipe from gzip: $!\n $cmd\n";
129
130 binmode(PIPEIN,":utf8");
131 my $infodb_line = "";
132 my $infodb_key = "";
133 while (defined ($infodb_line = <PIPEIN>))
134 {
135 $infodb_line =~ s/(\r\n)+$//; # more general than chomp
136
137 if ($infodb_line =~ /^\[([^\]]+)\]$/)
138 {
139 $infodb_key = $1;
140 }
141 elsif ($infodb_line =~ /^-{70}$/)
142 {
143 $infodb_map->{$infodb_key} = 1;
144 $infodb_key = "";
145 }
146 }
147
148 close (PIPEIN);
149}
150
151
152sub write_infodb_entry
153{
154
155 my $infodb_handle = shift(@_);
156 my $infodb_key = shift(@_);
157 my $infodb_map = shift(@_);
158
159 print $infodb_handle "[$infodb_key]\n";
160 foreach my $infodb_value_key (sort keys(%$infodb_map))
161 {
162 foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
163 {
164 if ($infodb_value =~ /-{70,}/)
165 {
166 # if value contains 70 or more hyphens in a row we need to escape them
167 # to prevent txt2db from treating them as a separator
168 $infodb_value =~ s/-/&\#045;/gi;
169 }
170 print $infodb_handle "<$infodb_value_key>" . $infodb_value . "\n";
171 }
172 }
173 print $infodb_handle '-' x 70, "\n";
174}
175
176
177
178sub write_infodb_rawentry
179{
180
181 my $infodb_handle = shift(@_);
182 my $infodb_key = shift(@_);
183 my $infodb_val = shift(@_);
184
185 print $infodb_handle "[$infodb_key]\n";
186 print $infodb_handle "$infodb_val\n";
187 print $infodb_handle '-' x 70, "\n";
188}
189
190 sub set_infodb_entry
191{
192 my $infodb_file_path = shift(@_);
193 my $infodb_key = shift(@_);
194 my $infodb_map = shift(@_);
195
196 print STDERR "***** gdbmtxtgz::set_infodb_entry() not implemented yet!\n";
197}
198
199
200
201sub delete_infodb_entry
202{
203
204 my $infodb_handle = shift(@_);
205 my $infodb_key = shift(@_);
206
207
208 # A minus at the end of a key (after the ]) signifies 'delete'
209 print $infodb_handle "[$infodb_key]-\n";
210
211 # The 70 minus signs are also needed, to help make the parsing by db2txt simple
212 print $infodb_handle '-' x 70, "\n";
213}
214
215
216
2171;
Note: See TracBrowser for help on using the repository browser.