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 |
|
---|
26 | import java.io.BufferedReader;
|
---|
27 | import java.io.InputStreamReader;
|
---|
28 | import java.io.IOException;
|
---|
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 |
|
---|
39 |
|
---|
40 | public 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 | String vallen_str = brin.readLine();
|
---|
227 | if (vallen_str==null) {
|
---|
228 | System.err.println("JdbmAPI: Command '" + cmd + "'ended prematurely");
|
---|
229 | encountered_error = true;
|
---|
230 | break;
|
---|
231 | }
|
---|
232 | int vallen = Integer.parseInt(vallen_str);
|
---|
233 | char[] valbuffer = new char[vallen];
|
---|
234 | brin.read(valbuffer,0,vallen);
|
---|
235 | skip_eol(brin);
|
---|
236 |
|
---|
237 | if (valbuffer.length==0) {
|
---|
238 | System.err.println("JdbmAPI: Command '" + cmd + "'ended prematurely");
|
---|
239 | encountered_error = true;
|
---|
240 | break;
|
---|
241 | }
|
---|
242 |
|
---|
243 | String val = new String(valbuffer);
|
---|
244 |
|
---|
245 | if (cmd.equals("Set")) {
|
---|
246 | jdbm_api.set(key,val);
|
---|
247 | }
|
---|
248 | else {
|
---|
249 | jdbm_api.append(key,val);
|
---|
250 | }
|
---|
251 | }
|
---|
252 | else {
|
---|
253 | System.err.println("JdbmAPI: Unrecognised command '" + cmd + "'");
|
---|
254 | encountered_error = true;
|
---|
255 | }
|
---|
256 |
|
---|
257 | // read in next command (may be null, signifying eof)
|
---|
258 | cmd = brin.readLine();
|
---|
259 | }
|
---|
260 |
|
---|
261 | jdbm_api.close();
|
---|
262 |
|
---|
263 | if (encountered_error) {
|
---|
264 | System.exit(-1);
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | catch (IOException e) {
|
---|
269 | e.printStackTrace();
|
---|
270 | }
|
---|
271 |
|
---|
272 | }
|
---|
273 |
|
---|
274 | }
|
---|
275 |
|
---|
276 |
|
---|