Changeset 21431 for main/trunk/greenstone3/src/java/org/greenstone
- Timestamp:
- 2010-01-06T16:52:28+13:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/JDBMWrapper.java
r16797 r21431 21 21 import org.apache.log4j.Logger; 22 22 23 import java.io.BufferedReader; 24 import java.io.InputStreamReader; 25 import java.io.IOException; 26 import java.io.UnsupportedEncodingException; 27 import java.io.OutputStreamWriter; 28 import java.io.PrintWriter; 29 30 import java.util.Properties; 31 import java.util.ArrayList; 32 33 import jdbm.RecordManager; 34 import jdbm.RecordManagerFactory; 35 import jdbm.helper.FastIterator; 36 import jdbm.htree.HTree; 37 23 38 public class JDBMWrapper 24 39 implements FlatDatabaseWrapper { … … 26 41 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.JDBMWrapper.class.getName()); 27 42 43 static String TNAME = "greenstone"; 44 45 RecordManager recman_; 46 HTree hashtable_; 47 48 static private PrintWriter utf8out = null; 49 50 static 51 { 52 try { 53 OutputStreamWriter osw = new OutputStreamWriter(System.out, "UTF-8"); 54 utf8out = new PrintWriter(osw, true); 55 } 56 catch (UnsupportedEncodingException e) { 57 System.out.println(e); 58 } 59 } 60 61 28 62 /** open database named filename, using mode */ 29 public boolean openDatabase(String filename, int mode) { 30 return false; 63 public boolean openDatabase(String db_filename, int mode) { 64 65 66 if (db_filename.endsWith(".jdb")) { 67 // remove file extension as JDBM does not expect it 68 db_filename = db_filename.substring(0,db_filename.length()-4); 69 } 70 71 // Map the mode value into equivalent JDBM 'must_exist' value 72 // currently this is very simple as there is only READ and WRITE 73 // (no WRITER_CREATE) 74 // => assume the database must exist 75 boolean must_exist = true; // default 76 77 try { 78 // create or open a record manager 79 Properties props = new Properties(); 80 recman_ = RecordManagerFactory.createRecordManager(db_filename, props); 81 82 // load existing table (if exists) otherwise create new one 83 long recid = recman_.getNamedObject(TNAME); 84 85 if (recid != 0) { 86 System.err.println("# Loading existing database table '" + TNAME +"' ..."); 87 hashtable_ = HTree.load(recman_, recid); 88 } 89 else { 90 91 if (must_exist) { 92 recman_.close(); 93 System.err.println("Database table '" + TNAME +"' does not exist."); 94 throw new IOException(); 95 } 96 else { 97 System.err.println("# No database table '" + TNAME +"' to set. Creating new one"); 98 hashtable_ = HTree.createInstance(recman_); 99 recman_.setNamedObject(TNAME, hashtable_.getRecid()); 100 } 101 } 102 } 103 catch (IOException e) { 104 logger.error("couldn't open database "+ db_filename); 105 return false; 106 } 107 108 109 return true; 31 110 } 32 111 … … 34 113 /** close the database associated with this wrapper */ 35 114 public void closeDatabase() { 36 115 try { 116 recman_.close(); 117 } 118 catch (IOException e) { 119 logger.error("Failed to close JDBM database"); 120 } 37 121 } 38 122 39 123 /** returns the value associated with the key */ 40 124 public String getValue(String key) { 41 return null; 125 126 String val; 127 128 try { 129 val = (String) hashtable_.get(key); 130 131 recman_.commit(); 132 } 133 catch (IOException e) { 134 logger.error("Failed get key " + key + "from JDBM database"); 135 return null; 136 } 137 138 return val; 42 139 } 43 140 … … 45 142 * printed for debugging purposes*/ 46 143 public String displayAllEntries() { 47 return "No entries."; 144 145 StringBuffer keys = new StringBuffer(); 146 147 try { 148 FastIterator iter = hashtable_.keys(); 149 String key = (String) iter.next(); 150 151 String nl = System.getProperty("line.separator"); 152 153 while (key != null) { 154 String val = (String) hashtable_.get(key); 155 keys.append("[" + key + "]" + nl); 156 keys.append(val + nl); 157 // 70 hypens 158 keys.append("----------------------------------------------------------------------" + nl); 159 key = (String) iter.next(); 160 } 161 162 recman_.commit(); 163 } 164 catch (IOException e) { 165 logger.error("Failed get all keys and values from JDBM database"); 166 return null; 167 } 168 169 return keys.toString(); 48 170 } 49 171 }
Note:
See TracChangeset
for help on using the changeset viewer.