froehlich 02/01/20 04:13:38 Modified: simplestore/src/java/org/apache/commons/simplestore SynchronizedStore.java Store.java SoftRefMemoryStore.java MRUMemoryStore.java JispFilesystemStore.java Log: Changed the Store interface. It's now extended by the java.util.Map interface. Revision Changes Path 1.2 +69 -41 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/SynchronizedStore.java Index: SynchronizedStore.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/SynchronizedStore.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- SynchronizedStore.java 15 Jan 2002 20:30:49 -0000 1.1 +++ SynchronizedStore.java 20 Jan 2002 12:13:37 -0000 1.2 @@ -8,13 +8,18 @@ package org.apache.commons.simplestore; +import java.util.Set; +import java.util.Collection; +import java.util.Map; import java.io.IOException; /** * * @author Juozas Baliuka <a href="mailto:[EMAIL PROTECTED]"> * [EMAIL PROTECTED]</a> - * @version $Revision: 1.1 $ + * @author Gerhard Froehlich <a href="mailto:[EMAIL PROTECTED]"> + * [EMAIL PROTECTED]</a> + * @version $Revision: 1.2 $ */ final class SynchronizedStore implements Store { @@ -30,22 +35,13 @@ } /** - * 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); + public Object remove(Object key) { + synchronized(store) { + return store.remove(key); } } @@ -55,7 +51,7 @@ * @param key the Key Object */ public boolean containsKey(Object key) { - synchronized(store){ + synchronized(store) { return store.containsKey(key); } } @@ -64,36 +60,14 @@ * Frees some object out of the Store. */ public void free() { - synchronized(store){ + 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); + public Object put(Object key, Object value) { + synchronized(store) { + return store.put(key,value); } } @@ -103,8 +77,62 @@ * @param key the Key Object */ public Object get(Object key) { - synchronized(store){ + synchronized(store) { return store.get(key); } + } + + public Set keySet() { + synchronized(store) { + return store.keySet(); + } + } + + public boolean containsValue(Object value) { + synchronized(store) { + return store.containsValue(value); + } + } + + public boolean isEmpty() { + synchronized(store) { + return store.isEmpty(); + } + } + + public int size() { + synchronized(store) { + return store.size(); + } + } + + public void putAll(Map t) { + synchronized(store) { + store.putAll(t); + } + } + + public void clear() { + synchronized(store) { + store.clear(); + } + } + + public Collection values() { + synchronized(store) { + return store.values(); + } + } + + public Set entrySet() { + synchronized(store) { + return store.entrySet(); + } + } + + public Store getNextStore() { + synchronized(store) { + return store.getNextStore(); + } } } 1.8 +10 -48 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/Store.java Index: Store.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/Store.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- Store.java 15 Jan 2002 19:54:24 -0000 1.7 +++ Store.java 20 Jan 2002 12:13:37 -0000 1.8 @@ -8,8 +8,7 @@ package org.apache.commons.simplestore; -import java.io.IOException; -import java.util.Enumeration; +import java.util.Map; /** * Interface for the Store implementations @@ -17,56 +16,19 @@ * @author Gerhard Froehlich <a href="mailto:[EMAIL PROTECTED]"> * [EMAIL PROTECTED]</a> */ -public interface Store { - +public interface Store extends Map { + /** - * Get the object associated to the given unique key. + * Returns the next Store in the tree * - * @param key the Key Object + * @return next Store in the tree */ - Object get(Object key); - + Store getNextStore(); + /** - * 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 - */ - void store(Object key, Object value) throws IOException; - - /** - * 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 - */ - void hold(Object key, Object value) throws IOException; - - /** - * Frees some object out of the Store. + * Frees some object out of the cache. Depends on the implemented + * algorithm. */ void free(); - - /** - * Remove the object associated to the given key. - * - * @param key the Key Object - */ - void remove(Object key); - - /** - * Indicates if the given key is associated to a contained object. - * - * @param key the Key Object - */ - boolean containsKey(Object key); - - /** - * Returns the list of used keys as an Enumeration of Objects. - */ - Enumeration keys(); } + 1.2 +47 -78 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/SoftRefMemoryStore.java Index: SoftRefMemoryStore.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/SoftRefMemoryStore.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- SoftRefMemoryStore.java 15 Jan 2002 20:30:49 -0000 1.1 +++ SoftRefMemoryStore.java 20 Jan 2002 12:13:37 -0000 1.2 @@ -9,15 +9,20 @@ package org.apache.commons.simplestore; import java.io.IOException; +import java.util.Collection; +import java.util.Map; import java.util.Enumeration; +import java.util.Set; /** * * @author Juozas Baliuka <a href="mailto:[EMAIL PROTECTED]"> * [EMAIL PROTECTED]</a> - * @version $Revision: 1.1 $ + * @author Gerhard Froehlich <a href="mailto:[EMAIL PROTECTED]"> + * [EMAIL PROTECTED]</a> + * @version $Revision: 1.2 $ */ -public class SoftRefMemoryStore +public class SoftRefMemoryStore implements Store { private static boolean DEBUG = false; @@ -27,56 +32,7 @@ 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) { @@ -131,8 +87,8 @@ /** * Returns the list of used keys as an Enumeration of Objects. */ - public Enumeration keys() { - return store.keys(); + public Set keySet() { + return map.keySet(); } /** @@ -140,9 +96,10 @@ * * @param key the Key Object */ - public void remove(Object key) { + public Object remove(Object key) { removeSoftRef(); map.remove(key); + return null; } /** @@ -152,8 +109,7 @@ */ public boolean containsKey(Object key) { removeSoftRef(); - if(map.containsKey(key))return true; - return store.containsKey(key); + return map.containsKey(key); } /** @@ -171,23 +127,10 @@ * @param key the Key Object * @param value the Value Object */ - public void store(Object key, Object object) throws IOException { + public Object put(Object key, Object object) { 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); + return null; } /** @@ -203,13 +146,39 @@ if(ref != null) { object = ref.get(); } - - if(object == null) { - object = store.get(key); - } - internalStoreObject(key,object); return object; } + + public boolean containsValue(Object value) { + return map.containsValue(value); + } + + public boolean isEmpty() { + return map.isEmpty(); + } + + public int size() { + return map.size(); + } + + public void putAll(Map t) { + map.putAll(t); + } + + public void clear() { + map.clear(); + } + + public Collection values() { + return map.values(); + } + + public Set entrySet() { + return map.entrySet(); + } + + public Store getNextStore() { + throw new UnsupportedOperationException("method not implemented yet"); + } } - 1.9 +67 -28 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/MRUMemoryStore.java Index: MRUMemoryStore.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/MRUMemoryStore.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- MRUMemoryStore.java 19 Jan 2002 14:49:59 -0000 1.8 +++ MRUMemoryStore.java 20 Jan 2002 12:13:37 -0000 1.9 @@ -10,9 +10,11 @@ import org.apache.commons.simplestore.cleanup.StoreJanitor; -import java.util.Enumeration; +import java.util.Set; +import java.util.Collection; import java.util.Hashtable; import java.util.LinkedList; +import java.util.Map; /** * This class provides a MRU cache algorithm. It combines a HashMap @@ -60,10 +62,11 @@ } /** - * Get the object associated to the given unique key. + * Returns the value to which this map maps the specified key. * - * @param key the Key Object - * @return the Object associated with Key Object + * @param key whose associated value is to be returned + * @return the value to which this map maps the specified key, + * or null if the map contains no mapping for this key */ public Object get(Object key) { Object tmpobject = this.mCache.get(key); @@ -74,35 +77,23 @@ } return null; } - - - /** - * 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) { - this.hold(key,value); - } - + /** - * This method holds the requested object in a HashMap combined with a - * LinkedList to create the MRU. It also stores objects onto the filesystem - * if configured. + * Associates the specified value with the specified key in this map + * (optional operation). If the map previously contained a mapping for + * this key, the old value is replaced. * - * @param key the Key Object - * @param value the Value Object + * @param key with which the specified value is to be associated. + * @param value to be associated with the specified key. */ - public void hold(Object key, Object value) { + public Object put(Object key, Object value) { while (this.mMRUList.size() >= this.mMaxObjects) { this.free(); } this.mCache.put(key, value); this.mMRUList.remove(key); this.mMRUList.addFirst(key); + return null; } /** @@ -110,9 +101,9 @@ * * @param key the Key object */ - public void remove(Object key) { - this.mCache.remove(key); + public Object remove(Object key) { this.mMRUList.remove(key); + return this.mCache.remove(key); } /** @@ -130,8 +121,8 @@ * * @return the enumeration of the cache */ - public Enumeration keys() { - return this.mCache.keys(); + public Set keySet() { + return this.mCache.keySet(); } /** @@ -147,5 +138,53 @@ } catch (Exception e) { e.printStackTrace(); } + } + + /** + * Returns the number of elements in this collection. + * + * return the number of elements in this collection + */ + public int size() { + return mCache.size(); + } + + /** + * Returns true if this collection contains no elements + * + * @return true if this collection contains no elements + */ + public boolean isEmpty() { + return mCache.isEmpty(); + } + + /** + * Returns true if this map maps one or more keys to the specified value. + * + * @param value value whose presence in this map is to be tested + * @return true if this map maps one or more keys to the specified value + */ + public boolean containsValue(Object value) { + return mCache.containsValue(value); + } + + public void putAll(Map t) { + throw new UnsupportedOperationException("method not implemented yet"); + } + + public void clear() { + throw new UnsupportedOperationException("method not implemented yet"); + } + + public Collection values() { + return mCache.values(); + } + + public Set entrySet() { + return mCache.entrySet(); + } + + public Store getNextStore() { + throw new UnsupportedOperationException("method not implemented yet"); } } 1.10 +45 -28 jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/JispFilesystemStore.java Index: JispFilesystemStore.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/JispFilesystemStore.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- JispFilesystemStore.java 15 Jan 2002 19:54:24 -0000 1.9 +++ JispFilesystemStore.java 20 Jan 2002 12:13:37 -0000 1.10 @@ -16,7 +16,10 @@ import java.io.File; import java.io.IOException; import java.io.Serializable; +import java.util.Collection; import java.util.Enumeration; +import java.util.Map; +import java.util.Set; /** * This store is based on the Jisp library @@ -45,7 +48,6 @@ private IndexedObjectDatabase mDatabase; private BTreeIndex mIndex; - /** * This method sets the name of the data file. Default * is default.dat. @@ -201,8 +203,7 @@ * @param value the Value Object * @exception IOException */ - public void store(Object key, Object value) - throws IOException { + public Object put(Object key, Object value) { if (value instanceof Serializable) { try { KeyObject[] keyArray = new KeyObject[1]; @@ -212,20 +213,9 @@ e.printStackTrace(); } } else { - throw new IOException("Object not Serializable"); + throw new IllegalArgumentException("Object not Serializable"); } - } - - /** - * Holds the given Object in the indexed data file. - * - * @param key the Key Object - * @param value the Value Object - * @exception IOException - */ - public void hold(Object key, Object value) - throws IOException { - this.store(key, value); + return null; } /** @@ -233,7 +223,7 @@ * not implemented, yet. */ public void free() { - //TODO: implementation + throw new UnsupportedOperationException("method not implemented yet"); } /** @@ -241,7 +231,7 @@ * * @param key the Key Object */ - public void remove(Object key) { + public Object remove(Object key) { try { KeyObject[] keyArray = new KeyObject[1]; keyArray[0] = this.wrapKeyObject(key); @@ -250,6 +240,7 @@ } catch (Exception e) { e.printStackTrace(); } + return null; } /** @@ -276,16 +267,6 @@ } /** - * Returns a Enumeration of all Keys in the indexed file - * - * @return Enumeration Object with all existing keys - */ - public Enumeration keys() { - //TODO: Implementation - return null; - } - - /** * This method wraps around the key Object a Jisp KeyObject. * * @param key the key Object @@ -299,6 +280,42 @@ //TODO: Implementation of Integer and Long keys return null; } + } + + public Set keySet() { + throw new UnsupportedOperationException("method not implemented yet"); + } + + public boolean containsValue(Object value) { + throw new UnsupportedOperationException("method not implemented yet"); + } + + public boolean isEmpty() { + throw new UnsupportedOperationException("method not implemented yet"); + } + + public int size() { + throw new UnsupportedOperationException("method not implemented yet"); + } + + public void putAll(Map t) { + throw new UnsupportedOperationException("method not implemented yet"); + } + + public void clear() { + throw new UnsupportedOperationException("method not implemented yet"); + } + + public Collection values() { + throw new UnsupportedOperationException("method not implemented yet"); + } + + public Set entrySet() { + throw new UnsupportedOperationException("method not implemented yet"); + } + + public Store getNextStore() { + throw new UnsupportedOperationException("method not implemented yet"); } }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>