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