001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.search.db;
007:
008: import com.sun.portal.search.soif.SOIF;
009: import com.sun.portal.search.rdm.*;
010:
011: import java.io.*;
012: import java.util.*;
013:
014: /**
015: * Test class for Partitioned databases.
016: */
017: public class TestDb {
018:
019: /*
020: * Test routines
021: */
022:
023: /**
024: * Regression tests
025: * @param st
026: * @throws RDMException */
027: public void test(SToken st) throws RDMException {
028: throw new RDMException("not implemented");
029: }
030:
031: /**
032: * Debug tests
033: * @param st
034: * @param cmd
035: */
036: public static void do_command(SToken st, String cmd) {
037:
038: String rootdb = "d:/test/cdbm/testroot.db";
039: String dbname = "test";
040: String parts[] = { "d:/test/cdbm/test1.db",
041: "d:/test/cdbm/test2.db" };
042: int rw = RDMDb.WRCREAT;
043: int mode = 0644;
044: DbCursor cursor = null;
045: String schema[] = { "DOCUMENT", "OBJECT",
046: "SCHEMA\u0152\u0153\u0154\u0155" }; // ? = \u20ac
047: String url[] = { "maze", "of", "twisty", "little", "passages",
048: "plugh\u0401\u0402\u0403\u0404" };
049: String att[] = { "colossal_cave",
050: "xyzzy\u0152\u0153\u0154\u0155",
051: "plugh\u0401\u0402\u0403\u0404" };
052:
053: try {
054: if (cmd.equals("chars")) {
055: // Unicode char info
056: int startch = 0, xnumch = 0x600, numch = 65536, endch = startch
057: + numch;
058: StringBuffer line = new StringBuffer();
059: for (int i = startch, c = 0;; ++i, ++c) {
060: if (c > 0 && (c % 16) == 0)
061: line.append(" ");
062: if ((c % 64) == 0) {
063: System.out.print(line.toString() + "\n" + i
064: + ":\t");
065: line.setLength(0);
066: Thread.sleep(5);
067: }
068: if (i == endch)
069: break;
070: line.append("" + (char) i);
071: }
072: System.out.println();
073: }
074: if (cmd.equals("c"))
075: PartitionedDb.create(st, rootdb, dbname, parts);
076: else if (cmd.equals("d"))
077: PartitionedDb.drop(st, rootdb, dbname);
078: else if (cmd.equals("e")) {
079: System.out.println("blah ? blah"
080: + "xyzzy\u0152\u0153\u0154\u0155");
081: } else if (cmd.equals("o")) {
082: if (cdbm != null)
083: cdbm.close(st);
084: cdbm = new PartitionedDb();
085: cdbm.open(st, rootdb, dbname, rw, mode);
086: if (!reuse)
087: cdbm.close(st);
088: } else if (cmd.equals("C")) {
089: if (cdbm != null)
090: cdbm.close(st);
091: } else if (cmd.equals("R")) {
092: reuse = !reuse;
093: System.out.println("reuse = " + reuse);
094: } else if (cmd.equals("p")) {
095: if (cdbm != null)
096: cdbm.close(st);
097: cdbm = new PartitionedDb();
098: cdbm.partopen(rootdb, parts[0], rw, mode);
099: if (!reuse)
100: cdbm.close(st);
101: } else if (cmd.equals("count")) {
102: if (!reuse) {
103: cdbm = new PartitionedDb();
104: cdbm.open(st, rootdb, dbname, rw, mode);
105: }
106: System.out.println("count = " + cdbm.count(st, null));
107: if (!reuse)
108: cdbm.close(st);
109: } else if (cmd.equals("i")) {
110: if (!reuse) {
111: cdbm = new PartitionedDb();
112: cdbm.open(st, rootdb, dbname, rw, mode);
113: }
114: for (int i = 0; i < url.length; ++i) {
115: SOIF s = new SOIF(schema[i % schema.length], url[i
116: % url.length]);
117: for (int j = 0; j < att.length; ++j) {
118: s.insert(att[j] + ".",
119: att[(j + 1) % att.length]);
120: for (int k = 0; k < att.length; ++k) {
121: // XXX hex2uint requires at least 8 byte key (eg MD5)
122: //SOIF s = new SOIF("@DOCUMENT { test1234\nauthor{5}: \u0398mwr\n}\n");
123: s.insert(att[j], att[k], k);
124: }
125: }
126: System.out.println(s);
127: Datum key = new Datum(url[i % url.length]);
128: Datum data = new Datum(s
129: .toByteArray(SOIFDb.ENCODING));
130: cdbm.store(st, key, data, 0, null);
131: }
132: if (!reuse)
133: cdbm.close(st);
134: } else if (cmd.equals("r")) {
135: if (!reuse) {
136: cdbm = new PartitionedDb();
137: cdbm.open(st, rootdb, dbname, rw, mode);
138: }
139: for (int i = 0; i < url.length; ++i) {
140: Datum key = new Datum(url[i % url.length]);
141: Datum data = new Datum();
142: int found = cdbm.fetch(st, key, data, 0, null);
143: if (found != 0)
144: throw new Exception("Expected SOIF missing: "
145: + url[i % url.length]);
146: SOIF s = new SOIF(data.get_data());
147: System.out.println(s);
148: }
149: if (!reuse)
150: cdbm.close(st);
151: } else if (cmd.equals("dump")) {
152: Datum key, data;
153: cursor = null;
154: if (!reuse) {
155: cdbm = new PartitionedDb();
156: cdbm.open(st, rootdb, dbname, rw, mode);
157: }
158: key = new Datum();
159: data = new Datum();
160: cursor = new DbCursor(cdbm, null, 0);
161: while (cursor.get(key, data, RDMDb.DB_NEXT) == 0)
162: System.out.println(new SOIF(data.get_data()));
163: cursor.close();
164: if (!reuse)
165: cdbm.close(st);
166: } else if (cmd.equals("t")) {
167: if (!reuse) {
168: cdbm = new PartitionedDb();
169: cdbm.open(st, rootdb, dbname, rw, mode);
170: }
171: cdbm.purge(st, null);
172: if (!reuse)
173: cdbm.close(st);
174: } else if (cmd.equals("x"))
175: System.exit(0);
176: else {
177: System.out.println("Commands:");
178: System.out.println("c: create db");
179: System.out.println("d: drop db");
180: System.out.println("o: open db");
181: System.out.println("C: close db");
182: System.out.println("R: toggle reuse of db");
183: System.out.println("p: open partition");
184: System.out.println("count: delete db");
185: System.out.println("i: insert SOIF");
186: System.out.println("q: query [q qlang qstring]");
187: System.out.println("r: retrieve SOIF");
188: System.out.println("t: purge");
189: System.out.println("dump: dump SOIF");
190: System.out.println("info: db info");
191: System.out.println("x: exit");
192: }
193: } catch (Exception e) {
194: System.out.println("Exception: " + e);
195: }
196: }
197:
198: static boolean reuse = true;
199: static PartitionedDb cdbm = null;
200:
201: /**
202: * CLI test program
203: * @param args
204: */
205: public static void main(String args[]) throws Exception {
206:
207: System.out.println("Partitioned test\n");
208: SToken st = new SToken(null, false, false, false);
209: if (args.length > 0) {
210: for (int i = 0; i < args.length; ++i) {
211: do_command(st, args[i]);
212: }
213: } else {
214: for (;;) {
215: java.io.BufferedReader br = new java.io.BufferedReader(
216: new java.io.InputStreamReader(System.in));
217: System.out.println("cmd> ");
218: String cmd = "";
219: try {
220: cmd = br.readLine();
221: } catch (java.io.IOException e) {
222: }
223: do_command(st, cmd);
224: }
225: }
226: }
227:
228: }
|