[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 | print STDERR "#Set document\ncmd: gdbmset$exe \"$database\" \"$oid\"\n" if $debug;
|
---|
| 96 |
|
---|
[18456] | 97 | `gdbmdel$exe "$database" "$oid"`;
|
---|
[17087] | 98 | }
|
---|
| 99 |
|
---|
| 100 |
|
---|
| 101 |
|
---|
[12844] | 102 | # /** This wraps John T's gdbmget executable to get the gdbm database entry for
|
---|
| 103 | # * a particular OID.
|
---|
| 104 | # *
|
---|
| 105 | # * @param $collection is the collection name.
|
---|
| 106 | # * @param $oid is the internal document id.
|
---|
| 107 | # *
|
---|
| 108 | # *
|
---|
| 109 | # * @author John Rowe, DL Consulting Ltd.
|
---|
| 110 | # * @author John Thompson, DL Consulting Ltd.
|
---|
| 111 | # */
|
---|
[17087] | 112 | sub gdbmCachedCollectionGet
|
---|
[12844] | 113 | {
|
---|
| 114 | my ($collection, $oid) = @_;
|
---|
| 115 | # Start by checking if this request is the same as the previous one, and if
|
---|
| 116 | # so return the cache version instead. This is an optimization to improve
|
---|
| 117 | # performance when checking if a certain GDBM document exists before
|
---|
| 118 | # creating a new node object
|
---|
| 119 | if($collection eq $gdbmget_previous_collection
|
---|
| 120 | && $oid eq $gdbmget_previous_oid)
|
---|
| 121 | {
|
---|
| 122 | print STDERR "#Get document - using cached value\n" if $debug;
|
---|
| 123 | return $gdbmget_previous_value;
|
---|
| 124 | }
|
---|
[17087] | 125 |
|
---|
[12844] | 126 | # Where's the database?
|
---|
[17087] | 127 | my $database = _getDatabasePath($collection);
|
---|
| 128 |
|
---|
[17285] | 129 | my $value = gdbmDatabaseGet($database,$oid);
|
---|
[17087] | 130 |
|
---|
[12844] | 131 | # Tidy up the ever growing number of newlines at the end of the value
|
---|
| 132 | $value =~ s/(\r?\n)+/$1/g;
|
---|
[17087] | 133 | # Why do we need the above line? At the very least it would seem
|
---|
| 134 | # better that the data going in to the database through 'set' is
|
---|
| 135 | # monitored for superfluous \r\n which are then removed before being
|
---|
| 136 | # saved in GDBM
|
---|
| 137 |
|
---|
[12844] | 138 | # Cache this result
|
---|
| 139 | $gdbmget_previous_collection = $collection;
|
---|
| 140 | $gdbmget_previous_oid = $oid;
|
---|
| 141 | $gdbmget_previous_value = $value;
|
---|
[17087] | 142 |
|
---|
[12844] | 143 | # Done
|
---|
| 144 | return $value;
|
---|
| 145 | }
|
---|
[17087] | 146 | # /** gdbmCachedCollectionGet **/
|
---|
[12844] | 147 |
|
---|
| 148 | # /** This wraps John T's gdbmset executable to set the gdbm database entry for
|
---|
| 149 | # * a particular OID. This does not yet report errors.
|
---|
| 150 | # *
|
---|
| 151 | # * @param $collection is the collection name.
|
---|
| 152 | # * @param $oid is the internal document id.
|
---|
| 153 | # * @param $value is the new value to set for the oid.
|
---|
| 154 | # *
|
---|
| 155 | # * @author John Rowe, DL Consulting Ltd.
|
---|
| 156 | # */
|
---|
[17087] | 157 | sub gdbmCachedCollectionSet
|
---|
[12844] | 158 | {
|
---|
| 159 | my ($collection, $oid, $value) = @_;
|
---|
[17087] | 160 |
|
---|
[12844] | 161 | # Where's the database?
|
---|
[17087] | 162 | my $database = _getDatabasePath($collection);
|
---|
| 163 |
|
---|
| 164 |
|
---|
[12844] | 165 | # Check whether value is set
|
---|
| 166 | if (defined($value))
|
---|
| 167 | {
|
---|
[17087] | 168 | gdbmDatabaseSet($database,$oid,$value);
|
---|
[12844] | 169 | }
|
---|
| 170 | else
|
---|
| 171 | {
|
---|
[17285] | 172 | gdbmDatabaseRemove($database,$oid);
|
---|
[12844] | 173 | }
|
---|
[17087] | 174 |
|
---|
[12844] | 175 | # Empty any cached values, as they may now be invalid
|
---|
[17087] | 176 |
|
---|
[12844] | 177 | # Cache this result
|
---|
| 178 | $gdbmget_previous_collection = "";
|
---|
| 179 | $gdbmget_previous_oid = "";
|
---|
| 180 | $gdbmget_previous_value = 0;
|
---|
| 181 | }
|
---|
[17087] | 182 | # /** gdbmCollectionSet **/
|
---|
[12844] | 183 |
|
---|
| 184 | # /** This works out the database path and returns it to the calling
|
---|
| 185 | # * calling function.
|
---|
| 186 | # *
|
---|
| 187 | # * @param $collection The current collection name
|
---|
| 188 | # *
|
---|
| 189 | # * @author John Rowe, DL Consulting Ltd.
|
---|
| 190 | # */
|
---|
[17087] | 191 |
|
---|
| 192 | sub _getDatabasePath
|
---|
[12844] | 193 | {
|
---|
[15890] | 194 | my $collection = shift(@_);
|
---|
[17087] | 195 |
|
---|
[12844] | 196 | # Find out the database extension
|
---|
[17087] | 197 | my $ext = &util::is_little_endian() ? ".ldb" : ".bdb";
|
---|
| 198 |
|
---|
[12844] | 199 | # Now return the full filename of the database
|
---|
[17087] | 200 |
|
---|
[12844] | 201 | return &util::filename_cat($ENV{'GSDLHOME'}, "collect", $collection, "index", "text", $collection.$ext);
|
---|
| 202 | }
|
---|
[17087] | 203 | # /** getDatabasePath **/
|
---|
[12844] | 204 |
|
---|
| 205 | 1;
|
---|