[21400] | 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;
|
---|
[21403] | 29 | import java.io.UnsupportedEncodingException;
|
---|
| 30 | import java.io.OutputStreamWriter;
|
---|
| 31 | import java.io.PrintWriter;
|
---|
[21400] | 32 |
|
---|
| 33 | import java.util.Properties;
|
---|
| 34 | import java.util.ArrayList;
|
---|
| 35 |
|
---|
| 36 | import jdbm.RecordManager;
|
---|
| 37 | import jdbm.RecordManagerFactory;
|
---|
| 38 | import jdbm.helper.FastIterator;
|
---|
| 39 | import jdbm.htree.HTree;
|
---|
| 40 |
|
---|
| 41 |
|
---|
| 42 |
|
---|
| 43 | public class JdbmAPI
|
---|
| 44 | {
|
---|
| 45 | static String TNAME = "greenstone";
|
---|
| 46 |
|
---|
| 47 | RecordManager recman_;
|
---|
| 48 | HTree hashtable_;
|
---|
| 49 |
|
---|
[21403] | 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 |
|
---|
[21402] | 63 | public JdbmAPI(String db_filename,boolean must_exist)
|
---|
[21400] | 64 | throws IOException
|
---|
| 65 | {
|
---|
[21403] | 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 |
|
---|
[21400] | 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) {
|
---|
[22124] | 79 | // System.err.println("# Loading existing database table '" + TNAME +"' ...");
|
---|
[21400] | 80 | hashtable_ = HTree.load(recman_, recid);
|
---|
| 81 | }
|
---|
| 82 | else {
|
---|
[21402] | 83 |
|
---|
| 84 | if (must_exist) {
|
---|
| 85 | recman_.close();
|
---|
[22124] | 86 | // System.err.println("Database table '" + TNAME +"' does not exist.");
|
---|
[21402] | 87 | throw new IOException();
|
---|
| 88 | }
|
---|
| 89 | else {
|
---|
[22124] | 90 | // System.err.println("# No database table '" + TNAME +"' to set. Creating new one");
|
---|
[21402] | 91 | hashtable_ = HTree.createInstance(recman_);
|
---|
| 92 | recman_.setNamedObject(TNAME, hashtable_.getRecid());
|
---|
| 93 | }
|
---|
[21400] | 94 | }
|
---|
| 95 | }
|
---|
[21402] | 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 | }
|
---|
[21400] | 103 |
|
---|
| 104 | public void append(String key, String val)
|
---|
| 105 | throws IOException
|
---|
[21402] | 106 | {
|
---|
[21400] | 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 | {
|
---|
[22040] | 133 | String val = (String) hashtable_.get(key);
|
---|
[21400] | 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();
|
---|
[22124] | 161 | // System.err.println("# Done");
|
---|
[21400] | 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') {
|
---|
[22124] | 175 | // System.err.println("JdbmAPI: Warning, expected end-of-line character, but encountered '" + (char)c + "' ord(" + c + ")");
|
---|
[21400] | 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 |
|
---|
[21403] | 217 | InputStreamReader isr=new InputStreamReader(System.in,"UTF-8");
|
---|
[21400] | 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();
|
---|
[21403] | 228 | utf8out.println(keys_len);
|
---|
[21400] | 229 |
|
---|
| 230 | for (int i=0; i<keys_len; i++) {
|
---|
| 231 | String key = (String) keys.get(i);
|
---|
[21403] | 232 | utf8out.println(key);
|
---|
[21400] | 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);
|
---|
[21403] | 241 | utf8out.println(val.length());
|
---|
| 242 | utf8out.println(val);
|
---|
[21400] | 243 | }
|
---|
| 244 | else {
|
---|
| 245 | jdbm_api.delete(key);
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | }
|
---|
| 249 | else {
|
---|
[22124] | 250 | // System.err.println("JdbmAPI: Command '" + cmd + "'ended prematurely");
|
---|
[21400] | 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) {
|
---|
[22124] | 261 | // System.err.println("JdbmAPI: Command '" + cmd + "'ended prematurely");
|
---|
[21400] | 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) {
|
---|
[22124] | 271 | // System.err.println("JdbmAPI: Command '" + cmd + "'ended prematurely");
|
---|
[21400] | 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 {
|
---|
[22124] | 286 | // System.err.println("JdbmAPI: Unrecognised command '" + cmd + "'");
|
---|
[21400] | 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 |
|
---|