1 | package GDBMUtils;
|
---|
2 |
|
---|
3 | use strict;
|
---|
4 | use util;
|
---|
5 |
|
---|
6 | my $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 | # */
|
---|
12 | my $gdbmget_previous_collection = "";
|
---|
13 | my $gdbmget_previous_oid = "";
|
---|
14 | my $gdbmget_previous_value = "";
|
---|
15 |
|
---|
16 |
|
---|
17 |
|
---|
18 | sub 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 |
|
---|
33 | sub 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 |
|
---|
56 |
|
---|
57 | sub gdbmDatabaseAppend
|
---|
58 | {
|
---|
59 | my ($database, $oid, $value) = @_;
|
---|
60 |
|
---|
61 | # Are we in windows? Do we need .exe?
|
---|
62 | my $exe = &util::get_os_exe();
|
---|
63 |
|
---|
64 | # Escape any speech marks in the value
|
---|
65 | $value =~ s/\"/\\\"/g;
|
---|
66 | # Set the document content
|
---|
67 | print STDERR "#Set document\ncmd: gdbmset$exe \"$database\" \"$oid\" \"$value\" append\n" if $debug;
|
---|
68 | `gdbmset$exe "$database" "$oid" "$value" append`;
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | sub gdbmDatabaseSet
|
---|
73 | {
|
---|
74 | my ($database, $oid, $value) = @_;
|
---|
75 |
|
---|
76 | # Are we in windows? Do we need .exe?
|
---|
77 | my $exe = &util::get_os_exe();
|
---|
78 |
|
---|
79 | # Escape any speech marks in the value
|
---|
80 | $value =~ s/\"/\\\"/g;
|
---|
81 | # Set the document content
|
---|
82 | print STDERR "#Set document\ncmd: gdbmset$exe \"$database\" \"$oid\" \"$value\"\n" if $debug;
|
---|
83 | `gdbmset$exe "$database" "$oid" "$value"`;
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | sub gdbmDatabaseRemove
|
---|
88 | {
|
---|
89 | my ($database, $oid) = @_;
|
---|
90 |
|
---|
91 | # Are we in windows? Do we need .exe?
|
---|
92 | my $exe = &util::get_os_exe();
|
---|
93 |
|
---|
94 | # Remove the document from the database
|
---|
95 |
|
---|
96 | my $cmd = "gdbmdel$exe \"$database\" \"$oid\"";
|
---|
97 | print STDERR "#Delete document\ncmd: $cmd" if $debug;
|
---|
98 |
|
---|
99 | `$cmd`;
|
---|
100 |
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 |
|
---|
105 | # /** This wraps John T's gdbmget executable to get the gdbm database entry for
|
---|
106 | # * a particular OID.
|
---|
107 | # *
|
---|
108 | # * @param $collection is the collection name.
|
---|
109 | # * @param $oid is the internal document id.
|
---|
110 | # *
|
---|
111 | # *
|
---|
112 | # * @author John Rowe, DL Consulting Ltd.
|
---|
113 | # * @author John Thompson, DL Consulting Ltd.
|
---|
114 | # */
|
---|
115 | sub gdbmCachedCollectionGet
|
---|
116 | {
|
---|
117 | my ($collection, $oid) = @_;
|
---|
118 | # Start by checking if this request is the same as the previous one, and if
|
---|
119 | # so return the cache version instead. This is an optimization to improve
|
---|
120 | # performance when checking if a certain GDBM document exists before
|
---|
121 | # creating a new node object
|
---|
122 | if($collection eq $gdbmget_previous_collection
|
---|
123 | && $oid eq $gdbmget_previous_oid)
|
---|
124 | {
|
---|
125 | print STDERR "#Get document - using cached value\n" if $debug;
|
---|
126 | return $gdbmget_previous_value;
|
---|
127 | }
|
---|
128 |
|
---|
129 | # Where's the database?
|
---|
130 | my $database = _getDatabasePath($collection);
|
---|
131 |
|
---|
132 | my $value = gdbmDatabaseGet($database,$oid);
|
---|
133 |
|
---|
134 | # Tidy up the ever growing number of newlines at the end of the value
|
---|
135 | $value =~ s/(\r?\n)+/$1/g;
|
---|
136 | # Why do we need the above line? At the very least it would seem
|
---|
137 | # better that the data going in to the database through 'set' is
|
---|
138 | # monitored for superfluous \r\n which are then removed before being
|
---|
139 | # saved in GDBM
|
---|
140 |
|
---|
141 | # Cache this result
|
---|
142 | $gdbmget_previous_collection = $collection;
|
---|
143 | $gdbmget_previous_oid = $oid;
|
---|
144 | $gdbmget_previous_value = $value;
|
---|
145 |
|
---|
146 | # Done
|
---|
147 | return $value;
|
---|
148 | }
|
---|
149 | # /** gdbmCachedCollectionGet **/
|
---|
150 |
|
---|
151 | # /** This wraps John T's gdbmset executable to set the gdbm database entry for
|
---|
152 | # * a particular OID. This does not yet report errors.
|
---|
153 | # *
|
---|
154 | # * @param $collection is the collection name.
|
---|
155 | # * @param $oid is the internal document id.
|
---|
156 | # * @param $value is the new value to set for the oid.
|
---|
157 | # *
|
---|
158 | # * @author John Rowe, DL Consulting Ltd.
|
---|
159 | # */
|
---|
160 | sub gdbmCachedCollectionSet
|
---|
161 | {
|
---|
162 | my ($collection, $oid, $value) = @_;
|
---|
163 |
|
---|
164 | # Where's the database?
|
---|
165 | my $database = _getDatabasePath($collection);
|
---|
166 |
|
---|
167 |
|
---|
168 | # Check whether value is set
|
---|
169 | if (defined($value))
|
---|
170 | {
|
---|
171 | gdbmDatabaseSet($database,$oid,$value);
|
---|
172 | }
|
---|
173 | else
|
---|
174 | {
|
---|
175 | gdbmDatabaseRemove($database,$oid);
|
---|
176 | }
|
---|
177 |
|
---|
178 | # Empty any cached values, as they may now be invalid
|
---|
179 |
|
---|
180 | # Cache this result
|
---|
181 | $gdbmget_previous_collection = "";
|
---|
182 | $gdbmget_previous_oid = "";
|
---|
183 | $gdbmget_previous_value = 0;
|
---|
184 | }
|
---|
185 | # /** gdbmCollectionSet **/
|
---|
186 |
|
---|
187 | # /** This works out the database path and returns it to the calling
|
---|
188 | # * calling function.
|
---|
189 | # *
|
---|
190 | # * @param $collection The current collection name
|
---|
191 | # *
|
---|
192 | # * @author John Rowe, DL Consulting Ltd.
|
---|
193 | # */
|
---|
194 |
|
---|
195 | sub _getDatabasePath
|
---|
196 | {
|
---|
197 | my $collection = shift(@_);
|
---|
198 |
|
---|
199 | # Find out the database extension
|
---|
200 | my $ext = &util::is_little_endian() ? ".ldb" : ".bdb";
|
---|
201 |
|
---|
202 | # Now return the full filename of the database
|
---|
203 |
|
---|
204 | return &util::filename_cat($ENV{'GSDLHOME'}, "collect", $collection, "index", "text", $collection.$ext);
|
---|
205 | }
|
---|
206 | # /** getDatabasePath **/
|
---|
207 |
|
---|
208 | 1;
|
---|