Map<byte[], Map<String, byte[]>> Because such Maps with byte[] keys cannot be stored in hash maps (bad hashing on byte[]) another ARC with comparable Maps has been added This will make it possible to move the HTCache class 'Cache' into the cora package because that class may be used either with RAM caches (ARCs) or with file-based caches (BEncodedHeaps) git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@7071 6c8d7289-2bf4-0310-a012-ef5d649a1542pull/1/head
parent
c60d0282fd
commit
cf07b34c2d
@ -0,0 +1,68 @@
|
||||
/**
|
||||
* ComparableARC
|
||||
* an Adaptive Replacement Cache for comparable objects
|
||||
* Copyright 2010 by Michael Peter Christen, mc@yacy.net, Frankfurt a. M., Germany
|
||||
* First released 24.08.2010 at http://yacy.net
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.cora.storage;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public final class ComparableARC<K, V> extends SimpleARC<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, ARC<K, V> {
|
||||
|
||||
|
||||
public ComparableARC(final int cacheSize, Comparator<? super K> comparator) {
|
||||
super.cacheSize = cacheSize / 2;
|
||||
super.levelA = new LimitedTreeMap<K, V>(this.cacheSize, comparator);
|
||||
super.levelB = new LimitedTreeMap<K, V>(this.cacheSize, comparator);
|
||||
}
|
||||
|
||||
private static class LimitedTreeMap<K, V> extends TreeMap<K, V> {
|
||||
private static final long serialVersionUID = -2276429187676080820L;
|
||||
int limit;
|
||||
LinkedList<K> keys;
|
||||
public LimitedTreeMap(final int cacheSize, Comparator<? super K> comparator) {
|
||||
super(comparator);
|
||||
this.limit = cacheSize;
|
||||
this.keys = new LinkedList<K>();
|
||||
}
|
||||
public V put(K k, V v) {
|
||||
V r = super.put(k, v);
|
||||
keys.add(k);
|
||||
if (keys.size() > this.limit) {
|
||||
K w = this.keys.removeFirst();
|
||||
V t = super.remove(w);
|
||||
assert t != null;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
public V remove(Object k) {
|
||||
V r = super.remove(k);
|
||||
this.keys.remove(k);
|
||||
return r;
|
||||
}
|
||||
public void clear() {
|
||||
super.clear();
|
||||
this.keys.clear();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
/**
|
||||
* HashARC
|
||||
* an Adaptive Replacement Cache for objects that can be compared using hashing
|
||||
* Copyright 2009 by Michael Peter Christen, mc@yacy.net, Frankfurt a. M., Germany
|
||||
* First released 17.04.2009 at http://yacy.net
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.cora.storage;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public final class HashARC<K, V> extends SimpleARC<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, ARC<K, V> {
|
||||
|
||||
public final static boolean accessOrder = false; // if false, then a insertion-order is used
|
||||
|
||||
public HashARC(final int cacheSize) {
|
||||
this.cacheSize = cacheSize / 2;
|
||||
super.levelA = new LinkedHashMap<K, V>(cacheSize, 0.1f, accessOrder) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
@Override protected boolean removeEldestEntry(final Map.Entry<K, V> eldest) {
|
||||
return size() > HashARC.this.cacheSize;
|
||||
}
|
||||
};
|
||||
this.levelB = new LinkedHashMap<K, V>(cacheSize, 0.1f, accessOrder) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
@Override protected boolean removeEldestEntry(final Map.Entry<K, V> eldest) {
|
||||
return size() > HashARC.this.cacheSize;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in new issue