Changeset 21402
- Timestamp:
- 2010-01-01T23:35:24+13:00 (13 years ago)
- 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 6 6 * University of Waikato, New Zealand. 7 7 * 8 * Copyright (C) 1999 The New Zealand Digital Library Project8 * Copyright (C) 2009 The New Zealand Digital Library Project 9 9 * 10 10 * This program is free software; you can redistribute it and/or modify … … 38 38 public class JdbDel 39 39 { 40 static String TNAME = "greenstone";41 42 RecordManager recman_;43 HTree hashtable_;44 45 public JdbDel(String db_filename)46 throws IOException47 {48 // create or open a record manager49 Properties props = new Properties();50 recman_ = RecordManagerFactory.createRecordManager(db_filename, props);51 52 // load existing table (if exists) otherwise report error53 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 IOException69 {70 hashtable_.remove(key);71 recman_.close();72 }73 74 75 40 public static void print_usage() 76 41 { … … 91 56 try { 92 57 String dbname = args[0]; 93 Jdb Del table = new JdbDel(dbname);58 JdbmAPI jdbm_api = new JdbmAPI(dbname,true); 94 59 95 60 String key = args[1]; 96 table.del(key); 61 62 jdbm_api.delete(key); 63 jdbm_api.close(); 97 64 } 98 65 -
main/trunk/greenstone2/common-src/src/jdbmedit/JdbGet.java
r21395 r21402 6 6 * University of Waikato, New Zealand. 7 7 * 8 * Copyright (C) 1999 The New Zealand Digital Library Project8 * Copyright (C) 2009 The New Zealand Digital Library Project 9 9 * 10 10 * This program is free software; you can redistribute it and/or modify … … 35 35 public class JdbGet 36 36 { 37 static String TNAME = "greenstone";38 39 RecordManager recman_;40 HTree hashtable_;41 42 43 public JdbGet(String db_filename)44 throws IOException45 {46 // create or open a record manager47 Properties props = new Properties();48 recman_ = RecordManagerFactory.createRecordManager(db_filename, props);49 50 // create or load table51 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 IOException64 {65 FastIterator iter;66 String val;67 68 val = (String) hashtable_.get(key);69 System.out.println(val);70 71 recman_.close();72 }73 74 37 75 38 public static void print_usage() … … 91 54 try { 92 55 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(); 95 61 } 96 62 catch (IOException e) { -
main/trunk/greenstone2/common-src/src/jdbmedit/JdbKeys.java
r21395 r21402 6 6 * University of Waikato, New Zealand. 7 7 * 8 * Copyright (C) 1999 The New Zealand Digital Library Project8 * Copyright (C) 2009 The New Zealand Digital Library Project 9 9 * 10 10 * This program is free software; you can redistribute it and/or modify … … 24 24 **********************************************************************/ 25 25 26 import java.io.IOException; 27 import java.util.Properties; 28 import java.util.ArrayList; 29 26 30 import jdbm.RecordManager; 27 31 import jdbm.RecordManagerFactory; … … 29 33 import jdbm.htree.HTree; 30 34 31 import java.io.IOException;32 import java.util.Properties;33 34 35 35 public class JdbKeys 36 36 { 37 static String TNAME = "greenstone";38 39 RecordManager recman_;40 HTree hashtable_;41 42 43 public JdbKeys(String db_filename)44 throws IOException45 {46 // create or open a record manager47 Properties props = new Properties();48 recman_ = RecordManagerFactory.createRecordManager(db_filename, props);49 50 // create or load table51 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 IOException64 {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 80 37 public static void print_usage() 81 38 { … … 95 52 try { 96 53 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(); 99 65 } 100 66 catch (IOException e) { -
main/trunk/greenstone2/common-src/src/jdbmedit/JdbSet.java
r21395 r21402 6 6 * University of Waikato, New Zealand. 7 7 * 8 * Copyright (C) 1999 The New Zealand Digital Library Project8 * Copyright (C) 2009 The New Zealand Digital Library Project 9 9 * 10 10 * This program is free software; you can redistribute it and/or modify … … 26 26 import java.io.BufferedInputStream; 27 27 import java.io.InputStream; 28 import java.io.IOException; 29 30 import java.util.Properties; 28 31 29 32 import jdbm.RecordManager; … … 32 35 import jdbm.htree.HTree; 33 36 34 import java.io.IOException;35 import java.util.Properties;36 37 37 38 38 39 public class JdbSet 39 40 { 40 static String TNAME = "greenstone";41 42 RecordManager recman_;43 HTree hashtable_;44 45 public JdbSet(String db_filename)46 throws IOException47 {48 // create or open a record manager49 Properties props = new Properties();50 recman_ = RecordManagerFactory.createRecordManager(db_filename, props);51 52 // load existing table (if exists) otherwise create new one53 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 IOException68 {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 IOException79 80 {81 hashtable_.put(key,val);82 recman_.close();83 }84 85 public void del(String key)86 throws IOException87 {88 hashtable_.remove(key);89 recman_.close();90 }91 92 93 41 public static void print_usage() 94 42 { 95 43 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(" \tadded 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"); 99 47 System.exit(-1); 100 48 } … … 112 60 try { 113 61 String dbname = args[0]; 114 Jdb Set table = new JdbSet(dbname);62 JdbmAPI jdbm_api = new JdbmAPI(dbname); 115 63 116 64 if (argc == 4) { … … 120 68 String val = args[2]; 121 69 122 table.append(key,val);70 jdbm_api.append(key,val); 123 71 } 124 72 else { … … 130 78 String key = args[1]; 131 79 String val = args[2]; 132 table.set(key,val);80 jdbm_api.set(key,val); 133 81 } 134 82 else { 135 83 String key = args[1]; 136 table.del(key);84 jdbm_api.delete(key); 137 85 } 86 87 jdbm_api.close(); 138 88 } 139 89 -
main/trunk/greenstone2/common-src/src/jdbmedit/JdbmAPI.java
r21401 r21402 45 45 HTree hashtable_; 46 46 47 public JdbmAPI(String db_filename )47 public JdbmAPI(String db_filename,boolean must_exist) 48 48 throws IOException 49 49 { … … 56 56 57 57 if (recid != 0) { 58 System. out.println("Loading existing database table '" + TNAME +"' ...");58 System.err.println("# Loading existing database table '" + TNAME +"' ..."); 59 59 hashtable_ = HTree.load(recman_, recid); 60 60 } 61 61 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); 66 81 } 67 82 68 83 public void append(String key, String val) 69 84 throws IOException 70 { 71 85 { 72 86 String orig_val = (String)hashtable_.get(key); 73 87 String new_val = orig_val + val; … … 127 141 { 128 142 recman_.close(); 143 System.err.println("# Done"); 129 144 } 130 145
Note:
See TracChangeset
for help on using the changeset viewer.