Changeset 21402


Ignore:
Timestamp:
01/01/10 23:35:24 (11 years ago)
Author:
davidb
Message:

Refactoring of class to use API

Location:
main/trunk/greenstone2/common-src/src/jdbmedit
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/common-src/src/jdbmedit/JdbDel.java

    r21395 r21402  
    66 * University of Waikato, New Zealand.
    77 *
    8  * Copyright (C) 1999  The New Zealand Digital Library Project
     8 * Copyright (C) 2009  The New Zealand Digital Library Project
    99 *
    1010 * This program is free software; you can redistribute it and/or modify
     
    3838public class JdbDel
    3939{
    40     static String TNAME = "greenstone";
    41 
    42     RecordManager  recman_;
    43     HTree          hashtable_;
    44 
    45     public JdbDel(String db_filename)
    46     throws IOException
    47     {
    48         // create or open a record manager
    49         Properties props = new Properties();
    50         recman_ = RecordManagerFactory.createRecordManager(db_filename, props);
    51 
    52         // load existing table (if exists) otherwise report error
    53         long recid = recman_.getNamedObject(TNAME);
    54 
    55     if (recid != 0) {
    56         System.out.println("Loading existing database table '" + TNAME +"' ...");
    57         hashtable_ = HTree.load(recman_, recid);
    58     }
    59     else {
    60         recman_.close();
    61         System.out.println("No database table '" + TNAME +"' to delete key from.");
    62         System.exit(-1);
    63     }
    64     }
    65    
    66 
    67     public void del(String key)
    68     throws IOException
    69     {
    70     hashtable_.remove(key);
    71     recman_.close();
    72     }
    73    
    74 
    7540    public static void print_usage()
    7641    {
     
    9156    try {
    9257        String dbname = args[0];
    93         JdbDel table = new JdbDel(dbname);
     58        JdbmAPI jdbm_api = new JdbmAPI(dbname,true);
    9459       
    9560        String key = args[1];
    96         table.del(key);
     61
     62        jdbm_api.delete(key);
     63        jdbm_api.close();
    9764    }
    9865
  • main/trunk/greenstone2/common-src/src/jdbmedit/JdbGet.java

    r21395 r21402  
    66 * University of Waikato, New Zealand.
    77 *
    8  * Copyright (C) 1999  The New Zealand Digital Library Project
     8 * Copyright (C) 2009  The New Zealand Digital Library Project
    99 *
    1010 * This program is free software; you can redistribute it and/or modify
     
    3535public class JdbGet
    3636{
    37     static String TNAME = "greenstone";
    38 
    39     RecordManager  recman_;
    40     HTree          hashtable_;
    41 
    42 
    43     public JdbGet(String db_filename)
    44     throws IOException
    45     {
    46         // create or open a record manager
    47         Properties props = new Properties();
    48         recman_ = RecordManagerFactory.createRecordManager(db_filename, props);
    49 
    50         // create or load table
    51         long recid = recman_.getNamedObject(TNAME);
    52         if (recid != 0) {
    53             hashtable_ = HTree.load(recman_, recid);
    54         }
    55     else {
    56             System.err.println("Failed to find database table '" + TNAME +"' ...");
    57         System.exit(-1);
    58         }
    59     }
    60 
    61 
    62     public void get(String key)
    63         throws IOException
    64     {
    65     FastIterator   iter;
    66     String         val;
    67 
    68     val = (String) hashtable_.get(key);
    69     System.out.println(val);
    70        
    71         recman_.close();
    72     }
    73 
    7437
    7538    public static void print_usage()
     
    9154        try {
    9255
    93             JdbGet table = new JdbGet(dbname);
    94             table.get(key);
     56            JdbmAPI jdbm_api = new JdbmAPI(dbname,true);
     57            String val = jdbm_api.get(key);
     58        System.out.println(val);
     59
     60        jdbm_api.close();
    9561        }
    9662    catch (IOException e) {
  • main/trunk/greenstone2/common-src/src/jdbmedit/JdbKeys.java

    r21395 r21402  
    66 * University of Waikato, New Zealand.
    77 *
    8  * Copyright (C) 1999  The New Zealand Digital Library Project
     8 * Copyright (C) 2009  The New Zealand Digital Library Project
    99 *
    1010 * This program is free software; you can redistribute it and/or modify
     
    2424 **********************************************************************/
    2525
     26import java.io.IOException;
     27import java.util.Properties;
     28import java.util.ArrayList;
     29
    2630import jdbm.RecordManager;
    2731import jdbm.RecordManagerFactory;
     
    2933import jdbm.htree.HTree;
    3034
    31 import java.io.IOException;
    32 import java.util.Properties;
    33 
    34 
    3535public class JdbKeys
    3636{
    37     static String TNAME = "greenstone";
    38 
    39     RecordManager  recman_;
    40     HTree          hashtable_;
    41 
    42 
    43     public JdbKeys(String db_filename)
    44     throws IOException
    45     {
    46         // create or open a record manager
    47         Properties props = new Properties();
    48         recman_ = RecordManagerFactory.createRecordManager(db_filename, props);
    49 
    50         // create or load table
    51         long recid = recman_.getNamedObject(TNAME);
    52         if (recid != 0) {
    53             hashtable_ = HTree.load(recman_, recid);
    54         }
    55     else {
    56             System.err.println("Failed to find database table '" + TNAME +"' ...");
    57         System.exit(-1);
    58         }
    59     }
    60 
    61 
    62     public void get_keys()
    63         throws IOException
    64     {
    65     FastIterator   iter;
    66     String         key;
    67     String         val;
    68 
    69         iter = hashtable_.keys();
    70         key = (String) iter.next();
    71         while (key != null) {
    72             System.out.println(key);
    73             key = (String) iter.next();
    74         }
    75        
    76         recman_.close();
    77     }
    78 
    79 
    8037    public static void print_usage()
    8138    {
     
    9552        try {
    9653
    97             JdbKeys table = new JdbKeys(dbname);
    98             table.get_keys();
     54            JdbmAPI jdbm_api = new JdbmAPI(dbname,true);
     55
     56        ArrayList keys = jdbm_api.get_keys();
     57           
     58        int keys_len = keys.size();
     59        for (int i=0; i<keys_len; i++) {
     60        String key = (String) keys.get(i);
     61        System.out.println(key);
     62        }
     63
     64        jdbm_api.close();
    9965        }
    10066    catch (IOException e) {
  • main/trunk/greenstone2/common-src/src/jdbmedit/JdbSet.java

    r21395 r21402  
    66 * University of Waikato, New Zealand.
    77 *
    8  * Copyright (C) 1999  The New Zealand Digital Library Project
     8 * Copyright (C) 2009  The New Zealand Digital Library Project
    99 *
    1010 * This program is free software; you can redistribute it and/or modify
     
    2626import java.io.BufferedInputStream;
    2727import java.io.InputStream;
     28import java.io.IOException;
     29
     30import java.util.Properties;
    2831
    2932import jdbm.RecordManager;
     
    3235import jdbm.htree.HTree;
    3336
    34 import java.io.IOException;
    35 import java.util.Properties;
    3637
    3738
    3839public class JdbSet
    3940{
    40     static String TNAME = "greenstone";
    41 
    42     RecordManager  recman_;
    43     HTree          hashtable_;
    44 
    45     public JdbSet(String db_filename)
    46     throws IOException
    47     {
    48         // create or open a record manager
    49         Properties props = new Properties();
    50         recman_ = RecordManagerFactory.createRecordManager(db_filename, props);
    51 
    52         // load existing table (if exists) otherwise create new one
    53         long recid = recman_.getNamedObject(TNAME);
    54 
    55     if (recid != 0) {
    56         System.out.println("Loading existing database table '" + TNAME +"' ...");
    57         hashtable_ = HTree.load(recman_, recid);
    58     }
    59     else {
    60         System.out.println("No database table '" + TNAME +"' to set.  Creating new one");
    61         hashtable_ = HTree.createInstance(recman_);
    62         recman_.setNamedObject(TNAME, hashtable_.getRecid());
    63     }
    64     }
    65    
    66     public void append(String key, String val)
    67     throws IOException
    68     {
    69            
    70     String orig_val = (String)hashtable_.get(key);           
    71     String new_val = orig_val + val;
    72 
    73     hashtable_.put(key,new_val);
    74     recman_.close();
    75     }
    76 
    77     public void set(String key, String val)
    78     throws IOException
    79            
    80     {
    81     hashtable_.put(key,val);
    82     recman_.close();
    83     }
    84 
    85     public void del(String key)
    86     throws IOException
    87     {
    88     hashtable_.remove(key);
    89     recman_.close();
    90     }
    91    
    92 
    9341    public static void print_usage()
    9442    {
    9543    System.err.println("Usage: java JdbSet database-name key [value] [append]");
    96     System.err.println("\t- if no value is given then the lexicon indicated by the key is removed");
    97     System.err.println("\t- if a value is given followed by 'append' then the value is ");
    98     System.err.println("\t  added to the existing entry rather than overwriting it");
     44    System.err.println("  - if no value is given then the lexicon indicated by the key is removed");
     45    System.err.println("  - if a value is given followed by 'append' then the value is ");
     46    System.err.println("    added to the existing entry rather than overwriting it");
    9947    System.exit(-1);
    10048    }
     
    11260    try {
    11361        String dbname = args[0];
    114         JdbSet table = new JdbSet(dbname);
     62        JdbmAPI jdbm_api = new JdbmAPI(dbname);
    11563       
    11664        if (argc == 4) {
     
    12068            String val = args[2];
    12169           
    122             table.append(key,val);
     70            jdbm_api.append(key,val);
    12371        }
    12472        else {
     
    13078        String key = args[1];
    13179        String val = args[2];
    132         table.set(key,val);
     80        jdbm_api.set(key,val);
    13381        }
    13482        else {
    13583        String key = args[1];
    136         table.del(key);
     84        jdbm_api.delete(key);
    13785        }
     86
     87        jdbm_api.close();
    13888    }
    13989
  • main/trunk/greenstone2/common-src/src/jdbmedit/JdbmAPI.java

    r21401 r21402  
    4545    HTree          hashtable_;
    4646
    47     public JdbmAPI(String db_filename)
     47    public JdbmAPI(String db_filename,boolean must_exist)
    4848    throws IOException
    4949    {
     
    5656
    5757    if (recid != 0) {
    58         System.out.println("Loading existing database table '" + TNAME +"' ...");
     58        System.err.println("# Loading existing database table '" + TNAME +"' ...");
    5959        hashtable_ = HTree.load(recman_, recid);
    6060    }
    6161    else {
    62         System.out.println("No database table '" + TNAME +"' to set.  Creating new one");
    63         hashtable_ = HTree.createInstance(recman_);
    64         recman_.setNamedObject(TNAME, hashtable_.getRecid());
    65     }
     62
     63        if (must_exist) {
     64        recman_.close();
     65        System.err.println("Database table '" + TNAME +"' does not exist.");
     66        throw new IOException();
     67        }
     68        else {
     69        System.err.println("# No database table '" + TNAME +"' to set.  Creating new one");
     70        hashtable_ = HTree.createInstance(recman_);
     71        recman_.setNamedObject(TNAME, hashtable_.getRecid());
     72        }
     73    }
     74    }
     75
     76    public JdbmAPI(String db_filename)
     77    throws IOException
     78    {
     79    // default is that database does not have to exist
     80    this(db_filename,false);
    6681    }
    6782   
    6883    public void append(String key, String val)
    6984    throws IOException
    70     {
    71            
     85    {           
    7286    String orig_val = (String)hashtable_.get(key);           
    7387    String new_val = orig_val + val;
     
    127141    {
    128142    recman_.close();
     143    System.err.println("# Done");
    129144    }
    130145
Note: See TracChangeset for help on using the changeset viewer.