2006-07-04 16:47:27 +02:00
|
|
|
// indexCollectionRI.java
|
2007-08-28 14:15:46 +02:00
|
|
|
// (C) 2006 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
|
|
|
|
// first published 03.07.2006 on http://yacy.net
|
2006-07-04 16:47:27 +02:00
|
|
|
//
|
|
|
|
// This is a part of YaCy, a peer-to-peer based web search engine
|
|
|
|
//
|
|
|
|
// $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
|
|
|
|
// $LastChangedRevision: 1986 $
|
|
|
|
// $LastChangedBy: orbiter $
|
|
|
|
//
|
|
|
|
// LICENSE
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
2006-07-03 00:24:13 +02:00
|
|
|
package de.anomic.index;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2006-08-05 21:18:33 +02:00
|
|
|
import java.util.HashSet;
|
2006-07-03 00:24:13 +02:00
|
|
|
import java.util.Iterator;
|
2006-08-01 12:30:55 +02:00
|
|
|
import java.util.Set;
|
2006-07-03 00:24:13 +02:00
|
|
|
|
2006-11-05 03:10:40 +01:00
|
|
|
import de.anomic.kelondro.kelondroBase64Order;
|
2007-03-08 17:15:40 +01:00
|
|
|
import de.anomic.kelondro.kelondroCloneableIterator;
|
2006-07-03 00:24:13 +02:00
|
|
|
import de.anomic.kelondro.kelondroCollectionIndex;
|
2006-08-05 21:18:33 +02:00
|
|
|
import de.anomic.kelondro.kelondroOutOfLimitsException;
|
2006-07-03 00:24:13 +02:00
|
|
|
import de.anomic.kelondro.kelondroRow;
|
2006-08-05 21:18:33 +02:00
|
|
|
import de.anomic.kelondro.kelondroRowSet;
|
2006-08-06 00:22:14 +02:00
|
|
|
import de.anomic.server.logging.serverLog;
|
2006-07-03 00:24:13 +02:00
|
|
|
|
2006-10-28 02:22:10 +02:00
|
|
|
public class indexCollectionRI implements indexRI {
|
2006-07-03 00:24:13 +02:00
|
|
|
|
|
|
|
kelondroCollectionIndex collectionIndex;
|
|
|
|
|
2008-02-19 10:14:07 +01:00
|
|
|
public indexCollectionRI(File path, String filenameStub, int maxpartition, kelondroRow payloadrow) {
|
2006-08-06 00:22:14 +02:00
|
|
|
try {
|
|
|
|
collectionIndex = new kelondroCollectionIndex(
|
|
|
|
path,
|
|
|
|
filenameStub,
|
|
|
|
12 /*keyLength*/,
|
2006-11-05 03:10:40 +01:00
|
|
|
kelondroBase64Order.enhancedCoder,
|
2006-08-06 00:22:14 +02:00
|
|
|
4 /*loadfactor*/,
|
2007-03-03 01:55:51 +01:00
|
|
|
maxpartition,
|
2006-11-05 03:10:40 +01:00
|
|
|
payloadrow);
|
2006-08-06 00:22:14 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
serverLog.logSevere("PLASMA", "unable to open collection index at " + path.toString() + ":" + e.getMessage());
|
|
|
|
}
|
2006-07-03 00:24:13 +02:00
|
|
|
}
|
|
|
|
|
2008-05-24 14:30:50 +02:00
|
|
|
public void clear() {
|
|
|
|
try {
|
|
|
|
collectionIndex.clear();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-28 02:22:10 +02:00
|
|
|
public long getUpdateTime(String wordHash) {
|
2007-09-04 01:43:55 +02:00
|
|
|
indexContainer entries = getContainer(wordHash, null);
|
2006-10-28 02:22:10 +02:00
|
|
|
if (entries == null) return 0;
|
|
|
|
return entries.updated();
|
|
|
|
}
|
|
|
|
|
2007-03-18 20:45:23 +01:00
|
|
|
public int size() {
|
2007-06-26 16:37:10 +02:00
|
|
|
return collectionIndex.size();
|
2006-07-03 00:24:13 +02:00
|
|
|
}
|
2006-07-27 00:40:11 +02:00
|
|
|
|
2007-11-12 02:14:51 +01:00
|
|
|
public int indexSize(String wordHash) {
|
2006-08-11 18:01:18 +02:00
|
|
|
try {
|
|
|
|
return collectionIndex.indexSize(wordHash.getBytes());
|
|
|
|
} catch (IOException e) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-22 13:54:56 +01:00
|
|
|
public int minMem() {
|
|
|
|
// calculate a minimum amount of memory that is necessary to use the index
|
|
|
|
// during runtime (after the object was initialized)
|
2007-08-20 19:36:43 +02:00
|
|
|
return 100 * 1024 /* overhead here */ + collectionIndex.minMem();
|
2006-12-22 13:54:56 +01:00
|
|
|
}
|
|
|
|
|
2007-12-28 04:39:36 +01:00
|
|
|
public synchronized kelondroCloneableIterator<indexContainer> wordContainers(String startWordHash, boolean rot) {
|
2006-07-26 13:21:51 +02:00
|
|
|
return new wordContainersIterator(startWordHash, rot);
|
2006-07-03 00:24:13 +02:00
|
|
|
}
|
|
|
|
|
2007-12-28 04:39:36 +01:00
|
|
|
public class wordContainersIterator implements kelondroCloneableIterator<indexContainer> {
|
2006-07-03 17:14:54 +02:00
|
|
|
|
2007-12-28 04:39:36 +01:00
|
|
|
private Iterator<Object[]> wci;
|
2007-03-08 17:15:40 +01:00
|
|
|
private boolean rot;
|
2006-07-03 17:14:54 +02:00
|
|
|
|
2006-07-26 13:21:51 +02:00
|
|
|
public wordContainersIterator(String startWordHash, boolean rot) {
|
2007-03-08 17:15:40 +01:00
|
|
|
this.rot = rot;
|
2007-03-08 23:07:17 +01:00
|
|
|
this.wci = collectionIndex.keycollections(startWordHash.getBytes(), kelondroBase64Order.zero(startWordHash.length()), rot);
|
2007-03-08 17:15:40 +01:00
|
|
|
}
|
|
|
|
|
2007-12-27 18:56:59 +01:00
|
|
|
public wordContainersIterator clone(Object secondWordHash) {
|
2007-03-08 23:07:17 +01:00
|
|
|
return new wordContainersIterator((String) secondWordHash, rot);
|
2006-07-03 17:14:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasNext() {
|
2006-08-05 21:18:33 +02:00
|
|
|
return wci.hasNext();
|
2006-07-03 17:14:54 +02:00
|
|
|
}
|
|
|
|
|
2007-12-28 04:39:36 +01:00
|
|
|
public indexContainer next() {
|
2006-08-05 21:18:33 +02:00
|
|
|
Object[] oo = (Object[]) wci.next();
|
2007-04-25 07:22:09 +02:00
|
|
|
if (oo == null) return null;
|
2006-08-05 21:18:33 +02:00
|
|
|
byte[] key = (byte[]) oo[0];
|
|
|
|
kelondroRowSet collection = (kelondroRowSet) oo[1];
|
|
|
|
if (collection == null) return null;
|
2006-12-05 03:47:51 +01:00
|
|
|
return new indexContainer(new String(key), collection);
|
2006-07-03 17:14:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void remove() {
|
2006-08-05 21:18:33 +02:00
|
|
|
wci.remove();
|
2006-07-03 17:14:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2007-01-08 14:13:30 +01:00
|
|
|
|
2007-11-12 02:14:51 +01:00
|
|
|
public boolean hasContainer(String wordHash) {
|
2007-01-08 14:13:30 +01:00
|
|
|
try {
|
|
|
|
return collectionIndex.has(wordHash.getBytes());
|
|
|
|
} catch (IOException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-28 04:39:36 +01:00
|
|
|
public indexContainer getContainer(String wordHash, Set<String> urlselection) {
|
2006-07-04 16:47:27 +02:00
|
|
|
try {
|
2006-12-06 13:51:46 +01:00
|
|
|
kelondroRowSet collection = collectionIndex.get(wordHash.getBytes());
|
2006-09-11 13:12:42 +02:00
|
|
|
if (collection != null) collection.select(urlselection);
|
2006-09-11 00:36:47 +02:00
|
|
|
if ((collection == null) || (collection.size() == 0)) return null;
|
2006-12-05 03:47:51 +01:00
|
|
|
return new indexContainer(wordHash, collection);
|
2006-07-04 16:47:27 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
return null;
|
|
|
|
}
|
2006-07-03 00:24:13 +02:00
|
|
|
}
|
|
|
|
|
2007-11-12 02:14:51 +01:00
|
|
|
public indexContainer deleteContainer(String wordHash) {
|
2006-07-04 16:47:27 +02:00
|
|
|
try {
|
2006-08-05 21:18:33 +02:00
|
|
|
kelondroRowSet collection = collectionIndex.delete(wordHash.getBytes());
|
|
|
|
if (collection == null) return null;
|
2006-12-05 03:47:51 +01:00
|
|
|
return new indexContainer(wordHash, collection);
|
2006-07-04 16:47:27 +02:00
|
|
|
} catch (IOException e) {
|
2006-08-05 21:18:33 +02:00
|
|
|
return null;
|
2006-07-04 16:47:27 +02:00
|
|
|
}
|
2006-07-03 00:24:13 +02:00
|
|
|
}
|
|
|
|
|
2007-11-12 02:14:51 +01:00
|
|
|
public boolean removeEntry(String wordHash, String urlHash) {
|
2007-12-28 04:39:36 +01:00
|
|
|
HashSet<String> hs = new HashSet<String>();
|
|
|
|
hs.add(urlHash);
|
2006-12-06 13:51:46 +01:00
|
|
|
return removeEntries(wordHash, hs) == 1;
|
2006-08-01 12:30:55 +02:00
|
|
|
}
|
|
|
|
|
2007-12-28 04:39:36 +01:00
|
|
|
public int removeEntries(String wordHash, Set<String> urlHashes) {
|
2006-08-05 21:18:33 +02:00
|
|
|
try {
|
2006-12-06 13:51:46 +01:00
|
|
|
return collectionIndex.remove(wordHash.getBytes(), urlHashes);
|
2006-08-05 21:18:33 +02:00
|
|
|
} catch (kelondroOutOfLimitsException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return 0;
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return 0;
|
|
|
|
}
|
2006-07-03 00:24:13 +02:00
|
|
|
}
|
|
|
|
|
2008-02-03 00:53:39 +01:00
|
|
|
public void addEntries(indexContainer newEntries) {
|
2006-12-07 03:40:57 +01:00
|
|
|
try {
|
2007-02-26 16:49:23 +01:00
|
|
|
collectionIndex.merge(newEntries);
|
2006-12-07 03:40:57 +01:00
|
|
|
} catch (kelondroOutOfLimitsException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2006-10-28 02:22:10 +02:00
|
|
|
}
|
2007-02-26 16:49:23 +01:00
|
|
|
|
2007-11-12 02:14:51 +01:00
|
|
|
public void close() {
|
2007-06-26 16:37:10 +02:00
|
|
|
collectionIndex.close();
|
2006-07-03 00:24:13 +02:00
|
|
|
}
|
2006-08-01 12:30:55 +02:00
|
|
|
|
2006-07-03 00:24:13 +02:00
|
|
|
}
|