Ignore:
Timestamp:
2010-01-01T23:35:24+13:00 (14 years ago)
Author:
davidb
Message:

Refactoring of class to use API

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.