directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r948098 - in /directory/apacheds/trunk/jdbm/src: main/java/jdbm/helper/MRU.java main/java/jdbm/helper/SoftCache.java main/java/jdbm/recman/CacheRecordManager.java test/java/jdbm/btree/TestBTree.java
Date Tue, 25 May 2010 16:54:23 GMT
Author: elecharny
Date: Tue May 25 16:54:23 2010
New Revision: 948098

URL: http://svn.apache.org/viewvc?rev=948098&view=rev
Log:
o Added more generic
o Minor refactoring (source formatting, renaming)
o Added a test

Modified:
    directory/apacheds/trunk/jdbm/src/main/java/jdbm/helper/MRU.java
    directory/apacheds/trunk/jdbm/src/main/java/jdbm/helper/SoftCache.java
    directory/apacheds/trunk/jdbm/src/main/java/jdbm/recman/CacheRecordManager.java
    directory/apacheds/trunk/jdbm/src/test/java/jdbm/btree/TestBTree.java

Modified: directory/apacheds/trunk/jdbm/src/main/java/jdbm/helper/MRU.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/jdbm/src/main/java/jdbm/helper/MRU.java?rev=948098&r1=948097&r2=948098&view=diff
==============================================================================
--- directory/apacheds/trunk/jdbm/src/main/java/jdbm/helper/MRU.java (original)
+++ directory/apacheds/trunk/jdbm/src/main/java/jdbm/helper/MRU.java Tue May 25 16:54:23 2010
@@ -66,7 +66,7 @@ import org.apache.directory.server.i18n.
  * @author <a href="mailto:boisvert@intalio.com">Alex Boisvert</a>
  * @version $Id: MRU.java,v 1.8 2005/06/25 23:12:31 doomdark Exp $
  */
