1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # dbutil::gdbm -- utility functions for writing to gdbm databases
|
---|
4 | #
|
---|
5 | # A component of the Greenstone digital library software
|
---|
6 | # from the New Zealand Digital Library Project at the
|
---|
7 | # University of Waikato, New Zealand.
|
---|
8 | #
|
---|
9 | # Copyright (C) 2009
|
---|
10 | #
|
---|
11 | # This program is free software; you can redistribute it and/or modify
|
---|
12 | # it under the terms of the GNU General Public License as published by
|
---|
13 | # the Free Software Foundation; either version 2 of the License, or
|
---|
14 | # (at your option) any later version.
|
---|
15 | #
|
---|
16 | # This program is distributed in the hope that it will be useful,
|
---|
17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | # GNU General Public License for more details.
|
---|
20 | #
|
---|
21 | # You should have received a copy of the GNU General Public License
|
---|
22 | # along with this program; if not, write to the Free Software
|
---|
23 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
24 | #
|
---|
25 | ###########################################################################
|
---|
26 |
|
---|
27 | package dbutil::gdbm;
|
---|
28 |
|
---|
29 | use strict;
|
---|
30 |
|
---|
31 | use dbutil::gdbmtxtgz;
|
---|
32 |
|
---|
33 | # -----------------------------------------------------------------------------
|
---|
34 | # GDBM IMPLEMENTATION
|
---|
35 | # -----------------------------------------------------------------------------
|
---|
36 |
|
---|
37 | sub open_infodb_write_handle
|
---|
38 | {
|
---|
39 | my $infodb_file_path = shift(@_);
|
---|
40 | my $opt_append = shift(@_);
|
---|
41 |
|
---|
42 | my $txt2db_exe = &util::filename_cat($ENV{'GSDLHOME'},"bin",$ENV{'GSDLOS'}, "txt2db" . &util::get_os_exe());
|
---|
43 | my $infodb_file_handle = undef;
|
---|
44 | my $cmd = "\"$txt2db_exe\"";
|
---|
45 | if ((defined $opt_append) && ($opt_append eq "append")) {
|
---|
46 | $cmd .= " -append";
|
---|
47 | }
|
---|
48 | $cmd .= " \"$infodb_file_path\"";
|
---|
49 |
|
---|
50 | if (!-e "$txt2db_exe" || !open($infodb_file_handle, "| $cmd"))
|
---|
51 | {
|
---|
52 | return undef;
|
---|
53 | }
|
---|
54 |
|
---|
55 | return $infodb_file_handle;
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 |
|
---|
60 | sub close_infodb_write_handle
|
---|
61 | {
|
---|
62 | my $infodb_handle = shift(@_);
|
---|
63 |
|
---|
64 | close($infodb_handle);
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | sub get_infodb_file_path
|
---|
69 | {
|
---|
70 | my $collection_name = shift(@_);
|
---|
71 | my $infodb_directory_path = shift(@_);
|
---|
72 |
|
---|
73 | my $infodb_file_extension = ".gdb";
|
---|
74 | my $infodb_file_name = &util::get_dirsep_tail($collection_name) . $infodb_file_extension;
|
---|
75 | return &util::filename_cat($infodb_directory_path, $infodb_file_name);
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | sub read_infodb_file
|
---|
80 | {
|
---|
81 | my $infodb_file_path = shift(@_);
|
---|
82 | my $infodb_map = shift(@_);
|
---|
83 |
|
---|
84 | open (PIPEIN, "db2txt \"$infodb_file_path\" |") || die "couldn't open pipe from db2txt \$infodb_file_path\"\n";
|
---|
85 | my $infodb_line = "";
|
---|
86 | my $infodb_key = "";
|
---|
87 | my $infodb_value = "";
|
---|
88 | while (defined ($infodb_line = <PIPEIN>))
|
---|
89 | {
|
---|
90 | if ($infodb_line =~ /^\[([^\]]+)\]$/)
|
---|
91 | {
|
---|
92 | $infodb_key = $1;
|
---|
93 | }
|
---|
94 | elsif ($infodb_line =~ /^-{70}$/)
|
---|
95 | {
|
---|
96 | $infodb_map->{$infodb_key} = $infodb_value;
|
---|
97 | $infodb_key = "";
|
---|
98 | $infodb_value = "";
|
---|
99 | }
|
---|
100 | else
|
---|
101 | {
|
---|
102 | $infodb_value .= $infodb_line;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | close (PIPEIN);
|
---|
107 | }
|
---|
108 |
|
---|
109 | sub read_infodb_keys
|
---|
110 | {
|
---|
111 | my $infodb_file_path = shift(@_);
|
---|
112 | my $infodb_map = shift(@_);
|
---|
113 |
|
---|
114 | open (PIPEIN, "gdbmkeys \"$infodb_file_path\" |") || die "couldn't open pipe from gdbmkeys \$infodb_file_path\"\n";
|
---|
115 | my $infodb_line = "";
|
---|
116 | my $infodb_key = "";
|
---|
117 | my $infodb_value = "";
|
---|
118 | while (defined ($infodb_line = <PIPEIN>))
|
---|
119 | {
|
---|
120 | chomp $infodb_line; # remove end of line
|
---|
121 |
|
---|
122 | $infodb_map->{$infodb_line} = 1;
|
---|
123 | }
|
---|
124 |
|
---|
125 | close (PIPEIN);
|
---|
126 | }
|
---|
127 |
|
---|
128 | sub write_infodb_entry
|
---|
129 | {
|
---|
130 | # With infodb_handle already set up, works the same as _gdbm_txtgz version
|
---|
131 | &dbutil::gdbmtxtgz::write_infodb_entry(@_);
|
---|
132 | }
|
---|
133 |
|
---|
134 | sub write_infodb_rawentry
|
---|
135 | {
|
---|
136 | # With infodb_handle already set up, works the same as _gdbm_txtgz version
|
---|
137 | &dbutil::gdbmtxtgz::write_infodb_rawentry(@_);
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | sub delete_infodb_entry
|
---|
142 | {
|
---|
143 | # With infodb_handle already set up, works the same as _gdbm_txtgz version
|
---|
144 | &dbutil::gdbmtxtgz::delete_infodb_entry(@_);
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 |
|
---|
149 | 1;
|
---|