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