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

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

added support for JDBM (or other) in place of GDBM: new interface - FlatDatabaseWrapper. Has basic functions for opening a database and getting the value for a key. GDBMWrapper and JDBMWrapper both inplement this. (JDBMWrapper waiting for code from Steve). GDBMWrapper much simpler now as no greenstone specific stuff is included. SimpleCollectionDatabase is the class that services use now to access their databases. This will create a new wrapper based on database type. Then getting DBInfo, convertinng between oid and docnums etc is all handled by this class.

  • Property svn:keywords set to Author Date Id Revision
File size: 2.6 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}
Note: See TracBrowser for help on using the repository browser.