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.*;
009: import com.sun.portal.search.rdm.*;
010:
011: import java.io.*;
012: import java.util.*;
013:
014: /**
015: */
016: public class TestRDMDb { //implements CDB, CDBPrivate {
017:
018: /**
019: * Debug tests
020: * @param st
021: * @param cmd
022: */
023: public static void do_command(SToken st, String cmd) {
024:
025: String rootdb = "d:/test/soifdb/testroot.db";
026: String dbname = "test";
027: String parts[] = { "d:/test/soifdb/test1.db",
028: "d:/test/soifdb/test2.db" };
029: int rw = RDMDb.WRCREAT;
030: int mode = 0644;
031: DbCursor cursor = null;
032: String schema[] = { "DOCUMENT", "OBJECT",
033: "SCHEMA\u0152\u0153\u0154\u0155" }; // ? = \u20ac
034: String url[] = { "maze", "of", "twisty", "little", "passages",
035: "plugh\u0401\u0402\u0403\u0404" };
036: String att[] = { "colossal_cave",
037: "xyzzy\u0152\u0153\u0154\u0155",
038: "plugh\u0401\u0402\u0403\u0404" };
039: String sortOrder = null;
040:
041: try {
042: if (cmd.equals("chars")) {
043: // Unicode char info
044: int startch = 0, xnumch = 0x600, numch = 65536, endch = startch
045: + numch;
046: StringBuffer line = new StringBuffer();
047: for (int i = startch, c = 0;; ++i, ++c) {
048: if (c > 0 && (c % 16) == 0)
049: line.append(" ");
050: if ((c % 64) == 0) {
051: System.out.print(line.toString() + "\n" + i
052: + ":\t");
053: line.setLength(0);
054: Thread.sleep(5);
055: }
056: if (i == endch)
057: break;
058: line.append("" + (char) i);
059: }
060: System.out.println();
061: }
062: if (cmd.equals("c"))
063: IndexedSOIFDb.create(st, rootdb, dbname, parts);
064: else if (cmd.equals("d"))
065: IndexedSOIFDb.drop(st, rootdb, dbname);
066: else if (cmd.equals("o")) {
067: if (cdbm != null)
068: cdbm.close(st);
069: cdbm = new IndexedSOIFDb();
070: cdbm.open(st, rootdb, dbname, rw, mode);
071: if (!reuse)
072: cdbm.close(st);
073: } else if (cmd.equals("C")) {
074: if (cdbm != null) {
075: cdbm.close(st);
076: cdbm = null;
077: }
078: } else if (cmd.equals("R")) {
079: reuse = !reuse;
080: System.out.println("reuse = " + reuse);
081: } else if (cmd.equals("p")) {
082: if (cdbm != null)
083: cdbm.close(st);
084: cdbm = new IndexedSOIFDb();
085: ((IndexedSOIFDb) cdbm).partopen(rootdb, parts[0], rw,
086: mode);
087: if (!reuse)
088: cdbm.close(st);
089: } else if (cmd.equals("count")) {
090: if (!reuse) {
091: cdbm = new IndexedSOIFDb();
092: cdbm.open(st, rootdb, dbname, rw, mode);
093: }
094: System.out.println("count = " + cdbm.count(st, null));
095: if (!reuse)
096: cdbm.close(st);
097: } else if (cmd.equals("i")) {
098: if (!reuse) {
099: cdbm = new IndexedSOIFDb();
100: cdbm.open(st, rootdb, dbname, rw, mode);
101: }
102: for (int i = 0; i < url.length; ++i) {
103: SOIF s = new SOIF(schema[i % schema.length], url[i
104: % url.length]);
105: s.insert("title", "title url = " + url[i]);
106: for (int j = 0; j < att.length; ++j) {
107: s.insert(att[j] + ".",
108: att[(j + 1) % att.length]);
109: for (int k = 0; k < att.length; ++k) {
110: s.insert(att[j], att[k], k);
111: }
112: }
113: System.out.println(s);
114: cdbm.store(st, s, null, 0, null);
115: }
116: if (!reuse)
117: cdbm.close(st);
118: } else if (cmd.equals("I")) {
119: if (!reuse) {
120: cdbm = new IndexedSOIFDb();
121: cdbm.open(st, rootdb, dbname, rw, mode);
122: }
123: SOIFInputStream ss = new SOIFInputStream(
124: "d:/small37.dat");
125: SOIF s = null;
126: while ((s = ss.readSOIF()) != null) {
127: System.out.println("Indexing: " + s.getURL());
128: cdbm.store(st, s, null, 0, null);
129: }
130: if (!reuse)
131: cdbm.close(st);
132: } else if (cmd.startsWith("Q ")) {
133: String qstring = cmd.substring(2);
134: if (!reuse) {
135: cdbm = new IndexedSOIFDb();
136: cdbm.open(st, rootdb, dbname, rw, mode);
137: }
138: int nHits = 10;
139: Set view = null;
140: int flags = 0;
141: RDMTransaction txn = null;
142: //cdbm.indexQuery(st, qstring, nHits, view, flags, txn);
143:
144: /*
145: AccumulatorSet aset = cdbm.search(st, qstring, sortOrder, nHits);
146: Hit[] hits = aset.getHits(nHits);
147: System.out.println("Query for [" + qstring + "] returned " + hits.length + " docs from" + aset.size() + " hits");
148: for (int i = 0; i < hits.length; ++i) {
149: String hiturl = (String)hits[i].getPartition().getSavedField("url", hits[i].getDocument());
150: if (url == null) {
151: // XXX fix old logging
152: CSxLog.error(1, 1, "Search hit has no URL");
153: continue;
154: }
155: SOIF s = cdbm.fetch(st, hiturl, null, 0, null);
156: if (s == null) {
157: // XXX fix old logging
158: CSxLog.error(1, 1, "Search hit not found in db: " + hiturl);
159: continue;
160: }
161: int score = (int)(hits[i].getScore()*50); // XXX for Okapi (0-100?)
162: s.insert("score", "" + score);
163: System.out.println("Hit " + i + ", score=" + score + ", url=" + hiturl);
164: System.out.println(s);
165: }
166: **/
167:
168: if (!reuse)
169: cdbm.close(st);
170: } else if (cmd.equals("r")) {
171: if (!reuse) {
172: cdbm = new IndexedSOIFDb();
173: cdbm.open(st, rootdb, dbname, rw, mode);
174: }
175: for (int i = 0; i < url.length; ++i) {
176: //Datum key = new Datum(url[i % url.length]);
177: //Datum data = new Datum();
178: //int found = cdbm.fetch(st, key, data, 0, null);
179: String u = url[i % url.length];
180: SOIF s = cdbm.fetch(st, u, null, 0, null);
181: if (s == null)
182: throw new Exception("Expected SOIF missing: "
183: + u);
184: System.out.println(s);
185: }
186: if (!reuse)
187: cdbm.close(st);
188: } else if (cmd.equals("dump")) {
189: Datum key, data;
190: cursor = null;
191: if (!reuse) {
192: cdbm = new IndexedSOIFDb();
193: cdbm.open(st, rootdb, dbname, rw, mode);
194: }
195: key = new Datum();
196: data = new Datum();
197: cursor = new DbCursor((IndexedSOIFDb) cdbm, null, 0);
198: while (cursor.get(key, data, RDMDb.DB_NEXT) == 0)
199: System.out.println(new SOIF(data.get_data()));
200: cursor.close();
201: if (!reuse)
202: cdbm.close(st);
203: } else if (cmd.startsWith("S ")) {
204: if (cmd.length() > 2)
205: sortOrder = cmd.substring(2);
206: else
207: sortOrder = null;
208: System.out.println("Sort order set to " + sortOrder);
209: } else if (cmd.equals("t")) {
210: if (!reuse) {
211: cdbm = new IndexedSOIFDb();
212: cdbm.open(st, rootdb, dbname, rw, mode);
213: }
214: cdbm.purge(st, null);
215: if (!reuse)
216: cdbm.close(st);
217: } else if (cmd.equals("X")) {
218: if (cdbm != null)
219: cdbm.close(st);
220: cdbm = new IndexedSOIFDb();
221: cdbm.open(st, rootdb, dbname, rw, mode);
222: ((IndexedSOIFDb) cdbm).reindex(st, null);
223: if (!reuse)
224: cdbm.close(st);
225: } else if (cmd.equals("x"))
226: System.exit(0);
227: else {
228: System.out.println("Commands:");
229: System.out.println("c: create db");
230: System.out.println("d: drop db");
231: System.out.println("o: open db");
232: System.out.println("C: close db");
233: System.out.println("R: toggle reuse of db");
234: System.out.println("p: open partition");
235: System.out.println("count: delete db");
236: System.out.println("i: insert SOIF");
237: System.out.println("q url: query a URL");
238: System.out.println("Q query: query arbitrary string");
239: System.out.println("O view_order: set view order");
240: System.out.println("r: retrieve SOIF");
241: System.out.println("X: reindex db");
242: System.out.println("t: purge");
243: System.out.println("dump: dump SOIF");
244: System.out.println("info: db info");
245: System.out.println("x: exit");
246: }
247: } catch (Exception e) {
248: System.out.println("Exception: " + e);
249: }
250: }
251:
252: static boolean reuse = true;
253: static RDMDb cdbm = null;
254:
255: /**
256: * CLI test program
257: * @param args
258: */
259: public static void main(String args[]) throws Exception {
260:
261: System.out.println("RDMDb test\n");
262: SToken st = new SToken(null, false, false, false);
263: if (args.length > 0) {
264: for (int i = 0; i < args.length; ++i) {
265: do_command(st, args[i]);
266: }
267: } else {
268: for (;;) {
269: java.io.BufferedReader br = new java.io.BufferedReader(
270: new java.io.InputStreamReader(System.in));
271: System.out.println("cmd> ");
272: String cmd = "";
273: try {
274: cmd = br.readLine();
275: } catch (java.io.IOException e) {
276: }
277: do_command(st, cmd);
278: }
279: }
280: }
281:
282: }
|