[15325] | 1 | /*
|
---|
| 2 | * JDBMWrapper.java
|
---|
| 3 | * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
|
---|
| 4 | *
|
---|
| 5 | * This program is free software; you can redistribute it and/or modify
|
---|
| 6 | * it under the terms of the GNU General Public License as published by
|
---|
| 7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 8 | * (at your option) any later version.
|
---|
| 9 | *
|
---|
| 10 | * This program is distributed in the hope that it will be useful,
|
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 13 | * GNU General Public License for more details.
|
---|
| 14 | *
|
---|
| 15 | * You should have received a copy of the GNU General Public License
|
---|
| 16 | * along with this program; if not, write to the Free Software
|
---|
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 18 | */
|
---|
| 19 | package org.greenstone.gsdl3.util;
|
---|
| 20 |
|
---|
| 21 | import org.apache.log4j.Logger;
|
---|
| 22 |
|
---|
[21431] | 23 | import java.io.BufferedReader;
|
---|
| 24 | import java.io.InputStreamReader;
|
---|
| 25 | import java.io.IOException;
|
---|
| 26 | import java.io.UnsupportedEncodingException;
|
---|
| 27 | import java.io.OutputStreamWriter;
|
---|
| 28 | import java.io.PrintWriter;
|
---|
| 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 |
|
---|
[24393] | 38 | public class JDBMWrapper implements FlatDatabaseWrapper
|
---|
| 39 | {
|
---|
[15325] | 40 |
|
---|
[24393] | 41 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.JDBMWrapper.class.getName());
|
---|
[21431] | 42 |
|
---|
[24393] | 43 | static String TNAME = "greenstone";
|
---|
[21431] | 44 |
|
---|
[24393] | 45 | RecordManager recman_ = null;
|
---|
[30516] | 46 | HTree hashtable_ = null;
|
---|
[22973] | 47 |
|
---|
[24393] | 48 | String db_filename_;
|
---|
[21431] | 49 |
|
---|
[24393] | 50 | static private PrintWriter utf8out = null;
|
---|
[21431] | 51 |
|
---|
[24393] | 52 | static
|
---|
| 53 | {
|
---|
| 54 | try
|
---|
| 55 | {
|
---|
| 56 | OutputStreamWriter osw = new OutputStreamWriter(System.out, "UTF-8");
|
---|
| 57 | utf8out = new PrintWriter(osw, true);
|
---|
| 58 | }
|
---|
| 59 | catch (UnsupportedEncodingException e)
|
---|
| 60 | {
|
---|
| 61 | System.out.println(e);
|
---|
| 62 | }
|
---|
[30264] | 63 | // Register this wrapper with DBHelper
|
---|
| 64 | DBHelper.registerDBTypeExt("jdbm",".jdb");
|
---|
[24393] | 65 | }
|
---|
[21431] | 66 |
|
---|
[24393] | 67 | /** open database named filename, using mode */
|
---|
| 68 | public boolean openDatabase(String db_filename, int mode)
|
---|
| 69 | {
|
---|
[27824] | 70 | String db_filename_with_ext = db_filename;
|
---|
[21431] | 71 |
|
---|
[24393] | 72 | if (db_filename.endsWith(".jdb"))
|
---|
| 73 | {
|
---|
| 74 | // remove file extension as JDBM does not expect it
|
---|
| 75 | db_filename = db_filename.substring(0, db_filename.length() - 4);
|
---|
| 76 | }
|
---|
[21431] | 77 |
|
---|
[24393] | 78 | // Map the mode value into equivalent JDBM 'must_exist' value
|
---|
| 79 | // currently this is very simple as there is only READ and WRITE
|
---|
| 80 | // (no WRITER_CREATE)
|
---|
| 81 | // => assume the database must exist
|
---|
| 82 | boolean must_exist = true; // default
|
---|
[22973] | 83 |
|
---|
[24393] | 84 | if (recman_ != null)
|
---|
| 85 | {
|
---|
| 86 | String message = "openDatabase() called when the class already has a database open\n";
|
---|
| 87 | message += " Use closeDatabase before opening the next one.\n";
|
---|
| 88 | message += " Existing database file: " + db_filename_ + "\n";
|
---|
| 89 | message += " New database file: " + db_filename + "\n";
|
---|
| 90 | logger.warn(message);
|
---|
| 91 | // consider closing it automatically?
|
---|
| 92 | }
|
---|
[22973] | 93 |
|
---|
[24393] | 94 | try
|
---|
| 95 | {
|
---|
| 96 | // create or open a record manager
|
---|
| 97 | Properties props = new Properties();
|
---|
| 98 | recman_ = RecordManagerFactory.createRecordManager(db_filename, props);
|
---|
[24860] | 99 |
|
---|
[24393] | 100 | // load existing table (if exists) otherwise create new one
|
---|
| 101 | long recid = recman_.getNamedObject(TNAME);
|
---|
[22973] | 102 |
|
---|
[24393] | 103 | if (recid != 0)
|
---|
| 104 | {
|
---|
[24860] | 105 | logger.info("# Loading existing database table '" + TNAME + "' ...");
|
---|
[24393] | 106 | hashtable_ = HTree.load(recman_, recid);
|
---|
| 107 | }
|
---|
| 108 | else
|
---|
| 109 | {
|
---|
| 110 | if (must_exist)
|
---|
| 111 | {
|
---|
| 112 | recman_.close();
|
---|
| 113 | recman_ = null;
|
---|
| 114 | db_filename_ = null;
|
---|
| 115 |
|
---|
[24860] | 116 | logger.error("Database table '" + TNAME + "' does not exist.");
|
---|
[24393] | 117 | throw new IOException();
|
---|
| 118 | }
|
---|
| 119 | else
|
---|
| 120 | {
|
---|
[24860] | 121 | logger.info("# No database table '" + TNAME + "' to set. Creating new one");
|
---|
[24393] | 122 | hashtable_ = HTree.createInstance(recman_);
|
---|
| 123 | recman_.setNamedObject(TNAME, hashtable_.getRecid());
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
[21431] | 126 | }
|
---|
[24393] | 127 | catch (IOException e)
|
---|
| 128 | {
|
---|
[27824] | 129 | e.printStackTrace();
|
---|
| 130 |
|
---|
| 131 | logger.error("couldn't open database " + db_filename_with_ext);
|
---|
[24393] | 132 | return false;
|
---|
| 133 | }
|
---|
[21431] | 134 |
|
---|
[24393] | 135 | db_filename_ = db_filename;
|
---|
[21431] | 136 |
|
---|
[24393] | 137 | return true;
|
---|
| 138 | }
|
---|
[15325] | 139 |
|
---|
[24393] | 140 | /** close the database associated with this wrapper */
|
---|
| 141 | public void closeDatabase()
|
---|
| 142 | {
|
---|
| 143 | try
|
---|
| 144 | {
|
---|
| 145 | if (recman_ != null)
|
---|
| 146 | {
|
---|
| 147 | recman_.close();
|
---|
| 148 | recman_ = null;
|
---|
| 149 | db_filename_ = null;
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 | catch (IOException e)
|
---|
| 153 | {
|
---|
| 154 | logger.error("Failed to close JDBM database");
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
[15325] | 157 |
|
---|
[24393] | 158 | /** returns the value associated with the key */
|
---|
| 159 | public String getValue(String key)
|
---|
| 160 | {
|
---|
[30516] | 161 | if (hashtable_ == null) {
|
---|
| 162 | logger.error("Database was not opened or not exist!");
|
---|
| 163 | return null;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[24393] | 166 | String val;
|
---|
[21431] | 167 |
|
---|
[24393] | 168 | try
|
---|
| 169 | {
|
---|
| 170 | val = (String) hashtable_.get(key);
|
---|
| 171 |
|
---|
| 172 | recman_.commit();
|
---|
| 173 | }
|
---|
| 174 | catch (IOException e)
|
---|
| 175 | {
|
---|
| 176 | logger.error("Failed get key " + key + "from JDBM database");
|
---|
| 177 | return null;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | return val;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | /**
|
---|
| 184 | * Sets the given key to the given value in the database
|
---|
| 185 | */
|
---|
| 186 | public boolean setValue(String key, String value)
|
---|
| 187 | {
|
---|
| 188 | try
|
---|
| 189 | {
|
---|
| 190 | hashtable_.put(key, value);
|
---|
| 191 | recman_.commit();
|
---|
| 192 | }
|
---|
| 193 | catch (Exception ex)
|
---|
| 194 | {
|
---|
| 195 | logger.error("Failed to set " + key + " = " + value + " in the JDBM database");
|
---|
| 196 | return false;
|
---|
| 197 | }
|
---|
| 198 | return true;
|
---|
| 199 | }
|
---|
[21431] | 200 |
|
---|
[24393] | 201 | /**
|
---|
| 202 | * Deletes the given key (and corresponding value) from the database
|
---|
| 203 | */
|
---|
| 204 | public boolean deleteKey(String key)
|
---|
| 205 | {
|
---|
| 206 | try
|
---|
| 207 | {
|
---|
| 208 | hashtable_.remove(key);
|
---|
| 209 | recman_.commit();
|
---|
| 210 | }
|
---|
| 211 | catch (Exception ex)
|
---|
| 212 | {
|
---|
| 213 | logger.error("Failed to delete key " + key + " in the JDBM database");
|
---|
| 214 | return false;
|
---|
| 215 | }
|
---|
| 216 | return true;
|
---|
| 217 | }
|
---|
[21431] | 218 |
|
---|
[24393] | 219 | /**
|
---|
| 220 | * returns a string of key-value entries that can be printed for debugging
|
---|
| 221 | * purposes
|
---|
| 222 | */
|
---|
| 223 | public String displayAllEntries()
|
---|
| 224 | {
|
---|
[15325] | 225 |
|
---|
[24393] | 226 | StringBuffer keys = new StringBuffer();
|
---|
[21431] | 227 |
|
---|
[24393] | 228 | try
|
---|
| 229 | {
|
---|
| 230 | FastIterator iter = hashtable_.keys();
|
---|
| 231 | String key = (String) iter.next();
|
---|
[21431] | 232 |
|
---|
[24393] | 233 | String nl = System.getProperty("line.separator");
|
---|
[21431] | 234 |
|
---|
[24393] | 235 | while (key != null)
|
---|
| 236 | {
|
---|
| 237 | String val = (String) hashtable_.get(key);
|
---|
| 238 | keys.append("[" + key + "]" + nl);
|
---|
| 239 | keys.append(val + nl);
|
---|
| 240 | // 70 hypens
|
---|
| 241 | keys.append("----------------------------------------------------------------------" + nl);
|
---|
| 242 | key = (String) iter.next();
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | recman_.commit();
|
---|
| 246 | }
|
---|
| 247 | catch (IOException e)
|
---|
| 248 | {
|
---|
| 249 | logger.error("Failed get all keys and values from JDBM database");
|
---|
| 250 | return null;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | return keys.toString();
|
---|
| 254 | }
|
---|
[16797] | 255 | }
|
---|