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

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

Development of JdbmAPI that supports -- as a persistent session -- interaction with the JDBM database

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