froehlich    02/01/26 04:00:47

  Modified:    simplestore/src/java/org/apache/commons/simplestore
                        SoftRefMemoryStore.java
  Log:
  applied patches from Juozas Baliuka [[EMAIL PROTECTED]].
  - re-factored SoftRefMemoryStore
  - added Swapping Simultation and test case
  
  Revision  Changes    Path
  1.6       +65 -34    
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.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- SoftRefMemoryStore.java   21 Jan 2002 22:02:36 -0000      1.5
  +++ SoftRefMemoryStore.java   26 Jan 2002 12:00:47 -0000      1.6
  @@ -20,19 +20,19 @@
    *      [EMAIL PROTECTED]</a>
    * @author Gerhard Froehlich <a href="mailto:[EMAIL PROTECTED]";>
    *      [EMAIL PROTECTED]</a>
  - * @version $Id: SoftRefMemoryStore.java,v 1.5 2002/01/21 22:02:36 froehlich Exp $
  + * @version $Id: SoftRefMemoryStore.java,v 1.6 2002/01/26 12:00:47 froehlich Exp $
    */
   public class SoftRefMemoryStore
   implements Store {
  -
  +    
       private static boolean DEBUG = true;
  -    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();
  - 
  +    private Swap swap;
  +    
       static class SoftRef extends java.lang.ref.SoftReference{
           Object key;
           private  SoftRef(Object key,Object object,java.lang.ref.ReferenceQueue 
queue) {
  @@ -41,48 +41,75 @@
           }
       }
       
  -    public static Store getInstance(Store store,int maxStrongRef) {
  -        return new SynchronizedStore( new  SoftRefMemoryStore(store, maxStrongRef) 
) ;
  +    static class StrongRef extends java.lang.Object{
  +        Object object;
  +        Swap queue;
  +        private  StrongRef(Object object, Swap queue ) {
  +            this.queue = queue;// used in finalize
  +            this.object = object;// add strong reference to value
  +        }
  +        
  +        public Object get(){
  +            return object;
  +        }
  +        
  +        protected void finalize() throws java.lang.Throwable {
  +            super.finalize();
  +            queue.add(object);
  +        }
       }
       
  -    /** Creates new SoftRefMemoryStore */
  -    protected SoftRefMemoryStore(Store store, int maxStrongRefCount) {
  -        if(store == null) {
  -            throw new NullPointerException();
  +    private Object makeValue(Object key, Object value,java.lang.ref.ReferenceQueue 
queue, Swap swap  ){
  +        if( swap == null ){
  +            return new SoftRef( key, value, queue);
           }
  +        else{
  +            return new SoftRef( key, new StrongRef( value, swap ), queue);
  +        }
  +    }
  +    
  +    public static Store getInstance(Swap swap, int maxStrongRef) {
  +        
  +        return new SynchronizedStore( new  SoftRefMemoryStore( swap, maxStrongRef ) 
) ;
  +        
  +    }
  +    
  +    /** Creates new SoftRefMemoryStore */
  +    protected SoftRefMemoryStore(Swap swap, int maxStrongRefCount) {
  +        this.swap = swap;
           if(maxStrongRefCount < 0) {
               throw new java.lang.IllegalArgumentException();
           }
  -        this.store = store;
           this.maxStrongRefCount = maxStrongRefCount;
           if(maxStrongRefCount > 0) {
               strongRefs = new Object[ maxStrongRefCount ];
           }
       }
  -
  -    // remove keys
  +    
  +    // remove unused 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());
  +                System.out.println( "Key " + ref.key + " removed from Reference 
queue, map size is " + map.size());
               }
               ref = (SoftRef)queue.poll();
           }
       }
       
  -    private void addStrongRef(Object object) { 
  +    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));
  -    }
  +         SoftRef ref = (SoftRef)makeValue(key,object,queue,swap);
  +         addStrongRef(ref.get());
  +         map.put(key,ref);
  +     }
       
       /**
        * Remove the object associated to the given key.
  @@ -91,8 +118,7 @@
        */
       public Object remove(Object key) {
           removeSoftRef();
  -        store.remove(key);
  -        return null;
  +        return map.remove(key);
       }
       
       /**
  @@ -102,13 +128,15 @@
        */
       public boolean containsKey(Object key) {
           removeSoftRef();
  -        return store.containsKey(key);
  +        return map.containsKey(key);
       }
       
       /**
        * Frees some object out of the Store.
        */
       public void free() {
  +       if( strongRefs != null ) 
  +         java.util.Arrays.fill(strongRefs,null);
           removeSoftRef();
       }
       
  @@ -122,7 +150,7 @@
        */
       public void put(Object key, Object object) {
           removeSoftRef();
  -        store.put(key,object);
  +        //  store.put(key,object);
           internalStoreObject(key,object);
       }
       
  @@ -134,33 +162,36 @@
       public Object get(Object key) {
           removeSoftRef();
           Object object = null;
  -        java.lang.ref.Reference ref = (java.lang.ref.Reference)map.get(key);
  +        SoftRef ref = (SoftRef)map.get(key);
           
           if(ref != null) {
  -            object = ref.get();
  +            
  +            Object value = ref.get();
  +            
  +            if( value != null && value instanceof StrongRef ){
  +                object = ((StrongRef)value).object;
  +            }else{
  +                object = value;
  +            }
           }
           
  -        if(object == null) {
  -            object = store.get(key);
  -        }
  -
  -        internalStoreObject(key,object);
  +        
           return object;
       }
       
       public boolean isEmpty()  {
  -        return store.isEmpty();  
  +        return map.isEmpty();
       }
       
       public int size()  {
  -        return store.size();  
  +        return map.size();
       }
       
       public void clear()  {
  -        store.clear();
  +        map.clear();
       }
       
       public Store getNextStore() {
  -        throw new UnsupportedOperationException("method not implemented yet");  
  +        throw new UnsupportedOperationException("method not implemented yet");
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to