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

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

Refactoring of class to use API

File size: 7.2 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;
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
38
39
40public class JdbmAPI
41{
42 static String TNAME = "greenstone";
43
44 RecordManager recman_;
45 HTree hashtable_;
46
47 public JdbmAPI(String db_filename,boolean must_exist)
48 throws IOException
49 {
50 // create or open a record manager
51 Properties props = new Properties();
52 recman_ = RecordManagerFactory.createRecordManager(db_filename, props);
53
54 // load existing table (if exists) otherwise create new one
55 long recid = recman_.getNamedObject(TNAME);
56
57 if (recid != 0) {
58 System.err.println("# Loading existing database table '" + TNAME +"' ...");
59 hashtable_ = HTree.load(recman_, recid);
60 }
61 else {
62
63 if (must_exist) {
64 recman_.close();
65 System.err.println("Database table '" + TNAME +"' does not exist.");
66 throw new IOException();
67 }
68 else {
69 System.err.println("# No database table '" + TNAME +"' to set. Creating new one");
70 hashtable_ = HTree.createInstance(recman_);
71 recman_.setNamedObject(TNAME, hashtable_.getRecid());
72 }
73 }
74 }
75
76 public JdbmAPI(String db_filename)
77 throws IOException
78 {
79 // default is that database does not have to exist
80 this(db_filename,false);
81 }
82
83 public void append(String key, String val)
84 throws IOException
85 {
86 String orig_val = (String)hashtable_.get(key);
87 String new_val = orig_val + val;
88
89 hashtable_.put(key,new_val);
90 recman_.commit();
91 }
92
93 public void set(String key, String val)
94 throws IOException
95
96 {
97 hashtable_.put(key,val);
98 recman_.commit();
99 }
100
101 public void delete(String key)
102 throws IOException
103 {
104 hashtable_.remove(key);
105 recman_.commit();
106 }
107
108
109 public String get(String key)
110 throws IOException
111 {
112 FastIterator iter;
113 String val;
114
115 val = (String) hashtable_.get(key);
116
117 recman_.commit();
118 return val;
119 }
120
121
122 public ArrayList get_keys()
123 throws IOException
124 {
125 ArrayList keys = new ArrayList();
126
127 FastIterator iter = hashtable_.keys();
128 String next_key = (String) iter.next();
129
130 while (next_key != null) {
131 keys.add(next_key);
132 next_key = (String) iter.next();
133 }
134
135 recman_.commit();
136 return keys;
137 }
138
139 public void close()
140 throws IOException
141 {
142 recman_.close();
143 System.err.println("# Done");
144 }
145
146
147 public static void skip_eol(BufferedReader brin)
148 throws IOException
149 {
150 int c = brin.read();
151 if (c == '\r') {
152 // Assume Windows style EOL
153 // consume another character
154 c = brin.read();
155 }
156 if (c != '\n') {
157 System.err.println("JdbmAPI: Warning, expected end-of-line character, but encountered '" + (char)c + "' ord(" + c + ")");
158 }
159 }
160
161 public static void print_usage()
162 {
163 System.err.println("");
164 System.err.println("Usage: java JdbmAPI database-name");
165 System.err.println("");
166 System.err.println(" - The JDBM commands to perform are then taken from standard-in");
167 System.err.println(" - Commands (with operands) are:");
168 System.err.println(" Keys");
169 System.err.println(" Get\\n<key>");
170 System.err.println(" Set\\n<key>\\n<vallen>\\n<val>");
171 System.err.println(" Append\\n<key>\\n<vallen>\\n<val>");
172 System.err.println(" Delete\\n<key>");
173 System.err.println("");
174 System.err.println(" - Operands are given on separate lines.");
175 System.err.println(" - In the case of <val> it is preceeded by its length.");
176 System.err.println("");
177 System.err.println(" - For example the following would set the key 'HASH0143' to 'testing':");
178 System.err.println(" Set");
179 System.err.println(" HASH0143");
180 System.err.println(" 7");
181 System.err.println(" testing");
182 System.err.println("");
183 System.exit(-1);
184 }
185
186 public static void main(String[] args)
187 {
188 int argc = args.length;
189
190 // sanity check
191 if (argc != 1) {
192 print_usage();
193 }
194
195 try {
196 String dbname = args[0];
197 JdbmAPI jdbm_api = new JdbmAPI(dbname);
198
199 InputStreamReader isr=new InputStreamReader(System.in);
200 BufferedReader brin = new BufferedReader(isr);
201
202 boolean encountered_error = false;
203
204 String cmd = brin.readLine();
205 while (cmd != null) {
206 if (cmd.equals("Keys")) {
207 ArrayList keys = jdbm_api.get_keys();
208
209 int keys_len = keys.size();
210 System.out.println(keys_len);
211
212 for (int i=0; i<keys_len; i++) {
213 String key = (String) keys.get(i);
214 System.out.println(key);
215 }
216 }
217 else if ((cmd.equals("Get")) || (cmd.equals("Delete"))) {
218 String key = brin.readLine();
219 if (key != null) {
220
221 if (cmd.equals("Get")) {
222 String val = jdbm_api.get(key);
223 System.out.println(val.length());
224 System.out.println(val);
225 }
226 else {
227 jdbm_api.delete(key);
228 }
229
230 }
231 else {
232 System.err.println("JdbmAPI: Command '" + cmd + "'ended prematurely");
233 encountered_error = true;
234 break;
235 }
236
237 }
238 else if ((cmd.equals("Set")) || (cmd.equals("Append"))) {
239 String key = brin.readLine();
240
241 String vallen_str = brin.readLine();
242 if (vallen_str==null) {
243 System.err.println("JdbmAPI: Command '" + cmd + "'ended prematurely");
244 encountered_error = true;
245 break;
246 }
247 int vallen = Integer.parseInt(vallen_str);
248 char[] valbuffer = new char[vallen];
249 brin.read(valbuffer,0,vallen);
250 skip_eol(brin);
251
252 if (valbuffer.length==0) {
253 System.err.println("JdbmAPI: Command '" + cmd + "'ended prematurely");
254 encountered_error = true;
255 break;
256 }
257
258 String val = new String(valbuffer);
259
260 if (cmd.equals("Set")) {
261 jdbm_api.set(key,val);
262 }
263 else {
264 jdbm_api.append(key,val);
265 }
266 }
267 else {
268 System.err.println("JdbmAPI: Unrecognised command '" + cmd + "'");
269 encountered_error = true;
270 }
271
272 // read in next command (may be null, signifying eof)
273 cmd = brin.readLine();
274 }
275
276 jdbm_api.close();
277
278 if (encountered_error) {
279 System.exit(-1);
280 }
281 }
282
283 catch (IOException e) {
284 e.printStackTrace();
285 }
286
287 }
288
289}
290
291
Note: See TracBrowser for help on using the repository browser.