[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 |
|
---|
[20610] | 33 | sub parseStringToHash
|
---|
[18456] | 34 | {
|
---|
[20610] | 35 | my ($val) = @_;
|
---|
[18456] | 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 |
|
---|
[20610] | 54 | sub gdbmRecordToHash
|
---|
| 55 | {
|
---|
| 56 | my ($database, $oid) = @_;
|
---|
| 57 |
|
---|
| 58 | my $val = gdbmDatabaseGet($database,$oid);
|
---|
| 59 |
|
---|
| 60 | return parseStringToHash($val);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[19499] | 63 | sub serializeHash
|
---|
| 64 | {
|
---|
| 65 | my ($hash) = @_;
|
---|
[18456] | 66 |
|
---|
[19499] | 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 |
|
---|
[17087] | 81 | sub gdbmDatabaseAppend
|
---|
[18456] | 82 | {
|
---|
[17087] | 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 |
|
---|
| 96 | sub 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 |
|
---|
| 111 | sub 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 |
|
---|
[18469] | 120 | my $cmd = "gdbmdel$exe \"$database\" \"$oid\"";
|
---|
[20806] | 121 | print STDERR "#Delete document\ncmd: $cmd\n" if $debug;
|
---|
[18469] | 122 |
|
---|
| 123 | `$cmd`;
|
---|
| 124 |
|
---|
[17087] | 125 | }
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 |
|
---|
[12844] | 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 | # */
|
---|
[17087] | 139 | sub gdbmCachedCollectionGet
|
---|
[12844] | 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 | }
|
---|
[17087] | 152 |
|
---|
[12844] | 153 | # Where's the database?
|
---|
[17087] | 154 | my $database = _getDatabasePath($collection);
|
---|
| 155 |
|
---|
[17285] | 156 | my $value = gdbmDatabaseGet($database,$oid);
|
---|
[17087] | 157 |
|
---|
[12844] | 158 | # Tidy up the ever growing number of newlines at the end of the value
|
---|
| 159 | $value =~ s/(\r?\n)+/$1/g;
|
---|
[17087] | 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 |
|
---|
[12844] | 165 | # Cache this result
|
---|
| 166 | $gdbmget_previous_collection = $collection;
|
---|
| 167 | $gdbmget_previous_oid = $oid;
|
---|
| 168 | $gdbmget_previous_value = $value;
|
---|
[17087] | 169 |
|
---|
[12844] | 170 | # Done
|
---|
| 171 | return $value;
|
---|
| 172 | }
|
---|
[17087] | 173 | # /** gdbmCachedCollectionGet **/
|
---|
[12844] | 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 | # */
|
---|
[17087] | 184 | sub gdbmCachedCollectionSet
|
---|
[12844] | 185 | {
|
---|
| 186 | my ($collection, $oid, $value) = @_;
|
---|
[17087] | 187 |
|
---|
[12844] | 188 | # Where's the database?
|
---|
[17087] | 189 | my $database = _getDatabasePath($collection);
|
---|
| 190 |
|
---|
| 191 |
|
---|
[12844] | 192 | # Check whether value is set
|
---|
| 193 | if (defined($value))
|
---|
| 194 | {
|
---|
[17087] | 195 | gdbmDatabaseSet($database,$oid,$value);
|
---|
[12844] | 196 | }
|
---|
| 197 | else
|
---|
| 198 | {
|
---|
[17285] | 199 | gdbmDatabaseRemove($database,$oid);
|
---|
[12844] | 200 | }
|
---|
[17087] | 201 |
|
---|
[12844] | 202 | # Empty any cached values, as they may now be invalid
|
---|
[17087] | 203 |
|
---|
[12844] | 204 | # Cache this result
|
---|
| 205 | $gdbmget_previous_collection = "";
|
---|
| 206 | $gdbmget_previous_oid = "";
|
---|
| 207 | $gdbmget_previous_value = 0;
|
---|
| 208 | }
|
---|
[17087] | 209 | # /** gdbmCollectionSet **/
|
---|
[12844] | 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 | # */
|
---|
[17087] | 218 |
|
---|
| 219 | sub _getDatabasePath
|
---|
[12844] | 220 | {
|
---|
[15890] | 221 | my $collection = shift(@_);
|
---|
[17087] | 222 |
|
---|
[18658] | 223 | # Return the full filename of the database
|
---|
[17087] | 224 |
|
---|
[18658] | 225 | return &util::filename_cat($ENV{'GSDLHOME'}, "collect", $collection, "index", "text", "$collection.gdb");
|
---|
[12844] | 226 | }
|
---|
[17087] | 227 | # /** getDatabasePath **/
|
---|
[12844] | 228 |
|
---|
| 229 | 1;
|
---|