mirror of
https://github.com/yacy/yacy_search_server.git
synced 2024-09-19 00:01:41 +02:00
strong speed enhancements for the index cache dump and restore:
storage and loading is 30 times faster! a cache of 100000 RWIs needed 180 seconds to store and 100 seconds to restore; now the same cache needs only 6 seconds to store and 3 seconds to restore. The cache size has decreased now by 30% (95 MB instead of 150 MB). git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@4634 6c8d7289-2bf4-0310-a012-ef5d649a1542
This commit is contained in:
parent
442204a1c8
commit
783a4c9edb
|
@ -28,320 +28,117 @@ package de.anomic.index;
|
|||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import de.anomic.kelondro.kelondroByteOrder;
|
||||
import de.anomic.kelondro.kelondroCloneableIterator;
|
||||
import de.anomic.kelondro.kelondroBytesIntMap;
|
||||
import de.anomic.kelondro.kelondroRow;
|
||||
import de.anomic.kelondro.kelondroRowSet;
|
||||
import de.anomic.server.serverMemory;
|
||||
import de.anomic.server.logging.serverLog;
|
||||
|
||||
public final class indexContainerHeap {
|
||||
|
||||
// class variables
|
||||
private final File databaseRoot;
|
||||
protected final SortedMap<String, indexContainer> cache; // wordhash-container
|
||||
private final serverLog log;
|
||||
private String indexArrayFileName;
|
||||
private kelondroRow payloadrow;
|
||||
|
||||
|
||||
public indexContainerHeap(File databaseRoot, kelondroRow payloadrow, String dumpname, serverLog log) {
|
||||
|
||||
// creates a new index cache
|
||||
// the cache has a back-end where indexes that do not fit in the cache are flushed
|
||||
this.databaseRoot = databaseRoot;
|
||||
this.cache = Collections.synchronizedSortedMap(new TreeMap<String, indexContainer>(new kelondroByteOrder.StringOrder(payloadrow.getOrdering())));
|
||||
this.log = log;
|
||||
this.indexArrayFileName = dumpname;
|
||||
this.payloadrow = payloadrow;
|
||||
|
||||
// read in dump of last session
|
||||
try {
|
||||
restore();
|
||||
} catch (IOException e){
|
||||
log.logSevere("unable to restore cache dump: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private void dump() throws IOException {
|
||||
log.logConfig("creating dump for index cache '" + indexArrayFileName + "', " + cache.size() + " words (and much more urls)");
|
||||
File indexDumpFile = new File(databaseRoot, indexArrayFileName);
|
||||
if (indexDumpFile.exists()) indexDumpFile.delete();
|
||||
OutputStream os = new BufferedOutputStream(new FileOutputStream(indexDumpFile));
|
||||
|
||||
public static void dumpHeap(File indexHeapFile, kelondroRow payloadrow, SortedMap<String, indexContainer> cache, serverLog log) throws IOException {
|
||||
if (log != null) log.logInfo("creating rwi heap dump '" + indexHeapFile.getName() + "', " + cache.size() + " rwi's");
|
||||
if (indexHeapFile.exists()) indexHeapFile.delete();
|
||||
OutputStream os = new BufferedOutputStream(new FileOutputStream(indexHeapFile), 64 * 1024);
|
||||
long startTime = System.currentTimeMillis();
|
||||
long messageTime = System.currentTimeMillis() + 5000;
|
||||
long wordsPerSecond = 0, wordcount = 0, urlcount = 0;
|
||||
Map.Entry<String, indexContainer> entry;
|
||||
long wordcount = 0, urlcount = 0;
|
||||
String wordHash;
|
||||
indexContainer container;
|
||||
|
||||
// write wCache
|
||||
synchronized (cache) {
|
||||
Iterator<Map.Entry<String, indexContainer>> i = cache.entrySet().iterator();
|
||||
while (i.hasNext()) {
|
||||
for (Map.Entry<String, indexContainer> entry: cache.entrySet()) {
|
||||
// get entries
|
||||
entry = i.next();
|
||||
wordHash = entry.getKey();
|
||||
container = entry.getValue();
|
||||
|
||||
// put entries on stack
|
||||
// put entries on heap
|
||||
if (container != null) {
|
||||
os.write(wordHash.getBytes());
|
||||
if (wordHash.length() < payloadrow.primaryKeyLength) {
|
||||
for (int i = 0; i < payloadrow.primaryKeyLength - wordHash.length(); i++) os.write(0);
|
||||
}
|
||||
os.write(container.exportCollection());
|
||||
}
|
||||
wordcount++;
|
||||
i.remove(); // free some mem
|
||||
|
||||
// write a log
|
||||
if (System.currentTimeMillis() > messageTime) {
|
||||
serverMemory.gc(1000, "indexRAMRI, for better statistic-1"); // for better statistic - thq
|
||||
wordsPerSecond = wordcount * 1000
|
||||
/ (1 + System.currentTimeMillis() - startTime);
|
||||
log.logInfo("dump status: " + wordcount
|
||||
+ " words done, "
|
||||
+ (cache.size() / (wordsPerSecond + 1))
|
||||
+ " seconds remaining, free mem = "
|
||||
+ (serverMemory.free() / 1024 / 1024)
|
||||
+ "MB");
|
||||
messageTime = System.currentTimeMillis() + 5000;
|
||||
}
|
||||
urlcount += container.size();
|
||||
}
|
||||
}
|
||||
os.flush();
|
||||
os.close();
|
||||
log.logInfo("finished dump of ram cache: " + urlcount + " word/URL relations in " + ((System.currentTimeMillis() - startTime) / 1000) + " seconds");
|
||||
if (log != null) log.logInfo("finished rwi heap dump: " + wordcount + " words, " + urlcount + " word/URL relations in " + ((System.currentTimeMillis() - startTime) / 1000) + " seconds");
|
||||
}
|
||||
|
||||
private void restore() throws IOException {
|
||||
File indexDumpFile = new File(databaseRoot, indexArrayFileName);
|
||||
if (!(indexDumpFile.exists())) return;
|
||||
|
||||
InputStream is = new BufferedInputStream(new FileInputStream(indexDumpFile));
|
||||
public static SortedMap<String, indexContainer> restoreHeap(File indexHeapFile, kelondroRow payloadrow, serverLog log) throws IOException {
|
||||
if (!(indexHeapFile.exists())) throw new IOException("file " + indexHeapFile + " does not exist");
|
||||
if (log != null) log.logInfo("restoring dump for rwi heap '" + indexHeapFile.getName() + "'");
|
||||
|
||||
long messageTime = System.currentTimeMillis() + 5000;
|
||||
long wordCount = 0;
|
||||
synchronized (cache) {
|
||||
String wordHash;
|
||||
byte[] word = new byte[12];
|
||||
while (is.available() > 0) {
|
||||
// read word
|
||||
is.read(word);
|
||||
wordHash = new String(word);
|
||||
// read collection
|
||||
|
||||
indexContainer container = new indexContainer(wordHash, kelondroRowSet.importRowSet(is, payloadrow));
|
||||
cache.put(wordHash, container);
|
||||
wordCount++;
|
||||
// protect against memory shortage
|
||||
//while (serverMemory.free() < 1000000) {flushFromMem(); java.lang.System.gc();}
|
||||
// write a log
|
||||
if (System.currentTimeMillis() > messageTime) {
|
||||
serverMemory.gc(1000, "indexRAMRI, for better statistic-2"); // for better statistic - thq
|
||||
log.logInfo("restoring status: " + wordCount + " words done, free mem = " + (serverMemory.free() / 1024 / 1024) + "MB");
|
||||
messageTime = System.currentTimeMillis() + 5000;
|
||||
}
|
||||
}
|
||||
long start = System.currentTimeMillis();
|
||||
SortedMap<String, indexContainer> cache = Collections.synchronizedSortedMap(new TreeMap<String, indexContainer>(new kelondroByteOrder.StringOrder(payloadrow.getOrdering())));
|
||||
DataInputStream is = new DataInputStream(new BufferedInputStream(new FileInputStream(indexHeapFile), 64*1024));
|
||||
|
||||
long urlCount = 0;
|
||||
String wordHash;
|
||||
byte[] word = new byte[payloadrow.primaryKeyLength];
|
||||
while (is.available() > 0) {
|
||||
// read word
|
||||
is.read(word);
|
||||
wordHash = new String(word);
|
||||
// read collection
|
||||
|
||||
indexContainer container = new indexContainer(wordHash, kelondroRowSet.importRowSet(is, payloadrow));
|
||||
cache.put(wordHash, container);
|
||||
urlCount += container.size();
|
||||
}
|
||||
is.close();
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return cache.size();
|
||||
}
|
||||
|
||||
public synchronized int indexSize(String wordHash) {
|
||||
indexContainer cacheIndex = (indexContainer) cache.get(wordHash);
|
||||
if (cacheIndex == null) return 0;
|
||||
return cacheIndex.size();
|
||||
}
|
||||
|
||||
public synchronized kelondroCloneableIterator<indexContainer> wordContainers(String startWordHash, boolean rot) {
|
||||
// we 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.
|
||||
return new wordContainerIterator(startWordHash, rot);
|
||||
}
|
||||
|
||||
public class wordContainerIterator implements kelondroCloneableIterator<indexContainer> {
|
||||
|
||||
// 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 boolean rot;
|
||||
private Iterator<indexContainer> iterator;
|
||||
|
||||
public wordContainerIterator(String startWordHash, boolean rot) {
|
||||
this.rot = rot;
|
||||
this.iterator = (startWordHash == null) ? cache.values().iterator() : cache.tailMap(startWordHash).values().iterator();
|
||||
// The collection's iterator will return the values in the order that their corresponding keys appear in the tree.
|
||||
}
|
||||
|
||||
public wordContainerIterator clone(Object secondWordHash) {
|
||||
return new wordContainerIterator((String) secondWordHash, rot);
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
if (rot) return true;
|
||||
return iterator.hasNext();
|
||||
}
|
||||
|
||||
public indexContainer next() {
|
||||
if (iterator.hasNext()) {
|
||||
return ((indexContainer) iterator.next()).topLevelClone();
|
||||
} else {
|
||||
// rotation iteration
|
||||
if (rot) {
|
||||
iterator = cache.values().iterator();
|
||||
return ((indexContainer) iterator.next()).topLevelClone();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
iterator.remove();
|
||||
}
|
||||
|
||||
if (log != null) log.logInfo("finished rwi heap restore: " + cache.size() + " words, " + urlCount + " word/URL relations in " + ((System.currentTimeMillis() - start) / 1000) + " seconds");
|
||||
return cache;
|
||||
}
|
||||
|
||||
public boolean hasContainer(String wordHash) {
|
||||
return cache.containsKey(wordHash);
|
||||
}
|
||||
|
||||
public int sizeContainer(String wordHash) {
|
||||
indexContainer c = (indexContainer) cache.get(wordHash);
|
||||
return (c == null) ? 0 : c.size();
|
||||
}
|
||||
|
||||
public synchronized indexContainer getContainer(String wordHash, Set<String> urlselection) {
|
||||
if (wordHash == null) return null;
|
||||
public static kelondroBytesIntMap indexHeap(File indexHeapFile, kelondroRow payloadrow, serverLog log) throws IOException {
|
||||
if (!(indexHeapFile.exists())) throw new IOException("file " + indexHeapFile + " does not exist");
|
||||
if (indexHeapFile.length() >= (long) Integer.MAX_VALUE) throw new IOException("file " + indexHeapFile + " too large, index can only be crated for files less than 2GB");
|
||||
if (log != null) log.logInfo("creating index for rwi heap '" + indexHeapFile.getName() + "'");
|
||||
|
||||
// retrieve container
|
||||
indexContainer container = (indexContainer) cache.get(wordHash);
|
||||
long start = System.currentTimeMillis();
|
||||
kelondroBytesIntMap index = new kelondroBytesIntMap(payloadrow.primaryKeyLength, (kelondroByteOrder) payloadrow.getOrdering(), 0);
|
||||
DataInputStream is = new DataInputStream(new BufferedInputStream(new FileInputStream(indexHeapFile), 64*1024));
|
||||
|
||||
// We must not use the container from cache to store everything we find,
|
||||
// as that container remains linked to in the cache and might be changed later
|
||||
// while the returned container is still in use.
|
||||
// create a clone from the container
|
||||
if (container != null) container = container.topLevelClone();
|
||||
|
||||
// select the urlselection
|
||||
if ((urlselection != null) && (container != null)) container.select(urlselection);
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
public synchronized indexContainer deleteContainer(String wordHash) {
|
||||
// returns the index that had been deleted
|
||||
indexContainer container = (indexContainer) cache.remove(wordHash);
|
||||
return container;
|
||||
}
|
||||
|
||||
public synchronized boolean removeEntry(String wordHash, String urlHash) {
|
||||
indexContainer c = (indexContainer) cache.get(wordHash);
|
||||
if ((c != null) && (c.remove(urlHash) != null)) {
|
||||
// removal successful
|
||||
if (c.size() == 0) {
|
||||
deleteContainer(wordHash);
|
||||
} else {
|
||||
cache.put(wordHash, c);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public synchronized int removeEntries(String wordHash, Set<String> urlHashes) {
|
||||
if (urlHashes.size() == 0) return 0;
|
||||
indexContainer c = (indexContainer) cache.get(wordHash);
|
||||
int count;
|
||||
if ((c != null) && ((count = c.removeEntries(urlHashes)) > 0)) {
|
||||
// removal successful
|
||||
if (c.size() == 0) {
|
||||
deleteContainer(wordHash);
|
||||
} else {
|
||||
cache.put(wordHash, c);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public synchronized int tryRemoveURLs(String urlHash) {
|
||||
// this tries to delete an index from the cache that has this
|
||||
// urlHash assigned. This can only work if the entry is really fresh
|
||||
// Such entries must be searched in the latest entries
|
||||
int delCount = 0;
|
||||
Iterator<Map.Entry<String, indexContainer>> i = cache.entrySet().iterator();
|
||||
Map.Entry<String, indexContainer> entry;
|
||||
String wordhash;
|
||||
indexContainer c;
|
||||
while (i.hasNext()) {
|
||||
entry = i.next();
|
||||
wordhash = entry.getKey();
|
||||
long urlCount = 0;
|
||||
String wordHash;
|
||||
byte[] word = new byte[payloadrow.primaryKeyLength];
|
||||
int seek = 0, seek0;
|
||||
while (is.available() > 0) {
|
||||
// remember seek position
|
||||
seek0 = seek;
|
||||
|
||||
// get container
|
||||
c = entry.getValue();
|
||||
if (c.remove(urlHash) != null) {
|
||||
if (c.size() == 0) {
|
||||
i.remove();
|
||||
} else {
|
||||
cache.put(wordhash, c); // superfluous?
|
||||
}
|
||||
delCount++;
|
||||
}
|
||||
}
|
||||
return delCount;
|
||||
}
|
||||
|
||||
public synchronized void addEntries(indexContainer container) {
|
||||
// this puts the entries into the cache, not into the assortment directly
|
||||
int added = 0;
|
||||
if ((container == null) || (container.size() == 0)) return;
|
||||
|
||||
// put new words into cache
|
||||
String wordHash = container.getWordHash();
|
||||
indexContainer entries = (indexContainer) cache.get(wordHash); // 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(wordHash, entries);
|
||||
}
|
||||
entries = null;
|
||||
}
|
||||
|
||||
public synchronized void addEntry(String wordHash, indexRWIRowEntry newEntry, long updateTime, boolean dhtCase) {
|
||||
indexContainer container = (indexContainer) cache.get(wordHash);
|
||||
if (container == null) container = new indexContainer(wordHash, this.payloadrow, 1);
|
||||
container.put(newEntry);
|
||||
cache.put(wordHash, container);
|
||||
}
|
||||
|
||||
public synchronized void close() {
|
||||
// dump cache
|
||||
try {
|
||||
dump();
|
||||
} catch (IOException e){
|
||||
log.logSevere("unable to dump cache: " + e.getMessage(), e);
|
||||
// read word
|
||||
is.read(word);
|
||||
wordHash = new String(word);
|
||||
seek += wordHash.length();
|
||||
|
||||
// read collection
|
||||
seek += kelondroRowSet.skipNextRowSet(is, payloadrow);
|
||||
index.addi(word, seek0);
|
||||
}
|
||||
is.close();
|
||||
if (log != null) log.logInfo("finished rwi heap indexing: " + urlCount + " word/URL relations in " + ((System.currentTimeMillis() - start) / 1000) + " seconds");
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,24 +37,21 @@ import java.util.TreeMap;
|
|||
|
||||
import de.anomic.kelondro.kelondroBase64Order;
|
||||
import de.anomic.kelondro.kelondroBufferedRA;
|
||||
import de.anomic.kelondro.kelondroByteOrder;
|
||||
import de.anomic.kelondro.kelondroCloneableIterator;
|
||||
import de.anomic.kelondro.kelondroException;
|
||||
import de.anomic.kelondro.kelondroFixedWidthArray;
|
||||
import de.anomic.kelondro.kelondroMScoreCluster;
|
||||
import de.anomic.kelondro.kelondroNaturalOrder;
|
||||
import de.anomic.kelondro.kelondroRow;
|
||||
import de.anomic.kelondro.kelondroRow.EntryIndex;
|
||||
import de.anomic.server.serverByteBuffer;
|
||||
import de.anomic.server.serverFileUtils;
|
||||
import de.anomic.server.serverMemory;
|
||||
import de.anomic.server.logging.serverLog;
|
||||
import de.anomic.yacy.yacySeedDB;
|
||||
|
||||
public final class indexRAMRI implements indexRI {
|
||||
public final class indexRAMRI implements indexRI, indexRIReader {
|
||||
|
||||
// class variables
|
||||
private final File databaseRoot;
|
||||
protected final SortedMap<String, indexContainer> cache; // wordhash-container
|
||||
protected SortedMap<String, indexContainer> cache; // wordhash-container
|
||||
private final kelondroMScoreCluster<String> hashScore;
|
||||
private final kelondroMScoreCluster<String> hashDate;
|
||||
private long initTime;
|
||||
|
@ -62,16 +59,14 @@ public final class indexRAMRI implements indexRI {
|
|||
public int cacheReferenceCountLimit;
|
||||
public long cacheReferenceAgeLimit;
|
||||
private final serverLog log;
|
||||
private String indexArrayFileName;
|
||||
private File indexArrayFile, indexHeapFile;
|
||||
private kelondroRow payloadrow;
|
||||
private kelondroRow bufferStructureBasis;
|
||||
|
||||
public indexRAMRI(File databaseRoot, kelondroRow payloadrow, int wCacheReferenceCountLimitInit, long wCacheReferenceAgeLimitInit, String dumpname, serverLog log) {
|
||||
public indexRAMRI(File databaseRoot, kelondroRow payloadrow, int wCacheReferenceCountLimitInit, long wCacheReferenceAgeLimitInit, String oldArrayName, String newHeapName, serverLog log) {
|
||||
|
||||
// creates a new index cache
|
||||
// the cache has a back-end where indexes that do not fit in the cache are flushed
|
||||
this.databaseRoot = databaseRoot;
|
||||
this.cache = Collections.synchronizedSortedMap(new TreeMap<String, indexContainer>());
|
||||
this.hashScore = new kelondroMScoreCluster<String>();
|
||||
this.hashDate = new kelondroMScoreCluster<String>();
|
||||
this.initTime = System.currentTimeMillis();
|
||||
|
@ -79,7 +74,8 @@ public final class indexRAMRI implements indexRI {
|
|||
this.cacheReferenceCountLimit = wCacheReferenceCountLimitInit;
|
||||
this.cacheReferenceAgeLimit = wCacheReferenceAgeLimitInit;
|
||||
this.log = log;
|
||||
this.indexArrayFileName = dumpname;
|
||||
this.indexArrayFile = new File(databaseRoot, oldArrayName);
|
||||
this.indexHeapFile = new File(databaseRoot, newHeapName);
|
||||
this.payloadrow = payloadrow;
|
||||
this.bufferStructureBasis = new kelondroRow(
|
||||
"byte[] wordhash-" + yacySeedDB.commonHashLength + ", " +
|
||||
|
@ -89,10 +85,25 @@ public final class indexRAMRI implements indexRI {
|
|||
kelondroBase64Order.enhancedCoder, 0);
|
||||
|
||||
// read in dump of last session
|
||||
try {
|
||||
restore();
|
||||
} catch (IOException e){
|
||||
log.logSevere("unable to restore cache dump: " + e.getMessage(), e);
|
||||
if (indexArrayFile.exists()) {
|
||||
this.cache = Collections.synchronizedSortedMap(new TreeMap<String, indexContainer>(new kelondroByteOrder.StringOrder(payloadrow.getOrdering())));
|
||||
try {
|
||||
restoreArray();
|
||||
} catch (IOException e){
|
||||
log.logSevere("unable to restore cache dump: " + e.getMessage(), e);
|
||||
}
|
||||
indexArrayFile.delete();
|
||||
if (indexArrayFile.exists()) log.logSevere("cannot delete old array file: " + indexArrayFile.toString() + "; please delete manually");
|
||||
} else if (indexHeapFile.exists()) {
|
||||
this.cache = null;
|
||||
try {
|
||||
//indexContainerHeap.indexHeap(this.indexHeapFile, payloadrow, log); // for testing
|
||||
this.cache = indexContainerHeap.restoreHeap(this.indexHeapFile, payloadrow, log);
|
||||
} catch (IOException e){
|
||||
log.logSevere("unable to restore cache dump: " + e.getMessage(), e);
|
||||
}
|
||||
} else {
|
||||
this.cache = Collections.synchronizedSortedMap(new TreeMap<String, indexContainer>(new kelondroByteOrder.StringOrder(payloadrow.getOrdering())));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -108,97 +119,12 @@ public final class indexRAMRI implements indexRI {
|
|||
return entries.updated();
|
||||
}
|
||||
|
||||
private void dump() throws IOException {
|
||||
log.logConfig("creating dump for index cache '" + indexArrayFileName + "', " + cache.size() + " words (and much more urls)");
|
||||
File indexDumpFile = new File(databaseRoot, indexArrayFileName);
|
||||
if (indexDumpFile.exists()) indexDumpFile.delete();
|
||||
kelondroFixedWidthArray dumpArray = null;
|
||||
kelondroBufferedRA writeBuffer = null;
|
||||
if (false /*serverMemory.available() > 50 * bufferStructureBasis.objectsize() * cache.size()*/) {
|
||||
writeBuffer = new kelondroBufferedRA();
|
||||
dumpArray = new kelondroFixedWidthArray(writeBuffer, indexDumpFile.getCanonicalPath(), bufferStructureBasis, 0);
|
||||
log.logInfo("started dump of ram cache: " + cache.size() + " words; memory-enhanced write");
|
||||
} else {
|
||||
dumpArray = new kelondroFixedWidthArray(indexDumpFile, bufferStructureBasis, 0);
|
||||
log.logInfo("started dump of ram cache: " + cache.size() + " words; low-memory write");
|
||||
}
|
||||
long startTime = System.currentTimeMillis();
|
||||
long messageTime = System.currentTimeMillis() + 5000;
|
||||
long wordsPerSecond = 0, wordcount = 0, urlcount = 0;
|
||||
Map.Entry<String, indexContainer> entry;
|
||||
String wordHash;
|
||||
indexContainer container;
|
||||
long updateTime;
|
||||
indexRWIRowEntry iEntry;
|
||||
kelondroRow.Entry row = dumpArray.row().newEntry();
|
||||
byte[] occ, time;
|
||||
|
||||
// write wCache
|
||||
synchronized (cache) {
|
||||
Iterator<Map.Entry<String, indexContainer>> i = cache.entrySet().iterator();
|
||||
while (i.hasNext()) {
|
||||
// get entries
|
||||
entry = i.next();
|
||||
wordHash = entry.getKey();
|
||||
updateTime = getUpdateTime(wordHash);
|
||||
container = entry.getValue();
|
||||
|
||||
// put entries on stack
|
||||
if (container != null) {
|
||||
Iterator<indexRWIRowEntry> ci = container.entries();
|
||||
occ = kelondroNaturalOrder.encodeLong(container.size(), 4);
|
||||
time = kelondroNaturalOrder.encodeLong(updateTime, 8);
|
||||
while (ci.hasNext()) {
|
||||
iEntry = ci.next();
|
||||
row.setCol(0, wordHash.getBytes());
|
||||
row.setCol(1, occ);
|
||||
row.setCol(2, time);
|
||||
row.setCol(3, iEntry.toKelondroEntry().bytes());
|
||||
dumpArray.set((int) urlcount++, row);
|
||||
}
|
||||
}
|
||||
wordcount++;
|
||||
i.remove(); // free some mem
|
||||
|
||||
// write a log
|
||||
if (System.currentTimeMillis() > messageTime) {
|
||||
serverMemory.gc(1000, "indexRAMRI, for better statistic-1"); // for better statistic - thq
|
||||
wordsPerSecond = wordcount * 1000
|
||||
/ (1 + System.currentTimeMillis() - startTime);
|
||||
log.logInfo("dump status: " + wordcount
|
||||
+ " words done, "
|
||||
+ (cache.size() / (wordsPerSecond + 1))
|
||||
+ " seconds remaining, free mem = "
|
||||
+ (serverMemory.free() / 1024 / 1024)
|
||||
+ "MB");
|
||||
messageTime = System.currentTimeMillis() + 5000;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (writeBuffer != null) {
|
||||
serverByteBuffer bb = writeBuffer.getBuffer();
|
||||
//System.out.println("*** byteBuffer size = " + bb.length());
|
||||
serverFileUtils.copy(bb.getBytes(), indexDumpFile);
|
||||
writeBuffer.close();
|
||||
}
|
||||
dumpArray.close();
|
||||
dumpArray = null;
|
||||
log.logInfo("finished dump of ram cache: " + urlcount + " word/URL relations in " + ((System.currentTimeMillis() - startTime) / 1000) + " seconds");
|
||||
}
|
||||
|
||||
private long restore() throws IOException {
|
||||
File indexDumpFile = new File(databaseRoot, indexArrayFileName);
|
||||
if (!(indexDumpFile.exists())) return 0;
|
||||
private long restoreArray() throws IOException {
|
||||
if (!(indexArrayFile.exists())) return 0;
|
||||
kelondroFixedWidthArray dumpArray;
|
||||
kelondroBufferedRA readBuffer = null;
|
||||
if (false /*serverMemory.available() > indexDumpFile.length() * 2*/) {
|
||||
readBuffer = new kelondroBufferedRA(new serverByteBuffer(serverFileUtils.read(indexDumpFile)));
|
||||
dumpArray = new kelondroFixedWidthArray(readBuffer, indexDumpFile.getCanonicalPath(), bufferStructureBasis, 0);
|
||||
log.logInfo("started restore of ram cache '" + indexArrayFileName + "', " + dumpArray.size() + " word/URL relations; memory-enhanced read");
|
||||
} else {
|
||||
dumpArray = new kelondroFixedWidthArray(indexDumpFile, bufferStructureBasis, 0);
|
||||
log.logInfo("started restore of ram cache '" + indexArrayFileName + "', " + dumpArray.size() + " word/URL relations; low-memory read");
|
||||
}
|
||||
dumpArray = new kelondroFixedWidthArray(indexArrayFile, bufferStructureBasis, 0);
|
||||
log.logInfo("started restore of ram cache '" + indexArrayFile.getName() + "', " + dumpArray.size() + " word/URL relations");
|
||||
long startTime = System.currentTimeMillis();
|
||||
long messageTime = System.currentTimeMillis() + 5000;
|
||||
long urlCount = 0, urlsPerSecond = 0;
|
||||
|
@ -233,7 +159,7 @@ public final class indexRAMRI implements indexRI {
|
|||
if (readBuffer != null) readBuffer.close();
|
||||
dumpArray.close();
|
||||
dumpArray = null;
|
||||
log.logConfig("finished restore: " + cache.size() + " words in " + ((System.currentTimeMillis() - startTime) / 1000) + " seconds");
|
||||
log.logInfo("finished restore: " + cache.size() + " words in " + ((System.currentTimeMillis() - startTime) / 1000) + " seconds");
|
||||
} catch (kelondroException e) {
|
||||
// restore failed
|
||||
log.logSevere("failed restore of indexCache array dump: " + e.getMessage(), e);
|
||||
|
@ -242,9 +168,9 @@ public final class indexRAMRI implements indexRI {
|
|||
}
|
||||
return urlCount;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// cache settings
|
||||
|
||||
public int maxURLinCache() {
|
||||
if (hashScore.size() == 0) return 0;
|
||||
return hashScore.getMaxScore();
|
||||
|
@ -514,7 +440,7 @@ public final class indexRAMRI implements indexRI {
|
|||
public synchronized void close() {
|
||||
// dump cache
|
||||
try {
|
||||
dump();
|
||||
indexContainerHeap.dumpHeap(this.indexHeapFile, this.payloadrow, this.cache, this.log);
|
||||
} catch (IOException e){
|
||||
log.logSevere("unable to dump cache: " + e.getMessage(), e);
|
||||
}
|
||||
|
|
41
source/de/anomic/index/indexRIReader.java
Normal file
41
source/de/anomic/index/indexRIReader.java
Normal file
|
@ -0,0 +1,41 @@
|
|||
// indexRIReader.java
|
||||
// (C) 2008 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
|
||||
// first published 2.4.2008 on http://yacy.net
|
||||
//
|
||||
// This is a part of YaCy, a peer-to-peer based web search engine
|
||||
//
|
||||
// $LastChangedDate: 2008-03-14 01:16:04 +0100 (Fr, 14 Mrz 2008) $
|
||||
// $LastChangedRevision: 4558 $
|
||||
// $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
|
||||
|
||||
package de.anomic.index;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import de.anomic.kelondro.kelondroCloneableIterator;
|
||||
|
||||
public interface indexRIReader {
|
||||
|
||||
public int size();
|
||||
public boolean hasContainer(String wordHash); // should only be used if in case that true is returned the getContainer is NOT called
|
||||
public indexContainer getContainer(String wordHash, Set<String> urlselection);
|
||||
public kelondroCloneableIterator<indexContainer> wordContainers(String startWordHash, boolean rot);
|
||||
public void close();
|
||||
|
||||
}
|
|
@ -584,20 +584,12 @@ public abstract class kelondroAbstractRecords implements kelondroRecords {
|
|||
try {
|
||||
byte[] d = getDescription();
|
||||
String s = new String(d).substring(0, 2);
|
||||
return orderBySignature(s);
|
||||
return kelondroNaturalOrder.orderBySignature(s);
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static kelondroByteOrder orderBySignature(String signature) {
|
||||
kelondroByteOrder oo = null;
|
||||
if (oo == null) oo = kelondroNaturalOrder.bySignature(signature);
|
||||
if (oo == null) oo = kelondroBase64Order.bySignature(signature);
|
||||
if (oo == null) oo = new kelondroNaturalOrder(true);
|
||||
return oo;
|
||||
}
|
||||
|
||||
public String filename() {
|
||||
return filename;
|
||||
}
|
||||
|
|
|
@ -70,6 +70,14 @@ public final class kelondroNaturalOrder extends kelondroAbstractOrder<byte[]> im
|
|||
return o;
|
||||
}
|
||||
|
||||
public static kelondroByteOrder orderBySignature(String signature) {
|
||||
kelondroByteOrder oo = null;
|
||||
if (oo == null) oo = kelondroNaturalOrder.bySignature(signature);
|
||||
if (oo == null) oo = kelondroBase64Order.bySignature(signature);
|
||||
if (oo == null) oo = new kelondroNaturalOrder(true);
|
||||
return oo;
|
||||
}
|
||||
|
||||
public final static kelondroByteOrder bySignature(String signature) {
|
||||
if (signature.equals("nd")) return new kelondroNaturalOrder(false);
|
||||
if (signature.equals("nu")) return new kelondroNaturalOrder(true);
|
||||
|
|
|
@ -68,4 +68,5 @@ public interface kelondroOrder<A> extends Comparator<A> {
|
|||
public void rotate(A zero); // defines that the ordering rotates, and sets the zero point for the rotation
|
||||
|
||||
public boolean equals(kelondroOrder<A> o); // used to compare different order objects; they may define the same ordering
|
||||
|
||||
}
|
||||
|
|
|
@ -24,8 +24,8 @@
|
|||
|
||||
package de.anomic.kelondro;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
@ -72,22 +72,36 @@ public class kelondroRowSet extends kelondroRowCollection implements kelondroInd
|
|||
}
|
||||
}
|
||||
|
||||
public static kelondroRowSet importRowSet(InputStream is, kelondroRow rowdef) throws IOException {
|
||||
public static kelondroRowSet importRowSet(DataInputStream is, kelondroRow rowdef) throws IOException {
|
||||
byte[] byte2 = new byte[2];
|
||||
byte[] byte4 = new byte[4];
|
||||
is.read(byte4); int size = (int) kelondroNaturalOrder.decodeLong(byte4);
|
||||
is.read(byte2); //short lastread = (short) kelondroNaturalOrder.decodeLong(byte2);
|
||||
is.read(byte2); //short lastwrote = (short) kelondroNaturalOrder.decodeLong(byte2);
|
||||
is.read(byte2); //String orderkey = new String(byte2);
|
||||
is.read(byte2); short ordercol = (short) kelondroNaturalOrder.decodeLong(byte2);
|
||||
is.read(byte2); short orderbound = (short) kelondroNaturalOrder.decodeLong(byte2);
|
||||
is.readFully(byte4); int size = (int) kelondroNaturalOrder.decodeLong(byte4);
|
||||
is.readFully(byte2); //short lastread = (short) kelondroNaturalOrder.decodeLong(byte2);
|
||||
is.readFully(byte2); //short lastwrote = (short) kelondroNaturalOrder.decodeLong(byte2);
|
||||
is.readFully(byte2); //String orderkey = new String(byte2);
|
||||
is.readFully(byte2); short ordercol = (short) kelondroNaturalOrder.decodeLong(byte2);
|
||||
is.readFully(byte2); short orderbound = (short) kelondroNaturalOrder.decodeLong(byte2);
|
||||
assert rowdef.primaryKeyIndex == ordercol;
|
||||
byte[] chunkcache = new byte[size * rowdef.objectsize];
|
||||
int c = is.read(chunkcache);
|
||||
assert c == chunkcache.length;
|
||||
is.readFully(chunkcache);
|
||||
return new kelondroRowSet(rowdef, size, chunkcache, orderbound);
|
||||
}
|
||||
|
||||
public static int skipNextRowSet(DataInputStream is, kelondroRow rowdef) throws IOException {
|
||||
byte[] byte2 = new byte[2];
|
||||
byte[] byte4 = new byte[4];
|
||||
is.readFully(byte4); int size = (int) kelondroNaturalOrder.decodeLong(byte4);
|
||||
is.readFully(byte2); //short lastread = (short) kelondroNaturalOrder.decodeLong(byte2);
|
||||
is.readFully(byte2); //short lastwrote = (short) kelondroNaturalOrder.decodeLong(byte2);
|
||||
is.readFully(byte2); //String orderkey = new String(byte2);
|
||||
is.readFully(byte2); short ordercol = (short) kelondroNaturalOrder.decodeLong(byte2);
|
||||
is.readFully(byte2);
|
||||
assert rowdef.primaryKeyIndex == ordercol;
|
||||
int skip = size * rowdef.objectsize;
|
||||
while (skip > 0) skip -= is.skip(skip);
|
||||
return size * rowdef.objectsize + 14;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
super.reset();
|
||||
this.profile = new kelondroProfile();
|
||||
|
|
|
@ -99,8 +99,8 @@ public final class plasmaWordIndex implements indexRI {
|
|||
|
||||
File textindexcache = new File(indexPrimaryTextLocation, "RICACHE");
|
||||
if (!(textindexcache.exists())) textindexcache.mkdirs();
|
||||
this.dhtOutCache = new indexRAMRI(textindexcache, indexRWIRowEntry.urlEntryRow, wCacheMaxChunk, wCacheMaxAge, "dump1.array", log);
|
||||
this.dhtInCache = new indexRAMRI(textindexcache, indexRWIRowEntry.urlEntryRow, wCacheMaxChunk, wCacheMaxAge, "dump2.array", log);
|
||||
this.dhtOutCache = new indexRAMRI(textindexcache, indexRWIRowEntry.urlEntryRow, wCacheMaxChunk, wCacheMaxAge, "dump1.array", "index.dhtout.heap", log);
|
||||
this.dhtInCache = new indexRAMRI(textindexcache, indexRWIRowEntry.urlEntryRow, wCacheMaxChunk, wCacheMaxAge, "dump2.array", "index.dhtin.heap", log);
|
||||
|
||||
// create collections storage path
|
||||
File textindexcollections = new File(indexPrimaryTextLocation, "RICOLLECTION");
|
||||
|
|
Loading…
Reference in New Issue
Block a user