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