Changeset 28667 for main


Ignore:
Timestamp:
2013-11-21T18:56:48+13:00 (10 years ago)
Author:
ak19
Message:

Jdb2Txt now supports sorting the output, to bring it in line with what db2txt does nowadays. This is needed for diffcol, since GS3 demo-lucene uses jdbm.

File:
1 edited

Legend:

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

    r21403 r28667  
    2727import jdbm.RecordManagerFactory;
    2828import jdbm.helper.FastIterator;
     29import jdbm.helper.StringComparator;
    2930import jdbm.htree.HTree;
    3031
     
    3334import java.io.OutputStreamWriter;
    3435import java.io.PrintWriter;
     36import java.util.Collections;
     37import java.util.Iterator;
    3538import java.util.Properties;
     39import java.util.Vector;
    3640
    3741
     
    4246    RecordManager  recman_;
    4347    HTree          hashtable_;
     48    boolean        sort_;
    4449
    4550    static private PrintWriter utf8out = null;
     
    5661    }
    5762
     63    // the default constructor retains default behaviour of not sorting the keys
    5864    public Jdb2Txt(String db_filename)
    5965    throws IOException
    6066    {
     67    this(db_filename, false);
     68    }
     69
     70    public Jdb2Txt(String db_filename, boolean sort)
     71    throws IOException
     72    {
     73    this.sort_ = sort;
     74
    6175    if (db_filename.endsWith(".jdb")) {
    6276        // remove file extension as JDBM does not expect it
     
    86100    String         key;
    87101    String         val;
     102   
     103    Vector keylist = sort_ ? new Vector() : null;
    88104
    89105        iter = hashtable_.keys();
     
    91107        while (key != null) {
    92108            val = (String) hashtable_.get(key);
    93             utf8out.println("[" + key + "]");
    94             utf8out.println(val);
    95         // 70 hypens
    96         utf8out.println("----------------------------------------------------------------------");
     109
     110        if(sort_) {
     111        keylist.add(key);
     112        }
     113        else {
     114        print_entry(key, val);
     115        }
    97116            key = (String) iter.next();
    98117        }
    99118       
     119    if(sort_) {
     120        Collections.sort(keylist, new StringComparator());
     121        Iterator i = keylist.iterator();
     122        while(i.hasNext()) {
     123        key = (String)i.next();
     124        val = (String) hashtable_.get(key);
     125        print_entry(key, val);
     126        }
     127    }
     128
    100129        recman_.close();
    101130    }
    102131
     132    public static void print_entry(String key, String value) {
     133    utf8out.println("[" + key + "]");
     134    utf8out.println(value);
     135    // 70 hypens
     136    utf8out.println("----------------------------------------------------------------------");
     137           
     138    }
    103139
    104140    public static void print_usage()
    105141    {
    106     System.err.println("Usage: java Jdb2Txt database-name");
     142    System.err.println("Usage: java Jdb2Txt [options] database-name");
     143    System.err.println("options: ");
     144    System.err.println(" -sort\tsort the keys to the database before output\n");
    107145    System.exit(-1);
    108146    }
     
    111149    public static void main(String[] args)
    112150    {
    113     if (args.length != 1) {
     151    if (args.length < 1 || args.length > 2) {
    114152        print_usage();
    115153    }
    116154
    117155    String dbname = args[0];
     156    boolean sort = false;
     157
     158    if(args.length == 2) {
     159        if(args[0].equals("-sort")) {
     160        sort = true;
     161        dbname = args[1];
     162        } else {
     163        System.err.println(args[0] + " is not a valid option\n");
     164        print_usage();
     165        }
     166    }
    118167
    119168        try {
    120169
    121             Jdb2Txt table = new Jdb2Txt(dbname);
     170            Jdb2Txt table = new Jdb2Txt(dbname, sort);
    122171            table.db2txt();
    123172        }
Note: See TracChangeset for help on using the changeset viewer.