Skip to content

Commit

Permalink
a better caching with less overhead and more appropriate
Browse files Browse the repository at this point in the history
synchronisation use in more than 10 different data objects

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@6250 6c8d7289-2bf4-0310-a012-ef5d649a1542
  • Loading branch information
orbiter committed Aug 7, 2009
1 parent 2e01bd9 commit 5cc17cc
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 66 deletions.
112 changes: 46 additions & 66 deletions source/de/anomic/kelondro/blob/MapView.java
Expand Up @@ -36,31 +36,25 @@
import java.util.Iterator;
import java.util.Map;

import de.anomic.kelondro.index.SimpleARC;
import de.anomic.kelondro.order.CloneableIterator;
import de.anomic.kelondro.order.NaturalOrder;
import de.anomic.kelondro.order.RotateIterator;
import de.anomic.kelondro.util.DateFormatter;
import de.anomic.kelondro.util.FileUtils;
import de.anomic.kelondro.util.ScoreCluster;
import de.anomic.kelondro.util.kelondroException;


public class MapView {

private BLOB blob;
private ScoreCluster<String> cacheScore;
private HashMap<String, Map<String, String>> cache;
private final long startup;
private final int cachesize;
private SimpleARC<String, Map<String, String>> cache;
private final char fillchar;


public MapView(final Heap blob, final int cachesize, char fillchar) {
this.blob = blob;
this.cache = new HashMap<String, Map<String, String>>();
this.cacheScore = new ScoreCluster<String>();
this.startup = System.currentTimeMillis();
this.cachesize = cachesize;
this.cache = new SimpleARC<String, Map<String, String>>(cachesize);
this.fillchar = fillchar;
/*
// debug
Expand Down Expand Up @@ -100,8 +94,7 @@ public int keylength() {
*/
public synchronized void clear() throws IOException {
this.blob.clear();
this.cache = new HashMap<String, Map<String, String>>();
this.cacheScore = new ScoreCluster<String>();
this.cache.clear();
}

private static String map2string(final Map<String, String> map, final String comment) {
Expand Down Expand Up @@ -141,40 +134,38 @@ private static Map<String, String> string2map(final String s) throws IOException
* @param newMap
* @throws IOException
*/
public synchronized void put(String key, final Map<String, String> newMap) throws IOException {
public void put(String key, final Map<String, String> newMap) throws IOException {
assert (key != null);
assert (key.length() > 0);
assert (newMap != null);
if (cacheScore == null) return; // may appear during shutdown
key = normalizeKey(key);

// write entry
blob.put(key.getBytes("UTF-8"), map2string(newMap, "W" + DateFormatter.formatShortSecond() + " ").getBytes("UTF-8"));

// check for space in cache
checkCacheSpace();

// write map to cache
cacheScore.setScore(key, (int) ((System.currentTimeMillis() - startup) / 1000));
cache.put(key, newMap);
synchronized (this) {
// write entry
blob.put(key.getBytes("UTF-8"), map2string(newMap, "W" + DateFormatter.formatShortSecond() + " ").getBytes("UTF-8"));

// write map to cache
cache.put(key, newMap);
}
}

/**
* remove a Map
* @param key the primary key
* @throws IOException
*/
public synchronized void remove(String key) throws IOException {
public void remove(String key) throws IOException {
// update elementCount
if (key == null) return;
key = normalizeKey(key);

// remove from cache
cacheScore.deleteScore(key);
cache.remove(key);

// remove from file
blob.remove(key.getBytes());
synchronized (this) {
// remove from cache
cache.remove(key);

// remove from file
blob.remove(key.getBytes());
}
}

/**
Expand All @@ -183,12 +174,14 @@ public synchronized void remove(String key) throws IOException {
* @return
* @throws IOException
*/
public synchronized boolean has(String key) throws IOException {
public boolean has(String key) throws IOException {
assert key != null;
if (cache == null) return false; // case may appear during shutdown
key = normalizeKey(key);
if (this.cache.containsKey(key)) return true;
return this.blob.has(key.getBytes());
synchronized (this) {
if (this.cache.containsKey(key)) return true;
return this.blob.has(key.getBytes());
}
}

/**
Expand All @@ -197,7 +190,7 @@ public synchronized boolean has(String key) throws IOException {
* @return
* @throws IOException
*/
public synchronized Map<String, String> get(final String key) throws IOException {
public Map<String, String> get(final String key) throws IOException {
if (key == null) return null;
return get(key, true);
}
Expand All @@ -208,43 +201,31 @@ private String normalizeKey(String key) {
return key;
}

protected synchronized Map<String, String> get(String key, final boolean storeCache) throws IOException {
protected Map<String, String> get(String key, final boolean storeCache) throws IOException {
// load map from cache
assert key != null;
if (cache == null) return null; // case may appear during shutdown
key = normalizeKey(key);

Map<String, String> map = cache.get(key);
if (map != null) return map;

// load map
if (!(blob.has(key.getBytes()))) return null;

// read object
final byte[] b = blob.get(key.getBytes());
if (b == null) return null;
map = string2map(new String(b, "UTF-8"));

if (storeCache) {
// cache it also
checkCacheSpace();
// write map to cache
cacheScore.setScore(key, (int) ((System.currentTimeMillis() - startup) / 1000));
cache.put(key, map);
}

// return value
return map;
}
synchronized (this) {
Map<String, String> map = cache.get(key);
if (map != null) return map;

// load map
if (!(blob.has(key.getBytes()))) return null;

// read object
final byte[] b = blob.get(key.getBytes());
if (b == null) return null;
map = string2map(new String(b, "UTF-8"));

if (storeCache) {
// write map to cache
cache.put(key, map);
}

private synchronized void checkCacheSpace() {
// check for space in cache
if (cache == null) return; // may appear during shutdown
if (cache.size() >= cachesize) {
// delete one entry
final String delkey = cacheScore.getMinObject();
cacheScore.deleteScore(delkey);
cache.remove(delkey);
// return value
return map;
}
}

Expand Down Expand Up @@ -298,9 +279,8 @@ public synchronized int size() {
/**
* close the Map table
*/
public void close() {
public synchronized void close() {
cache = null;
cacheScore = null;

// close file
if (blob != null) blob.close(true);
Expand Down
22 changes: 22 additions & 0 deletions source/de/anomic/kelondro/index/SimpleARC.java
@@ -1,4 +1,5 @@
// SimpleARC.java
// a Simple Adaptive Replacement Cache
// (C) 2009 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
// first published 17.04.2009 on http://yacy.net
//
Expand Down Expand Up @@ -90,6 +91,27 @@ public V get(K s) {
return v;
}

/**
* check if the map contains the key
* @param s
* @return
*/
public boolean containsKey(K s) {
if (this.levelB.containsKey(s)) return true;
return this.levelA.containsKey(s);
}

/**
* remove an entry from the cache
* @param s
* @return the old value
*/
public V remove(K s) {
V r = this.levelB.remove(s);
if (r != null) return r;
return this.levelA.remove(s);
}

/**
* clear the cache
*/
Expand Down

0 comments on commit 5cc17cc

Please sign in to comment.