1 | package org.greenstone.gdbm;
|
---|
2 |
|
---|
3 | import org.greenstone.gsdl3.util.OID;
|
---|
4 | import au.com.pharos.gdbm.GdbmFile;
|
---|
5 | import au.com.pharos.packing.*;
|
---|
6 | import au.com.pharos.gdbm.GdbmException;
|
---|
7 |
|
---|
8 | /** java wrapper class for gdbm - uses Java-GDBM written by Martin Pool
|
---|
9 | * replaces gdbmclass in the old version
|
---|
10 | */
|
---|
11 |
|
---|
12 | public class GDBMWrapper {
|
---|
13 |
|
---|
14 | // Values must match those from gdbm.h - uses the definitions from GdbmFile !
|
---|
15 | /** Indicates that the caller will just read the database. Many
|
---|
16 | * readers may share a database. */
|
---|
17 | public final static int READER = GdbmFile.READER;
|
---|
18 |
|
---|
19 | /** The caller wants read/write access to an existing database and
|
---|
20 | * requires exclusive access. */
|
---|
21 | public final static int WRITER = GdbmFile.WRITER;
|
---|
22 |
|
---|
23 | /** The caller wants exclusive read/write access, and the database
|
---|
24 | * should be created if it does not already exist. */
|
---|
25 | public final static int WRCREAT = GdbmFile.WRCREAT;
|
---|
26 |
|
---|
27 | /** The caller wants exclusive read/write access, and the database
|
---|
28 | * should be replaced if it already exists. */
|
---|
29 | public final static int NEWDB = GdbmFile.NEWDB;
|
---|
30 |
|
---|
31 | protected GdbmFile db_=null;
|
---|
32 |
|
---|
33 | /** open the database filename, with mode mode - uses the constants
|
---|
34 | above, eg GdbmFile.WRITER */
|
---|
35 | public boolean openDatabase(String filename, int mode){
|
---|
36 | try {
|
---|
37 | if (db_!=null) {
|
---|
38 | db_.close();
|
---|
39 | }
|
---|
40 | db_ = new GdbmFile(filename, mode);
|
---|
41 | } catch ( GdbmException e) { // the database wasn't opened or created
|
---|
42 | System.err.println("GDBMWrapper: couldn't open database "+filename);
|
---|
43 | return false;
|
---|
44 | }
|
---|
45 | db_.setKeyPacking(new StringPacking());
|
---|
46 | db_.setValuePacking(new StringPacking());
|
---|
47 | return true;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /** cloase the database associated with this wrapper */
|
---|
51 | public void closeDatabase() {
|
---|
52 | try {
|
---|
53 | db_.close();
|
---|
54 | } catch (GdbmException e) {
|
---|
55 | // should never get here - close never actually throws an exception
|
---|
56 | System.err.println("GDBMWrapper: error on close()");
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | /** returns a DBInfo object containing all the name-value pairs for
|
---|
61 | * main_key
|
---|
62 | * @see DBInfo
|
---|
63 | */
|
---|
64 | public DBInfo getInfo(String main_key) {
|
---|
65 | if (db_==null) {
|
---|
66 | return null;
|
---|
67 | }
|
---|
68 | String s_info;
|
---|
69 | try {
|
---|
70 | s_info = (String)db_.fetch(main_key);
|
---|
71 | } catch (GdbmException e) {
|
---|
72 | System.err.println("GDBMWrapper: couldn't get record");
|
---|
73 | return null;
|
---|
74 | }
|
---|
75 | if (s_info==null) {
|
---|
76 | // record not present
|
---|
77 | System.err.println("GDBMWrapper: key "+main_key+" not present in db");
|
---|
78 | return null;
|
---|
79 | }
|
---|
80 | DBInfo info = new DBInfo();
|
---|
81 |
|
---|
82 | String [] lines = s_info.split("\n");
|
---|
83 | String key;
|
---|
84 | String value;
|
---|
85 | for (int i=0; i<lines.length; i++) {
|
---|
86 | //System.out.println("line:"+lines[i]);
|
---|
87 | int a = lines[i].indexOf('<');
|
---|
88 | int b= lines[i].indexOf('>');
|
---|
89 | if (a==-1 || b==-1) {
|
---|
90 | System.err.println("error: bad format in db");
|
---|
91 | }
|
---|
92 | else {
|
---|
93 | key=lines[i].substring(a+1, b);
|
---|
94 | value=lines[i].substring(b+1);
|
---|
95 | //System.out.println("key="+key+", val="+value);
|
---|
96 | info.addInfo(key, value);
|
---|
97 |
|
---|
98 | }
|
---|
99 | }
|
---|
100 | return info;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /** sets all the name-value pairs in info as the content for key key
|
---|
104 | * TODO - not implemented yet */
|
---|
105 | public boolean setInfo(String key, DBInfo info) {
|
---|
106 | if (db_==null) {
|
---|
107 | return false;
|
---|
108 | }
|
---|
109 | return true;
|
---|
110 | }
|
---|
111 | /** sets the key value as info
|
---|
112 | * TODO - not implemented yet */
|
---|
113 | public boolean setInfo (String key, String info) {
|
---|
114 | if (db_==null) {
|
---|
115 | return false;
|
---|
116 | }
|
---|
117 | return true;
|
---|
118 | }
|
---|
119 | /** deletes the entry for key
|
---|
120 | * TODO - not implemented yet */
|
---|
121 | public boolean deleteKey(String key) {
|
---|
122 | if (db_==null) {
|
---|
123 | return false;
|
---|
124 | }
|
---|
125 | return true;
|
---|
126 | }
|
---|
127 |
|
---|
128 | // greenstone convenience methods - should these go into a separate class?
|
---|
129 | // yes I think so.
|
---|
130 | /** converts a greenstone OID to internal docnum */
|
---|
131 | public long oid2Docnum(String OID) {
|
---|
132 | DBInfo info = getInfo(OID);
|
---|
133 | long real_num = Long.parseLong(info.getInfo("docnum"));
|
---|
134 | return real_num;
|
---|
135 | }
|
---|
136 | /** converts a docnum to greenstone OID */
|
---|
137 | public String docnum2Oid(long docnum) {
|
---|
138 | DBInfo info = getInfo(Long.toString(docnum));
|
---|
139 | String oid = info.getInfo("section");
|
---|
140 | return oid;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /** translates oids containing .pr, .fc, .lc, .ns, .ps into proper oids
|
---|
144 | * a suffix is expected to be present so test before using
|
---|
145 | * TODO: fc, lc, ns, ps, .rt .ss*/
|
---|
146 | public String translateOID(String oid) {
|
---|
147 |
|
---|
148 | int p = oid.lastIndexOf('.');
|
---|
149 | if (p != oid.length()-3) {
|
---|
150 | System.out.println("translateoid error: '.' is not the third to last char!!");
|
---|
151 | return oid;
|
---|
152 | }
|
---|
153 |
|
---|
154 | String top = oid.substring(0, p);
|
---|
155 | String suff = oid.substring(p+1);
|
---|
156 | if (suff.equals("pr")) {
|
---|
157 | return OID.getParent(top);
|
---|
158 | } else if (suff.equals("rt")) {
|
---|
159 | return OID.getTop(top);
|
---|
160 | } else {
|
---|
161 | int sibling_num = 0;
|
---|
162 | if (suff.equals("ss")) {
|
---|
163 | // we have to remove the sib num before we get top
|
---|
164 | p = top.lastIndexOf('.');
|
---|
165 | sibling_num = Integer.parseInt(top.substring(p+1));
|
---|
166 | top = top.substring(0, p);
|
---|
167 | }
|
---|
168 |
|
---|
169 | // need to get info out of gdbm db -
|
---|
170 | String doc_id = top;
|
---|
171 | if (suff.endsWith("s")) {
|
---|
172 | doc_id = OID.getParent(top);
|
---|
173 | }
|
---|
174 | DBInfo info = getInfo(doc_id);
|
---|
175 | if (info==null) {
|
---|
176 | System.out.println("info is null!!");
|
---|
177 | return top;
|
---|
178 | }
|
---|
179 | String contains = info.getInfo("contains");
|
---|
180 | contains.replaceAll("\"", doc_id);
|
---|
181 | if (contains.equals("")) {
|
---|
182 | // something is wrong
|
---|
183 | return top;
|
---|
184 | }
|
---|
185 | contains = contains.replaceAll("\"", doc_id);
|
---|
186 | String [] children = contains.split(";");
|
---|
187 | if (suff.equals("fc")) {
|
---|
188 | return children[0];
|
---|
189 | } else if (suff.equals("lc")) {
|
---|
190 | return children[children.length-1];
|
---|
191 | } else {
|
---|
192 | if (suff.equals("ss")) {
|
---|
193 | return children[sibling_num-1];
|
---|
194 | }
|
---|
195 | // find the position that we are at.
|
---|
196 | int i=0;
|
---|
197 | while (i<children.length) {
|
---|
198 | if (children[i].equals(top)) {
|
---|
199 | break;
|
---|
200 | }
|
---|
201 | i++;
|
---|
202 | }
|
---|
203 |
|
---|
204 | if (suff.equals("ns")) {
|
---|
205 | if (i==children.length-1)return children[i];
|
---|
206 | return children[i+1];
|
---|
207 | } else if (suff.equals("ps")) {
|
---|
208 | if (i==0) return children[i];
|
---|
209 | return children[i-1];
|
---|
210 | }
|
---|
211 | }
|
---|
212 | }
|
---|
213 | return top;
|
---|
214 | }
|
---|
215 | }
|
---|