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

Last change on this file since 14926 was 12844, checked in by mdewsnip, 18 years ago

Incremental building and dynamic GDBM updating code, many thanks to John Rowe and John Thompson at DL Consulting Ltd.

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