source: main/trunk/greenstone2/common-src/src/jdbmedit/JdbmAPI.java@ 22040

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

Minor tidy up of Java code

File size: 7.8 KB
Line 
1/**********************************************************************
2 *
3 * JdbmAPI.java --
4 * A component of the Greenstone digital library software
5 * from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * Copyright (C) 2010 The New Zealand Digital Library Project
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 **********************************************************************/
25
26import java.io.BufferedReader;
27import java.io.InputStreamReader;
28import java.io.IOException;
29import java.io.UnsupportedEncodingException;
30import java.io.OutputStreamWriter;
31import java.io.PrintWriter;
32
33import java.util.Properties;
34import java.util.ArrayList;
35
36import jdbm.RecordManager;
37import jdbm.RecordManagerFactory;
38import jdbm.helper.FastIterator;
39import jdbm.htree.HTree;
40
41
42
43public class JdbmAPI
44{
45 static String TNAME = "greenstone";
46
47 RecordManager recman_;
48 HTree hashtable_;
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 public JdbmAPI(String db_filename,boolean must_exist)
64 throws IOException
65 {
66 if (db_filename.endsWith(".jdb")) {
67 // remove file extension as JDBM does not expect it
68 db_filename = db_filename.substring(0,db_filename.length()-4);
69 }
70
71 // create or open a record manager
72 Properties props = new Properties();
73 recman_ = RecordManagerFactory.createRecordManager(db_filename, props);
74
75 // load existing table (if exists) otherwise create new one
76 long recid = recman_.getNamedObject(TNAME);
77
78 if (recid != 0) {
79 System.err.println("# Loading existing database table '" + TNAME +"' ...");
80 hashtable_ = HTree.load(recman_, recid);
81 }
82 else {
83
84 if (must_exist) {
85 recman_.close();
86 System.err.println("Database table '" + TNAME +"' does not exist.");
87 throw new IOException();
88 }
89 else {
90 System.err.println("# No database table '" + TNAME +"' to set. Creating new one");
91 hashtable_ = HTree.createInstance(recman_);
92 recman_.setNamedObject(TNAME, hashtable_.getRecid());
93 }
94 }
95 }
96
97 public JdbmAPI(String db_filename)
98 throws IOException
99 {
100 // default is that database does not have to exist
101 this(db_filename,false);
102 }
103
104 public void append(String key, String val)
105 throws IOException
106 {
107 String orig_val = (String)hashtable_.get(key);
108 String new_val = orig_val + val;
109
110 hashtable_.put(key,new_val);
111 recman_.commit();
112 }
113
114 public void set(String key, String val)
115 throws IOException
116
117 {
118 hashtable_.put(key,val);
119 recman_.commit();
120 }
121
122 public void delete(String key)
123 throws IOException
124 {
125 hashtable_.remove(key);
126 recman_.commit();
127 }
128
129
130 public String get(String key)
131 throws IOException
132 {
133 String val = (String) hashtable_.get(key);
134
135 recman_.commit();
136 return val;
137 }
138
139
140 public ArrayList get_keys()
141 throws IOException
142 {
143 ArrayList keys = new ArrayList();
144
145 FastIterator iter = hashtable_.keys();
146 String next_key = (String) iter.next();
147
148 while (next_key != null) {
149 keys.add(next_key);
150 next_key = (String) iter.next();
151 }
152
153 recman_.commit();
154 return keys;
155 }
156
157 public void close()
158 throws IOException
159 {
160 recman_.close();
161 System.err.println("# Done");
162 }
163
164
165 public static void skip_eol(BufferedReader brin)
166 throws IOException
167 {
168 int c = brin.read();
169 if (c == '\r') {
170 // Assume Windows style EOL
171 // consume another character
172 c = brin.read();
173 }
174 if (c != '\n') {
175 System.err.println("JdbmAPI: Warning, expected end-of-line character, but encountered '" + (char)c + "' ord(" + c + ")");
176 }
177 }
178
179 public static void print_usage()
180 {
181 System.err.println("");
182 System.err.println("Usage: java JdbmAPI database-name");
183 System.err.println("");
184 System.err.println(" - The JDBM commands to perform are then taken from standard-in");
185 System.err.println(" - Commands (with operands) are:");
186 System.err.println(" Keys");
187 System.err.println(" Get\\n<key>");
188 System.err.println(" Set\\n<key>\\n<vallen>\\n<val>");
189 System.err.println(" Append\\n<key>\\n<vallen>\\n<val>");
190 System.err.println(" Delete\\n<key>");
191 System.err.println("");
192 System.err.println(" - Operands are given on separate lines.");
193 System.err.println(" - In the case of <val> it is preceeded by its length.");
194 System.err.println("");
195 System.err.println(" - For example the following would set the key 'HASH0143' to 'testing':");
196 System.err.println(" Set");
197 System.err.println(" HASH0143");
198 System.err.println(" 7");
199 System.err.println(" testing");
200 System.err.println("");
201 System.exit(-1);
202 }
203
204 public static void main(String[] args)
205 {
206 int argc = args.length;
207
208 // sanity check
209 if (argc != 1) {
210 print_usage();
211 }
212
213 try {
214 String dbname = args[0];
215 JdbmAPI jdbm_api = new JdbmAPI(dbname);
216
217 InputStreamReader isr=new InputStreamReader(System.in,"UTF-8");
218 BufferedReader brin = new BufferedReader(isr);
219
220 boolean encountered_error = false;
221
222 String cmd = brin.readLine();
223 while (cmd != null) {
224 if (cmd.equals("Keys")) {
225 ArrayList keys = jdbm_api.get_keys();
226
227 int keys_len = keys.size();
228 utf8out.println(keys_len);
229
230 for (int i=0; i<keys_len; i++) {
231 String key = (String) keys.get(i);
232 utf8out.println(key);
233 }
234 }
235 else if ((cmd.equals("Get")) || (cmd.equals("Delete"))) {
236 String key = brin.readLine();
237 if (key != null) {
238
239 if (cmd.equals("Get")) {
240 String val = jdbm_api.get(key);
241 utf8out.println(val.length());
242 utf8out.println(val);
243 }
244 else {
245 jdbm_api.delete(key);
246 }
247
248 }
249 else {
250 System.err.println("JdbmAPI: Command '" + cmd + "'ended prematurely");
251 encountered_error = true;
252 break;
253 }
254
255 }
256 else if ((cmd.equals("Set")) || (cmd.equals("Append"))) {
257 String key = brin.readLine();
258
259 String vallen_str = brin.readLine();
260 if (vallen_str==null) {
261 System.err.println("JdbmAPI: Command '" + cmd + "'ended prematurely");
262 encountered_error = true;
263 break;
264 }
265 int vallen = Integer.parseInt(vallen_str);
266 char[] valbuffer = new char[vallen];
267 brin.read(valbuffer,0,vallen);
268 skip_eol(brin);
269
270 if (valbuffer.length==0) {
271 System.err.println("JdbmAPI: Command '" + cmd + "'ended prematurely");
272 encountered_error = true;
273 break;
274 }
275
276 String val = new String(valbuffer);
277
278 if (cmd.equals("Set")) {
279 jdbm_api.set(key,val);
280 }
281 else {
282 jdbm_api.append(key,val);
283 }
284 }
285 else {
286 System.err.println("JdbmAPI: Unrecognised command '" + cmd + "'");
287 encountered_error = true;
288 }
289
290 // read in next command (may be null, signifying eof)
291 cmd = brin.readLine();
292 }
293
294 jdbm_api.close();
295
296 if (encountered_error) {
297 System.exit(-1);
298 }
299 }
300
301 catch (IOException e) {
302 e.printStackTrace();
303 }
304
305 }
306
307}
308
309
Note: See TracBrowser for help on using the repository browser.