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

Last change on this file since 16797 was 16797, checked in by ak19, 16 years ago

Added method displayAllEntries() to return a display String of the key, value pairs in the database for debugging purposes

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