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 |
|
---|
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 |
|
---|
38 | public class JDBMWrapper
|
---|
39 | implements FlatDatabaseWrapper {
|
---|
40 |
|
---|
41 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.JDBMWrapper.class.getName());
|
---|
42 |
|
---|
43 | static String TNAME = "greenstone";
|
---|
44 |
|
---|
45 | RecordManager recman_;
|
---|
46 | HTree hashtable_;
|
---|
47 |
|
---|
48 | static private PrintWriter utf8out = null;
|
---|
49 |
|
---|
50 | static
|
---|
51 | {
|
---|
52 | try {
|
---|
53 | OutputStreamWriter osw = new OutputStreamWriter(System.out, "UTF-8");
|
---|
54 | utf8out = new PrintWriter(osw, true);
|
---|
55 | }
|
---|
56 | catch (UnsupportedEncodingException e) {
|
---|
57 | System.out.println(e);
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | /** open database named filename, using mode */
|
---|
63 | public boolean openDatabase(String db_filename, int mode) {
|
---|
64 |
|
---|
65 |
|
---|
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 |
|
---|
71 | // Map the mode value into equivalent JDBM 'must_exist' value
|
---|
72 | // currently this is very simple as there is only READ and WRITE
|
---|
73 | // (no WRITER_CREATE)
|
---|
74 | // => assume the database must exist
|
---|
75 | boolean must_exist = true; // default
|
---|
76 |
|
---|
77 | try {
|
---|
78 | // create or open a record manager
|
---|
79 | Properties props = new Properties();
|
---|
80 | recman_ = RecordManagerFactory.createRecordManager(db_filename, props);
|
---|
81 |
|
---|
82 | // load existing table (if exists) otherwise create new one
|
---|
83 | long recid = recman_.getNamedObject(TNAME);
|
---|
84 |
|
---|
85 | if (recid != 0) {
|
---|
86 | System.err.println("# Loading existing database table '" + TNAME +"' ...");
|
---|
87 | hashtable_ = HTree.load(recman_, recid);
|
---|
88 | }
|
---|
89 | else {
|
---|
90 |
|
---|
91 | if (must_exist) {
|
---|
92 | recman_.close();
|
---|
93 | System.err.println("Database table '" + TNAME +"' does not exist.");
|
---|
94 | throw new IOException();
|
---|
95 | }
|
---|
96 | else {
|
---|
97 | System.err.println("# No database table '" + TNAME +"' to set. Creating new one");
|
---|
98 | hashtable_ = HTree.createInstance(recman_);
|
---|
99 | recman_.setNamedObject(TNAME, hashtable_.getRecid());
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 | catch (IOException e) {
|
---|
104 | logger.error("couldn't open database "+ db_filename);
|
---|
105 | return false;
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | return true;
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | /** close the database associated with this wrapper */
|
---|
114 | public void closeDatabase() {
|
---|
115 | try {
|
---|
116 | recman_.close();
|
---|
117 | }
|
---|
118 | catch (IOException e) {
|
---|
119 | logger.error("Failed to close JDBM database");
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | /** returns the value associated with the key */
|
---|
124 | public String getValue(String key) {
|
---|
125 |
|
---|
126 | String val;
|
---|
127 |
|
---|
128 | try {
|
---|
129 | val = (String) hashtable_.get(key);
|
---|
130 |
|
---|
131 | recman_.commit();
|
---|
132 | }
|
---|
133 | catch (IOException e) {
|
---|
134 | logger.error("Failed get key " + key + "from JDBM database");
|
---|
135 | return null;
|
---|
136 | }
|
---|
137 |
|
---|
138 | return val;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /** returns a string of key-value entries that can be
|
---|
142 | * printed for debugging purposes*/
|
---|
143 | public String displayAllEntries() {
|
---|
144 |
|
---|
145 | StringBuffer keys = new StringBuffer();
|
---|
146 |
|
---|
147 | try {
|
---|
148 | FastIterator iter = hashtable_.keys();
|
---|
149 | String key = (String) iter.next();
|
---|
150 |
|
---|
151 | String nl = System.getProperty("line.separator");
|
---|
152 |
|
---|
153 | while (key != null) {
|
---|
154 | String val = (String) hashtable_.get(key);
|
---|
155 | keys.append("[" + key + "]" + nl);
|
---|
156 | keys.append(val + nl);
|
---|
157 | // 70 hypens
|
---|
158 | keys.append("----------------------------------------------------------------------" + nl);
|
---|
159 | key = (String) iter.next();
|
---|
160 | }
|
---|
161 |
|
---|
162 | recman_.commit();
|
---|
163 | }
|
---|
164 | catch (IOException e) {
|
---|
165 | logger.error("Failed get all keys and values from JDBM database");
|
---|
166 | return null;
|
---|
167 | }
|
---|
168 |
|
---|
169 | return keys.toString();
|
---|
170 | }
|
---|
171 | }
|
---|