summaryrefslogtreecommitdiff
path: root/source/de/anomic/kelondro/kelondroStack.java
blob: 8b9df2e86b37dffc106d43d0d381ca3c04cbcc6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// kelondroStack.java
// -----------------------
// part of The Kelondro Database
// (C) by Michael Peter Christen; mc@anomic.de
// first published on http://www.anomic.de
// Frankfurt, Germany, 2004
// last change: 11.01.2004
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Using this software in any meaning (reading, learning, copying, compiling,
// running) means that you agree that the Author(s) is (are) not responsible
// for cost, loss of data or any harm that may be caused directly or indirectly
// by usage of this softare or this documentation. The usage of this software
// is on your own risk. The installation and usage (starting/running) of this
// software may allow other people or application to access your computer and
// any attached devices and is highly dependent on the configuration of the
// software which must be done by the user of the software; the author(s) is
// (are) also not responsible for proper configuration and usage of the
// software, even if provoked by documentation provided together with
// the software.
//
// Any changes to this file according to the GPL as documented in the file
// gpl.txt aside this file in the shipment you received can be done to the
// lines that follows this copyright notice here, but changes must not be
// done inside the copyright notive above. A re-distribution must contain
// the intact and unchanged copyright notice.
// Contributions and changes to the program code must be marked as such.

/*
  This class extends the kelondroRecords and adds a stack structure
*/

package de.anomic.kelondro;

import java.io.*;
import java.util.*;

public class kelondroStack extends kelondroRecords {

    // define the Over-Head-Array
    private static short thisOHBytes   = 0; // our record definition does not need extra bytes
    private static short thisOHHandles = 2; // and two handles overhead for a double-chained list
    private static short thisFHandles  = 2; // two file handles for root handle and handle to last lement

    // define pointers for OH array access
    private static int left  = 0; // pointer for OHHandle-array: handle()-Value of left child Node
    private static int right = 1; // pointer for OHHandle-array: handle()-Value of right child Node
    private static int root  = 0; // pointer for FHandles-array: pointer to root node
    private static int toor  = 1; // pointer for FHandles-array: pointer to root node

    public kelondroStack(File file, long buffersize, int key, int value) throws IOException {
	this(file, buffersize, new int[] {key, value});
    }

    public kelondroStack(File file, long buffersize, int[] columns) throws IOException {
	// this creates a new tree
	super(file, buffersize, thisOHBytes, thisOHHandles, columns, thisFHandles, columns.length /*txtProps*/, 80 /*txtPropWidth*/);
	setHandle(root, null); // define the root value
	setHandle(toor, null); // define the toor value
    }

    public kelondroStack(File file, long buffersize) throws IOException{
	// this opens a file with an existing tree
	super(file, buffersize);
    }

    public class Counter implements Iterator {
	Handle nextHandle = null;
	public Counter() throws IOException {
	    nextHandle = getHandle(root);
	}
	public boolean hasNext() {
	    return (nextHandle != null);
	}
	public Object next() {
	    Handle ret = nextHandle;
	    try {
		nextHandle = getNode(nextHandle, null, 0).getOHHandle()[right];
	    } catch (IOException e) {
		System.err.println("IO error at Counter:next()");
	    }
	    return getNode(ret, null, 0);
	}
	public void remove() {
	    throw new UnsupportedOperationException("no remove here..");
	}
    }

    
    public synchronized void push(byte[][] row) throws IOException {
	if (row.length != columns()) throw new IllegalArgumentException("push: wrong row length " + row.length + "; must be " + columns());
	// check if there is already a stack
	if (getHandle(toor) == null) {
	    if (getHandle(root) != null) throw new RuntimeException("push: internal organisation of root and toor");
	    // create node
	    Node n = newNode(row);
	    n.save();
	    n.setOHHandle(new Handle[] {null, null});
	    n.setValues(row);
	    // assign handles
	    setHandle(root, n.handle());
	    setHandle(toor, n.handle());
	    // thats it
	} else {
	    // expand the list at the end
	    Node n = newNode(row);
	    n.save();
	    n.setOHHandle(new Handle[] {getHandle(toor),null});
	    Node n1 = getNode(getHandle(toor), null, 0);
	    n1.setOHHandle(new Handle[] {n1.getOHHandle()[left], n.handle()});
	    // assign handles
	    setHandle(toor, n.handle());
	    // thats it
	}
    }

    public synchronized byte[][] pop() throws IOException {
	// return row ontop of the stack and shrink stack by one
	Handle h = getHandle(toor);
	if (h == null) return null;
	Node n = getNode(h, null, 0);
	byte[][] ret = n.getValues();
	// shrink stack
	Handle l = n.getOHHandle()[left];
	if (l == null) {
	    // the stack will be empty, write the root handle
	    setHandle(root, null);
	} else {
	    // un-link the previous record
	    Node k = getNode(l, null, 0);
	    k.setOHHandle(new Handle[] {k.getOHHandle()[left], null});
	}
	setHandle(toor, l);
	deleteNode(h);
	return ret;
    }

    public synchronized byte[][] top() throws IOException {
	// return row ontop of the stack
	Handle h = getHandle(toor);
	if (h == null) return null;
	return getNode(h, null, 0).getValues();
    }

    public synchronized byte[][] top(int dist) throws IOException {
	// return row ontop of the stack
	// with dist == 0 this is the same function as with top()
	Handle h = getHandle(toor);
	if (h == null) return null;
	if (dist >= size()) return null; // that would exceed the stack
	while (dist-- > 0) h = getNode(h, null, 0).getOHHandle()[left]; // track through elements
	return getNode(h, null, 0).getValues();
    }

    public synchronized byte[][] pot() throws IOException {
	// return row on the bottom of the stack and remove record
	Handle h = getHandle(root);
	if (h == null) return null;
	Node n = getNode(h, null, 0);
	byte[][] ret = n.getValues();
	// shrink stack
	Handle r = n.getOHHandle()[right];
	if (r == null) {
	    // the stack will be empty, write the toor handle
	    setHandle(toor, null);
	} else {
	    // un-link the next record
	    Node k = getNode(r, null, 0);
	    k.setOHHandle(new Handle[] {null, k.getOHHandle()[right]});
	}
	setHandle(root, r);
	deleteNode(h);
	return ret;
    }

    public synchronized byte[][] bot() throws IOException {
	// return row on the bottom of the stack
	Handle h = getHandle(root);
	if (h == null) return null;
	return getNode(h, null, 0).getValues();
    }

    public synchronized byte[][] bot(int dist) throws IOException {
	// return row on bottom of the stack
	// with dist == 0 this is the same function as with bot()
	Handle h = getHandle(root);
	if (h == null) return null;
	if (dist >= size()) return null; // that would exceed the stack
	while (dist-- > 0) h = getNode(h, null, 0).getOHHandle()[right]; // track through elements
	return getNode(h, null, 0).getValues();
    }

    /*
    public synchronized byte[][] seekPop(byte[] key, long maxdepth) throws IOException {
    }

    public synchronized byte[][] seekPot(byte[] key, long maxdepth) throws IOException {
    }
    */

    public Iterator iterator() {
	// iterates the elements in an ordered way. returns Node - type Objects
	try {
	    return new Counter();
	} catch (IOException e) {
	    throw new RuntimeException("error creating an iteration: " + e.getMessage());
	}
    }

    public int imp(File file, String separator) throws IOException {
	// imports a value-separated file, returns number of records that have been read
	RandomAccessFile f = new RandomAccessFile(file,"r");
	String s;
	StringTokenizer st;
	int recs = 0;
	byte[][] buffer = new byte[columns()][];
	int c;
	int line = 0;
	while ((s = f.readLine()) != null) {
	    s = s.trim();
	    line++;
	    if ((s.length() > 0) && (!(s.startsWith("#")))) {
		st = new StringTokenizer(s, separator);
		// buffer the entry
		c = 0;
		while ((c < columns()) && (st.hasMoreTokens())) {
		    buffer[c++] = st.nextToken().trim().getBytes();
		}
		if ((st.hasMoreTokens()) || (c != columns())) {
		    System.err.println("inapropriate number of entries in line " + line);
		} else {
		    push(buffer);
		    recs++;
		}

	    }
	}
	return recs;
    }

    public String hp(Handle h) {
	if (h == null) return "NULL"; else return h.toString();
    }

    public void print() {
	super.print(false);
	Handle h;
	Node n;
	try {
	    Iterator it = iterator();
	    while (it.hasNext()) {
		h = (Handle) it.next();
		n = getNode(h, null, 0);
		System.out.println("> NODE " + hp(h) +
				   "; left " + hp(n.getOHHandle()[left]) + ", right " + hp(n.getOHHandle()[right]));
		System.out.print("  KEY:'" + (new String(n.getValues()[0])).trim() + "'");
		for (int j = 1; j < columns(); j++)
		    System.out.print(", V[" + j + "]:'" + (new String(n.getValues()[j])).trim() + "'");
		System.out.println();
	    }
	    System.out.println();
	} catch (IOException e) {
	    System.out.println("File error: " + e.getMessage());
	}
    }

    private static void cmd(String[] args) {
	System.out.print("kelondroStack ");
	for (int i = 0; i < args.length; i++) System.out.print(args[i] + " ");
	System.out.println("");
	byte[] ret = null;
	try {
	    if ((args.length > 4) || (args.length < 2)) {
		System.err.println("usage: kelondroStack -c|-p|-v|-g|-i|-s [file]|[key [value]] <db-file>");
		System.err.println("( create, push, view, (g)pop, imp, shell)");
		System.exit(0);
	    } else if (args.length == 2) {
		kelondroStack fm = new kelondroStack(new File(args[1]), 0x100000);
		if (args[0].equals("-v")) {
		    fm.print();
		    ret = null;
		} else if (args[0].equals("-g")) {
		    fm = new kelondroStack(new File(args[1]), 0x100000);
		    byte[][] ret2 = fm.pop();
		    ret = ((ret2 == null) ? null : ret2[1]); 
		    fm.close();
		}
		fm.close();
	    } else if (args.length == 3) {
		if (args[0].equals("-i")) {
		    kelondroStack fm = new kelondroStack(new File(args[2]), 0x100000);
		    int i = fm.imp(new File(args[1]),";");
		    fm.close();
		    ret = (i + " records imported").getBytes();
		} else if (args[0].equals("-s")) {
		    String db = args[2];
		    BufferedReader f = new BufferedReader(new FileReader(args[1]));
		    String m;
		    while (true) {
			m = f.readLine();
			if (m == null) break;
			if ((m.length() > 1) && (!m.startsWith("#"))) {
			    m = m + " " + db;
			    cmd(line2args(m));
			}
		    }
		    ret = null;
		}
	    } else if (args.length == 4) {
		if (args[0].equals("-c")) {
		    // create <keylen> <valuelen> <filename>
		    File f = new File(args[3]);
		    if (f.exists()) f.delete();
		    int[] lens = new int[2];
		    lens[0] = Integer.parseInt(args[1]);
		    lens[1] = Integer.parseInt(args[2]);
		    kelondroStack fm = new kelondroStack(f, 0x100000, lens);
		    fm.close();
		} else if (args[0].equals("-p")) {
		    kelondroStack fm = new kelondroStack(new File(args[3]), 0x100000);
		    fm.push(new byte[][] {args[1].getBytes(), args[2].getBytes()});
		    fm.close();
		}
	    }
	    if (ret == null)
		System.out.println("NULL");
	    else
		System.out.println(new String(ret));
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
    
    public static void main(String[] args) {
	cmd(args);
    }
    
}