[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;
|
---|
| 46 | HTree hashtable_;
|
---|
[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 | }
|
---|
| 63 | }
|
---|
[21431] | 64 |
|
---|
[24393] | 65 | /** open database named filename, using mode */
|
---|
| 66 | public boolean openDatabase(String db_filename, int mode)
|
---|
| 67 | {
|
---|
[21431] | 68 |
|
---|
[24393] | 69 | if (db_filename.endsWith(".jdb"))
|
---|
| 70 | {
|
---|
| 71 | // remove file extension as JDBM does not expect it
|
---|
| 72 | db_filename = db_filename.substring(0, db_filename.length() - 4);
|
---|
| 73 | }
|
---|
[21431] | 74 |
|
---|
[24393] | 75 | // Map the mode value into equivalent JDBM 'must_exist' value
|
---|
| 76 | // currently this is very simple as there is only READ and WRITE
|
---|
| 77 | // (no WRITER_CREATE)
|
---|
| 78 | // => assume the database must exist
|
---|
| 79 | boolean must_exist = true; // default
|
---|
[22973] | 80 |
|
---|
[24393] | 81 | if (recman_ != null)
|
---|
| 82 | {
|
---|
| 83 | String message = "openDatabase() called when the class already has a database open\n";
|
---|
| 84 | message += " Use closeDatabase before opening the next one.\n";
|
---|
| 85 | message += " Existing database file: " + db_filename_ + "\n";
|
---|
| 86 | message += " New database file: " + db_filename + "\n";
|
---|
| 87 | logger.warn(message);
|
---|
| 88 | // consider closing it automatically?
|
---|
| 89 | }
|
---|
[22973] | 90 |
|
---|
[24393] | 91 | try
|
---|
| 92 | {
|
---|
| 93 | // create or open a record manager
|
---|
| 94 | Properties props = new Properties();
|
---|
| 95 | recman_ = RecordManagerFactory.createRecordManager(db_filename, props);
|
---|
[22973] | 96 |
|
---|
[24393] | 97 | // load existing table (if exists) otherwise create new one
|
---|
| 98 | long recid = recman_.getNamedObject(TNAME);
|
---|
[22973] | 99 |
|
---|
[24393] | 100 | if (recid != 0)
|
---|
| 101 | {
|
---|
| 102 | System.err.println("# Loading existing database table '" + TNAME + "' ...");
|
---|
| 103 | hashtable_ = HTree.load(recman_, recid);
|
---|
| 104 | }
|
---|
| 105 | else
|
---|
| 106 | {
|
---|
| 107 |
|
---|
| 108 | if (must_exist)
|
---|
| 109 | {
|
---|
| 110 | recman_.close();
|
---|
| 111 | recman_ = null;
|
---|
| 112 | db_filename_ = null;
|
---|
| 113 |
|
---|
| 114 | System.err.println("Database table '" + TNAME + "' does not exist.");
|
---|
| 115 | throw new IOException();
|
---|
| 116 | }
|
---|
| 117 | else
|
---|
| 118 | {
|
---|
| 119 | System.err.println("# No database table '" + TNAME + "' to set. Creating new one");
|
---|
| 120 | hashtable_ = HTree.createInstance(recman_);
|
---|
| 121 | recman_.setNamedObject(TNAME, hashtable_.getRecid());
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
[21431] | 124 | }
|
---|
[24393] | 125 | catch (IOException e)
|
---|
| 126 | {
|
---|
| 127 | logger.error("couldn't open database " + db_filename);
|
---|
| 128 | return false;
|
---|
| 129 | }
|
---|
[21431] | 130 |
|
---|
[24393] | 131 | db_filename_ = db_filename;
|
---|
[21431] | 132 |
|
---|
[24393] | 133 | return true;
|
---|
| 134 | }
|
---|
[15325] | 135 |
|
---|
[24393] | 136 | /** close the database associated with this wrapper */
|
---|
| 137 | public void closeDatabase()
|
---|
| 138 | {
|
---|
| 139 | try
|
---|
| 140 | {
|
---|
| 141 | if (recman_ != null)
|
---|
| 142 | {
|
---|
| 143 | recman_.close();
|
---|
| 144 | recman_ = null;
|
---|
| 145 | db_filename_ = null;
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 | catch (IOException e)
|
---|
| 149 | {
|
---|
| 150 | logger.error("Failed to close JDBM database");
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
[15325] | 153 |
|
---|
[24393] | 154 | /** returns the value associated with the key */
|
---|
| 155 | public String getValue(String key)
|
---|
| 156 | {
|
---|
[15325] | 157 |
|
---|
[24393] | 158 | String val;
|
---|
[21431] | 159 |
|
---|
[24393] | 160 | try
|
---|
| 161 | {
|
---|
| 162 | val = (String) hashtable_.get(key);
|
---|
| 163 |
|
---|
| 164 | recman_.commit();
|
---|
| 165 | }
|
---|
| 166 | catch (IOException e)
|
---|
| 167 | {
|
---|
| 168 | logger.error("Failed get key " + key + "from JDBM database");
|
---|
| 169 | return null;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | return val;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | /**
|
---|
| 176 | * Sets the given key to the given value in the database
|
---|
| 177 | */
|
---|
| 178 | public boolean setValue(String key, String value)
|
---|
| 179 | {
|
---|
| 180 | try
|
---|
| 181 | {
|
---|
| 182 | hashtable_.put(key, value);
|
---|
| 183 | recman_.commit();
|
---|
| 184 | }
|
---|
| 185 | catch (Exception ex)
|
---|
| 186 | {
|
---|
| 187 | logger.error("Failed to set " + key + " = " + value + " in the JDBM database");
|
---|
| 188 | return false;
|
---|
| 189 | }
|
---|
| 190 | return true;
|
---|
| 191 | }
|
---|
[21431] | 192 |
|
---|
[24393] | 193 | /**
|
---|
| 194 | * Deletes the given key (and corresponding value) from the database
|
---|
| 195 | */
|
---|
| 196 | public boolean deleteKey(String key)
|
---|
| 197 | {
|
---|
| 198 | try
|
---|
| 199 | {
|
---|
| 200 | hashtable_.remove(key);
|
---|
| 201 | recman_.commit();
|
---|
| 202 | }
|
---|
| 203 | catch (Exception ex)
|
---|
| 204 | {
|
---|
| 205 | logger.error("Failed to delete key " + key + " in the JDBM database");
|
---|
| 206 | return false;
|
---|
| 207 | }
|
---|
| 208 | return true;
|
---|
| 209 | }
|
---|
[21431] | 210 |
|
---|
[24393] | 211 | /**
|
---|
| 212 | * returns a string of key-value entries that can be printed for debugging
|
---|
| 213 | * purposes
|
---|
| 214 | */
|
---|
| 215 | public String displayAllEntries()
|
---|
| 216 | {
|
---|
[15325] | 217 |
|
---|
[24393] | 218 | StringBuffer keys = new StringBuffer();
|
---|
[21431] | 219 |
|
---|
[24393] | 220 | try
|
---|
| 221 | {
|
---|
| 222 | FastIterator iter = hashtable_.keys();
|
---|
| 223 | String key = (String) iter.next();
|
---|
[21431] | 224 |
|
---|
[24393] | 225 | String nl = System.getProperty("line.separator");
|
---|
[21431] | 226 |
|
---|
[24393] | 227 | while (key != null)
|
---|
| 228 | {
|
---|
| 229 | String val = (String) hashtable_.get(key);
|
---|
| 230 | keys.append("[" + key + "]" + nl);
|
---|
| 231 | keys.append(val + nl);
|
---|
| 232 | // 70 hypens
|
---|
| 233 | keys.append("----------------------------------------------------------------------" + nl);
|
---|
| 234 | key = (String) iter.next();
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | recman_.commit();
|
---|
| 238 | }
|
---|
| 239 | catch (IOException e)
|
---|
| 240 | {
|
---|
| 241 | logger.error("Failed get all keys and values from JDBM database");
|
---|
| 242 | return null;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | return keys.toString();
|
---|
| 246 | }
|
---|
[16797] | 247 | }
|
---|