source: greenstone3/trunk/src/java/org/greenstone/gsdl3/util/GDBMWrapper.java@ 17907

Last change on this file since 17907 was 17907, checked in by ak19, 15 years ago

Inserted code to first look for whether the db file is already there. If not, it looks for the txtgz file. If that exists, it unzips the txtgz file and converts it to the ldb (bdb?) database file. The same process was followed in the original Greenstone2 code (gdbmclass.cpp in common-src/src/lib).

  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1/*
2 * GDBMWrapper.java
3 * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.util;
20
21import au.com.pharos.gdbm.GdbmFile;
22import au.com.pharos.packing.*;
23import au.com.pharos.gdbm.GdbmException;
24
25import org.apache.log4j.Logger;
26
27import java.io.UnsupportedEncodingException;
28import java.io.File;
29
30/** java wrapper class for gdbm - uses Java-GDBM written by Martin Pool
31 * replaces gdbmclass in the old version
32 */
33
34public class GDBMWrapper
35 implements FlatDatabaseWrapper {
36
37 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.GDBMWrapper.class.getName());
38
39 /* GdbmFile modes:
40 READER - read access, many readers may share the database
41 WRITER - read/write access, exclusive access
42 WRCREAT - read/write access, create db if doesn't exist
43 NEWDB - read/write access, db should be replaced if exists
44 */
45
46
47 protected GdbmFile db_=null;
48
49 /** open the database filename, with mode mode - uses the constants
50 above, eg GdbmFile.WRITER */
51 public boolean openDatabase(String filename, int mode) {
52 // need to convert mode to GdbmFile mode
53 if (mode == READ) {
54 mode = GdbmFile.READER;
55 } else if (mode == WRITE) {
56 mode = GdbmFile.WRITER;
57 } else {
58 logger.error ("invalid mode, "+mode+ ", opening db for reading only");
59 mode = GdbmFile.READER;
60 }
61 try {
62 if (db_!=null) {
63 db_.close();
64 }
65
66 // The java version of the C++ code in common-src/src/lib/gdbmclass.cpp
67 if(mode == GdbmFile.READER) {
68 // Looking to read in the database
69 // => check to see if .ldb/.bdb file already there
70 // if not (first time) then generate using txt2db
71 if (!new File(filename).exists()) {
72 logger.warn("Database file " + filename + " does not exist. Looking for txtgz version of db file.");
73
74 // need to generate architecture native GDBM file using txt2db
75
76 // replace sought after gdbm filename ext with ".txt.gz"
77
78 int extension = filename.lastIndexOf('.');
79 String txtgzFilename = filename.substring(0, extension) + ".txt.gz";
80 if(new File(txtgzFilename).exists()) {
81 // Test to make sure Perl is on the path
82 // On Linux, the output of the test goes to STDOUT so redirect it to STDERR
83 String cmdTest = "perl -v 2>&1";
84 //String cmdTest = "echo %PATH%";
85 int returnValue = Processing.runProcess(cmdTest);
86 if (returnValue != 0) {
87 logger.error("Tried to find Perl. Return exit value of running "
88 + cmdTest + ": " + returnValue + ", (expected this to be 0)");
89 logger.error("Check that Perl is set in your PATH environment variable.");
90 //log.error("At present, PATH=" + System.getenv("PATH"));
91 }
92
93 String cmd = "perl -S txtgz-to-gdbm.pl \"" + txtgzFilename + "\" \"" + filename + "\"";
94 returnValue = Processing.runProcess(cmd);
95 // For some reason, launching this command with gsdl_system() still returns 1
96 // even when it returns 0 when run from the command-line. We can check whether
97 // we succeeded by looking at whether the output database file was created.
98 if (returnValue != 0) {
99 logger.warn("Warning, non-zero return value on running command \"" + cmd + "\": " + returnValue);
100 if (!new File(filename).exists()) {
101 logger.error("Tried to run command \"" + cmd + "\", but it failed");
102 }
103 }
104 }
105 }
106 }
107
108 db_ = new GdbmFile(filename, mode);
109 } catch ( GdbmException e) { // the database wasn't opened or created
110 logger.error("couldn't open database "+filename);
111 return false;
112 }
113 db_.setKeyPacking(new StringPacking());
114 db_.setValuePacking(new StringPacking());
115 return true;
116 }
117
118 /** close the database associated with this wrapper */
119 public void closeDatabase() {
120 try {
121 if (db_ != null) {
122 db_.close();
123 db_ = null;
124 }
125 } catch (GdbmException e) {
126 // should never get here - close never actually throws an exception
127 logger.error("error on close()");
128 }
129 }
130
131 public String getValue(String key) {
132 if (db_==null) {
133 return null;
134 }
135 String s_info;
136 try {
137 try {
138 // The key is UTF8: do db lookup using the UTF8 version of key
139 s_info = (String)db_.fetch(key.getBytes("UTF-8"));
140 } catch(UnsupportedEncodingException e) {
141 logger.warn("utf8 key for " + key
142 + " unrecognised. Retrying with default encoding.");
143 // retry db lookup using default encoding of key instead of UTF8
144 s_info = (String)db_.fetch(key);
145 }
146 } catch (GdbmException e) {
147 logger.error("couldn't get record");
148 return null;
149 }
150 if (s_info==null) {
151 // record not present
152 logger.error("key "+key+" not present in db");
153 return null;
154 }
155 return s_info;
156 }
157
158 /** sets the key value as info
159 * TODO - not implemented yet */
160 public boolean setValue (String key, String value) {
161 if (db_==null) {
162 return false;
163 }
164 return false;
165 }
166 /** deletes the entry for key
167 * TODO - not implemented yet */
168 public boolean deleteKey(String key) {
169 if (db_==null) {
170 return false;
171 }
172 return false;
173 }
174
175 /** returns a string of key-value entries that
176 * can be printed for debugging purposes. */
177 public String displayAllEntries() {
178 StringBuffer output = new StringBuffer();
179 try{
180 java.util.Enumeration e = db_.keys();
181 while(e.hasMoreElements()) {
182 Object key = e.nextElement();
183 Object value = db_.fetch(key);
184
185 output.append("key href: ");
186 output.append((String)key);
187 output.append("\tvalue ID: ");
188 output.append((String)value);
189 output.append("\n");
190 //logger.warn("key: " + key + "\tvalue: " + value);
191
192 String urlkey = java.net.URLEncoder.encode((String)key, "UTF8");
193 output.append("URL encoded key: " + urlkey);
194 //logger.warn("URL encoded key: " + urlkey);
195 }
196 } catch(UnsupportedEncodingException e) {
197 logger.warn("Trouble converting key to UTF-8.");
198 } catch(Exception e) {
199 logger.warn("Exception encountered when trying to displayAllEntries():" + e);
200 }
201 return output.toString();
202 }
203}
Note: See TracBrowser for help on using the repository browser.