Ignore:
Timestamp:
2011-08-12T09:57:26+12:00 (13 years ago)
Author:
sjm84
Message:

Adding in the server-side code for the Document Maker as well as several other enhancements

File:
1 edited

Legend:

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

    r22973 r24393  
    3636import jdbm.htree.HTree;
    3737
    38 public class JDBMWrapper
    39   implements FlatDatabaseWrapper {
    40  
    41   static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.JDBMWrapper.class.getName());
    42 
    43     static String TNAME = "greenstone";
    44 
    45     RecordManager  recman_ = null;
    46     HTree          hashtable_;
    47 
    48     String db_filename_;
    49 
    50     static private PrintWriter utf8out = null;
    51 
    52     static
    53     {
    54         try {
    55             OutputStreamWriter osw = new OutputStreamWriter(System.out, "UTF-8");
    56             utf8out = new PrintWriter(osw, true);
    57         }
    58         catch (UnsupportedEncodingException e) {
    59             System.out.println(e);
    60         }
    61     }
    62 
    63 
    64   /** open database named filename, using mode */
    65   public boolean openDatabase(String db_filename, int mode) {
    66      
    67 
    68       if (db_filename.endsWith(".jdb")) {
    69       // remove file extension as JDBM does not expect it
    70       db_filename = db_filename.substring(0,db_filename.length()-4);
    71       }
    72 
    73       // Map the mode value into equivalent JDBM 'must_exist' value
    74       // currently this is very simple as there is only READ and WRITE
    75       // (no WRITER_CREATE)
    76       // => assume the database must exist
    77       boolean must_exist = true; // default
    78 
    79       if (recman_ != null) {
    80       String message = "openDatabase() called when the class already has a database open\n";
    81       message += "  Use closeDatabase before opening the next one.\n";
    82       message += "  Existing database file: " + db_filename_ + "\n";
    83       message += "  New database file:      " + db_filename + "\n";
    84       logger.warn(message);
    85       // consider closing it automatically?
    86       }
    87 
    88 
    89       try {
    90       // create or open a record manager
    91       Properties props = new Properties();
    92       recman_ = RecordManagerFactory.createRecordManager(db_filename, props);
    93      
    94       // load existing table (if exists) otherwise create new one
    95       long recid = recman_.getNamedObject(TNAME);
    96      
    97       if (recid != 0) {
    98           System.err.println("# Loading existing database table '" + TNAME +"' ...");
    99           hashtable_ = HTree.load(recman_, recid);
    100       }
    101       else {
    102          
    103           if (must_exist) {
    104           recman_.close();
    105           recman_ = null;
    106           db_filename_ = null;
    107 
    108           System.err.println("Database table '" + TNAME +"' does not exist.");
    109           throw new IOException();
    110           }
    111           else {
    112           System.err.println("# No database table '" + TNAME +"' to set.  Creating new one");
    113             hashtable_ = HTree.createInstance(recman_);
    114             recman_.setNamedObject(TNAME, hashtable_.getRecid());
    115         }
    116       }
    117       }
    118       catch (IOException e) {     
    119     logger.error("couldn't open database "+ db_filename);
    120     return false;
    121       }
    122 
    123       db_filename_ = db_filename;
    124 
    125       return true;
    126   }
    127 
    128 
    129   /** close the database associated with this wrapper */
    130   public void closeDatabase() {
    131       try {
    132       if (recman_ != null) {
    133           recman_.close();
    134           recman_ = null;
    135           db_filename_ = null;
    136       }
    137       }
    138       catch (IOException e) {     
    139     logger.error("Failed to close JDBM database");
    140       }
    141   }
    142 
    143   /** returns the value associated with the key */
    144   public String getValue(String key) {
    145 
    146       String val;
    147 
    148       try {
    149       val = (String) hashtable_.get(key);
    150        
    151       recman_.commit();
    152       }
    153       catch (IOException e) {     
    154     logger.error("Failed get key " + key + "from JDBM database");
    155     return null;
    156       }
    157 
    158       return val;
    159   }
    160 
    161   /** returns a string of key-value entries that can be
    162    *    printed for debugging purposes*/
    163   public String displayAllEntries() {
    164 
    165       StringBuffer keys = new StringBuffer();
    166 
    167       try {
    168       FastIterator   iter = hashtable_.keys();
    169       String         key  = (String) iter.next();
    170      
    171       String nl = System.getProperty("line.separator");
    172      
    173       while (key != null) {
    174           String val = (String) hashtable_.get(key);
    175           keys.append("[" + key + "]" + nl);
    176           keys.append(val + nl);
    177           // 70 hypens
    178           keys.append("----------------------------------------------------------------------" + nl);
    179           key = (String) iter.next();
    180       }
    181 
    182       recman_.commit();
    183       }
    184       catch (IOException e) {     
    185     logger.error("Failed get all keys and values from JDBM database");
    186     return null;
    187       }
    188        
    189       return keys.toString();
    190   }
     38public class JDBMWrapper implements FlatDatabaseWrapper
     39{
     40
     41    static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.JDBMWrapper.class.getName());
     42
     43    static String TNAME = "greenstone";
     44
     45    RecordManager recman_ = null;
     46    HTree hashtable_;
     47
     48    String db_filename_;
     49
     50    static private PrintWriter utf8out = null;
     51
     52    static
     53    {
     54        try
     55        {
     56            OutputStreamWriter osw = new OutputStreamWriter(System.out, "UTF-8");
     57            utf8out = new PrintWriter(osw, true);
     58        }
     59        catch (UnsupportedEncodingException e)
     60        {
     61            System.out.println(e);
     62        }
     63    }
     64
     65    /** open database named filename, using mode */
     66    public boolean openDatabase(String db_filename, int mode)
     67    {
     68
     69        if (db_filename.endsWith(".jdb"))
     70        {
     71            // remove file extension as JDBM does not expect it
     72            db_filename = db_filename.substring(0, db_filename.length() - 4);
     73        }
     74
     75        // Map the mode value into equivalent JDBM 'must_exist' value
     76        // currently this is very simple as there is only READ and WRITE
     77        // (no WRITER_CREATE)
     78        // => assume the database must exist
     79        boolean must_exist = true; // default
     80
     81        if (recman_ != null)
     82        {
     83            String message = "openDatabase() called when the class already has a database open\n";
     84            message += "  Use closeDatabase before opening the next one.\n";
     85            message += "  Existing database file: " + db_filename_ + "\n";
     86            message += "  New database file:      " + db_filename + "\n";
     87            logger.warn(message);
     88            // consider closing it automatically?
     89        }
     90
     91        try
     92        {
     93            // create or open a record manager
     94            Properties props = new Properties();
     95            recman_ = RecordManagerFactory.createRecordManager(db_filename, props);
     96
     97            // load existing table (if exists) otherwise create new one
     98            long recid = recman_.getNamedObject(TNAME);
     99
     100            if (recid != 0)
     101            {
     102                System.err.println("# Loading existing database table '" + TNAME + "' ...");
     103                hashtable_ = HTree.load(recman_, recid);
     104            }
     105            else
     106            {
     107
     108                if (must_exist)
     109                {
     110                    recman_.close();
     111                    recman_ = null;
     112                    db_filename_ = null;
     113
     114                    System.err.println("Database table '" + TNAME + "' does not exist.");
     115                    throw new IOException();
     116                }
     117                else
     118                {
     119                    System.err.println("# No database table '" + TNAME + "' to set.  Creating new one");
     120                    hashtable_ = HTree.createInstance(recman_);
     121                    recman_.setNamedObject(TNAME, hashtable_.getRecid());
     122                }
     123            }
     124        }
     125        catch (IOException e)
     126        {
     127            logger.error("couldn't open database " + db_filename);
     128            return false;
     129        }
     130
     131        db_filename_ = db_filename;
     132
     133        return true;
     134    }
     135
     136    /** close the database associated with this wrapper */
     137    public void closeDatabase()
     138    {
     139        try
     140        {
     141            if (recman_ != null)
     142            {
     143                recman_.close();
     144                recman_ = null;
     145                db_filename_ = null;
     146            }
     147        }
     148        catch (IOException e)
     149        {
     150            logger.error("Failed to close JDBM database");
     151        }
     152    }
     153
     154    /** returns the value associated with the key */
     155    public String getValue(String key)
     156    {
     157
     158        String val;
     159
     160        try
     161        {
     162            val = (String) hashtable_.get(key);
     163           
     164            recman_.commit();
     165        }
     166        catch (IOException e)
     167        {
     168            logger.error("Failed get key " + key + "from JDBM database");
     169            return null;
     170        }
     171       
     172        return val;
     173    }
     174   
     175    /**
     176     * Sets the given key to the given value in the database
     177     */
     178    public boolean setValue(String key, String value)
     179    {
     180        try
     181        {
     182            hashtable_.put(key, value);
     183            recman_.commit();
     184        }
     185        catch (Exception ex)
     186        {
     187            logger.error("Failed to set " + key + " = " + value + " in the JDBM database");
     188            return false;
     189        }
     190        return true;
     191    }
     192
     193    /**
     194     * Deletes the given key (and corresponding value) from the database
     195     */
     196    public boolean deleteKey(String key)
     197    {
     198        try
     199        {
     200            hashtable_.remove(key);
     201            recman_.commit();
     202        }
     203        catch (Exception ex)
     204        {
     205            logger.error("Failed to delete key " + key + " in the JDBM database");
     206            return false;
     207        }
     208        return true;
     209    }
     210
     211    /**
     212     * returns a string of key-value entries that can be printed for debugging
     213     * purposes
     214     */
     215    public String displayAllEntries()
     216    {
     217
     218        StringBuffer keys = new StringBuffer();
     219
     220        try
     221        {
     222            FastIterator iter = hashtable_.keys();
     223            String key = (String) iter.next();
     224
     225            String nl = System.getProperty("line.separator");
     226
     227            while (key != null)
     228            {
     229                String val = (String) hashtable_.get(key);
     230                keys.append("[" + key + "]" + nl);
     231                keys.append(val + nl);
     232                // 70 hypens
     233                keys.append("----------------------------------------------------------------------" + nl);
     234                key = (String) iter.next();
     235            }
     236
     237            recman_.commit();
     238        }
     239        catch (IOException e)
     240        {
     241            logger.error("Failed get all keys and values from JDBM database");
     242            return null;
     243        }
     244
     245        return keys.toString();
     246    }
    191247}
Note: See TracChangeset for help on using the changeset viewer.