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

Last change on this file since 21856 was 21856, checked in by davidb, 14 years ago

read_infodb_entry() implemented for sqlite. new write_infodb_rawentry() added for main dbutil.dm class

File size: 4.2 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 return undef;
56 }
57
58 return $infodb_file_handle;
59}
60
61
62sub close_infodb_write_handle
63{
64 my $infodb_handle = shift(@_);
65
66 close($infodb_handle);
67}
68
69
70sub get_infodb_file_path
71{
72 my $collection_name = shift(@_);
73 my $infodb_directory_path = shift(@_);
74
75 my $infodb_file_name = &util::get_dirsep_tail($collection_name).".txt.gz";
76 return &util::filename_cat($infodb_directory_path, $infodb_file_name);
77}
78
79
80sub read_infodb_file
81{
82 my $infodb_file_path = shift(@_);
83 my $infodb_map = shift(@_);
84
85 my $cmd = "gzip --decompress \"$infodb_file_path\"";
86
87 open (PIPEIN, "$cmd |")
88 || die "Error: Couldn't open pipe from gzip: $!\n $cmd\n";
89
90 my $infodb_line = "";
91 my $infodb_key = "";
92 my $infodb_value = "";
93 while (defined ($infodb_line = <PIPEIN>))
94 {
95 if ($infodb_line =~ /^\[([^\]]+)\]$/)
96 {
97 $infodb_key = $1;
98 }
99 elsif ($infodb_line =~ /^-{70}$/)
100 {
101 $infodb_map->{$infodb_key} = $infodb_value;
102 $infodb_key = "";
103 $infodb_value = "";
104 }
105 else
106 {
107 $infodb_value .= $infodb_line;
108 }
109 }
110
111 close (PIPEIN);
112}
113
114
115sub write_infodb_entry
116{
117
118 my $infodb_handle = shift(@_);
119 my $infodb_key = shift(@_);
120 my $infodb_map = shift(@_);
121
122 print $infodb_handle "[$infodb_key]\n";
123 foreach my $infodb_value_key (keys(%$infodb_map))
124 {
125 foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
126 {
127 if ($infodb_value =~ /-{70,}/)
128 {
129 # if value contains 70 or more hyphens in a row we need to escape them
130 # to prevent txt2db from treating them as a separator
131 $infodb_value =~ s/-/&\#045;/gi;
132 }
133 print $infodb_handle "<$infodb_value_key>" . $infodb_value . "\n";
134 }
135 }
136 print $infodb_handle '-' x 70, "\n";
137}
138
139
140
141sub write_infodb_rawentry
142{
143
144 my $infodb_handle = shift(@_);
145 my $infodb_key = shift(@_);
146 my $infodb_val = shift(@_);
147
148 print $infodb_handle "[$infodb_key]\n";
149 print $infodb_handle "$infodb_val\n";
150 print $infodb_handle '-' x 70, "\n";
151}
152
153
154
155sub delete_infodb_entry
156{
157
158 my $infodb_handle = shift(@_);
159 my $infodb_key = shift(@_);
160
161
162 # A minus at the end of a key (after the ]) signifies 'delete'
163 print $infodb_handle "[$infodb_key]-\n";
164
165 # The 70 minus signs are also needed, to help make the parsing by db2txt simple
166 print $infodb_handle '-' x 70, "\n";
167}
168
169
170
1711;
Note: See TracBrowser for help on using the repository browser.