forked from I2P_Developers/i2p.i2p
* LHMCache: New util, replacing several private versions
This commit is contained in:
@ -14,7 +14,6 @@ import java.util.Collection;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@ -24,6 +23,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
|
|||||||
import net.i2p.I2PAppContext;
|
import net.i2p.I2PAppContext;
|
||||||
import net.i2p.data.DataHelper;
|
import net.i2p.data.DataHelper;
|
||||||
import net.i2p.data.SimpleDataStructure;
|
import net.i2p.data.SimpleDataStructure;
|
||||||
|
import net.i2p.util.LHMCache;
|
||||||
import net.i2p.util.Log;
|
import net.i2p.util.Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -658,7 +658,7 @@ public class KBucketSet<T extends SimpleDataStructure> {
|
|||||||
public Range(T us, int bValue) {
|
public Range(T us, int bValue) {
|
||||||
_bValue = bValue;
|
_bValue = bValue;
|
||||||
_bigUs = new BigInteger(1, us.getData());
|
_bigUs = new BigInteger(1, us.getData());
|
||||||
_distanceCache = new LHM(256);
|
_distanceCache = new LHMCache(256);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return 0 to max-1 or -1 for us */
|
/** @return 0 to max-1 or -1 for us */
|
||||||
@ -697,20 +697,6 @@ public class KBucketSet<T extends SimpleDataStructure> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class LHM<K, V> extends LinkedHashMap<K, V> {
|
|
||||||
private final int _max;
|
|
||||||
|
|
||||||
public LHM(int max) {
|
|
||||||
super(max, 0.75f, true);
|
|
||||||
_max = max;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
|
|
||||||
return size() > _max;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* For Collections.binarySearch.
|
* For Collections.binarySearch.
|
||||||
* getRangeBegin == getRangeEnd.
|
* getRangeBegin == getRangeEnd.
|
||||||
|
@ -18,7 +18,6 @@ import java.net.Socket;
|
|||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
@ -44,6 +43,7 @@ import net.i2p.internal.I2CPMessageQueue;
|
|||||||
import net.i2p.internal.InternalClientManager;
|
import net.i2p.internal.InternalClientManager;
|
||||||
import net.i2p.internal.QueuedI2CPMessageReader;
|
import net.i2p.internal.QueuedI2CPMessageReader;
|
||||||
import net.i2p.util.I2PAppThread;
|
import net.i2p.util.I2PAppThread;
|
||||||
|
import net.i2p.util.LHMCache;
|
||||||
import net.i2p.util.Log;
|
import net.i2p.util.Log;
|
||||||
import net.i2p.util.SimpleScheduler;
|
import net.i2p.util.SimpleScheduler;
|
||||||
import net.i2p.util.SimpleTimer;
|
import net.i2p.util.SimpleTimer;
|
||||||
@ -140,7 +140,7 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
|
|||||||
/**
|
/**
|
||||||
* @since 0.8.9
|
* @since 0.8.9
|
||||||
*/
|
*/
|
||||||
private static final LookupCache _lookupCache = new LookupCache(16);
|
private static final Map<Hash, Destination> _lookupCache = new LHMCache(16);
|
||||||
|
|
||||||
/** SSL interface (only) @since 0.8.3 */
|
/** SSL interface (only) @since 0.8.3 */
|
||||||
protected static final String PROP_ENABLE_SSL = "i2cp.SSL";
|
protected static final String PROP_ENABLE_SSL = "i2cp.SSL";
|
||||||
@ -985,21 +985,4 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
|
|||||||
buf.append(getPrefix());
|
buf.append(getPrefix());
|
||||||
return buf.toString();
|
return buf.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @since 0.8.9
|
|
||||||
*/
|
|
||||||
private static class LookupCache extends LinkedHashMap<Hash, Destination> {
|
|
||||||
private final int _max;
|
|
||||||
|
|
||||||
public LookupCache(int max) {
|
|
||||||
super(max, 0.75f, true);
|
|
||||||
_max = max;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected boolean removeEldestEntry(Map.Entry<Hash, Destination> eldest) {
|
|
||||||
return size() > _max;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,7 @@ import net.i2p.data.DataFormatException;
|
|||||||
import net.i2p.data.DataHelper;
|
import net.i2p.data.DataHelper;
|
||||||
import net.i2p.data.Destination;
|
import net.i2p.data.Destination;
|
||||||
import net.i2p.data.Hash;
|
import net.i2p.data.Hash;
|
||||||
|
import net.i2p.util.LHMCache;
|
||||||
import net.i2p.util.Log;
|
import net.i2p.util.Log;
|
||||||
import net.i2p.util.SecureFileOutputStream;
|
import net.i2p.util.SecureFileOutputStream;
|
||||||
|
|
||||||
@ -134,7 +135,7 @@ public class BlockfileNamingService extends DummyNamingService {
|
|||||||
super(context);
|
super(context);
|
||||||
_lists = new ArrayList();
|
_lists = new ArrayList();
|
||||||
_invalid = new ArrayList();
|
_invalid = new ArrayList();
|
||||||
_negativeCache = new LHM(NEGATIVE_CACHE_SIZE);
|
_negativeCache = new LHMCache(NEGATIVE_CACHE_SIZE);
|
||||||
BlockFile bf = null;
|
BlockFile bf = null;
|
||||||
RAIFile raf = null;
|
RAIFile raf = null;
|
||||||
boolean readOnly = false;
|
boolean readOnly = false;
|
||||||
|
@ -7,13 +7,13 @@
|
|||||||
*/
|
*/
|
||||||
package net.i2p.client.naming;
|
package net.i2p.client.naming;
|
||||||
|
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import net.i2p.I2PAppContext;
|
import net.i2p.I2PAppContext;
|
||||||
import net.i2p.data.Destination;
|
import net.i2p.data.Destination;
|
||||||
|
import net.i2p.util.LHMCache;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Dummy naming service that can only handle base64 and b32 destinations.
|
* A Dummy naming service that can only handle base64 and b32 destinations.
|
||||||
@ -30,7 +30,7 @@ class DummyNamingService extends NamingService {
|
|||||||
* Classes should take care to call removeCache() for any entries that
|
* Classes should take care to call removeCache() for any entries that
|
||||||
* are invalidated.
|
* are invalidated.
|
||||||
*/
|
*/
|
||||||
private static final Map<String, Destination> _cache = new LHM(CACHE_MAX_SIZE);
|
private static final Map<String, Destination> _cache = new LHMCache(CACHE_MAX_SIZE);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The naming service should only be constructed and accessed through the
|
* The naming service should only be constructed and accessed through the
|
||||||
@ -115,18 +115,4 @@ class DummyNamingService extends NamingService {
|
|||||||
_cache.clear();
|
_cache.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static class LHM<K, V> extends LinkedHashMap<K, V> {
|
|
||||||
private final int _max;
|
|
||||||
|
|
||||||
public LHM(int max) {
|
|
||||||
super(max, 0.75f, true);
|
|
||||||
_max = max;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
|
|
||||||
return size() > _max;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -6,10 +6,10 @@ import java.io.InputStream;
|
|||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import net.i2p.I2PAppContext;
|
import net.i2p.I2PAppContext;
|
||||||
|
import net.i2p.util.LHMCache;
|
||||||
import net.i2p.util.SimpleByteCache;
|
import net.i2p.util.SimpleByteCache;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -71,7 +71,7 @@ public class SDSCache<V extends SimpleDataStructure> {
|
|||||||
*/
|
*/
|
||||||
public SDSCache(Class<V> rvClass, int len, int max) {
|
public SDSCache(Class<V> rvClass, int len, int max) {
|
||||||
int size = (int) (max * FACTOR);
|
int size = (int) (max * FACTOR);
|
||||||
_cache = new LHM(size);
|
_cache = new LHMCache(size);
|
||||||
_datalen = len;
|
_datalen = len;
|
||||||
try {
|
try {
|
||||||
_rvCon = rvClass.getConstructor(conArg);
|
_rvCon = rvClass.getConstructor(conArg);
|
||||||
@ -181,18 +181,4 @@ public class SDSCache<V extends SimpleDataStructure> {
|
|||||||
rv ^= (data[i] << (i*8));
|
rv ^= (data[i] << (i*8));
|
||||||
return Integer.valueOf(rv);
|
return Integer.valueOf(rv);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class LHM<K, V> extends LinkedHashMap<K, V> {
|
|
||||||
private final int _max;
|
|
||||||
|
|
||||||
public LHM(int max) {
|
|
||||||
super(max, 0.75f, true);
|
|
||||||
_max = max;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
|
|
||||||
return size() > _max;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
24
core/java/src/net/i2p/util/LHMCache.java
Normal file
24
core/java/src/net/i2p/util/LHMCache.java
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package net.i2p.util;
|
||||||
|
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A LinkedHashMap with a maximum size, for use as
|
||||||
|
* an LRU cache. Unsynchronized.
|
||||||
|
*
|
||||||
|
* @since 0.9.3
|
||||||
|
*/
|
||||||
|
public class LHMCache<K, V> extends LinkedHashMap<K, V> {
|
||||||
|
private final int _max;
|
||||||
|
|
||||||
|
public LHMCache(int max) {
|
||||||
|
super(max, 0.75f, true);
|
||||||
|
_max = max;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
|
||||||
|
return size() > _max;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user