source: gsdl/trunk/perllib/GDBMUtils.pm@ 19620

Last change on this file since 19620 was 19499, checked in by davidb, 15 years ago

Additional work on supporting Greenstone CGI-based API

  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
1package GDBMUtils;
2
3use strict;
4use util;
5
6my $debug = 0;
7
8# /** Global variables to hold a strings containing:
9# * the last collection, oid and value
10# * a gdbmCachedCollectionGet() was performed on.
11# */
12my $gdbmget_previous_collection = "";
13my $gdbmget_previous_oid = "";
14my $gdbmget_previous_value = "";
15
16
17
18sub gdbmDatabaseGet
19{
20 my ($database, $oid) = @_;
21
22 # Are we in windows? Do we need .exe?
23 my $exe = &util::get_os_exe();
24
25 # Retrieve the raw document content
26 print STDERR "#Get document\ncmd: gdbmget$exe \"$database\" \"$oid\"\n" if $debug;
27 my $value = `gdbmget$exe "$database" "$oid"`;
28
29 # Done
30 return $value;
31}
32
33sub gdbmRecordToHash
34{
35 my ($database, $oid) = @_;
36
37 my $val = gdbmDatabaseGet($database,$oid);
38
39 my $rec = {};
40
41 while ($val =~ m/^<(.*?)>(.*)$/mg) {
42 my $metaname = $1;
43 my $metavalue = $2;
44
45 if (!defined $rec->{$metaname}) {
46 $rec->{$metaname} = [ $metavalue ];
47 }
48 else {
49 push(@{$rec->{$metaname}},$metavalue);
50 }
51 }
52
53 return $rec;
54}
55
56sub serializeHash
57{
58 my ($hash) = @_;
59
60 my $shash = "";
61
62 foreach my $metaname (keys %$hash) {
63 my $metavals = $hash->{$metaname};
64 foreach my $metaval (@$metavals) {
65 # need to escape chars HTML entities ??
66 $shash .= "<$metaname>$metaval\n";
67 }
68 }
69
70 return $shash;
71}
72
73
74sub gdbmDatabaseAppend
75{
76 my ($database, $oid, $value) = @_;
77
78 # Are we in windows? Do we need .exe?
79 my $exe = &util::get_os_exe();
80
81 # Escape any speech marks in the value
82 $value =~ s/\"/\\\"/g;
83 # Set the document content
84 print STDERR "#Set document\ncmd: gdbmset$exe \"$database\" \"$oid\" \"$value\" append\n" if $debug;
85 `gdbmset$exe "$database" "$oid" "$value" append`;
86}
87
88
89sub gdbmDatabaseSet
90 {
91 my ($database, $oid, $value) = @_;
92
93 # Are we in windows? Do we need .exe?
94 my $exe = &util::get_os_exe();
95
96 # Escape any speech marks in the value
97 $value =~ s/\"/\\\"/g;
98 # Set the document content
99 print STDERR "#Set document\ncmd: gdbmset$exe \"$database\" \"$oid\" \"$value\"\n" if $debug;
100 `gdbmset$exe "$database" "$oid" "$value"`;
101}
102
103
104sub gdbmDatabaseRemove
105 {
106 my ($database, $oid) = @_;
107
108 # Are we in windows? Do we need .exe?
109 my $exe = &util::get_os_exe();
110
111 # Remove the document from the database
112
113 my $cmd = "gdbmdel$exe \"$database\" \"$oid\"";
114 print STDERR "#Delete document\ncmd: $cmd" if $debug;
115
116 `$cmd`;
117
118}
119
120
121
122# /** This wraps John T's gdbmget executable to get the gdbm database entry for
123# * a particular OID.
124# *
125# * @param $collection is the collection name.
126# * @param $oid is the internal document id.
127# *
128# *
129# * @author John Rowe, DL Consulting Ltd.
130# * @author John Thompson, DL Consulting Ltd.
131# */
132sub gdbmCachedCollectionGet
133 {
134 my ($collection, $oid) = @_;
135 # Start by checking if this request is the same as the previous one, and if
136 # so return the cache version instead. This is an optimization to improve
137 # performance when checking if a certain GDBM document exists before
138 # creating a new node object
139 if($collection eq $gdbmget_previous_collection
140 && $oid eq $gdbmget_previous_oid)
141 {
142 print STDERR "#Get document - using cached value\n" if $debug;
143 return $gdbmget_previous_value;
144 }
145
146 # Where's the database?
147 my $database = _getDatabasePath($collection);
148
149 my $value = gdbmDatabaseGet($database,$oid);
150
151 # Tidy up the ever growing number of newlines at the end of the value
152 $value =~ s/(\r?\n)+/$1/g;
153 # Why do we need the above line? At the very least it would seem
154 # better that the data going in to the database through 'set' is
155 # monitored for superfluous \r\n which are then removed before being
156 # saved in GDBM
157
158 # Cache this result
159 $gdbmget_previous_collection = $collection;
160 $gdbmget_previous_oid = $oid;
161 $gdbmget_previous_value = $value;
162
163 # Done
164 return $value;
165 }
166# /** gdbmCachedCollectionGet **/
167
168# /** This wraps John T's gdbmset executable to set the gdbm database entry for
169# * a particular OID. This does not yet report errors.
170# *
171# * @param $collection is the collection name.
172# * @param $oid is the internal document id.
173# * @param $value is the new value to set for the oid.
174# *
175# * @author John Rowe, DL Consulting Ltd.
176# */
177sub gdbmCachedCollectionSet
178 {
179 my ($collection, $oid, $value) = @_;
180
181 # Where's the database?
182 my $database = _getDatabasePath($collection);
183
184
185 # Check whether value is set
186 if (defined($value))
187 {
188 gdbmDatabaseSet($database,$oid,$value);
189 }
190 else
191 {
192 gdbmDatabaseRemove($database,$oid);
193 }
194
195 # Empty any cached values, as they may now be invalid
196
197 # Cache this result
198 $gdbmget_previous_collection = "";
199 $gdbmget_previous_oid = "";
200 $gdbmget_previous_value = 0;
201 }
202# /** gdbmCollectionSet **/
203
204# /** This works out the database path and returns it to the calling
205# * calling function.
206# *
207# * @param $collection The current collection name
208# *
209# * @author John Rowe, DL Consulting Ltd.
210# */
211
212sub _getDatabasePath
213 {
214 my $collection = shift(@_);
215
216 # Return the full filename of the database
217
218 return &util::filename_cat($ENV{'GSDLHOME'}, "collect", $collection, "index", "text", "$collection.gdb");
219 }
220# /** getDatabasePath **/
221
2221;
Note: See TracBrowser for help on using the repository browser.