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

Last change on this file since 30516 was 30516, checked in by Georgiy Litvinov, 8 years ago

Moved to incremental rebuild in webeditor

File size: 5.9 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_ = null;
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 if (hashtable_ == null) {
162 logger.error("Database was not opened or not exist!");
163 return null;
164 }
165
166 String val;
167
168 try
169 {
170 val = (String) hashtable_.get(key);
171
172 recman_.commit();
173 }
174 catch (IOException e)
175 {
176 logger.error("Failed get key " + key + "from JDBM database");
177 return null;
178 }
179
180 return val;
181 }
182
183 /**
184 * Sets the given key to the given value in the database
185 */
186 public boolean setValue(String key, String value)
187 {
188 try
189 {
190 hashtable_.put(key, value);
191 recman_.commit();
192 }
193 catch (Exception ex)
194 {
195 logger.error("Failed to set " + key + " = " + value + " in the JDBM database");
196 return false;
197 }
198 return true;
199 }
200
201 /**
202 * Deletes the given key (and corresponding value) from the database
203 */
204 public boolean deleteKey(String key)
205 {
206 try
207 {
208 hashtable_.remove(key);
209 recman_.commit();
210 }
211 catch (Exception ex)
212 {
213 logger.error("Failed to delete key " + key + " in the JDBM database");
214 return false;
215 }
216 return true;
217 }
218
219 /**
220 * returns a string of key-value entries that can be printed for debugging
221 * purposes
222 */
223 public String displayAllEntries()
224 {
225
226 StringBuffer keys = new StringBuffer();
227
228 try
229 {
230 FastIterator iter = hashtable_.keys();
231 String key = (String) iter.next();
232
233 String nl = System.getProperty("line.separator");
234
235 while (key != null)
236 {
237 String val = (String) hashtable_.get(key);
238 keys.append("[" + key + "]" + nl);
239 keys.append(val + nl);
240 // 70 hypens
241 keys.append("----------------------------------------------------------------------" + nl);
242 key = (String) iter.next();
243 }
244
245 recman_.commit();
246 }
247 catch (IOException e)
248 {
249 logger.error("Failed get all keys and values from JDBM database");
250 return null;
251 }
252
253 return keys.toString();
254 }
255}
Note: See TracBrowser for help on using the repository browser.