Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 12811 invoked from network); 24 May 2010 15:21:15 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 24 May 2010 15:21:15 -0000 Received: (qmail 11689 invoked by uid 500); 24 May 2010 15:21:15 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 11637 invoked by uid 500); 24 May 2010 15:21:15 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 11630 invoked by uid 99); 24 May 2010 15:21:15 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 24 May 2010 15:21:15 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 24 May 2010 15:21:12 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 4F152238899C; Mon, 24 May 2010 15:20:50 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r947666 - in /directory/apacheds/trunk/jdbm/src/main/java/jdbm: helper/MRU.java recman/CacheRecordManager.java Date: Mon, 24 May 2010 15:20:50 -0000 To: commits@directory.apache.org From: elecharny@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100524152050.4F152238899C@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: elecharny Date: Mon May 24 15:20:37 2010 New Revision: 947666 URL: http://svn.apache.org/viewvc?rev=947666&view=rev Log: o Replaced legacy classes like hashtable by Map o More generic added o Code refactoring Modified: directory/apacheds/trunk/jdbm/src/main/java/jdbm/helper/MRU.java directory/apacheds/trunk/jdbm/src/main/java/jdbm/recman/CacheRecordManager.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=947666&r1=947665&r2=947666&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 Mon May 24 15:20:37 2010 @@ -47,9 +47,13 @@ package jdbm.helper; + +import java.util.ArrayList; import java.util.Enumeration; -import java.util.Hashtable; -import java.util.Vector; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; import org.apache.directory.server.i18n.I18n; @@ -62,67 +66,77 @@ import org.apache.directory.server.i18n. * @author Alex Boisvert * @version $Id: MRU.java,v 1.8 2005/06/25 23:12:31 doomdark Exp $ */ -public class MRU implements CachePolicy +public class MRU implements CachePolicy { - - /** Cached object hashtable */ - Hashtable _hash = new Hashtable(); + /** Cached object Map */ + Map map = new HashMap(); /** * Maximum number of objects in the cache. */ - int _max; + int max; /** * Beginning of linked-list of cache elements. First entry is element * which has been used least recently. */ - CacheEntry _first; + CacheEntry first; /** * End of linked-list of cache elements. Last entry is element * which has been used most recently. */ - CacheEntry _last; - + CacheEntry last; /** * Cache eviction listeners */ - Vector listeners = new Vector(); + List listeners = new ArrayList(); /** * Construct an MRU with a given maximum number of objects. */ - public MRU(int max) { - if (max <= 0) { - throw new IllegalArgumentException( I18n.err( I18n.ERR_528 )); + public MRU( int max ) + { + if ( max <= 0 ) + { + throw new IllegalArgumentException( I18n.err( I18n.ERR_528 ) ); } - _max = max; + + this.max = max; } /** * Place an object in the cache. */ - public void put(Object key, Object value) throws CacheEvictionException { - CacheEntry entry = (CacheEntry)_hash.get(key); - if (entry != null) { - entry.setValue(value); - touchEntry(entry); - } else { + public void put( Object key, Object value ) throws CacheEvictionException + { + CacheEntry entry = ( CacheEntry ) map.get( key ); + + if ( entry != null ) + { + entry.setValue( value ); + touchEntry( entry ); + } + else + { - if (_hash.size() == _max) { + if ( map.size() == max ) + { // purge and recycle entry entry = purgeEntry(); - entry.setKey(key); - entry.setValue(value); - } else { - entry = new CacheEntry(key, value); + entry.setKey( key ); + entry.setValue( value ); } - addEntry(entry); - _hash.put(entry.getKey(), entry); + else + { + entry = new CacheEntry( key, value ); + } + + addEntry( entry ); + map.put( entry.getKey(), entry ); } } @@ -130,12 +144,17 @@ public class MRU implements CachePolicy /** * Obtain an object in the cache */ - public Object get(Object key) { - CacheEntry entry = (CacheEntry)_hash.get(key); - if (entry != null) { - touchEntry(entry); + public Object get( Object key ) + { + CacheEntry entry = ( CacheEntry ) map.get( key ); + + if ( entry != null ) + { + touchEntry( entry ); return entry.getValue(); - } else { + } + else + { return null; } } @@ -144,11 +163,14 @@ public class MRU implements CachePolicy /** * Remove an object from the cache */ - public void remove(Object key) { - CacheEntry entry = (CacheEntry)_hash.get(key); - if (entry != null) { - removeEntry(entry); - _hash.remove(entry.getKey()); + public void remove( Object key ) + { + CacheEntry entry = ( CacheEntry ) map.get( key ); + + if ( entry != null ) + { + removeEntry( entry ); + map.remove( entry.getKey() ); } } @@ -156,54 +178,68 @@ public class MRU implements CachePolicy /** * Remove all objects from the cache */ - public void removeAll() { - _hash = new Hashtable(); - _first = null; - _last = null; + public void removeAll() + { + map = new HashMap(); + first = null; + last = null; } /** * Enumerate elements' values in the cache */ - public Enumeration elements() { - return new MRUEnumeration(_hash.elements()); + public Enumeration elements() + { + return new MRUEnumeration( map.values().iterator() ); } + /** * Add a listener to this cache policy * * @param listener Listener to add to this policy */ - public void addListener(CachePolicyListener listener) { - if (listener == null) { + public void addListener( CachePolicyListener listener ) + { + if ( listener == null ) + { throw new IllegalArgumentException( I18n.err( I18n.ERR_539_BAD_BLOCK_ID ) ); } - if ( ! listeners.contains(listener)) { - listeners.addElement(listener); + + if ( !listeners.contains( listener ) ) + { + listeners.add( listener ); } } + /** * Remove a listener from this cache policy * * @param listener Listener to remove from this policy */ - public void removeListener(CachePolicyListener listener) { - listeners.removeElement(listener); + public void removeListener( CachePolicyListener listener ) + { + listeners.remove( listener ); } + /** * Add a CacheEntry. Entry goes at the end of the list. */ - protected void addEntry(CacheEntry entry) { - if (_first == null) { - _first = entry; - _last = entry; - } else { - _last.setNext(entry); - entry.setPrevious(_last); - _last = entry; + protected void addEntry( CacheEntry entry ) + { + if ( first == null ) + { + first = entry; + last = entry; + } + else + { + last.setNext( entry ); + entry.setPrevious( last ); + last = entry; } } @@ -211,57 +247,75 @@ public class MRU implements CachePolicy /** * Remove a CacheEntry from linked list */ - protected void removeEntry(CacheEntry entry) { - if (entry == _first) { - _first = entry.getNext(); - } - if (_last == entry) { - _last = entry.getPrevious(); + protected void removeEntry( CacheEntry entry ) + { + if ( entry == first ) + { + first = entry.getNext(); + } + + if ( last == entry ) + { + last = entry.getPrevious(); } + CacheEntry previous = entry.getPrevious(); CacheEntry next = entry.getNext(); - if (previous != null) { - previous.setNext(next); - } - if (next != null) { - next.setPrevious(previous); - } - entry.setPrevious(null); - entry.setNext(null); + + if ( previous != null ) + { + previous.setNext( next ); + } + + if ( next != null ) + { + next.setPrevious( previous ); + } + + entry.setPrevious( null ); + entry.setNext( null ); } + /** * Place entry at the end of linked list -- Most Recently Used */ - protected void touchEntry(CacheEntry entry) { - if (_last == entry) { + protected void touchEntry( CacheEntry entry ) + { + if ( last == entry ) + { return; } - removeEntry(entry); - addEntry(entry); + + removeEntry( entry ); + addEntry( entry ); } + /** * Purge least recently used object from the cache * * @return recyclable CacheEntry */ - protected CacheEntry purgeEntry() throws CacheEvictionException { - CacheEntry entry = _first; + protected CacheEntry purgeEntry() throws CacheEvictionException + { + CacheEntry entry = first; // Notify policy listeners first. if any of them throw an // eviction exception, then the internal data structure // remains untouched. CachePolicyListener listener; - for (int i=0; i implements Enumeration +{ + Iterator elements; - MRUEnumeration(Enumeration enume) { - _enum = enume; + + MRUEnumeration( Iterator elements ) + { + this.elements = elements; } - public boolean hasMoreElements() { - return _enum.hasMoreElements(); + + public boolean hasMoreElements() + { + return elements.hasNext(); } - public Object nextElement() { - CacheEntry entry = (CacheEntry)_enum.nextElement(); - return entry.getValue(); + + public K nextElement() + { + CacheEntry entry = elements.next(); + + return (K)entry.getValue(); } } 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=947666&r1=947665&r2=947666&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 Mon May 24 15:20:37 2010 @@ -154,6 +154,7 @@ public class CacheRecordManager implemen checkIfClosed(); long recid = recMgr.insert( obj, serializer ); + try { cache.put( recid, new CacheEntry( recid, obj, serializer, false ) ); @@ -256,10 +257,12 @@ public class CacheRecordManager implemen checkIfClosed(); CacheEntry entry = cache.get( recid ); + if ( entry == null ) { entry = new CacheEntry( recid, null, serializer, false ); entry.obj = recMgr.fetch( recid, serializer ); + try { cache.put( recid, entry );