You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
407 lines
15 KiB
407 lines
15 KiB
16 years ago
|
// ReferenceContainerCache.java
|
||
16 years ago
|
// (C) 2008 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
|
||
|
// first published 30.03.2008 on http://yacy.net
|
||
|
//
|
||
|
// This is a part of YaCy, a peer-to-peer based web search engine
|
||
|
//
|
||
16 years ago
|
// $LastChangedDate: 2009-10-10 01:32:08 +0200 (Sa, 10 Okt 2009) $
|
||
|
// $LastChangedRevision: 6393 $
|
||
|
// $LastChangedBy: orbiter $
|
||
16 years ago
|
//
|
||
|
// 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
|
||
|
|
||
16 years ago
|
package net.yacy.kelondro.rwi;
|
||
16 years ago
|
|
||
|
import java.io.File;
|
||
|
import java.io.IOException;
|
||
16 years ago
|
import java.util.Arrays;
|
||
|
import java.util.Comparator;
|
||
16 years ago
|
import java.util.Iterator;
|
||
|
import java.util.Map;
|
||
|
import java.util.Set;
|
||
16 years ago
|
import java.util.concurrent.ConcurrentHashMap;
|
||
16 years ago
|
|
||
16 years ago
|
import net.yacy.kelondro.index.Row;
|
||
16 years ago
|
import net.yacy.kelondro.logging.Log;
|
||
16 years ago
|
import net.yacy.kelondro.order.Base64Order;
|
||
|
import net.yacy.kelondro.order.ByteOrder;
|
||
|
import net.yacy.kelondro.order.CloneableIterator;
|
||
16 years ago
|
|
||
16 years ago
|
import de.anomic.kelondro.blob.HeapWriter;
|
||
16 years ago
|
import de.anomic.kelondro.util.ByteArray;
|
||
16 years ago
|
import de.anomic.kelondro.util.FileUtils;
|
||
16 years ago
|
|
||
16 years ago
|
public final class ReferenceContainerCache<ReferenceType extends Reference> extends AbstractIndex<ReferenceType> implements Index<ReferenceType>, IndexReader<ReferenceType>, Iterable<ReferenceContainer<ReferenceType>> {
|
||
16 years ago
|
|
||
16 years ago
|
private final Row payloadrow;
|
||
16 years ago
|
protected final ByteOrder termOrder;
|
||
16 years ago
|
private final ContainerOrder<ReferenceType> containerOrder;
|
||
16 years ago
|
protected Map<ByteArray, ReferenceContainer<ReferenceType>> cache;
|
||
16 years ago
|
|
||
|
/**
|
||
|
* opens an existing heap file in undefined mode
|
||
|
* after this a initialization should be made to use the heap:
|
||
|
* either a read-only or read/write mode initialization
|
||
|
* @param payloadrow
|
||
|
* @param log
|
||
|
*/
|
||
16 years ago
|
public ReferenceContainerCache(final ReferenceFactory<ReferenceType> factory, final Row payloadrow, ByteOrder termOrder) {
|
||
|
super(factory);
|
||
16 years ago
|
this.payloadrow = payloadrow;
|
||
16 years ago
|
this.termOrder = termOrder;
|
||
16 years ago
|
this.containerOrder = new ContainerOrder<ReferenceType>(this.termOrder);
|
||
|
this.cache = new ConcurrentHashMap<ByteArray, ReferenceContainer<ReferenceType>>();
|
||
16 years ago
|
}
|
||
|
|
||
|
public Row rowdef() {
|
||
|
return this.payloadrow;
|
||
|
}
|
||
|
|
||
|
public void clear() {
|
||
|
if (cache != null) cache.clear();
|
||
|
}
|
||
|
|
||
|
public void close() {
|
||
|
this.cache = null;
|
||
|
}
|
||
|
|
||
16 years ago
|
public void dump(final File heapFile, int writeBuffer) {
|
||
16 years ago
|
assert this.cache != null;
|
||
|
Log.logInfo("indexContainerRAMHeap", "creating rwi heap dump '" + heapFile.getName() + "', " + cache.size() + " rwi's");
|
||
16 years ago
|
if (heapFile.exists()) FileUtils.deletedelete(heapFile);
|
||
16 years ago
|
File tmpFile = new File(heapFile.getParentFile(), heapFile.getName() + ".prt");
|
||
16 years ago
|
HeapWriter dump;
|
||
|
try {
|
||
16 years ago
|
dump = new HeapWriter(tmpFile, heapFile, payloadrow.primaryKeyLength, Base64Order.enhancedCoder, writeBuffer);
|
||
16 years ago
|
} catch (IOException e1) {
|
||
|
e1.printStackTrace();
|
||
|
return;
|
||
|
}
|
||
16 years ago
|
final long startTime = System.currentTimeMillis();
|
||
16 years ago
|
|
||
|
// sort the map
|
||
16 years ago
|
ReferenceContainer<ReferenceType>[] cachecopy = sortedClone();
|
||
16 years ago
|
|
||
|
// write wCache
|
||
16 years ago
|
long wordcount = 0, urlcount = 0;
|
||
16 years ago
|
byte[] wordHash = null, lwh;
|
||
16 years ago
|
for (final ReferenceContainer<ReferenceType> container: cachecopy) {
|
||
16 years ago
|
// get entries
|
||
|
lwh = wordHash;
|
||
16 years ago
|
wordHash = container.getTermHash();
|
||
16 years ago
|
|
||
|
// check consistency: entries must be ordered
|
||
|
assert (lwh == null || this.ordering().compare(wordHash, lwh) > 0);
|
||
|
|
||
|
// put entries on heap
|
||
|
if (container != null && wordHash.length == payloadrow.primaryKeyLength) {
|
||
|
//System.out.println("Dump: " + wordHash);
|
||
|
try {
|
||
|
dump.add(wordHash, container.exportCollection());
|
||
|
} catch (IOException e) {
|
||
|
e.printStackTrace();
|
||
16 years ago
|
}
|
||
16 years ago
|
urlcount += container.size();
|
||
16 years ago
|
}
|
||
16 years ago
|
wordcount++;
|
||
16 years ago
|
}
|
||
16 years ago
|
try {
|
||
16 years ago
|
dump.close(true);
|
||
16 years ago
|
Log.logInfo("indexContainerRAMHeap", "finished rwi heap dump: " + wordcount + " words, " + urlcount + " word/URL relations in " + (System.currentTimeMillis() - startTime) + " milliseconds");
|
||
|
} catch (IOException e) {
|
||
16 years ago
|
Log.logSevere("indexContainerRAMHeap", "failed rwi heap dump: " + e.getMessage(), e);
|
||
16 years ago
|
} finally {
|
||
|
dump = null;
|
||
|
}
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
@SuppressWarnings("unchecked")
|
||
|
public ReferenceContainer<ReferenceType>[] sortedClone() {
|
||
|
ReferenceContainer<ReferenceType>[] cachecopy = new ReferenceContainer[cache.size()];
|
||
|
synchronized (cache) {
|
||
|
int p = 0;
|
||
|
for (final Map.Entry<ByteArray, ReferenceContainer<ReferenceType>> entry: cache.entrySet()) {
|
||
|
cachecopy[p++] = entry.getValue();
|
||
|
}
|
||
|
}
|
||
|
Arrays.sort(cachecopy, this.containerOrder);
|
||
|
return cachecopy;
|
||
|
}
|
||
16 years ago
|
|
||
16 years ago
|
public int size() {
|
||
|
return (this.cache == null) ? 0 : this.cache.size();
|
||
|
}
|
||
|
|
||
16 years ago
|
|
||
16 years ago
|
|
||
16 years ago
|
public int maxReferences() {
|
||
16 years ago
|
// iterate to find the max score
|
||
|
int max = 0;
|
||
16 years ago
|
for (ReferenceContainer<ReferenceType> container : cache.values()) {
|
||
16 years ago
|
if (container.size() > max) max = container.size();
|
||
|
}
|
||
|
return max;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* return an iterator object that creates top-level-clones of the indexContainers
|
||
|
* in the cache, so that manipulations of the iterated objects do not change
|
||
|
* objects in the cache.
|
||
|
*/
|
||
16 years ago
|
public synchronized CloneableIterator<ReferenceContainer<ReferenceType>> references(final byte[] startWordHash, final boolean rot) {
|
||
16 years ago
|
return new heapCacheIterator(startWordHash, rot);
|
||
|
}
|
||
|
|
||
|
|
||
16 years ago
|
public Iterator<ReferenceContainer<ReferenceType>> iterator() {
|
||
16 years ago
|
return references(null, false);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* cache iterator: iterates objects within the heap cache. This can only be used
|
||
|
* for write-enabled heaps, read-only heaps do not have a heap cache
|
||
|
*/
|
||
16 years ago
|
public class heapCacheIterator implements CloneableIterator<ReferenceContainer<ReferenceType>>, Iterable<ReferenceContainer<ReferenceType>> {
|
||
16 years ago
|
|
||
|
// this class exists, because the wCache cannot be iterated with rotation
|
||
|
// and because every indexContainer Object that is iterated must be returned as top-level-clone
|
||
|
// so this class simulates wCache.tailMap(startWordHash).values().iterator()
|
||
|
// plus the mentioned features
|
||
|
|
||
|
private final boolean rot;
|
||
16 years ago
|
private ReferenceContainer<ReferenceType>[] cachecopy;
|
||
|
private int p;
|
||
16 years ago
|
private byte[] latestTermHash;
|
||
16 years ago
|
|
||
16 years ago
|
public heapCacheIterator(byte[] startWordHash, final boolean rot) {
|
||
16 years ago
|
this.rot = rot;
|
||
16 years ago
|
if (startWordHash != null && startWordHash.length == 0) startWordHash = null;
|
||
16 years ago
|
this.cachecopy = sortedClone();
|
||
|
this.p = 0;
|
||
16 years ago
|
if (startWordHash != null) {
|
||
|
while ( (this.p < this.cachecopy.length) &&
|
||
|
(termOrder.compare(this.cachecopy[this.p].getTermHash(), startWordHash) < 0)
|
||
|
) this.p++;
|
||
|
}
|
||
16 years ago
|
this.latestTermHash = null;
|
||
16 years ago
|
// The collection's iterator will return the values in the order that their corresponding keys appear in the tree.
|
||
|
}
|
||
|
|
||
|
public heapCacheIterator clone(final Object secondWordHash) {
|
||
16 years ago
|
return new heapCacheIterator((byte[]) secondWordHash, rot);
|
||
16 years ago
|
}
|
||
|
|
||
|
public boolean hasNext() {
|
||
16 years ago
|
if (rot) return this.cachecopy.length > 0;
|
||
16 years ago
|
return this.p < this.cachecopy.length;
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
public ReferenceContainer<ReferenceType> next() {
|
||
16 years ago
|
if (this.p < this.cachecopy.length) {
|
||
|
ReferenceContainer<ReferenceType> c = this.cachecopy[this.p++];
|
||
16 years ago
|
this.latestTermHash = c.getTermHash();
|
||
|
return c.topLevelClone();
|
||
16 years ago
|
}
|
||
|
// rotation iteration
|
||
|
if (!rot) {
|
||
|
return null;
|
||
|
}
|
||
16 years ago
|
if (this.cachecopy.length == 0) return null;
|
||
16 years ago
|
p = 0;
|
||
|
ReferenceContainer<ReferenceType> c = this.cachecopy[this.p++];
|
||
16 years ago
|
this.latestTermHash = c.getTermHash();
|
||
|
return c.topLevelClone();
|
||
16 years ago
|
}
|
||
|
|
||
|
public void remove() {
|
||
16 years ago
|
System.arraycopy(this.cachecopy, this.p, this.cachecopy, this.p - 1, this.cachecopy.length - p);
|
||
16 years ago
|
cache.remove(new ByteArray(this.latestTermHash));
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
public Iterator<ReferenceContainer<ReferenceType>> iterator() {
|
||
16 years ago
|
return this;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* test if a given key is in the heap
|
||
|
* this works with heaps in write- and read-mode
|
||
|
* @param key
|
||
|
* @return true, if the key is used in the heap; false othervise
|
||
|
*/
|
||
16 years ago
|
public boolean has(final byte[] key) {
|
||
16 years ago
|
return this.cache.containsKey(new ByteArray(key));
|
||
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* get a indexContainer from a heap
|
||
|
* @param key
|
||
|
* @return the indexContainer if one exist, null otherwise
|
||
|
*/
|
||
16 years ago
|
public ReferenceContainer<ReferenceType> get(final byte[] key, Set<String> urlselection) {
|
||
16 years ago
|
ReferenceContainer<ReferenceType> c = this.cache.get(new ByteArray(key));
|
||
16 years ago
|
if (urlselection == null) return c;
|
||
16 years ago
|
if (c == null) return null;
|
||
|
// because this is all in RAM, we must clone the entries (flat)
|
||
16 years ago
|
ReferenceContainer<ReferenceType> c1 = new ReferenceContainer<ReferenceType>(factory, c.getTermHash(), c.size());
|
||
16 years ago
|
Iterator<ReferenceType> e = c.entries();
|
||
|
ReferenceType ee;
|
||
16 years ago
|
while (e.hasNext()) {
|
||
|
ee = e.next();
|
||
16 years ago
|
if (urlselection.contains(ee.metadataHash())) c1.add(ee);
|
||
16 years ago
|
}
|
||
|
return c1;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* return the size of the container with corresponding key
|
||
|
* @param key
|
||
|
* @return
|
||
|
*/
|
||
16 years ago
|
public int count(final byte[] key) {
|
||
16 years ago
|
ReferenceContainer<ReferenceType> c = this.cache.get(new ByteArray(key));
|
||
16 years ago
|
if (c == null) return 0;
|
||
|
return c.size();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* delete a indexContainer from the heap cache. This can only be used for write-enabled heaps
|
||
|
* @param wordHash
|
||
|
* @return the indexContainer if the cache contained the container, null othervise
|
||
|
*/
|
||
16 years ago
|
public ReferenceContainer<ReferenceType> delete(final byte[] termHash) {
|
||
16 years ago
|
// returns the index that had been deleted
|
||
|
assert this.cache != null;
|
||
16 years ago
|
return cache.remove(new ByteArray(termHash));
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
public boolean remove(final byte[] termHash, final String urlHash) {
|
||
16 years ago
|
assert this.cache != null;
|
||
16 years ago
|
ByteArray tha = new ByteArray(termHash);
|
||
16 years ago
|
synchronized (cache) {
|
||
|
final ReferenceContainer<ReferenceType> c = cache.get(tha);
|
||
|
if ((c != null) && (c.remove(urlHash) != null)) {
|
||
|
// removal successful
|
||
|
if (c.size() == 0) {
|
||
|
delete(termHash);
|
||
|
} else {
|
||
|
cache.put(tha, c);
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
16 years ago
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
16 years ago
|
public int remove(final byte[] termHash, final Set<String> urlHashes) {
|
||
16 years ago
|
assert this.cache != null;
|
||
|
if (urlHashes.size() == 0) return 0;
|
||
16 years ago
|
ByteArray tha = new ByteArray(termHash);
|
||
16 years ago
|
int count;
|
||
16 years ago
|
synchronized (cache) {
|
||
|
final ReferenceContainer<ReferenceType> c = cache.get(tha);
|
||
|
if ((c != null) && ((count = c.removeEntries(urlHashes)) > 0)) {
|
||
|
// removal successful
|
||
|
if (c.size() == 0) {
|
||
|
delete(termHash);
|
||
|
} else {
|
||
|
cache.put(tha, c);
|
||
|
}
|
||
|
return count;
|
||
|
}
|
||
16 years ago
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
16 years ago
|
public void add(final ReferenceContainer<ReferenceType> container) {
|
||
16 years ago
|
// this puts the entries into the cache
|
||
16 years ago
|
assert this.cache != null;
|
||
16 years ago
|
if (this.cache == null || container == null || container.size() == 0) return;
|
||
16 years ago
|
|
||
|
// put new words into cache
|
||
16 years ago
|
ByteArray tha = new ByteArray(container.getTermHash());
|
||
16 years ago
|
int added = 0;
|
||
|
synchronized (cache) {
|
||
16 years ago
|
ReferenceContainer<ReferenceType> entries = cache.get(tha); // null pointer exception? wordhash != null! must be cache==null
|
||
|
if (entries == null) {
|
||
|
entries = container.topLevelClone();
|
||
|
added = entries.size();
|
||
|
} else {
|
||
|
added = entries.putAllRecent(container);
|
||
|
}
|
||
|
if (added > 0) {
|
||
|
cache.put(tha, entries);
|
||
|
}
|
||
|
entries = null;
|
||
|
return;
|
||
16 years ago
|
}
|
||
|
}
|
||
|
|
||
16 years ago
|
public void add(final byte[] termHash, final ReferenceType newEntry) {
|
||
16 years ago
|
assert this.cache != null;
|
||
16 years ago
|
ByteArray tha = new ByteArray(termHash);
|
||
16 years ago
|
|
||
16 years ago
|
// first access the cache without synchronization
|
||
|
ReferenceContainer<ReferenceType> container = cache.remove(tha);
|
||
16 years ago
|
if (container == null) container = new ReferenceContainer<ReferenceType>(factory, termHash, 1);
|
||
16 years ago
|
container.put(newEntry);
|
||
|
|
||
16 years ago
|
// synchronization: check if the entry is still empty and set new value
|
||
16 years ago
|
synchronized (cache) {
|
||
16 years ago
|
ReferenceContainer<ReferenceType> containerNew = cache.put(tha, container);
|
||
|
if (containerNew == null) return;
|
||
16 years ago
|
if (container == containerNew) {
|
||
|
// The containers are the same, so nothing needs to be done
|
||
|
return;
|
||
|
}
|
||
16 years ago
|
// Now merge the smaller container into the lager.
|
||
|
// The other way around can become very slow
|
||
|
if (container.size() >= containerNew.size()) {
|
||
|
container.putAllRecent(containerNew);
|
||
|
cache.put(tha, container);
|
||
|
} else {
|
||
|
containerNew.putAllRecent(container);
|
||
|
cache.put(tha, containerNew);
|
||
|
}
|
||
16 years ago
|
}
|
||
16 years ago
|
}
|
||
|
|
||
|
public int minMem() {
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
public ByteOrder ordering() {
|
||
16 years ago
|
return this.termOrder;
|
||
16 years ago
|
}
|
||
16 years ago
|
|
||
16 years ago
|
public static class ContainerOrder<ReferenceType extends Reference> implements Comparator<ReferenceContainer<ReferenceType>> {
|
||
|
private ByteOrder o;
|
||
|
public ContainerOrder(ByteOrder order) {
|
||
|
this.o = order;
|
||
|
}
|
||
|
public int compare(ReferenceContainer<ReferenceType> arg0, ReferenceContainer<ReferenceType> arg1) {
|
||
|
if (arg0 == arg1) return 0;
|
||
|
if (arg0 == null) return -1;
|
||
|
if (arg1 == null) return 1;
|
||
|
return o.compare(arg0.getTermHash(), arg1.getTermHash());
|
||
|
}
|
||
|
}
|
||
|
|
||
16 years ago
|
}
|