Ignore:
Timestamp:
2010-01-06T16:52:28+13:00 (14 years ago)
Author:
davidb
Message:

Initial implementation for JDBM Database backend. Tested on the gs2mgppdemo collection

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/JDBMWrapper.java

    r16797 r21431  
    2121import org.apache.log4j.Logger;
    2222
     23import java.io.BufferedReader;
     24import java.io.InputStreamReader;
     25import java.io.IOException;
     26import java.io.UnsupportedEncodingException;
     27import java.io.OutputStreamWriter;
     28import java.io.PrintWriter;
     29
     30import java.util.Properties;
     31import java.util.ArrayList;
     32
     33import jdbm.RecordManager;
     34import jdbm.RecordManagerFactory;
     35import jdbm.helper.FastIterator;
     36import jdbm.htree.HTree;
     37
    2338public class JDBMWrapper
    2439  implements FlatDatabaseWrapper {
     
    2641  static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.JDBMWrapper.class.getName());
    2742
     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
    2862  /** 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;
    31110  }
    32111
     
    34113  /** close the database associated with this wrapper */
    35114  public void closeDatabase() {
    36 
     115      try {
     116      recman_.close();
     117      }
     118      catch (IOException e) {     
     119    logger.error("Failed to close JDBM database");
     120      }
    37121  }
    38122
    39123  /** returns the value associated with the key */
    40124  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;
    42139  }
    43140
     
    45142   *    printed for debugging purposes*/
    46143  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();
    48170  }
    49171}
Note: See TracChangeset for help on using the changeset viewer.