[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 |
|
---|
| 56 |
|
---|
[17087] | 57 | sub gdbmDatabaseAppend
|
---|
[18456] | 58 | {
|
---|
[17087] | 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 |
|
---|
[18469] | 96 | my $cmd = "gdbmdel$exe \"$database\" \"$oid\"";
|
---|
| 97 | print STDERR "#Delete document\ncmd: $cmd" if $debug;
|
---|
| 98 |
|
---|
| 99 | `$cmd`;
|
---|
| 100 |
|
---|
[17087] | 101 | }
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 |
|
---|
[12844] | 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 | # */
|
---|
[17087] | 115 | sub gdbmCachedCollectionGet
|
---|
[12844] | 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 | }
|
---|
[17087] | 128 |
|
---|
[12844] | 129 | # Where's the database?
|
---|
[17087] | 130 | my $database = _getDatabasePath($collection);
|
---|
| 131 |
|
---|
[17285] | 132 | my $value = gdbmDatabaseGet($database,$oid);
|
---|
[17087] | 133 |
|
---|
[12844] | 134 | # Tidy up the ever growing number of newlines at the end of the value
|
---|
| 135 | $value =~ s/(\r?\n)+/$1/g;
|
---|
[17087] | 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 |
|
---|
[12844] | 141 | # Cache this result
|
---|
| 142 | $gdbmget_previous_collection = $collection;
|
---|
| 143 | $gdbmget_previous_oid = $oid;
|
---|
| 144 | $gdbmget_previous_value = $value;
|
---|
[17087] | 145 |
|
---|
[12844] | 146 | # Done
|
---|
| 147 | return $value;
|
---|
| 148 | }
|
---|
[17087] | 149 | # /** gdbmCachedCollectionGet **/
|
---|
[12844] | 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 | # */
|
---|
[17087] | 160 | sub gdbmCachedCollectionSet
|
---|
[12844] | 161 | {
|
---|
| 162 | my ($collection, $oid, $value) = @_;
|
---|
[17087] | 163 |
|
---|
[12844] | 164 | # Where's the database?
|
---|
[17087] | 165 | my $database = _getDatabasePath($collection);
|
---|
| 166 |
|
---|
| 167 |
|
---|
[12844] | 168 | # Check whether value is set
|
---|
| 169 | if (defined($value))
|
---|
| 170 | {
|
---|
[17087] | 171 | gdbmDatabaseSet($database,$oid,$value);
|
---|
[12844] | 172 | }
|
---|
| 173 | else
|
---|
| 174 | {
|
---|
[17285] | 175 | gdbmDatabaseRemove($database,$oid);
|
---|
[12844] | 176 | }
|
---|
[17087] | 177 |
|
---|
[12844] | 178 | # Empty any cached values, as they may now be invalid
|
---|
[17087] | 179 |
|
---|
[12844] | 180 | # Cache this result
|
---|
| 181 | $gdbmget_previous_collection = "";
|
---|
| 182 | $gdbmget_previous_oid = "";
|
---|
| 183 | $gdbmget_previous_value = 0;
|
---|
| 184 | }
|
---|
[17087] | 185 | # /** gdbmCollectionSet **/
|
---|
[12844] | 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 | # */
|
---|
[17087] | 194 |
|
---|
| 195 | sub _getDatabasePath
|
---|
[12844] | 196 | {
|
---|
[15890] | 197 | my $collection = shift(@_);
|
---|
[17087] | 198 |
|
---|
[18658] | 199 | # Return the full filename of the database
|
---|
[17087] | 200 |
|
---|
[18658] | 201 | return &util::filename_cat($ENV{'GSDLHOME'}, "collect", $collection, "index", "text", "$collection.gdb");
|
---|
[12844] | 202 | }
|
---|
[17087] | 203 | # /** getDatabasePath **/
|
---|
[12844] | 204 |
|
---|
| 205 | 1;
|
---|