1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # dbutil::gdbml -- extends the dbutil::gdbm class by adding simple file
|
---|
4 | # locking so as to allow for multiple readers/writers (albeit slow and not
|
---|
5 | # supported over network filesystems)
|
---|
6 | #
|
---|
7 | # A component of the Greenstone digital library software
|
---|
8 | # from the New Zealand Digital Library Project at the
|
---|
9 | # University of Waikato, New Zealand.
|
---|
10 | #
|
---|
11 | # Copyright (C) 2013
|
---|
12 | #
|
---|
13 | # This program is free software; you can redistribute it and/or modify
|
---|
14 | # it under the terms of the GNU General Public License as published by
|
---|
15 | # the Free Software Foundation; either version 2 of the License, or
|
---|
16 | # (at your option) any later version.
|
---|
17 | #
|
---|
18 | # This program is distributed in the hope that it will be useful,
|
---|
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
21 | # GNU General Public License for more details.
|
---|
22 | #
|
---|
23 | # You should have received a copy of the GNU General Public License
|
---|
24 | # along with this program; if not, write to the Free Software
|
---|
25 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
26 | #
|
---|
27 | ###########################################################################
|
---|
28 |
|
---|
29 | package dbutil::gdbml;
|
---|
30 |
|
---|
31 | use strict;
|
---|
32 |
|
---|
33 | use dbutil::gdbm;
|
---|
34 |
|
---|
35 | BEGIN
|
---|
36 | {
|
---|
37 | @dbutil::gdbml::ISA = ('dbutil::gdbm');
|
---|
38 | }
|
---|
39 |
|
---|
40 | # -----------------------------------------------------------------------------
|
---|
41 | # GDBM IMPLEMENTATION
|
---|
42 | # -----------------------------------------------------------------------------
|
---|
43 |
|
---|
44 | sub open_infodb_write_handle
|
---|
45 | {
|
---|
46 | my $infodb_file_path = shift(@_);
|
---|
47 | my $opt_append = shift(@_);
|
---|
48 |
|
---|
49 | # note the use of locking enabled txt2db [jmt12]
|
---|
50 | # also, we assume the path is correct to find the executable [jmt12]
|
---|
51 | my $txt2db_exe = "txt2dbl" . &util::get_os_exe();
|
---|
52 | my $infodb_file_handle = undef;
|
---|
53 | my $cmd = "\"$txt2db_exe\"";
|
---|
54 | if ((defined $opt_append) && ($opt_append eq "append")) {
|
---|
55 | $cmd .= " -append";
|
---|
56 | }
|
---|
57 | $cmd .= " \"$infodb_file_path\"";
|
---|
58 |
|
---|
59 | if(!open($infodb_file_handle, "| $cmd"))
|
---|
60 | {
|
---|
61 |
|
---|
62 | print STDERR "Error: Failed to open pipe to $cmd\n";
|
---|
63 | print STDERR " $!\n";
|
---|
64 | return undef;
|
---|
65 | }
|
---|
66 |
|
---|
67 | binmode($infodb_file_handle,":utf8");
|
---|
68 |
|
---|
69 | return $infodb_file_handle;
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | sub read_infodb_file
|
---|
74 | {
|
---|
75 | my $infodb_file_path = shift(@_);
|
---|
76 | my $infodb_map = shift(@_);
|
---|
77 |
|
---|
78 | # note the use of locking enabled db2txt [jmt12]
|
---|
79 | open (PIPEIN, "db2txtl \"$infodb_file_path\" |") || die "couldn't open pipe from db2txt \$infodb_file_path\"\n";
|
---|
80 |
|
---|
81 | binmode(PIPEIN,":utf8");
|
---|
82 |
|
---|
83 | my $infodb_line = "";
|
---|
84 | my $infodb_key = "";
|
---|
85 | my $infodb_value = "";
|
---|
86 | while (defined ($infodb_line = <PIPEIN>))
|
---|
87 | {
|
---|
88 | if ($infodb_line =~ /^\[([^\]]+)\]$/)
|
---|
89 | {
|
---|
90 | $infodb_key = $1;
|
---|
91 | }
|
---|
92 | elsif ($infodb_line =~ /^-{70}$/)
|
---|
93 | {
|
---|
94 | $infodb_map->{$infodb_key} = $infodb_value;
|
---|
95 | $infodb_key = "";
|
---|
96 | $infodb_value = "";
|
---|
97 | }
|
---|
98 | else
|
---|
99 | {
|
---|
100 | $infodb_value .= $infodb_line;
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | close (PIPEIN);
|
---|
105 | }
|
---|
106 |
|
---|
107 | sub read_infodb_keys
|
---|
108 | {
|
---|
109 | die("[gdbmkeys] not implemented for parallel building");
|
---|
110 | }
|
---|
111 |
|
---|
112 | sub set_infodb_entry
|
---|
113 | {
|
---|
114 | my $infodb_file_path = shift(@_);
|
---|
115 | my $infodb_key = shift(@_);
|
---|
116 | my $infodb_map = shift(@_);
|
---|
117 |
|
---|
118 | # HTML escape anything that is not part of the "contains" metadata value
|
---|
119 | foreach my $k (keys %$infodb_map) {
|
---|
120 | my @escaped_v = ();
|
---|
121 | foreach my $v (@{$infodb_map->{$k}}) {
|
---|
122 | if ($k eq "contains") {
|
---|
123 | push(@escaped_v, $v);
|
---|
124 | }
|
---|
125 | else {
|
---|
126 | my $ev = &ghtml::unescape_html($v);
|
---|
127 | push(@escaped_v, $ev);
|
---|
128 | }
|
---|
129 | }
|
---|
130 | $infodb_map->{$k} = \@escaped_v;
|
---|
131 | }
|
---|
132 |
|
---|
133 | # Generate the record string
|
---|
134 | my $serialized_infodb_map = &dbutil::convert_infodb_hash_to_string($infodb_map);
|
---|
135 | ### print STDERR "**** ser dr\n$serialized_infodb_map\n\n\n";
|
---|
136 |
|
---|
137 | # Store it into GDBM using 'txt2db -append' which despite its name
|
---|
138 | # actually replaces the record if it already exists
|
---|
139 | # note the use of locking enabled txt2db [jmt12]
|
---|
140 | my $cmd = "txt2dbl -append \"$infodb_file_path\"";
|
---|
141 |
|
---|
142 | my $status = undef;
|
---|
143 | if(!open(GOUT, "| $cmd"))
|
---|
144 | {
|
---|
145 | print STDERR "Error: gdbm::set_infodb_entry() failed to open pipe to: $cmd\n";
|
---|
146 | print STDERR " $!\n";
|
---|
147 | $status = -1;
|
---|
148 | }
|
---|
149 | else {
|
---|
150 | binmode(GOUT,":utf8");
|
---|
151 |
|
---|
152 | print GOUT "[$infodb_key]\n";
|
---|
153 | print GOUT "$serialized_infodb_map\n";
|
---|
154 |
|
---|
155 | close(GOUT);
|
---|
156 | $status = 0; # as in exit status of cmd OK
|
---|
157 | }
|
---|
158 |
|
---|
159 | return $status;
|
---|
160 | }
|
---|
161 |
|
---|
162 | 1;
|
---|