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

Last change on this file since 30264 was 30264, checked in by jmt12, 9 years ago

Added static block to ensure database wrapper registers its name and extension with the DBHelper.

File size: 5.8 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 implements FlatDatabaseWrapper
39{
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 {
56 OutputStreamWriter osw = new OutputStreamWriter(System.out, "UTF-8");
57 utf8out = new PrintWriter(osw, true);
58 }
59 catch (UnsupportedEncodingException e)
60 {
61 System.out.println(e);
62 }
63 // Register this wrapper with DBHelper
64 DBHelper.registerDBTypeExt("jdbm",".jdb");
65 }
66
67 /** open database named filename, using mode */
68 public boolean openDatabase(String db_filename, int mode)
69 {
70 String db_filename_with_ext = db_filename;
71
72 if (db_filename.endsWith(".jdb"))
73 {
74 // remove file extension as JDBM does not expect it
75 db_filename = db_filename.substring(0, db_filename.length() - 4);
76 }
77
78 // Map the mode value into equivalent JDBM 'must_exist' value
79 // currently this is very simple as there is only READ and WRITE
80 // (no WRITER_CREATE)
81 // => assume the database must exist
82 boolean must_exist = true; // default
83
84 if (recman_ != null)
85 {
86 String message = "openDatabase() called when the class already has a database open\n";
87 message += " Use closeDatabase before opening the next one.\n";
88 message += " Existing database file: " + db_filename_ + "\n";
89 message += " New database file: " + db_filename + "\n";
90 logger.warn(message);
91 // consider closing it automatically?
92 }
93
94 try
95 {
96 // create or open a record manager
97 Properties props = new Properties();
98 recman_ = RecordManagerFactory.createRecordManager(db_filename, props);
99
100 // load existing table (if exists) otherwise create new one
101 long recid = recman_.getNamedObject(TNAME);
102
103 if (recid != 0)
104 {
105 logger.info("# Loading existing database table '" + TNAME + "' ...");
106 hashtable_ = HTree.load(recman_, recid);
107 }
108 else
109 {
110 if (must_exist)
111 {
112 recman_.close();
113 recman_ = null;
114 db_filename_ = null;
115
116 logger.error("Database table '" + TNAME + "' does not exist.");
117 throw new IOException();
118 }
119 else
120 {
121 logger.info("# No database table '" + TNAME + "' to set. Creating new one");
122 hashtable_ = HTree.createInstance(recman_);
123 recman_.setNamedObject(TNAME, hashtable_.getRecid());
124 }
125 }
126 }
127 catch (IOException e)
128 {
129 e.printStackTrace();
130
131 logger.error("couldn't open database " + db_filename_with_ext);
132 return false;
133 }
134
135 db_filename_ = db_filename;
136
137 return true;
138 }
139
140 /** close the database associated with this wrapper */
141 public void closeDatabase()
142 {
143 try
144 {
145 if (recman_ != null)
146 {
147 recman_.close();
148 recman_ = null;
149 db_filename_ = null;
150 }
151 }
152 catch (IOException e)
153 {
154 logger.error("Failed to close JDBM database");
155 }
156 }
157
158 /** returns the value associated with the key */
159 public String getValue(String key)
160 {
161
162 String val;
163
164 try
165 {
166 val = (String) hashtable_.get(key);
167
168 recman_.commit();
169 }
170 catch (IOException e)
171 {
172 logger.error("Failed get key " + key + "from JDBM database");
173 return null;
174 }
175
176 return val;
177 }
178
179 /**
180 * Sets the given key to the given value in the database
181 */
182 public boolean setValue(String key, String value)
183 {
184 try
185 {
186 hashtable_.put(key, value);
187 recman_.commit();
188 }
189 catch (Exception ex)
190 {
191 logger.error("Failed to set " + key + " = " + value + " in the JDBM database");
192 return false;
193 }
194 return true;
195 }
196
197 /**
198 * Deletes the given key (and corresponding value) from the database
199 */
200 public boolean deleteKey(String key)
201 {
202 try
203 {
204 hashtable_.remove(key);
205 recman_.commit();
206 }
207 catch (Exception ex)
208 {
209 logger.error("Failed to delete key " + key + " in the JDBM database");
210 return false;
211 }
212 return true;
213 }
214
215 /**
216 * returns a string of key-value entries that can be printed for debugging
217 * purposes
218 */
219 public String displayAllEntries()
220 {
221
222 StringBuffer keys = new StringBuffer();
223
224 try
225 {
226 FastIterator iter = hashtable_.keys();
227 String key = (String) iter.next();
228
229 String nl = System.getProperty("line.separator");
230
231 while (key != null)
232 {
233 String val = (String) hashtable_.get(key);
234 keys.append("[" + key + "]" + nl);
235 keys.append(val + nl);
236 // 70 hypens
237 keys.append("----------------------------------------------------------------------" + nl);
238 key = (String) iter.next();
239 }
240
241 recman_.commit();
242 }
243 catch (IOException e)
244 {
245 logger.error("Failed get all keys and values from JDBM database");
246 return null;
247 }
248
249 return keys.toString();
250 }
251}
Note: See TracBrowser for help on using the repository browser.