froehlich 02/01/15 12:30:49
Added: simplestore/src/java/org/apache/commons/simplestore
SynchronizedStore.java SoftRefMemoryStore.java
Log:
applied classes from Juozas Baliuka [[EMAIL PROTECTED]]
Revision Changes Path
1.1
jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/SynchronizedStore.java
Index: SynchronizedStore.java
===================================================================
/*****************************************************************************
* Copyright (C) The Apache Software Foundation. All rights reserved. *
* ------------------------------------------------------------------------- *
* This software is published under the terms of the Apache Software License *
* version 1.1, a copy of which has been included with this distribution in *
* the LICENSE file. *
*****************************************************************************/
package org.apache.commons.simplestore;
import java.io.IOException;
/**
*
* @author Juozas Baliuka <a href="mailto:[EMAIL PROTECTED]">
* [EMAIL PROTECTED]</a>
* @version $Revision: 1.1 $
*/
final class SynchronizedStore
implements Store {
private Store store;
/** Creates new SynchronizedStore */
public SynchronizedStore(Store store) {
if(store == null) {
throw new NullPointerException();
}
this.store = store;
}
/**
* Returns the list of used keys as an Enumeration of Objects.
*/
public java.util.Enumeration keys() {
synchronized(store){ // not very meaningful here
return store.keys();
}
}
/**
* Remove the object associated to the given key.
*
* @param key the Key Object
*/
public void remove(Object key) {
synchronized(store){
store.remove(key);
}
}
/**
* Indicates if the given key is associated to a contained object.
*
* @param key the Key Object
*/
public boolean containsKey(Object key) {
synchronized(store){
return store.containsKey(key);
}
}
/**
* Frees some object out of the Store.
*/
public void free() {
synchronized(store){
store.free();
}
}
/**
* Store the given object in a persistent state. It is up to the
* caller to ensure that the key has a persistent state across
* different JVM executions.
*
* @param key the Key Object
* @param value the Value Object
*/
public void store(Object key, Object value) throws IOException {
synchronized(store){
store.store(key,value);
}
}
/**
* Holds the given object in a volatile state. This means
* the object store will discard held objects if the
* virtual machine is restarted or some error happens.
*
* @param key the Key Object
* @param value the Value Object
*/
public void hold(Object key, Object value) throws IOException {
synchronized(store){
store.hold(key,value);
}
}
/**
* Get the object associated to the given unique key.
*
* @param key the Key Object
*/
public Object get(Object key) {
synchronized(store){
return store.get(key);
}
}
}
1.1
jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/SoftRefMemoryStore.java
Index: SoftRefMemoryStore.java
===================================================================
/*****************************************************************************
* Copyright (C) The Apache Software Foundation. All rights reserved. *
* ------------------------------------------------------------------------- *
* This software is published under the terms of the Apache Software License *
* version 1.1, a copy of which has been included with this distribution in *
* the LICENSE file. *
*****************************************************************************/
package org.apache.commons.simplestore;
import java.io.IOException;
import java.util.Enumeration;
/**
*
* @author Juozas Baliuka <a href="mailto:[EMAIL PROTECTED]">
* [EMAIL PROTECTED]</a>
* @version $Revision: 1.1 $
*/
public class SoftRefMemoryStore
implements Store {
private static boolean DEBUG = false;
private Store store;
private int maxStrongRefCount;
private Object [] strongRefs;
private int current = 0;
private java.util.Map map = new java.util.HashMap();
private java.lang.ref.ReferenceQueue queue = new java.lang.ref.ReferenceQueue();
/** main for testing */
public static void main(String args[])throws Exception{
final int OBJECT_SIZE = 0xFFFF;
final int MAX_STRONG_REF = 20;
final int ITERATIONS = MAX_STRONG_REF*2;
DEBUG = true;
SoftRefMemoryStore mStore = new SoftRefMemoryStore(
new Store() {
public void free(){}
public Enumeration keys(){ return null; }
public boolean containsKey(Object key){ return false;}
public void remove(Object obj){}
public void store(Object key,Object object){}
public void hold(Object key,Object object){}
public Object get(Object key) {
return new int[OBJECT_SIZE];
}
}
,MAX_STRONG_REF);
System.out.println("operations ... ");
Object obj = mStore.get(new Integer(-1));//strong ref
for(int i = 0; i < ITERATIONS ; i++ ) {
Object key = new Integer(i);
Object o = mStore.get(key);// soft ref after iteration;
mStore.get(new Integer(0));//MFU and MRU
}
System.gc();
mStore.get(new Integer(0));//remove unused objects, private iterator can't
do it.
System.out.println("iterating cache ");
System.out.println("must contain key -1");
System.out.println("must contain 0 if MAX_STRONG_REF > 0 ... ");
if( ! mStore.map.containsKey(new Integer(-1)) ||
!( mStore.map.containsKey(new Integer(0)) && MAX_STRONG_REF > 0 )
)
System.out.println("failed");
System.out.println("cache size is " + mStore.map.size()
+ " it depends on GC possible MAX_STRONG_REF < SIZE" );
java.util.Iterator i = mStore.map.keySet().iterator();
while(i.hasNext()) {
System.out.println(i.next());
}
}
static class SoftRef extends java.lang.ref.SoftReference{
Object key;
private SoftRef(Object key,Object object,java.lang.ref.ReferenceQueue
queue) {
super(object,queue);
this.key = key;
}
}
public static Store getInstance(Store store,int maxStrongRef) {
return new SynchronizedStore( new SoftRefMemoryStore(store, maxStrongRef) )
;
}
/** Creates new SoftRefMemoryStore */
protected SoftRefMemoryStore(Store store, int maxStrongRefCount) {
if(store == null) {
throw new NullPointerException();
}
if(maxStrongRefCount < 0) {
throw new java.lang.IllegalArgumentException();
}
this.store = store;
this.maxStrongRefCount = maxStrongRefCount;
if(maxStrongRefCount > 0) {
strongRefs = new Object[ maxStrongRefCount ];
}
}
// remove keys
private void removeSoftRef(){
SoftRef ref = (SoftRef)queue.poll();
while( ref != null ) {
map.remove(ref.key);
if(DEBUG) {
System.out.println( "Key " + ref.key + " removed from queue, map
size is " + map.size());
}
ref = (SoftRef)queue.poll();
}
}
private void addStrongRef(Object object) {
if( strongRefs != null ) {
strongRefs[ ( current++ ) % maxStrongRefCount ] = object;
}
}
private void internalStoreObject(Object key, Object object) {
addStrongRef(object);
map.put(key,new SoftRef(key,object,queue));
}
/**
* Returns the list of used keys as an Enumeration of Objects.
*/
public Enumeration keys() {
return store.keys();
}
/**
* Remove the object associated to the given key.
*
* @param key the Key Object
*/
public void remove(Object key) {
removeSoftRef();
map.remove(key);
}
/**
* Indicates if the given key is associated to a contained object.
*
* @param key the Key Object
*/
public boolean containsKey(Object key) {
removeSoftRef();
if(map.containsKey(key))return true;
return store.containsKey(key);
}
/**
* Frees some object out of the Store.
*/
public void free() {
removeSoftRef();
}
/**
* Store the given object in a persistent state. It is up to the
* caller to ensure that the key has a persistent state across
* different JVM executions.
*
* @param key the Key Object
* @param value the Value Object
*/
public void store(Object key, Object object) throws IOException {
removeSoftRef();
store.store(key,object);
internalStoreObject(key,object);
}
/**
* Holds the given object in a volatile state. This means
* the object store will discard held objects if the
* virtual machine is restarted or some error happens.
*
* @param key the Key Object
* @param value the Value Object
*/
public void hold(Object key, Object value) throws IOException {
store(key,value);
}
/**
* Get the object associated to the given unique key.
*
* @param key the Key Object
*/
public Object get(Object key) {
removeSoftRef();
Object object = null;
java.lang.ref.Reference ref = (java.lang.ref.Reference)map.get(key);
if(ref != null) {
object = ref.get();
}
if(object == null) {
object = store.get(key);
}
internalStoreObject(key,object);
return object;
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>