source: gs2-extensions/tdb/trunk/src/java/org/greenstone/tdbjava/TDBWrapper.java@ 30256

Last change on this file since 30256 was 30256, checked in by jmt12, 9 years ago

Moved a comment into the static block... just because I could

File size: 5.5 KB
Line 
1/*
2 * TDBWrapper.java
3 * Copyright (C) 2015 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 */
19package org.greenstone.gsdl3.util;
20
21import java.net.URLEncoder;
22import java.util.Enumeration;
23
24import org.apache.log4j.Logger;
25import org.greenstone.gsdl3.util.DBHelper;
26import org.greenstone.gsdl3.util.FlatDatabaseWrapper;
27
28import org.greenstone.tdbjava.TDBJava;
29
30/**
31 * Java wrapper class for TDB - uses TDBJava (a JNI wrapper to TDB)
32 */
33public class TDBWrapper
34 implements FlatDatabaseWrapper
35{
36 // A logger for messaging (mostly error)
37 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.TDBWrapper.class.getName());
38
39 static
40 {
41 /** Register this Wrapper with the DBHelper */
42 DBHelper.registerDBTypeExt("tdb",".tdb");
43 logger.info("Registered TDB database type with file extension .tdb");
44 }
45
46 // The open database (if any)
47 protected TDBJava db_ = null;
48
49
50 /** @function openDatabase(String, int)
51 * Open the database filename, with mode mode
52 */
53 public boolean openDatabase(String filename, int mode)
54 {
55 // convert mode to TDBJava open mode
56 if (mode == this.READ) {
57 mode = TDBJava.O_READONLY;
58 }
59 else if (mode == this.WRITE) {
60 mode = TDBJava.O_DEFAULT;
61 }
62 else {
63 logger.error("invalid mode, " + mode + ", opening db for reading only");
64 mode = TDBJava.O_READONLY;
65 }
66
67 try {
68 if (this.db_ != null) {
69 this.db_.close();
70 }
71 this.db_ = new TDBJava(filename, TDBJava.TDB_DEFAULT, mode);
72 }
73 catch (Exception e) {
74 logger.error("couldn't open database: " + filename);
75 logger.error(" Exception: " + e.toString());
76 return false;
77 }
78 logger.info("opened with tdb_flags=" + TDBJava.TDB_DEFAULT + " and mode=" + mode);
79 return true;
80 }
81 /** openDatabase(String, int) **/
82
83
84 /** @function closeDatabase
85 * Close the database associated with this wrapper
86 */
87 public void closeDatabase()
88 {
89 if (this.db_ != null) {
90 try {
91 this.db_.close();
92 this.db_ = null;
93 // Hack: force Windows to let go of the gdb file
94 if (System.getProperty("os.name").startsWith("Windows")) {
95 System.gc();
96 }
97 }
98 catch (Exception e) {
99 logger.error("error on close()");
100 logger.error(" Exception: " + e.toString());
101 }
102 }
103 }
104 /** closeDatabase() **/
105
106 /** @function getValue(String)
107 */
108 public String getValue(String key)
109 {
110 String value = null;
111 if (null != this.db_) {
112 try {
113 value = this.db_.fetch(key);
114 }
115 catch (Exception e) {
116 logger.error("error on getValue(" + key + ")");
117 logger.error(" Exception: " + e.toString());
118 }
119 }
120 if (null == value) {
121 logger.error("key " + key + " not present in db");
122 }
123 return value;
124 }
125 /** getValue(String) **/
126
127 /** @function setValue(String, String)
128 * Sets the given key to the given value in the database
129 */
130 public boolean setValue(String key, String value)
131 {
132 boolean result = false;
133 if (this.db_ == null) {
134 logger.error("database is not open");
135 }
136 else if (!this.db_.isWritable()) {
137 logger.error("database is read-only");
138 }
139 else {
140 try {
141 this.db_.store(key, value);
142 result = true;
143 }
144 catch (Exception e) {
145 logger.error("error storing " + key + " = " + value);
146 logger.error(" Exception: " + e.toString());
147 }
148 }
149 return result;
150 }
151 /** setValue(String, String) **/
152
153 /** @function deleteKey(String key)
154 * Deletes the entry for given key
155 */
156 public boolean deleteKey(String key)
157 {
158 boolean result = false;
159 if (null == this.db_) {
160 logger.error("database is not open");
161 }
162 else if (!this.db_.isWritable()) {
163 logger.error("database is read-only");
164 }
165 else {
166 try {
167 this.db_.delete(key);
168 result = true;
169 }
170 catch (Exception e) {
171 logger.error("error deleting key " + key);
172 logger.error(" Exception: " + e.toString());
173 }
174 }
175 return result;
176 }
177 /** deleteKey(String) **/
178
179 /** @function displayAllEntries()
180 * Returns a string of key-value entries that can be printed for debugging
181 * purposes.
182 */
183 public String displayAllEntries()
184 {
185 StringBuffer output = new StringBuffer();
186 if (null == this.db_) {
187 logger.error("database is not open");
188 }
189 else {
190 try {
191 Enumeration keys = this.db_.keys();
192 while (keys.hasMoreElements()) {
193 String key = (String) keys.nextElement();
194 String value = this.db_.fetch(key);
195 String urlkey = URLEncoder.encode((String) key, "UTF8");
196 output.append("key href: ");
197 output.append(key);
198 output.append("\tvalue ID: ");
199 output.append(value);
200 output.append("\n");
201 output.append("URL encoded key: " + urlkey);
202 }
203 }
204 catch (Exception e) {
205 logger.error("error when trying to display all entries");
206 logger.error(" Exception: " + e.toString());
207 }
208 }
209 return output.toString();
210 }
211 /** displayAllEntries() **/
212
213}
214/** Class: TDBWrapper **/
Note: See TracBrowser for help on using the repository browser.