-public class MRU implements CachePolicy
+public class MRU<K, V> implements CachePolicy<K, V>
 {
     /** Cached object Map */
     Map<Object, CacheEntry> map = new HashMap<Object, CacheEntry>();
@@ -111,9 +111,9 @@ public class MRU implements CachePolicy
     /**
      * Place an object in the cache.
      */
-    public void put( Object key, Object value ) throws CacheEvictionException
+    public void put( K key, V value ) throws CacheEvictionException
     {
-        CacheEntry entry = ( CacheEntry ) map.get( key );
+        CacheEntry entry = map.get( key );
         
         if ( entry != null )
         {
@@ -144,14 +144,14 @@ public class MRU implements CachePolicy
     /**
      * Obtain an object in the cache
      */
-    public Object get( Object key )
+    public V get( K key )
     {
-        CacheEntry entry = ( CacheEntry ) map.get( key );
+        CacheEntry entry = map.get( key );
         
         if ( entry != null )
         {
             touchEntry( entry );
-            return entry.getValue();
+            return (V)entry.getValue();
         }
         else
         {
@@ -163,9 +163,9 @@ public class MRU implements CachePolicy
     /**
      * Remove an object from the cache
      */
-    public void remove( Object key )
+    public void remove( K key )
     {
-        CacheEntry entry = ( CacheEntry ) map.get( key );
+        CacheEntry entry = map.get( key );
         
         if ( entry != null )
         {
@@ -189,9 +189,9 @@ public class MRU implements CachePolicy
     /**
      * Enumerate elements' values in the cache
      */
-    public Enumeration<Object> elements()
+    public Enumeration<V> elements()
     {
-        return new MRUEnumeration<Object>( map.values().iterator() );
+        return new MRUEnumeration<V>( map.values().iterator() );
     }
 
 
@@ -245,35 +245,34 @@ public class MRU implements CachePolicy
 
 
     /**
-     * Remove a CacheEntry from linked list
+     * Remove a CacheEntry from linked list, and relink the 
+     * remaining element sin the list.
      */
     protected void removeEntry( CacheEntry entry )
     {
         if ( entry == first )
         {
             first = entry.getNext();
+            
+            if ( first != null )
+            {
+                first.setPrevious( null );
+            }
         }
-        
-        if ( last == entry )
+        else if ( last == entry )
         {
             last = entry.getPrevious();
+            
+            if ( last != null )
+            {
+                last.setNext( null );
+            }
         }
-        
-        CacheEntry previous = entry.getPrevious();
-        CacheEntry next = entry.getNext();
-        
-        if ( previous != null )
-        {
-            previous.setNext( next );
-        }
-        
-        if ( next != null )
+        else
         {
-            next.setPrevious( previous );
+            entry.getPrevious().setNext( entry.getNext() );
+            entry.getNext().setPrevious( entry.getPrevious() );
         }
-        
-        entry.setPrevious( null );
-        entry.setNext( null );
     }
 
 
@@ -393,7 +392,7 @@ class CacheEntry
  * Enumeration wrapper to return actual user objects instead of
  * CacheEntries.
  */
-class MRUEnumeration<K> implements Enumeration<K>
+class MRUEnumeration<V> implements Enumeration<V>
 {
     Iterator<CacheEntry> elements;
 
@@ -410,10 +409,10 @@ class MRUEnumeration<K> implements Enume
     }
 
 
-    public K nextElement()
+    public V nextElement()
     {
         CacheEntry entry = elements.next();
         
-        return (K)entry.getValue();
+        return (V)entry.getValue();
     }
 }

Modified: directory/apacheds/trunk/jdbm/src/main/java/jdbm/helper/SoftCache.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/jdbm/src/main/java/jdbm/helper/SoftCache.java?rev=948098&r1=948097&r2=948098&view=diff
==============================================================================
--- directory/apacheds/trunk/jdbm/src/main/java/jdbm/helper/SoftCache.java (original)
+++ directory/apacheds/trunk/jdbm/src/main/java/jdbm/helper/SoftCache.java Tue May 25 16:54:23
2010
@@ -46,6 +46,7 @@
  */
 package jdbm.helper;
 
+
 import java.lang.ref.ReferenceQueue;
 import java.lang.ref.SoftReference;
 import java.lang.ref.Reference;
@@ -55,6 +56,7 @@ import java.util.HashMap;
 
 import org.apache.directory.server.i18n.I18n;
 
+
 /**
  * Wraps a deterministic cache policy with a <q>Level-2</q> cache based on
  * J2SE's {@link SoftReference soft references}. Soft references allow
@@ -81,13 +83,15 @@ import org.apache.directory.server.i18n.
  * @author <a href="mailto:dranatunga@users.sourceforge.net">Dilum Ranatunga</a>
  * @version $Id: SoftCache.java,v 1.1 2003/11/01 13:29:27 dranatunga Exp $
  */
-public class SoftCache implements CachePolicy {
+public class SoftCache implements CachePolicy
+{
     private static final int INITIAL_CAPACITY = 128;
     private static final float DEFAULT_LOAD_FACTOR = 1.5f;
 
-    private final ReferenceQueue _clearQueue = new ReferenceQueue();
-    private final CachePolicy _internal;
-    private final Map _cacheMap;
+    private final ReferenceQueue clearQueue = new ReferenceQueue();
+    private final CachePolicy internal;
+    private final Map map;
+
 
     /**
      * Creates a soft-reference based L2 cache with a {@link MRU} cache as
@@ -97,10 +101,12 @@ public class SoftCache implements CacheP
      * {@link #get(Object) get( )s} first try the L1 cache anyway. The
      * internal MRU is given a capacity of 128 elements.
      */
-    public SoftCache() {
-        this(new MRU(INITIAL_CAPACITY));
+    public SoftCache()
+    {
+        this( new MRU( INITIAL_CAPACITY ) );
     }
 
+
     /**
      * Creates a soft-reference based L2 cache wrapping the specified
      * L1 cache.
@@ -108,10 +114,12 @@ public class SoftCache implements CacheP
      * @param internal non null internal cache.
      * @throws NullPointerException if the internal cache is null.
      */
-    public SoftCache(CachePolicy internal) throws NullPointerException {
-        this(DEFAULT_LOAD_FACTOR, internal);
+    public SoftCache( CachePolicy internal ) throws NullPointerException
+    {
+        this( DEFAULT_LOAD_FACTOR, internal );
     }
 
+
     /**
      * Creates a soft-reference based L2 cache wrapping the specified
      * L1 cache. This constructor is somewhat implementation-specific,
@@ -124,14 +132,18 @@ public class SoftCache implements CacheP
      * @throws IllegalArgumentException if the load factor is nonpositive.
      * @throws NullPointerException if the internal cache is null.
      */
-    public SoftCache(float loadFactor, CachePolicy internal) throws IllegalArgumentException,
NullPointerException {
-        if (internal == null) {
+    public SoftCache( float loadFactor, CachePolicy internal ) throws IllegalArgumentException,
NullPointerException
+    {
+        if ( internal == null )
+        {
             throw new NullPointerException( I18n.err( I18n.ERR_531 ) );
         }
-        _internal = internal;
-        _cacheMap = new HashMap(INITIAL_CAPACITY, loadFactor);
+        
+        this.internal = internal;
+        map = new HashMap( INITIAL_CAPACITY, loadFactor );
     }
 
+
     /**
      * Adds the specified value to the cache under the specified key. Note
      * that the object is added to both this and the internal cache.
@@ -141,17 +153,23 @@ public class SoftCache implements CacheP
      *         would have experienced while evicting an object it currently
      *         cached.
      */
-    public void put(Object key, Object value) throws CacheEvictionException {
-        if (key == null) {
+    public void put( Object key, Object value ) throws CacheEvictionException
+    {
+        if ( key == null )
+        {
             throw new IllegalArgumentException( I18n.err( I18n.ERR_532 ) );
-        } else if (value == null) {
+        }
+        else if ( value == null )
+        {
             throw new IllegalArgumentException( I18n.err( I18n.ERR_533 ) );
         }
-        _internal.put(key, value);
+        
+        internal.put( key, value );
         removeClearedEntries();
-        _cacheMap.put(key, new Entry(key, value, _clearQueue));
+        map.put( key, new Entry( key, value, clearQueue ) );
     }
 
+
     /**
      * Gets the object cached under the specified key.
      * <p>
@@ -172,89 +190,115 @@ public class SoftCache implements CacheP
      * @return the object stored under the key specified; null if the
      *         object is not (nolonger) accessible via this cache.
      */
-    public Object get(Object key) {
+    public Object get( Object key )
+    {
         // first try the internal cache.
-        Object value = _internal.get(key);
-        if (value != null) {
+        Object value = internal.get( key );
+        
+        if ( value != null )
+        {
             return value;
         }
+        
         // poll and remove cleared references.
         removeClearedEntries();
-        Entry entry = (Entry)_cacheMap.get(key);
-        if (entry == null) { // object is not in cache.
+        Entry entry = ( Entry ) map.get( key );
+        
+        if ( entry == null )
+        { // object is not in cache.
             return null;
         }
+        
         value = entry.getValue();
-        if (value == null) { // object was in cache, but it was cleared.
+        
+        if ( value == null )
+        { // object was in cache, but it was cleared.
             return null;
         }
+        
         // we have the object. so we try to re-insert it into internal cache
-        try {
-            _internal.put(key, value);
-        } catch (CacheEvictionException e) {
+        try
+        {
+            internal.put( key, value );
+        }
+        catch ( CacheEvictionException e )
+        {
             // if the internal cache causes a fuss, we kick the object out.
-            _cacheMap.remove(key);
+            map.remove( key );
             return null;
         }
+        
         return value;
     }
 
+
     /**
      * Removes any object stored under the key specified. Note that the
      * object is removed from both this (L2) and the internal (L1)
      * cache.
      * @param key the key whose object should be removed
      */
-    public void remove(Object key) {
-        _cacheMap.remove(key);
-        _internal.remove(key);
+    public void remove( Object key )
+    {
+        map.remove( key );
+        internal.remove( key );
     }
 
+
     /**
      * Removes all objects in this (L2) and its internal (L1) cache.
      */
-    public void removeAll() {
-        _cacheMap.clear();
-        _internal.removeAll();
+    public void removeAll()
+    {
+        map.clear();
+        internal.removeAll();
     }
 
+
     /**
      * Gets all the objects stored by the internal (L1) cache.
      * @return an enumeration of objects in internal cache.
      */
-    public Enumeration elements() {
-        return _internal.elements();
+    public Enumeration elements()
+    {
+        return internal.elements();
     }
 
+
     /**
      * Adds the specified listener to this cache. Note that the events
      * fired by this correspond to the <em>internal</em> cache's events.
      * @param listener the (non-null) listener to add to this policy
      * @throws IllegalArgumentException if listener is null.
      */
-    public void addListener(CachePolicyListener listener)
-            throws IllegalArgumentException {
-        _internal.addListener(listener);
+    public void addListener( CachePolicyListener listener ) throws IllegalArgumentException
+    {
+        internal.addListener( listener );
     }
 
+
     /**
      * Removes a listener that was added earlier.
      * @param listener the listener to remove.
      */
-    public void removeListener(CachePolicyListener listener) {
-        _internal.removeListener(listener);
+    public void removeListener( CachePolicyListener listener )
+    {
+        internal.removeListener( listener );
     }
 
+
     /**
      * Cleans the mapping structure of any obsolete entries. This is usually
      * called before insertions and lookups on the mapping structure. The
      * runtime of this is usually very small, but it can be as expensive as
      * n * log(n) if a large number of soft references were recently cleared.
      */
-    private final void removeClearedEntries() {
-        for (Reference r = _clearQueue.poll(); r != null; r = _clearQueue.poll()) {
-            Object key = ((Entry)r).getKey();
-            _cacheMap.remove(key);
+    private final void removeClearedEntries()
+    {
+        for ( Reference r = clearQueue.poll(); r != null; r = clearQueue.poll() )
+        {
+            Object key = ( ( Entry ) r ).getKey();
+            map.remove( key );
         }
     }
 
@@ -265,31 +309,38 @@ public class SoftCache implements CacheP
      * keys drastically improves the performance of removing the pair
      * from the map (see {@link SoftCache#removeClearedEntries()}.)
      */
-    private static class Entry extends SoftReference {
-        private final Object _key;
+    private static class Entry extends SoftReference
+    {
+        private final Object key;
+
 
         /**
          * Constructor that uses <code>value</code> as the soft
          * reference's referent.
          */
-        public Entry(Object key, Object value, ReferenceQueue queue) {
-            super(value, queue);
-            _key = key;
+        public Entry( Object key, Object value, ReferenceQueue queue )
+        {
+            super( value, queue );
+            this.key = key;
         }
 
+
         /**
          * Gets the key
          * @return the key associated with this value.
          */
-        final Object getKey() {
-            return _key;
+        final Object getKey()
+        {
+            return key;
         }
 
+
         /**
          * Gets the value
          * @return the value; null if it is no longer accessible
          */
-        final Object getValue() {
+        final Object getValue()
+        {
             return this.get();
         }
     }

Modified: directory/apacheds/trunk/jdbm/src/main/java/jdbm/recman/CacheRecordManager.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/jdbm/src/main/java/jdbm/recman/CacheRecordManager.java?rev=948098&r1=948097&r2=948098&view=diff
==============================================================================
--- directory/apacheds/trunk/jdbm/src/main/java/jdbm/recman/CacheRecordManager.java (original)
+++ directory/apacheds/trunk/jdbm/src/main/java/jdbm/recman/CacheRecordManager.java Tue May
25 16:54:23 2010
@@ -72,22 +72,22 @@ import org.apache.directory.server.i18n.
 public class CacheRecordManager implements RecordManager
 {
     /** Wrapped RecordManager */
-    protected RecordManager recMgr;
+    protected RecordManager recordManager;
 
     /** Cache for underlying RecordManager */
-    protected CachePolicy<Long,CacheEntry> cache;
+    protected CachePolicy<Long, CacheEntry> cache;
 
 
     /**
      * Construct a CacheRecordManager wrapping another RecordManager and
      * using a given cache policy.
      *
-     * @param recMgr Wrapped RecordManager
+     * @param recordManager Wrapped RecordManager
      * @param cache Cache policy
      */
-    public CacheRecordManager( RecordManager recMgr, CachePolicy<Long,CacheEntry> cache
)
+    public CacheRecordManager( RecordManager recordManager, CachePolicy<Long,CacheEntry>
cache )
     {
-        if ( recMgr == null ) 
+        if ( recordManager == null ) 
         {
             throw new IllegalArgumentException( I18n.err( I18n.ERR_517 ) );
         }
@@ -97,9 +97,8 @@ public class CacheRecordManager implemen
             throw new IllegalArgumentException( I18n.err( I18n.ERR_542 ) );
         }
 
-        this.recMgr = recMgr;
+        this.recordManager = recordManager;
         this.cache = cache;
-        
         this.cache.addListener( new CacheListener() );
     }
 
@@ -112,7 +111,7 @@ public class CacheRecordManager implemen
      */
     public RecordManager getRecordManager()
     {
-        return recMgr;
+        return recordManager;
     }
 
     
@@ -153,7 +152,7 @@ public class CacheRecordManager implemen
     {
         checkIfClosed();
 
-        long recid = recMgr.insert( obj, serializer );
+        long recid = recordManager.insert( obj, serializer );
         
         try 
         {
@@ -163,6 +162,7 @@ public class CacheRecordManager implemen
         {
             throw new WrappedRuntimeException( except );
         }
+        
         return recid;
     }
 
@@ -177,7 +177,10 @@ public class CacheRecordManager implemen
     {
         checkIfClosed();
 
-        recMgr.delete( recid );
+        // Remove the entry from the underlying storage
+        recordManager.delete( recid );
+        
+        // And now update the cache
         cache.remove( recid );
     }
 
@@ -205,12 +208,10 @@ public class CacheRecordManager implemen
      */
     public synchronized void update( long recid, Object obj, Serializer serializer ) throws
IOException
     {
-        CacheEntry  entry;
-        
         checkIfClosed();
 
         try {
-            entry = cache.get( recid );
+            CacheEntry entry = cache.get( recid );
             
             if ( entry != null ) 
             {
@@ -261,7 +262,7 @@ public class CacheRecordManager implemen
         if ( entry == null ) 
         {
             entry = new CacheEntry( recid, null, serializer, false );
-            entry.obj = recMgr.fetch( recid, serializer );
+            entry.obj = recordManager.fetch( recid, serializer );
             
             try 
             {
@@ -294,8 +295,8 @@ public class CacheRecordManager implemen
         checkIfClosed();
 
         updateCacheEntries();
-        recMgr.close();
-        recMgr = null;
+        recordManager.close();
+        recordManager = null;
         cache = null;
     }
 
@@ -310,7 +311,7 @@ public class CacheRecordManager implemen
     {
         checkIfClosed();
 
-        return recMgr.getRootCount();
+        return recordManager.getRootCount();
     }
 
 
@@ -323,7 +324,7 @@ public class CacheRecordManager implemen
     {
         checkIfClosed();
 
-        return recMgr.getRoot( id );
+        return recordManager.getRoot( id );
     }
 
 
@@ -336,7 +337,7 @@ public class CacheRecordManager implemen
     {
         checkIfClosed();
 
-        recMgr.setRoot( id, rowid );
+        recordManager.setRoot( id, rowid );
     }
 
 
@@ -347,7 +348,7 @@ public class CacheRecordManager implemen
     {
         checkIfClosed();
         updateCacheEntries();
-        recMgr.commit();
+        recordManager.commit();
     }
 
 
@@ -358,7 +359,7 @@ public class CacheRecordManager implemen
     {
         checkIfClosed();
 
-        recMgr.rollback();
+        recordManager.rollback();
 
         // discard all cache entries since we don't know which entries
         // where part of the transaction
@@ -374,7 +375,7 @@ public class CacheRecordManager implemen
     {
         checkIfClosed();
 
-        return recMgr.getNamedObject( name );
+        return recordManager.getNamedObject( name );
     }
 
 
@@ -385,7 +386,7 @@ public class CacheRecordManager implemen
     {
         checkIfClosed();
 
-        recMgr.setNamedObject( name, recid );
+        recordManager.setNamedObject( name, recid );
     }
 
 
@@ -394,7 +395,7 @@ public class CacheRecordManager implemen
      */
     private void checkIfClosed() throws IllegalStateException
     {
-        if ( recMgr == null ) 
+        if ( recordManager == null ) 
         {
             throw new IllegalStateException( I18n.err( I18n.ERR_538 ) );
         }
@@ -414,13 +415,12 @@ public class CacheRecordManager implemen
             
             if ( entry.isDirty ) 
             {
-                recMgr.update( entry.recid, entry.obj, entry.serializer );
+                recordManager.update( entry.recid, entry.obj, entry.serializer );
                 entry.isDirty = false;
             }
         }
     }
 
-    
     /**
      * A class to store a cached entry. 
      */
@@ -457,7 +457,7 @@ public class CacheRecordManager implemen
             {
                 try 
                 {
-                    recMgr.update( entry.recid, entry.obj, entry.serializer );
+                    recordManager.update( entry.recid, entry.obj, entry.serializer );
                 } 
                 catch ( IOException except ) 
                 {

Modified: directory/apacheds/trunk/jdbm/src/test/java/jdbm/btree/TestBTree.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/jdbm/src/test/java/jdbm/btree/TestBTree.java?rev=948098&r1=948097&r2=948098&view=diff
==============================================================================
--- directory/apacheds/trunk/jdbm/src/test/java/jdbm/btree/TestBTree.java (original)
+++ directory/apacheds/trunk/jdbm/src/test/java/jdbm/btree/TestBTree.java Tue May 25 16:54:23
2010
@@ -71,7 +71,6 @@ import static org.junit.Assert.fail;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertNotSame;
 
 
 /**
@@ -336,6 +335,33 @@ public class TestBTree
 
 
     /**
+     *  Test to insert many objects into one btree
+     */
+    @Test
+    public void testInsertMany() throws IOException
+    {
+        BTree<String, String> tree;
+
+        RecordManager recordManager = RecordManagerFactory.createRecordManager( "test" );
+        tree = new BTree<String, String>( recordManager, new StringComparator() );
+        tree.setPageSize( 4 );
+
+        // insert different objects and retrieve them
+        tree.insert( "test1", "value1", false );
+        tree.insert( "test2", "value2", false );
+        tree.insert( "test3", "value3", false );
+        tree.insert( "test4", "value4", false );
+        tree.insert( "test5", "value5", false );
+        tree.insert( "test6", "value6", false );
+        
+        assertEquals( "value2", tree.find( "test2" ) );
+        assertEquals( "value1", tree.find( "test1" ) );
+
+        recordManager.close();
+    }
+
+
+    /**
      *  Test to remove  objects from the btree. (cdaller)
      */
     @Test



Mime
View raw message