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-11-03 00:30:09 +01:00
|
|
|
public indexCollectionRI(final File path, final String filenameStub, final int maxpartition, final kelondroRow payloadrow, boolean useCommons) {
|
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,
|
2008-11-03 00:30:09 +01:00
|
|
|
payloadrow,
|
|
|
|
useCommons);
|
2008-08-02 14:12:04 +02:00
|
|
|
} catch (final IOException e) {
|
2006-08-06 00:22:14 +02:00
|
|
|
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();
|
2008-08-02 14:12:04 +02:00
|
|
|
} catch (final IOException e) {
|
2008-05-24 14:30:50 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-11 18:01:18 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
public synchronized kelondroCloneableIterator<indexContainer> wordContainers(final String startWordHash, final 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
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
private final Iterator<Object[]> wci;
|
|
|
|
private final boolean rot;
|
2006-07-03 17:14:54 +02:00
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
public wordContainersIterator(final String startWordHash, final 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
|
|
|
}
|
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
public wordContainersIterator clone(final 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() {
|
2008-08-02 14:12:04 +02:00
|
|
|
final Object[] oo = wci.next();
|
2007-04-25 07:22:09 +02:00
|
|
|
if (oo == null) return null;
|
2008-08-02 14:12:04 +02:00
|
|
|
final byte[] key = (byte[]) oo[0];
|
|
|
|
final kelondroRowSet collection = (kelondroRowSet) oo[1];
|
2006-08-05 21:18:33 +02:00
|
|
|
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
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
public boolean hasContainer(final String wordHash) {
|
2008-08-02 15:57:00 +02:00
|
|
|
return collectionIndex.has(wordHash.getBytes());
|
2007-01-08 14:13:30 +01:00
|
|
|
}
|
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
public indexContainer getContainer(final String wordHash, final Set<String> urlselection) {
|
2006-07-04 16:47:27 +02:00
|
|
|
try {
|
2008-08-02 14:12:04 +02:00
|
|
|
final 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);
|
2008-08-02 14:12:04 +02:00
|
|
|
} catch (final IOException e) {
|
2006-07-04 16:47:27 +02:00
|
|
|
return null;
|
|
|
|
}
|
2006-07-03 00:24:13 +02:00
|
|
|
}
|
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
public indexContainer deleteContainer(final String wordHash) {
|
2006-07-04 16:47:27 +02:00
|
|
|
try {
|
2008-08-02 14:12:04 +02:00
|
|
|
final kelondroRowSet collection = collectionIndex.delete(wordHash.getBytes());
|
2006-08-05 21:18:33 +02:00
|
|
|
if (collection == null) return null;
|
2006-12-05 03:47:51 +01:00
|
|
|
return new indexContainer(wordHash, collection);
|
2008-08-02 14:12:04 +02:00
|
|
|
} catch (final 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
|
|
|
}
|
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
public boolean removeEntry(final String wordHash, final String urlHash) {
|
|
|
|
final HashSet<String> hs = new HashSet<String>();
|
2007-12-28 04:39:36 +01:00
|
|
|
hs.add(urlHash);
|
2006-12-06 13:51:46 +01:00
|
|
|
return removeEntries(wordHash, hs) == 1;
|
2006-08-01 12:30:55 +02:00
|
|
|
}
|
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
public int removeEntries(final String wordHash, final 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);
|
2008-08-02 14:12:04 +02:00
|
|
|
} catch (final kelondroOutOfLimitsException e) {
|
2006-08-05 21:18:33 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
return 0;
|
2008-08-02 14:12:04 +02:00
|
|
|
} catch (final IOException e) {
|
2006-08-05 21:18:33 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
return 0;
|
|
|
|
}
|
2006-07-03 00:24:13 +02:00
|
|
|
}
|
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
public void addEntries(final indexContainer newEntries) {
|
2009-01-06 14:51:59 +01:00
|
|
|
if (newEntries == null) return;
|
2006-12-07 03:40:57 +01:00
|
|
|
try {
|
2007-02-26 16:49:23 +01:00
|
|
|
collectionIndex.merge(newEntries);
|
2008-08-02 14:12:04 +02:00
|
|
|
} catch (final kelondroOutOfLimitsException e) {
|
2006-12-07 03:40:57 +01:00
|
|
|
e.printStackTrace();
|
2008-08-02 14:12:04 +02:00
|
|
|
} catch (final IOException e) {
|
2006-12-07 03:40:57 +01:00
|
|
|
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
|
|
|
}
|