source: greenstone3/trunk/src/java/org/greenstone/gsdl3/util/GDBMWrapper.java@ 16869

Last change on this file since 16869 was 16869, checked in by kjdon, 16 years ago

added license message

  • Property svn:keywords set to Author Date Id Revision
File size: 4.7 KB
Line 
1/*
2 * GDBMWrapper.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 */
19package org.greenstone.gsdl3.util;
20
21import au.com.pharos.gdbm.GdbmFile;
22import au.com.pharos.packing.*;
23import au.com.pharos.gdbm.GdbmException;
24
25import org.apache.log4j.Logger;
26
27import java.io.UnsupportedEncodingException;
28
29/** java wrapper class for gdbm - uses Java-GDBM written by Martin Pool
30 * replaces gdbmclass in the old version
31 */
32
33public class GDBMWrapper
34 implements FlatDatabaseWrapper {
35
36 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.GDBMWrapper.class.getName());
37
38 /* GdbmFile modes:
39 READER - read access, many readers may share the database
40 WRITER - read/write access, exclusive access
41 WRCREAT - read/write access, create db if doesn't exist
42 NEWDB - read/write access, db should be replaced if exists
43 */
44
45
46 protected GdbmFile db_=null;
47
48 /** open the database filename, with mode mode - uses the constants
49 above, eg GdbmFile.WRITER */
50 public boolean openDatabase(String filename, int mode) {
51 // need to convert mode to GdbmFile mode
52 if (mode == READ) {
53 mode = GdbmFile.READER;
54 } else if (mode == WRITE) {
55 mode = GdbmFile.WRITER;
56 } else {
57 logger.error ("invalid mode, "+mode+ ", opening db for reading only");
58 mode = GdbmFile.READER;
59 }
60
61 try {
62 if (db_!=null) {
63 db_.close();
64 }
65 db_ = new GdbmFile(filename, mode);
66 } catch ( GdbmException e) { // the database wasn't opened or created
67 logger.error("couldn't open database "+filename);
68 return false;
69 }
70 db_.setKeyPacking(new StringPacking());
71 db_.setValuePacking(new StringPacking());
72 return true;
73 }
74
75 /** close the database associated with this wrapper */
76 public void closeDatabase() {
77 try {
78 if (db_ != null) {
79 db_.close();
80 db_ = null;
81 }
82 } catch (GdbmException e) {
83 // should never get here - close never actually throws an exception
84 logger.error("error on close()");
85 }
86 }
87
88 public String getValue(String key) {
89 if (db_==null) {
90 return null;
91 }
92 String s_info;
93 try {
94 try {
95 // The key is UTF8: do db lookup using the UTF8 version of key
96 s_info = (String)db_.fetch(key.getBytes("UTF-8"));
97 } catch(UnsupportedEncodingException e) {
98 logger.warn("utf8 key for " + key
99 + " unrecognised. Retrying with default encoding.");
100 // retry db lookup using default encoding of key instead of UTF8
101 s_info = (String)db_.fetch(key);
102 }
103 } catch (GdbmException e) {
104 logger.error("couldn't get record");
105 return null;
106 }
107 if (s_info==null) {
108 // record not present
109 logger.error("key "+key+" not present in db");
110 return null;
111 }
112 return s_info;
113 }
114
115 /** sets the key value as info
116 * TODO - not implemented yet */
117 public boolean setValue (String key, String value) {
118 if (db_==null) {
119 return false;
120 }
121 return false;
122 }
123 /** deletes the entry for key
124 * TODO - not implemented yet */
125 public boolean deleteKey(String key) {
126 if (db_==null) {
127 return false;
128 }
129 return false;
130 }
131
132 /** returns a string of key-value entries that
133 * can be printed for debugging purposes. */
134 public String displayAllEntries() {
135 StringBuffer output = new StringBuffer();
136 try{
137 java.util.Enumeration e = db_.keys();
138 while(e.hasMoreElements()) {
139 Object key = e.nextElement();
140 Object value = db_.fetch(key);
141
142 output.append("key href: ");
143 output.append((String)key);
144 output.append("\tvalue ID: ");
145 output.append((String)value);
146 output.append("\n");
147 //logger.warn("key: " + key + "\tvalue: " + value);
148
149 String urlkey = java.net.URLEncoder.encode((String)key, "UTF8");
150 output.append("URL encoded key: " + urlkey);
151 //logger.warn("URL encoded key: " + urlkey);
152 }
153 } catch(UnsupportedEncodingException e) {
154 logger.warn("Trouble converting key to UTF-8.");
155 } catch(Exception e) {
156 logger.warn("Exception encountered when trying to displayAllEntries():" + e);
157 }
158 return output.toString();
159 }
160}
Note: See TracBrowser for help on using the repository browser.