001: // indexCollectionRI.java
002: // (C) 2006 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
003: // first published 03.07.2006 on http://yacy.net
004: //
005: // This is a part of YaCy, a peer-to-peer based web search engine
006: //
007: // $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
008: // $LastChangedRevision: 1986 $
009: // $LastChangedBy: orbiter $
010: //
011: // LICENSE
012: //
013: // This program is free software; you can redistribute it and/or modify
014: // it under the terms of the GNU General Public License as published by
015: // the Free Software Foundation; either version 2 of the License, or
016: // (at your option) any later version.
017: //
018: // This program is distributed in the hope that it will be useful,
019: // but WITHOUT ANY WARRANTY; without even the implied warranty of
020: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
021: // GNU General Public License for more details.
022: //
023: // You should have received a copy of the GNU General Public License
024: // along with this program; if not, write to the Free Software
025: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
026:
027: package de.anomic.index;
028:
029: import java.io.File;
030: import java.io.IOException;
031: import java.util.HashSet;
032: import java.util.Iterator;
033: import java.util.List;
034: import java.util.Set;
035:
036: import de.anomic.kelondro.kelondroBase64Order;
037: import de.anomic.kelondro.kelondroCloneableIterator;
038: import de.anomic.kelondro.kelondroCollectionIndex;
039: import de.anomic.kelondro.kelondroOutOfLimitsException;
040: import de.anomic.kelondro.kelondroRow;
041: import de.anomic.kelondro.kelondroRowSet;
042: import de.anomic.server.logging.serverLog;
043:
044: public class indexCollectionRI implements indexRI {
045:
046: kelondroCollectionIndex collectionIndex;
047:
048: public indexCollectionRI(File path, String filenameStub,
049: long preloadTime, int maxpartition, kelondroRow payloadrow) {
050: try {
051: collectionIndex = new kelondroCollectionIndex(path,
052: filenameStub, 12 /*keyLength*/,
053: kelondroBase64Order.enhancedCoder, preloadTime,
054: 4 /*loadfactor*/, maxpartition, payloadrow);
055: } catch (IOException e) {
056: serverLog.logSevere("PLASMA",
057: "unable to open collection index at "
058: + path.toString() + ":" + e.getMessage());
059: }
060: }
061:
062: public long getUpdateTime(String wordHash) {
063: indexContainer entries = getContainer(wordHash, null);
064: if (entries == null)
065: return 0;
066: return entries.updated();
067: }
068:
069: public int size() {
070: return collectionIndex.size();
071: }
072:
073: public int indexSize(String wordHash) {
074: try {
075: return collectionIndex.indexSize(wordHash.getBytes());
076: } catch (IOException e) {
077: return 0;
078: }
079: }
080:
081: public int minMem() {
082: // calculate a minimum amount of memory that is necessary to use the index
083: // during runtime (after the object was initialized)
084: return 100 * 1024 /* overhead here */+ collectionIndex
085: .minMem();
086: }
087:
088: public synchronized kelondroCloneableIterator<indexContainer> wordContainers(
089: String startWordHash, boolean rot) {
090: return new wordContainersIterator(startWordHash, rot);
091: }
092:
093: public class wordContainersIterator implements
094: kelondroCloneableIterator<indexContainer> {
095:
096: private Iterator<Object[]> wci;
097: private boolean rot;
098:
099: public wordContainersIterator(String startWordHash, boolean rot) {
100: this .rot = rot;
101: this .wci = collectionIndex.keycollections(startWordHash
102: .getBytes(), kelondroBase64Order.zero(startWordHash
103: .length()), rot);
104: }
105:
106: public wordContainersIterator clone(Object secondWordHash) {
107: return new wordContainersIterator((String) secondWordHash,
108: rot);
109: }
110:
111: public boolean hasNext() {
112: return wci.hasNext();
113: }
114:
115: public indexContainer next() {
116: Object[] oo = (Object[]) wci.next();
117: if (oo == null)
118: return null;
119: byte[] key = (byte[]) oo[0];
120: kelondroRowSet collection = (kelondroRowSet) oo[1];
121: if (collection == null)
122: return null;
123: return new indexContainer(new String(key), collection);
124: }
125:
126: public void remove() {
127: wci.remove();
128: }
129:
130: }
131:
132: public boolean hasContainer(String wordHash) {
133: try {
134: return collectionIndex.has(wordHash.getBytes());
135: } catch (IOException e) {
136: return false;
137: }
138: }
139:
140: public indexContainer getContainer(String wordHash,
141: Set<String> urlselection) {
142: try {
143: kelondroRowSet collection = collectionIndex.get(wordHash
144: .getBytes());
145: if (collection != null)
146: collection.select(urlselection);
147: if ((collection == null) || (collection.size() == 0))
148: return null;
149: return new indexContainer(wordHash, collection);
150: } catch (IOException e) {
151: return null;
152: }
153: }
154:
155: public indexContainer deleteContainer(String wordHash) {
156: try {
157: kelondroRowSet collection = collectionIndex.delete(wordHash
158: .getBytes());
159: if (collection == null)
160: return null;
161: return new indexContainer(wordHash, collection);
162: } catch (IOException e) {
163: return null;
164: }
165: }
166:
167: public boolean removeEntry(String wordHash, String urlHash) {
168: HashSet<String> hs = new HashSet<String>();
169: hs.add(urlHash);
170: return removeEntries(wordHash, hs) == 1;
171: }
172:
173: public int removeEntries(String wordHash, Set<String> urlHashes) {
174: try {
175: return collectionIndex.remove(wordHash.getBytes(),
176: urlHashes);
177: } catch (kelondroOutOfLimitsException e) {
178: e.printStackTrace();
179: return 0;
180: } catch (IOException e) {
181: e.printStackTrace();
182: return 0;
183: }
184: }
185:
186: public void addEntries(indexContainer newEntries) {
187: try {
188: collectionIndex.merge(newEntries);
189: } catch (kelondroOutOfLimitsException e) {
190: e.printStackTrace();
191: } catch (IOException e) {
192: e.printStackTrace();
193: }
194: }
195:
196: public void addMultipleEntries(List<indexContainer> containerList) {
197: try {
198: for (int i = 0; i < containerList.size(); i++)
199: collectionIndex.merge(containerList.get(i));
200: //collectionIndex.mergeMultiple(containerList);
201: } catch (kelondroOutOfLimitsException e) {
202: e.printStackTrace();
203: } catch (IOException e) {
204: e.printStackTrace();
205: }
206: }
207:
208: public void close() {
209: collectionIndex.close();
210: }
211:
212: }
|