source: gsdl/trunk/perllib/GDBMUtils.pm@ 15890

Last change on this file since 15890 was 15890, checked in by mdewsnip, 16 years ago

Adding "use strict", and fixing problems identified.

  • Property svn:keywords set to Author Date Id Revision
File size: 3.8 KB
Line 
1package GDBMUtils;
2
3use strict;
4use util;
5
6my $debug = 0;
7
8# /** Global variable to hold a string containing the last collection a gdbmGet
9# * was performed on.
10# */
11my $gdbmget_previous_collection = "";
12# /** Global variable to hold a string containing the last oid a gdbmGet was
13# * performed on.
14# */
15my $gdbmget_previous_oid = "";
16# /** Global variable to hold a string containing the resulting value of the
17# * last gdbmGet request.
18# */
19my $gdbmget_previous_value = "";
20
21# /** This wraps John T's gdbmget executable to get the gdbm database entry for
22# * a particular OID.
23# *
24# * @param $collection is the collection name.
25# * @param $oid is the internal document id.
26# *
27# *
28# * @author John Rowe, DL Consulting Ltd.
29# * @author John Thompson, DL Consulting Ltd.
30# */
31sub gdbmGet()
32 {
33 my ($collection, $oid) = @_;
34 # Start by checking if this request is the same as the previous one, and if
35 # so return the cache version instead. This is an optimization to improve
36 # performance when checking if a certain GDBM document exists before
37 # creating a new node object
38 if($collection eq $gdbmget_previous_collection
39 && $oid eq $gdbmget_previous_oid)
40 {
41 print STDERR "#Get document - using cached value\n" if $debug;
42 return $gdbmget_previous_value;
43 }
44 # Where's the database?
45 my $database = &getDatabasePath($collection);
46 # Are we in windows? Do we need .exe?
47 my $exe = "";
48 $exe = ".exe" if $ENV{'GSDLOS'} =~ /^windows$/i;
49 # Retrieve the raw document content
50 print STDERR "#Get document\ncmd: gdbmget$exe \"$database\" \"$oid\"\n" if $debug;
51 my $value = `gdbmget$exe "$database" "$oid"`;
52 # Tidy up the ever growing number of newlines at the end of the value
53 $value =~ s/(\r?\n)+/$1/g;
54 # Cache this result
55 $gdbmget_previous_collection = $collection;
56 $gdbmget_previous_oid = $oid;
57 $gdbmget_previous_value = $value;
58 # Done
59 return $value;
60 }
61# /** gdbmGet() **/
62
63# /** This wraps John T's gdbmset executable to set the gdbm database entry for
64# * a particular OID. This does not yet report errors.
65# *
66# * @param $collection is the collection name.
67# * @param $oid is the internal document id.
68# * @param $value is the new value to set for the oid.
69# *
70# * @author John Rowe, DL Consulting Ltd.
71# */
72sub gdbmSet()
73 {
74 my ($collection, $oid, $value) = @_;
75 # Where's the database?
76 my $database = &getDatabasePath($collection);
77 # Are we in windows? Do we need .exe?
78 my $exe = &util::get_os_exe();
79 # Check whether value is set
80 if (defined($value))
81 {
82 # Escape any speech marks in the value
83 $value =~ s/\"/\\\"/g;
84 # Set the document content
85 print STDERR "#Set document\ncmd: gdbmset$exe \"$database\" \"$oid\" \"$value\"\n" if $debug;
86 `gdbmset$exe "$database" "$oid" "$value"`;
87 }
88 else
89 {
90 # Remove the document from the database
91 print STDERR "#Set document\ncmd: gdbmset$exe \"$database\" \"$oid\"\n" if $debug;
92 `gdbmset$exe "$database" "$oid"`;
93 }
94 # Empty any cached values, as they may now be invalid
95 # Cache this result
96 $gdbmget_previous_collection = "";
97 $gdbmget_previous_oid = "";
98 $gdbmget_previous_value = 0;
99 }
100# /** gdbmSet() **/
101
102# /** This works out the database path and returns it to the calling
103# * calling function.
104# *
105# * @param $collection The current collection name
106# *
107# * @author John Rowe, DL Consulting Ltd.
108# */
109sub getDatabasePath()
110 {
111 my $collection = shift(@_);
112 # Find out the database extension
113 my $ext = ".bdb";
114 $ext = ".ldb" if &util::is_little_endian();
115 # Now return the full filename of the database
116 return &util::filename_cat($ENV{'GSDLHOME'}, "collect", $collection, "index", "text", $collection.$ext);
117 }
118# /** getDatabasePath() **/
119
1201;
Note: See TracBrowser for help on using the repository browser.