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.rdm.*;
009: import com.sun.portal.search.soif.*;
010: import com.sun.portal.search.util.*;
011:
012: import java.util.*;
013: import java.io.*;
014:
015: import java.util.logging.Level;
016:
017: /**
018: * SOIF Database API
019: *
020: * <p>- handles SOIF db i/o
021: * <p>- provides a template for similar, non-SOIF layers, eg, XML, etc.
022: * <p>- hides db implementation details (eg, db might not contain SOIF)
023: * <p>- hides the mechanics of P/NP data
024: */
025: public class IndexedSOIFDb extends SOIFDb implements RDMDb {
026:
027: NovaDb searchdb = new NovaDb(); // XXX need a factory
028:
029: /**
030: * store_view - insert/replace RD in database, filtered by view
031: */
032: public void store(SToken st, SOIF insoif, Set view, int flags,
033: RDMTransaction t) throws RDMException {
034:
035: // check the permission
036: if (!RDMSecurityManager.getInstance().checkDatabasePermission(
037: st, RDMSecurityManager.DB_PERMISSION_WRITE, insoif)) {
038: throw new RDMException("Permission denied:" + "database="
039: + st.getDatabaseName() + ":" + "Action:"
040: + RDMSecurityManager.DB_PERMISSION_WRITE);
041: }
042:
043: insoif = st.appendVirtualDBTag(insoif);
044: insoif = st.appendReadAcl(insoif);
045: if ((flags & NOSTORE) == 0) {
046: super .store(st, insoif, view, flags, t);
047: }
048:
049: if ((flags & NOINDEX) == 0) {
050: if (!insoif.contains(SOIFDb.NOREINDEX)) {
051: // need to reindex this SOIF
052: searchdb.store(st, insoif, view, flags, t);
053: } else
054: insoif.remove(SOIFDb.NOREINDEX);
055: }
056:
057: }
058:
059: /**
060: * delete - delete RD from database XXX with View?
061: */
062: public void delete(SToken st, SOIF insoif, Set view, int flags,
063: RDMTransaction t) throws RDMException {
064:
065: // check the permission
066: if (!RDMSecurityManager.getInstance().checkDatabasePermission(
067: st, RDMSecurityManager.DB_PERMISSION_WRITE, null)) {
068: throw new RDMException("Permission denied:" + "database="
069: + st.getDatabaseName() + ":" + "Action:"
070: + RDMSecurityManager.DB_PERMISSION_WRITE);
071: }
072:
073: if ((flags & NOSTORE) == 0) {
074: super .delete(st, insoif, flags, t);
075: }
076:
077: if ((flags & NOINDEX) == 0) {
078: int pflags = flags & 0xFF000000;
079: flags &= 0x00FFFFFF;
080: if ((pflags & RDMDb.PONLY) != 0) {
081: // P delete may require reindex
082: if (!insoif.contains(SOIFDb.NOREINDEX))
083: searchdb.store(st, insoif, view, flags, t);
084: } else
085: // Entire RD, or NP only delete implies index delete
086: searchdb.delete(st, insoif, null, 0, t);
087: }
088:
089: }
090:
091: /**
092: * update - update an RD
093: */
094: public void update(SToken st, SOIF insoif, Set view, int flags,
095: RDMTransaction t) throws RDMException {
096:
097: // check the permission
098: if (!RDMSecurityManager.getInstance().checkDatabasePermission(
099: st, RDMSecurityManager.DB_PERMISSION_WRITE, null)) {
100: throw new RDMException("Permission denied:" + "database="
101: + st.getDatabaseName() + ":" + "Action:"
102: + RDMSecurityManager.DB_PERMISSION_WRITE);
103: }
104:
105: //insoif = st.appendVirtualDBTag(insoif);
106:
107: // always update, even if not storing, for correct index semantics
108: super .update(st, insoif, view, flags, t);
109:
110: if ((flags & NOINDEX) == 0) {
111: // This does not allow for update resulting in a delete - can it? XXX yes...
112: if (!insoif.contains(SOIFDb.NOREINDEX)) {
113: // need to reindex this SOIF
114: searchdb.store(st, insoif, view, flags, t);
115: } else
116: insoif.remove(SOIFDb.NOREINDEX);
117: }
118:
119: }
120:
121: /**
122: * Delete the named db.
123: * @param st
124: * @param rootdir
125: * @param dbname
126: * @throws RDMException */
127: static public void drop(SToken st, String rootdir, String dbname)
128: throws RDMException {
129: PartitionedDb.drop(st, rootdir, dbname);
130: //XXX searchdb.drop();
131: }
132:
133: /**
134: * @param st
135: * @param t
136: * @param flags
137: * @throws RDMException
138: * @return
139: */
140: public void optimize(SToken st) throws RDMException {
141: super .optimize(st);
142: searchdb.optimize(st);
143: }
144:
145: public int purge(SToken st, RDMTransaction t) throws RDMException {
146: searchdb.purge(st, t); // drop indexes first to avoid false hits
147: return super .purge(st, t);
148: }
149:
150: /**
151: * open -- Opens a database
152: * - rootdir -- db home dir
153: * - dbname -- name of database from root.db (e.g., default)
154: * - rw -- RDMDb.WRITER or RDMDb.WRCREAT or RDMDb.READER
155: * - mode -- Unix mode
156: * @param st
157: * @param rootdir
158: * @param dbname
159: * @param rw
160: * @param mode
161: * @throws RDMException */
162: public void open(SToken st, String rootdir, String dbname, int rw,
163: int mode) throws RDMException {
164: super .open(st, rootdir, dbname, rw, mode);
165: searchdb.open(st, rootdir, dbname, rw, mode);
166:
167: }
168:
169: /**
170: * Closes db and index extents
171: * @param st
172: * @throws RDMException */
173: public void close(SToken st) throws RDMException {
174: super .close(st);
175: searchdb.close(st);
176: }
177:
178: public String getName() {
179: //return searchdb.getName;
180: return "Nova Search"; // XXX
181: }
182:
183: public RDMResultSet search(SToken st, String query, int numHits,
184: Set view, String sortOrder, RDMTransaction t)
185: throws RDMException {
186:
187: // check the permission
188: if (!RDMSecurityManager.getInstance().checkDatabasePermission(
189: st, RDMSecurityManager.DB_PERMISSION_READ, null)) {
190: throw new RDMException("Permission denied:" + "database="
191: + st.getDatabaseName() + ":" + "Action:"
192: + RDMSecurityManager.DB_PERMISSION_READ);
193:
194: }
195:
196: // continue with search
197: RDMResultSet rs = searchdb.search(st, query, numHits, view,
198: sortOrder, t);
199: // XXX switch from searchdb to 'this' db in the result set.
200: // Need this to correctly fetch/highlight RDs since novadb is not a real db (yet...)
201: // Probably should have IndexedSOIFResultSet (groan...)
202: rs.database = this ;
203: return rs;
204: }
205:
206: public void store(SToken st, SOIFInputStream ss, Set view,
207: int flags, RDMTransaction t) throws RDMException {
208: try {
209: SOIF s;
210: while ((s = ss.readSOIF()) != null) {
211: store(st, s, view, flags, t);
212: }
213: } catch (Exception e) {
214: throw new RDMException(e);
215: }
216: }
217:
218: /**
219: * Delete a SOIF stream. Should do automatic batching if appropriate.
220: */
221: public void delete(SToken st, SOIFInputStream ss, Set view,
222: int flags, RDMTransaction t) throws RDMException {
223: try {
224: SOIF s;
225: while ((s = ss.readSOIF()) != null) {
226: delete(st, s, view, flags, t);
227: }
228: } catch (Exception e) {
229: throw new RDMException(e);
230: }
231: }
232:
233: /**
234: * Number of docs.
235: */
236: public int count(SToken st, RDMTransaction t) throws RDMException {
237: return searchdb.count(st, t);
238: }
239:
240: public void recover(SToken st, String dbhome, boolean fatal)
241: throws RDMException {
242: super .recover(st, dbhome, fatal);
243: searchdb.recover(st, dbhome, fatal);
244: }
245:
246: public void reindex(SToken st, RDMTransaction t)
247: throws RDMException {
248: searchdb.purge(st, t);
249: int flags = 0;
250: DbCursor cursor = new DbCursor(this , t, flags);
251: // XXX should be dealing with SOIF cursor here...
252: Datum key = new Datum();
253: Datum data = new Datum();
254: while (cursor.get(key, data, RDMDb.DB_NEXT) == 0) {
255: if (new String(key.get_data()).endsWith(".per"))
256: // skip persistent records
257: continue;
258: try {
259: SOIF s = new SOIF(data.get_data());
260: SOIF ms = fetch(st, s.getURL(), null, 0, t);
261: if (st.validateVirtualDBTag(ms)) {
262: searchdb.store(st, ms, null, 0, t);
263: }
264: } catch (Exception e) {
265: throw new RDMException(e); // XXX log here and continue
266: }
267: }
268: searchdb.indexBatch(st);
269: }
270:
271: public void setIndexBatchSize(SToken st, int n) throws RDMException {
272: searchdb.setIndexBatchSize(st, n);
273: }
274:
275: public void indexBatch(SToken st) throws RDMException {
276: searchdb.indexBatch(st);
277: }
278:
279: public SOIF fetch(SToken st, String url, Set view, int flags,
280: RDMTransaction t) throws RDMException {
281: // check the permission
282: if (!RDMSecurityManager.getInstance().checkDatabasePermission(
283: st, RDMSecurityManager.DB_PERMISSION_READ, null)) {
284: throw new RDMException("Permission denied:" + "database="
285: + st.getDatabaseName() + ":" + "Action:"
286: + RDMSecurityManager.DB_PERMISSION_READ);
287: }
288:
289: return super.fetch(st, url, view, flags, t);
290:
291: }
292:
293: }
|