/** * This creates an unbounded version. Setting the max objects will result in spooling on * subsequent puts. */ public AbstractLRUMap() { list = new DoubleLinkedList<LRUElementDescriptor<K, V>>(); // normal hashtable is faster for // sequential keys. map = new ConcurrentHashMap<K, LRUElementDescriptor<K, V>>(); }
/** * Adds an item to the end of the queue, which is the front of the list. * <p> * @param object */ public void add( T object ) { if ( list.size() >= maxSize ) { list.removeLast(); } list.addFirst( new DoubleLinkedListNode<T>( object ) ); }
/** * Makes the item the last in the list. * <p> * @param me */ @Override protected void adjustListForGet( MemoryElementDescriptor<K, V> me ) { list.makeLast( me ); } }
/** verify that it's added last. */ public void testMakeLast_wasAlone() { // SETUP DoubleLinkedList<DoubleLinkedListNode<String>> list = new DoubleLinkedList<DoubleLinkedListNode<String>>(); String payload1 = "payload1"; DoubleLinkedListNode<String> node1 = new DoubleLinkedListNode<String>( payload1 ); list.addFirst( node1 ); // DO WORK list.makeLast( node1 ); // VERIFY assertEquals( "Wrong size", 1, list.size() ); assertEquals( "Wrong last", node1, list.getLast() ); assertEquals( "Wrong first", node1, list.getFirst() ); }
/** verify that the last is added when the list is empty. */ public void testAddLast_Empty() { // SETUP DoubleLinkedList<DoubleLinkedListNode<String>> list = new DoubleLinkedList<DoubleLinkedListNode<String>>(); String payload1 = "payload1"; DoubleLinkedListNode<String> node1 = new DoubleLinkedListNode<String>( payload1 ); // WO WORK list.addLast( node1 ); // VERIFY assertEquals( "Wrong last", node1, list.getLast() ); }
LRUElementDescriptor<K, V> first = list.getFirst(); old = map.put(first.getKey(), first); list.remove( old ); try LRUElementDescriptor<K, V> last = list.getLast(); if (last != null) verifyCache(); list.removeLast();
/** verify that the entries are dumped. */ public void testDumpEntries_DebugTrue() { // SETUP StringWriter stringWriter = new StringWriter(); TestLogConfigurationUtil.configureLogger( stringWriter, DoubleLinkedList.class.getName() ); DoubleLinkedList<DoubleLinkedListNode<String>> list = new DoubleLinkedList<DoubleLinkedListNode<String>>(); String payload1 = "payload1"; DoubleLinkedListNode<String> node1 = new DoubleLinkedListNode<String>( payload1 ); String payload2 = "payload2"; DoubleLinkedListNode<String> node2 = new DoubleLinkedListNode<String>( payload2 ); list.addLast( node1 ); list.addLast( node2 ); list.debugDumpEntries(); // WO WORK String result = stringWriter.toString(); // VERIFY assertTrue( "Missing node in log dump", result.indexOf( payload1 ) != -1 ); assertTrue( "Missing node in log dump", result.indexOf( payload2 ) != -1 ); } }
/** * Dump the cache entries from first to list for debugging. */ @SuppressWarnings("unchecked") // No generics for public fields public void dumpCacheEntries() { log.debug( "dumpingCacheEntries" ); for ( MemoryElementDescriptor<K, V> me = list.getFirst(); me != null; me = (MemoryElementDescriptor<K, V>) me.next ) { log.debug( "dumpCacheEntries> key=" + me.ce.getKey() + ", val=" + me.ce.getVal() ); } }
/** * Returns the size of the list. * <p> * @return int */ private int dumpCacheSize() { return list.size(); }
+ list.size() + " elements"); log.debug("verifycache: checking linked list by key "); for (MemoryElementDescriptor<K, V> li = list.getFirst(); li != null; li = (MemoryElementDescriptor<K, V>) li.next) for (MemoryElementDescriptor<K, V> li3 = list.getFirst(); li3 != null; li3 = (MemoryElementDescriptor<K, V>) li3.next) for (MemoryElementDescriptor<K, V> li2 = list.getFirst(); li2 != null; li2 = (MemoryElementDescriptor<K, V>) li2.next)
/** * Adds a new node to the start of the link list. * <p> * @param key * @param val The feature to be added to the First */ private void addFirst(K key, V val) { lock.lock(); try { LRUElementDescriptor<K, V> me = new LRUElementDescriptor<K, V>(key, val); list.addFirst( me ); } finally { lock.unlock(); } }
/** * Removes the specified node from the link list. * <p> * @return The last node if there was one to remove. */ public synchronized T removeLast() { if ( log.isDebugEnabled() ) { log.debug( "removing last node" ); } T temp = last; if ( last != null ) { remove( last ); } return temp; }
/** * Adds a new node to the end of the link list. * <p> * @param ce The feature to be added to the First * @return MemoryElementDescriptor */ protected MemoryElementDescriptor<K, V> addLast( ICacheElement<K, V> ce ) { lock.lock(); try { MemoryElementDescriptor<K, V> me = new MemoryElementDescriptor<K, V>(ce); list.addLast(me); verifyCache(ce.getKey()); return me; } finally { lock.unlock(); } }
/** * Makes the item the first in the list. * <p> * @param me */ @Override protected void adjustListForGet( MemoryElementDescriptor<K, V> me ) { list.makeFirst( me ); } }
/** verify that it's added last. */ public void testMakeLast_wasAlone() { // SETUP DoubleLinkedList<DoubleLinkedListNode<String>> list = new DoubleLinkedList<DoubleLinkedListNode<String>>(); String payload1 = "payload1"; DoubleLinkedListNode<String> node1 = new DoubleLinkedListNode<String>( payload1 ); list.addFirst( node1 ); // DO WORK list.makeLast( node1 ); // VERIFY assertEquals( "Wrong size", 1, list.size() ); assertEquals( "Wrong last", node1, list.getLast() ); assertEquals( "Wrong first", node1, list.getFirst() ); }
/** verify that the last is added when the list is empty. */ public void testAddLast_Empty() { // SETUP DoubleLinkedList<DoubleLinkedListNode<String>> list = new DoubleLinkedList<DoubleLinkedListNode<String>>(); String payload1 = "payload1"; DoubleLinkedListNode<String> node1 = new DoubleLinkedListNode<String>( payload1 ); // WO WORK list.addLast( node1 ); // VERIFY assertEquals( "Wrong last", node1, list.getLast() ); }
LRUElementDescriptor<K, V> first = list.getFirst(); old = map.put(first.getKey(), first); list.remove( old ); try LRUElementDescriptor<K, V> last = list.getLast(); if (last != null) verifyCache(); list.removeLast();
/** verify that the entries are dumped. */ public void testDumpEntries_DebugTrue() { // SETUP StringWriter stringWriter = new StringWriter(); TestLogConfigurationUtil.configureLogger( stringWriter, DoubleLinkedList.class.getName() ); DoubleLinkedList<DoubleLinkedListNode<String>> list = new DoubleLinkedList<DoubleLinkedListNode<String>>(); String payload1 = "payload1"; DoubleLinkedListNode<String> node1 = new DoubleLinkedListNode<String>( payload1 ); String payload2 = "payload2"; DoubleLinkedListNode<String> node2 = new DoubleLinkedListNode<String>( payload2 ); list.addLast( node1 ); list.addLast( node2 ); list.debugDumpEntries(); // WO WORK String result = stringWriter.toString(); // VERIFY assertTrue( "Missing node in log dump", result.indexOf( payload1 ) != -1 ); assertTrue( "Missing node in log dump", result.indexOf( payload2 ) != -1 ); } }
/** * Dump the cache entries from first to list for debugging. */ @SuppressWarnings("unchecked") // No generics for public fields public void dumpCacheEntries() { log.debug( "dumpingCacheEntries" ); for ( LRUElementDescriptor<K, V> me = list.getFirst(); me != null; me = (LRUElementDescriptor<K, V>) me.next ) { if ( log.isDebugEnabled() ) { log.debug( "dumpCacheEntries> key=" + me.getKey() + ", val=" + me.getPayload() ); } } }