source: gsdl/trunk/perllib/dbutil.pm@ 15710

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

(Adding new DB support) Adding new get_infodb_file_path() function to dbutil to abstract away the GDBM endian stuff, and changed basebuilder::make_infodatabase() to use it.

File size: 3.4 KB
Line 
1###########################################################################
2#
3# dbutil.pm -- utility functions for writing to different databases
4# Copyright (C) 2008 DL Consulting Ltd
5#
6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26package dbutil;
27
28use strict;
29
30
31sub get_infodb_file_path
32{
33 my $collection_name = shift(@_);
34 my $infodb_directory_path = shift(@_);
35
36 return &get_infodb_file_path_gdbm($collection_name, $infodb_directory_path);
37}
38
39
40sub read_infodb_file
41{
42 my $infodb_file_path = shift(@_);
43 my $infodb_map = shift(@_);
44
45 &read_infodb_file_gdbm($infodb_file_path, $infodb_map);
46}
47
48
49sub write_infodb_entry
50{
51 my $infodb_handle = shift(@_);
52 my $infodb_key = shift(@_);
53 my $infodb_map = shift(@_);
54
55 &write_infodb_entry_gdbm($infodb_handle, $infodb_key, $infodb_map);
56}
57
58
59
60# ----------------------------------------------------------------------------------------
61# GDBM FUNCTIONS
62# ----------------------------------------------------------------------------------------
63
64sub get_infodb_file_path_gdbm
65{
66 my $collection_name = shift(@_);
67 my $infodb_directory_path = shift(@_);
68
69 my $infodb_file_extension = (&util::is_little_endian() ? ".ldb" : ".bdb");
70 my $infodb_file_name = &util::get_dirsep_tail($collection_name) . $infodb_file_extension;
71 return &util::filename_cat($infodb_directory_path, $infodb_file_name);
72}
73
74
75sub read_infodb_file_gdbm
76{
77 my $infodb_file_path = shift(@_);
78 my $infodb_map = shift(@_);
79
80 open (PIPEIN, "db2txt \"$infodb_file_path\" |") || die "couldn't open pipe from db2txt\n";
81 my $infodb_line = "";
82 my $infodb_key = "";
83 my $infodb_value = "";
84 while (defined ($infodb_line = <PIPEIN>))
85 {
86 if ($infodb_line =~ /^\[([^\]]+)\]$/)
87 {
88 $infodb_key = $1;
89 }
90 elsif ($infodb_line =~ /^-{70}$/)
91 {
92 $infodb_map->{$infodb_key} = $infodb_value;
93 $infodb_key = "";
94 $infodb_value = "";
95 }
96 else
97 {
98 $infodb_value .= $infodb_line;
99 }
100 }
101
102 close (PIPEIN);
103}
104
105
106sub write_infodb_entry_gdbm
107{
108 my $infodb_handle = shift(@_);
109 my $infodb_key = shift(@_);
110 my $infodb_map = shift(@_);
111
112 print $infodb_handle "[$infodb_key]\n";
113 foreach my $infodb_value_key (keys(%$infodb_map))
114 {
115 foreach my $infodb_value (@{$infodb_map->{$infodb_value_key}})
116 {
117 if ($infodb_value =~ /-{70,}/)
118 {
119 # if value contains 70 or more hyphens in a row we need to escape them
120 # to prevent txt2db from treating them as a separator
121 $infodb_value =~ s/-/&\#045;/gi;
122 }
123 print $infodb_handle "<$infodb_value_key>" . $infodb_value . "\n";
124 }
125 }
126 print $infodb_handle '-' x 70, "\n";
127}
128
129
1301;
Note: See TracBrowser for help on using the repository browser.