[12844] | 1 | package GDBMUtils;
|
---|
| 2 |
|
---|
[15890] | 3 | use strict;
|
---|
[12844] | 4 | use util;
|
---|
| 5 |
|
---|
[15890] | 6 | my $debug = 0;
|
---|
[12844] | 7 |
|
---|
[17087] | 8 | # /** Global variables to hold a strings containing:
|
---|
| 9 | # * the last collection, oid and value
|
---|
| 10 | # * a gdbmCachedCollectionGet() was performed on.
|
---|
[12844] | 11 | # */
|
---|
| 12 | my $gdbmget_previous_collection = "";
|
---|
| 13 | my $gdbmget_previous_oid = "";
|
---|
| 14 | my $gdbmget_previous_value = "";
|
---|
| 15 |
|
---|
[17087] | 16 |
|
---|
| 17 |
|
---|
| 18 | sub gdbmDatabaseGet
|
---|
[18456] | 19 | {
|
---|
[17087] | 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;
|
---|
[18456] | 31 | }
|
---|
[17087] | 32 |
|
---|
[18456] | 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 |
|
---|
[19499] | 56 | sub serializeHash
|
---|
| 57 | {
|
---|
| 58 | my ($hash) = @_;
|
---|
[18456] | 59 |
|
---|
[19499] | 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 |
|
---|
[17087] | 74 | sub gdbmDatabaseAppend
|
---|
[18456] | 75 | {
|
---|
[17087] | 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 |
|
---|
| 89 | sub 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 |
|
---|
| 104 | sub 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 |
|
---|
[18469] | 113 | my $cmd = "gdbmdel$exe \"$database\" \"$oid\"";
|
---|
| 114 | print STDERR "#Delete document\ncmd: $cmd" if $debug;
|
---|
| 115 |
|
---|
| 116 | `$cmd`;
|
---|
| 117 |
|
---|
[17087] | 118 | }
|
---|
| 119 |
|
---|
| 120 |
|
---|
| 121 |
|
---|
[12844] | 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 | # */
|
---|
[17087] | 132 | sub gdbmCachedCollectionGet
|
---|
[12844] | 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 | }
|
---|
[17087] | 145 |
|
---|
[12844] | 146 | # Where's the database?
|
---|
[17087] | 147 | my $database = _getDatabasePath($collection);
|
---|
| 148 |
|
---|
[17285] | 149 | my $value = gdbmDatabaseGet($database,$oid);
|
---|
[17087] | 150 |
|
---|
[12844] | 151 | # Tidy up the ever growing number of newlines at the end of the value
|
---|
| 152 | $value =~ s/(\r?\n)+/$1/g;
|
---|
[17087] | 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 |
|
---|
[12844] | 158 | # Cache this result
|
---|
| 159 | $gdbmget_previous_collection = $collection;
|
---|
| 160 | $gdbmget_previous_oid = $oid;
|
---|
| 161 | $gdbmget_previous_value = $value;
|
---|
[17087] | 162 |
|
---|
[12844] | 163 | # Done
|
---|
| 164 | return $value;
|
---|
| 165 | }
|
---|
[17087] | 166 | # /** gdbmCachedCollectionGet **/
|
---|
[12844] | 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 | # */
|
---|
[17087] | 177 | sub gdbmCachedCollectionSet
|
---|
[12844] | 178 | {
|
---|
| 179 | my ($collection, $oid, $value) = @_;
|
---|
[17087] | 180 |
|
---|
[12844] | 181 | # Where's the database?
|
---|
[17087] | 182 | my $database = _getDatabasePath($collection);
|
---|
| 183 |
|
---|
| 184 |
|
---|
[12844] | 185 | # Check whether value is set
|
---|
| 186 | if (defined($value))
|
---|
| 187 | {
|
---|
[17087] | 188 | gdbmDatabaseSet($database,$oid,$value);
|
---|
[12844] | 189 | }
|
---|
| 190 | else
|
---|
| 191 | {
|
---|
[17285] | 192 | gdbmDatabaseRemove($database,$oid);
|
---|
[12844] | 193 | }
|
---|
[17087] | 194 |
|
---|
[12844] | 195 | # Empty any cached values, as they may now be invalid
|
---|
[17087] | 196 |
|
---|
[12844] | 197 | # Cache this result
|
---|
| 198 | $gdbmget_previous_collection = "";
|
---|
| 199 | $gdbmget_previous_oid = "";
|
---|
| 200 | $gdbmget_previous_value = 0;
|
---|
| 201 | }
|
---|
[17087] | 202 | # /** gdbmCollectionSet **/
|
---|
[12844] | 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 | # */
|
---|
[17087] | 211 |
|
---|
| 212 | sub _getDatabasePath
|
---|
[12844] | 213 | {
|
---|
[15890] | 214 | my $collection = shift(@_);
|
---|
[17087] | 215 |
|
---|
[18658] | 216 | # Return the full filename of the database
|
---|
[17087] | 217 |
|
---|
[18658] | 218 | return &util::filename_cat($ENV{'GSDLHOME'}, "collect", $collection, "index", "text", "$collection.gdb");
|
---|
[12844] | 219 | }
|
---|
[17087] | 220 | # /** getDatabasePath **/
|
---|
[12844] | 221 |
|
---|
| 222 | 1;
|
---|