001: package com.sun.portal.search.autoclassify;
002:
003: import java.util.*;
004: import java.io.*;
005: import com.sun.portal.search.soif.*;
006: import com.sun.portal.search.db.Datum;
007: import com.sleepycat.db.*;
008:
009: /** <p>Title: RDStore</p>
010: * <p>Description: Used for storing temporary RDs for modification. The object
011: * combine a hashtable in memory and Berkeley database for storage. Only when the
012: * number of RDs exceed given hashtable size, it will only used hashtable for
013: * performance.</p>
014: * <p>Copyright: Copyright (c) 2002</p>
015: * <p>Company: Sun Microsystems</p>
016: * @author Allen Chiang
017: * @version 1.0
018: */
019:
020: public class RDStore {
021:
022: HashMap store = new HashMap();
023: Db table = null;
024: String dbFileName = null;
025: private int cacheSize = 10000;
026:
027: /** Construct a instance with hashtable size and database file path
028: * @param cacheSize memory hashtable size
029: * @param dbFilename database file path
030: * @throws Exception Any cause to create the store.
031: */
032: public RDStore(int cacheSize, String dbPath) throws Exception {
033: this .cacheSize = cacheSize;
034: dbFileName = dbPath;
035: table = new Db(null, 0);
036: table.set_error_stream(System.err);
037: table.set_errpfx("RDStore:");
038: table.open(dbFileName, null, Db.DB_BTREE, Db.DB_CREATE
039: | Db.DB_TRUNCATE, 0644);
040:
041: }
042:
043: /** Perform housekeeping before the object was discarded. Such as delete database
044: * file.
045: */
046: public void close() {
047: try {
048: if (table != null) {
049: table.close(0);
050: }
051: File f = new File(dbFileName);
052: f.delete();
053: } catch (Exception e) {
054: //ignore
055: }
056: }
057:
058: /** Getting a RD by its url.
059: * @param urlStr URL string for the RD
060: * @return Return the rd by given url string. Return null if the url doesn't exist.
061: */
062: synchronized public SOIF getRD(String urlStr) {
063: Datum key = null;
064: Datum data = null;
065:
066: SOIF s = (SOIF) store.get(urlStr);
067: if (s == null) {
068: key = new Datum(urlStr);
069: data = new Datum();
070: try {
071: int rcode = table.get(null, key, data, 0);
072: if (rcode == 0) {
073: s = new SOIF(data.get_data(), "UTF-8");
074: store.put(urlStr, s);
075: }
076: } catch (Exception e) {
077: System.out
078: .println("table.get() throws exception with message: "
079: + e.getMessage());
080: }
081: }
082: return s;
083: }
084:
085: private void pushToDB() throws Exception {
086: Datum key = null;
087: Datum data = null;
088: Iterator keyIt = store.keySet().iterator();
089: while (keyIt.hasNext()) {
090: String url = (String) keyIt.next();
091: SOIF rd = (SOIF) store.get(url);
092: key = new Datum(url);
093: data = new Datum(rd.toByteArray());
094: table.put(null, key, data, 0);
095: }
096: store.clear();
097: }
098:
099: /** Putting a rd into store
100: * @param s A RD in soif object
101: */
102: synchronized public void putRD(SOIF s) {
103: store.put(s.getURL(), s);
104: if (store.size() > cacheSize) {
105: try {
106: pushToDB();
107: } catch (Exception e) {
108: System.out
109: .println("Exception throws at pushToDB() with message:"
110: + e.getMessage());
111: }
112: }
113: }
114:
115: /** Dumping all RDs into a SOIF format file
116: * @param fileName The file name for the SOIF file
117: * @throws Exception Fail to dump the RD.
118: * @return Number of RDs was dumped. If it is 0, then the SOIF file won't be created.
119: */
120: public int CreateSOIF(String fileName) throws Exception {
121: Iterator keyIt = store.keySet().iterator();
122: SOIFOutputStream sOut = null;
123: int rdCount = 0;
124: while (keyIt.hasNext()) {
125: String url = (String) keyIt.next();
126: SOIF rd = (SOIF) store.get(url);
127: if (sOut == null) {
128: sOut = new SOIFOutputStream(fileName);
129: }
130: sOut.write(rd);
131: rdCount++;
132: }
133: if (table != null) {
134: Dbc cursor = table.cursor(null, 0);
135: Datum key = new Datum();
136: Datum data = new Datum();
137:
138: while (cursor.get(key, data, Db.DB_NEXT) == 0) {
139: SOIF rd = new SOIF(data.get_data(), "UTF-8");
140: if (store.get(rd.getURL()) == null) {
141: if (sOut == null) {
142: sOut = new SOIFOutputStream(fileName);
143: }
144: sOut.write(rd);
145: rdCount++;
146: }
147: }
148: ;
149: }
150: if (sOut != null) {
151: sOut.close();
152: }
153: return rdCount;
154: }
155:
156: }
|