1 | ###############################################################################
|
---|
2 | #
|
---|
3 | # GDBMTXTGZ.pm -- utility functions for writing to gdbm-txtgz databases
|
---|
4 | #
|
---|
5 | # A component of the Greenstone digital library software from the New Zealand
|
---|
6 | # Digital Library Project at the University of Waikato, New Zealand.
|
---|
7 | #
|
---|
8 | # Copyright (C) 1999-2015 New Zealand Digital Library Project
|
---|
9 | #
|
---|
10 | # This program is free software; you can redistribute it and/or modify it under
|
---|
11 | # the terms of the GNU General Public License as published by the Free Software
|
---|
12 | # Foundation; either version 2 of the License, or (at your option) any later
|
---|
13 | # version.
|
---|
14 | #
|
---|
15 | # This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
16 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
---|
17 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
---|
18 | # more details.
|
---|
19 | #
|
---|
20 | # You should have received a copy of the GNU General Public License along with
|
---|
21 | # this program; if not, write to the Free Software Foundation, Inc., 675 Mass
|
---|
22 | # Ave, Cambridge, MA 02139, USA.
|
---|
23 | #
|
---|
24 | ###############################################################################
|
---|
25 |
|
---|
26 | package DBDrivers::GDBMTXTGZ;
|
---|
27 |
|
---|
28 | # Pragma
|
---|
29 | use strict;
|
---|
30 |
|
---|
31 | # Libraries
|
---|
32 | use util;
|
---|
33 | use FileUtils;
|
---|
34 | use DBDrivers::GDBM;
|
---|
35 |
|
---|
36 | sub BEGIN
|
---|
37 | {
|
---|
38 | @DBDrivers::GDBMTXTGZ::ISA = ( 'DBDrivers::GDBM' );
|
---|
39 | }
|
---|
40 |
|
---|
41 | sub new
|
---|
42 | {
|
---|
43 | my $class = shift(@_);
|
---|
44 | return bless ($self, $class);
|
---|
45 | }
|
---|
46 |
|
---|
47 | # -----------------------------------------------------------------------------
|
---|
48 | # GDBM TXT-GZ IMPLEMENTATION
|
---|
49 | # -----------------------------------------------------------------------------
|
---|
50 |
|
---|
51 | # With infodb_handle already set up, these functions work the same as parent version
|
---|
52 | # sub close_infodb_write_handle {}
|
---|
53 | # sub delete_infodb_entry {}
|
---|
54 | # sub write_infodb_entry {}
|
---|
55 | # sub write_infodb_rawentry {}
|
---|
56 |
|
---|
57 |
|
---|
58 | ## @function open_infodb_write_handle(string)
|
---|
59 | #
|
---|
60 | # Keep infodb in GDBM neutral form => save data as compressed text file, read
|
---|
61 | # for txt2db to be run on it later (i.e. by the runtime system, first time the
|
---|
62 | # collection is ever accessed). This makes it easier distribute pre-built
|
---|
63 | # collections to various architectures.
|
---|
64 | #
|
---|
65 | # NB: even if two architectures are little endian (e.g. Intel and ARM procesors)
|
---|
66 | # GDBM does *not* guarantee that the database generated on one will work on the
|
---|
67 | # other
|
---|
68 | #
|
---|
69 | sub open_infodb_write_handle
|
---|
70 | {
|
---|
71 | my $infodb_file_path = shift(@_);
|
---|
72 |
|
---|
73 | # Greenstone ships with gzip for windows, on $PATH
|
---|
74 | my $infodb_file_handle = undef;
|
---|
75 | if (!open($infodb_file_handle, "| gzip - > \"$infodb_file_path\"")) {
|
---|
76 | print STDERR "Error: Failed to open pipe to gzip - > \"$infodb_file_path\"\n";
|
---|
77 | print STDERR " $!\n";
|
---|
78 | return undef;
|
---|
79 | }
|
---|
80 | binmode($infodb_file_handle,":utf8");
|
---|
81 | return $infodb_file_handle;
|
---|
82 | }
|
---|
83 | ## open_infodb_write_handle(string) => filehandle ##
|
---|
84 |
|
---|
85 |
|
---|
86 | ## @function get_infodb_file_path(string, string)
|
---|
87 | #
|
---|
88 | sub get_infodb_file_path
|
---|
89 | {
|
---|
90 | my $collection_name = shift(@_);
|
---|
91 | my $infodb_directory_path = shift(@_);
|
---|
92 |
|
---|
93 | my $infodb_file_name = &util::get_dirsep_tail($collection_name).".txt.gz";
|
---|
94 | return &util::filename_cat($infodb_directory_path, $infodb_file_name);
|
---|
95 | }
|
---|
96 | ## get_infodb_file_path(string, string) => string ##
|
---|
97 |
|
---|
98 |
|
---|
99 | ## @function read_infodb_file(string, hashmap)
|
---|
100 | #
|
---|
101 | sub read_infodb_file
|
---|
102 | {
|
---|
103 | my $infodb_file_path = shift(@_);
|
---|
104 | my $infodb_map = shift(@_);
|
---|
105 |
|
---|
106 | my $cmd = "gzip --decompress --to-stdout \"$infodb_file_path\"";
|
---|
107 |
|
---|
108 | open (PIPEIN, "$cmd |") || die "Error: Couldn't open pipe from gzip: $!\n $cmd\n";
|
---|
109 |
|
---|
110 | binmode(PIPEIN,":utf8");
|
---|
111 | my $infodb_line = "";
|
---|
112 | my $infodb_key = "";
|
---|
113 | my $infodb_value = "";
|
---|
114 | while (defined ($infodb_line = <PIPEIN>)) {
|
---|
115 | $infodb_line =~ s/(\r\n)+$//; # more general than chomp
|
---|
116 |
|
---|
117 | if ($infodb_line =~ /^\[([^\]]+)\]$/) {
|
---|
118 | $infodb_key = $1;
|
---|
119 | }
|
---|
120 | elsif ($infodb_line =~ /^-{70}$/) {
|
---|
121 | $infodb_map->{$infodb_key} = $infodb_value;
|
---|
122 | $infodb_key = "";
|
---|
123 | $infodb_value = "";
|
---|
124 | }
|
---|
125 | else {
|
---|
126 | $infodb_value .= $infodb_line;
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | close (PIPEIN);
|
---|
131 | }
|
---|
132 | ## read_infodb_file(string, hashmap) => void ##
|
---|
133 |
|
---|
134 |
|
---|
135 | ## @function read_infodb_keys(string, hashmap)
|
---|
136 | #
|
---|
137 | sub read_infodb_keys
|
---|
138 | {
|
---|
139 | my $infodb_file_path = shift(@_);
|
---|
140 | my $infodb_map = shift(@_);
|
---|
141 |
|
---|
142 | my $cmd = "gzip --decompress --to-stdout \"$infodb_file_path\"";
|
---|
143 |
|
---|
144 | open (PIPEIN, "$cmd |") || die "Error: Couldn't open pipe from gzip: $!\n $cmd\n";
|
---|
145 |
|
---|
146 | binmode(PIPEIN,":utf8");
|
---|
147 | my $infodb_line = "";
|
---|
148 | my $infodb_key = "";
|
---|
149 | while (defined ($infodb_line = <PIPEIN>)) {
|
---|
150 | $infodb_line =~ s/(\r\n)+$//; # more general than chomp
|
---|
151 |
|
---|
152 | if ($infodb_line =~ /^\[([^\]]+)\]$/) {
|
---|
153 | $infodb_key = $1;
|
---|
154 | }
|
---|
155 | elsif ($infodb_line =~ /^-{70}$/) {
|
---|
156 | $infodb_map->{$infodb_key} = 1;
|
---|
157 | $infodb_key = "";
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | close (PIPEIN);
|
---|
162 | }
|
---|
163 | ## read_infodb_keys(string, hashmap) => void ##
|
---|
164 |
|
---|
165 |
|
---|
166 | ## @function set_infodb_entry(string, string, hashmap)
|
---|
167 | #
|
---|
168 | sub set_infodb_entry
|
---|
169 | {
|
---|
170 | my $infodb_file_path = shift(@_);
|
---|
171 | my $infodb_key = shift(@_);
|
---|
172 | my $infodb_map = shift(@_);
|
---|
173 |
|
---|
174 | print STDERR "***** gdbmtxtgz::set_infodb_entry() not implemented yet!\n";
|
---|
175 | }
|
---|
176 | ## set_infodb_entry(string, string, hashmap) => void ##
|
---|
177 |
|
---|
178 | 1;
|
---|