1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * JdbSet.java --
|
---|
4 | * A component of the Greenstone digital library software
|
---|
5 | * from the New Zealand Digital Library Project at the
|
---|
6 | * University of Waikato, New Zealand.
|
---|
7 | *
|
---|
8 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
9 | *
|
---|
10 | * This program is free software; you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation; either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * This program is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with this program; if not, write to the Free Software
|
---|
22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | *
|
---|
24 | **********************************************************************/
|
---|
25 |
|
---|
26 | import java.io.BufferedInputStream;
|
---|
27 | import java.io.InputStream;
|
---|
28 |
|
---|
29 | import jdbm.RecordManager;
|
---|
30 | import jdbm.RecordManagerFactory;
|
---|
31 | import jdbm.helper.FastIterator;
|
---|
32 | import jdbm.htree.HTree;
|
---|
33 |
|
---|
34 | import java.io.IOException;
|
---|
35 | import java.util.Properties;
|
---|
36 |
|
---|
37 |
|
---|
38 | public class JdbSet
|
---|
39 | {
|
---|
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 |
|
---|
93 | public static void print_usage()
|
---|
94 | {
|
---|
95 | 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");
|
---|
99 | System.exit(-1);
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | public static void main(String[] args)
|
---|
104 | {
|
---|
105 | int argc = args.length;
|
---|
106 |
|
---|
107 | // sanity check
|
---|
108 | if ((argc < 2) || (argc>4)) {
|
---|
109 | print_usage();
|
---|
110 | }
|
---|
111 |
|
---|
112 | try {
|
---|
113 | String dbname = args[0];
|
---|
114 | JdbSet table = new JdbSet(dbname);
|
---|
115 |
|
---|
116 | if (argc == 4) {
|
---|
117 |
|
---|
118 | if (args[3].equals("append")) {
|
---|
119 | String key = args[1];
|
---|
120 | String val = args[2];
|
---|
121 |
|
---|
122 | table.append(key,val);
|
---|
123 | }
|
---|
124 | else {
|
---|
125 | System.err.println("Error: Unrecognised option " + args[3]);
|
---|
126 | print_usage();
|
---|
127 | }
|
---|
128 | }
|
---|
129 | else if (argc == 3) {
|
---|
130 | String key = args[1];
|
---|
131 | String val = args[2];
|
---|
132 | table.set(key,val);
|
---|
133 | }
|
---|
134 | else {
|
---|
135 | String key = args[1];
|
---|
136 | table.del(key);
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | catch (IOException e) {
|
---|
141 | e.printStackTrace();
|
---|
142 | }
|
---|
143 |
|
---|
144 | }
|
---|
145 |
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|