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

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

parseStringToHash brought out as a useful subroutine in its own right

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