source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/JDBMWrapper.java@ 24221

Last change on this file since 24221 was 22973, checked in by davidb, 14 years ago

Code changed to use Class.forName to dynamically load required database wrapper class (e.g. GDBMWrapper or JDBMWrapper). This is so the code can be compiled up with some of these database wrapper classes optionally switched out (e.g. name changed to .java.tmp). Additional routine also added to test is a database is open. In JDBMWrapper, the close routine was modified so it only closes the database one. Our code seems to call 'cleanup' more than once, and letting it close a JDBMWrapper class a second time throws an exception deep within JDBM implementation.

File size: 5.3 KB
Line 
1/*
2 * JDBMWrapper.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 org.apache.log4j.Logger;
22
23import java.io.BufferedReader;
24import java.io.InputStreamReader;
25import java.io.IOException;
26import java.io.UnsupportedEncodingException;
27import java.io.OutputStreamWriter;
28import java.io.PrintWriter;
29
30import java.util.Properties;
31import java.util.ArrayList;
32
33import jdbm.RecordManager;
34import jdbm.RecordManagerFactory;
35import jdbm.helper.FastIterator;
36import jdbm.htree.HTree;
37
38public class JDBMWrapper
39 implements FlatDatabaseWrapper {
40
41 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.JDBMWrapper.class.getName());
42
43 static String TNAME = "greenstone";
44
45 RecordManager recman_ = null;
46 HTree hashtable_;
47
48 String db_filename_;
49
50 static private PrintWriter utf8out = null;
51
52 static
53 {
54 try {
55 OutputStreamWriter osw = new OutputStreamWriter(System.out, "UTF-8");
56 utf8out = new PrintWriter(osw, true);
57 }
58 catch (UnsupportedEncodingException e) {
59 System.out.println(e);
60 }
61 }
62
63
64 /** open database named filename, using mode */
65 public boolean openDatabase(String db_filename, int mode) {
66
67
68 if (db_filename.endsWith(".jdb")) {
69 // remove file extension as JDBM does not expect it
70 db_filename = db_filename.substring(0,db_filename.length()-4);
71 }
72
73 // Map the mode value into equivalent JDBM 'must_exist' value
74 // currently this is very simple as there is only READ and WRITE
75 // (no WRITER_CREATE)
76 // => assume the database must exist
77 boolean must_exist = true; // default
78
79 if (recman_ != null) {
80 String message = "openDatabase() called when the class already has a database open\n";
81 message += " Use closeDatabase before opening the next one.\n";
82 message += " Existing database file: " + db_filename_ + "\n";
83 message += " New database file: " + db_filename + "\n";
84 logger.warn(message);
85 // consider closing it automatically?
86 }
87
88
89 try {
90 // create or open a record manager
91 Properties props = new Properties();
92 recman_ = RecordManagerFactory.createRecordManager(db_filename, props);
93
94 // load existing table (if exists) otherwise create new one
95 long recid = recman_.getNamedObject(TNAME);
96
97 if (recid != 0) {
98 System.err.println("# Loading existing database table '" + TNAME +"' ...");
99 hashtable_ = HTree.load(recman_, recid);
100 }
101 else {
102
103 if (must_exist) {
104 recman_.close();
105 recman_ = null;
106 db_filename_ = null;
107
108 System.err.println("Database table '" + TNAME +"' does not exist.");
109 throw new IOException();
110 }
111 else {
112 System.err.println("# No database table '" + TNAME +"' to set. Creating new one");
113 hashtable_ = HTree.createInstance(recman_);
114 recman_.setNamedObject(TNAME, hashtable_.getRecid());
115 }
116 }
117 }
118 catch (IOException e) {
119 logger.error("couldn't open database "+ db_filename);
120 return false;
121 }
122
123 db_filename_ = db_filename;
124
125 return true;
126 }
127
128
129 /** close the database associated with this wrapper */
130 public void closeDatabase() {
131 try {
132 if (recman_ != null) {
133 recman_.close();
134 recman_ = null;
135 db_filename_ = null;
136 }
137 }
138 catch (IOException e) {
139 logger.error("Failed to close JDBM database");
140 }
141 }
142
143 /** returns the value associated with the key */
144 public String getValue(String key) {
145
146 String val;
147
148 try {
149 val = (String) hashtable_.get(key);
150
151 recman_.commit();
152 }
153 catch (IOException e) {
154 logger.error("Failed get key " + key + "from JDBM database");
155 return null;
156 }
157
158 return val;
159 }
160
161 /** returns a string of key-value entries that can be
162 * printed for debugging purposes*/
163 public String displayAllEntries() {
164
165 StringBuffer keys = new StringBuffer();
166
167 try {
168 FastIterator iter = hashtable_.keys();
169 String key = (String) iter.next();
170
171 String nl = System.getProperty("line.separator");
172
173 while (key != null) {
174 String val = (String) hashtable_.get(key);
175 keys.append("[" + key + "]" + nl);
176 keys.append(val + nl);
177 // 70 hypens
178 keys.append("----------------------------------------------------------------------" + nl);
179 key = (String) iter.next();
180 }
181
182 recman_.commit();
183 }
184 catch (IOException e) {
185 logger.error("Failed get all keys and values from JDBM database");
186 return null;
187 }
188
189 return keys.toString();
190 }
191}
Note: See TracBrowser for help on using the repository browser.