Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 89741 invoked from network); 28 May 2002 16:08:50 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 28 May 2002 16:08:50 -0000 Received: (qmail 5749 invoked by uid 97); 28 May 2002 16:08:49 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@jakarta.apache.org Received: (qmail 5726 invoked by uid 97); 28 May 2002 16:08:48 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 5703 invoked by uid 98); 28 May 2002 16:08:47 -0000 X-Antivirus: nagoya (v4198 created Apr 24 2002) X-Qmail-Scanner-Mail-From: baliuka@centras.lt via jim.skynet.vl X-Qmail-Scanner: 1.03 (Clean. Processed in 1.488524 secs) Message-ID: <008a01c20661$f101da20$0111010a@user> From: "Juozas Baliuka" To: "Jakarta Commons Developers List" References: Subject: Re: [SUBMIT][Collections] ReferenceMap, take 2 Date: Tue, 28 May 2002 18:08:44 +0200 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_0087_01C20672.B47C5240" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N ------=_NextPart_000_0087_01C20672.B47C5240 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Hi, It is some Cache implementation from sandbox, it doe's not implements Map interface, but some ideas can be useful, it implements LRU, but doe's not lose referenced values. And I think it is no need for Reference map abstarction it is three Reference types in JAVA and all of them do almost the same in practice. ----- Original Message ----- From: "Jack, Paul" To: "'Jakarta Commons Developers List'" Sent: Tuesday, May 28, 2002 5:48 PM Subject: RE: [SUBMIT][Collections] ReferenceMap, take 2 > > Just an idea....is it possible to write ReferenceMap as a > > wrapper around > > another Map? If so, then we could have ReferenceSortedMap, > > ReferenceList > > etc. all within ReferenceCollections. > > Yes, but not efficiently. For weak or soft keys, you'd have > to create a bunch of Reference objects that you don't actually > need: > > public Object get(Object key) { > key = toReference(keyType, key); > Object value = underlyingMap.get(key); > if (valueType > HARD) return ((Reference)value).get(); > return value; > } > > Also I'm not convinced it's possible to write the iterator > effectively: The hasNext() method must guarantee that the > next call to next() will return a valid object; however, the > Reference to that object might have cleared between the > call to hasNext() and next(). (Though I haven't spent too > much time thinking about this problem, there may be perfectly > sound solutions to it). > > Though it would be nice to have a referenced SequencedHashMap > or LRUMap. > > Also purging elements from an arbitrary list would be an > expensive operation (you'd have to traverse the entire list > looking for the purged element, and then invoke remove(index), > which would traverse the list again for a sequential list or > require a memory copy for a random access list). > > > Also, in your tests I noted that you call System.gc() to > > cause the garbage > > collector to run. Does this not just 'suggest' that the gc be > > run, thus > > making the tests unreliable? > > It's true. The tests should use their own ReferenceQueue > to wait until the objects have been GC'd. I'll modify the > tests accordingly. > > -Paul > > > -- > To unsubscribe, e-mail: > For additional commands, e-mail: > ------=_NextPart_000_0087_01C20672.B47C5240 Content-Type: application/octet-stream; name="SoftRefMemoryCache.java" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="SoftRefMemoryCache.java" /* * The Apache Software License, Version 1.1 * * * Copyright (c) 2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software = itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache Cocoon" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . */ package org.apache.commons.simplestore.cache.impl; import org.apache.commons.simplestore.cache.Cache; import java.lang.ref.ReferenceQueue; import java.lang.ref.SoftReference; import java.util.Map; /** *@author Juozas Baliuka * baliuka@mwm.lt *@author Gerhard Froehlich * g-froehlich@gmx.de *@version $Id: SoftRefMemoryCache.java,v 1.4 2002/03/10 12:32:30 = baliuka Exp $ */ public class SoftRefMemoryCache implements Cache, = org.apache.commons.simplestore.tools.Constants { =20 private int m_current =3D 0; private int m_maxStrongRefCount; private Map m_map; private Object[] m_strongRefs; private ReferenceQueue m_queue =3D new ReferenceQueue(); /** * Creates new SoftRefMemoryCache * *@param map *@param maxStrongRefCount */ protected SoftRefMemoryCache(Map map, int maxStrongRefCount) { this.m_map =3D map; if (maxStrongRefCount < 0) { throw new java.lang.IllegalArgumentException(); } this.m_maxStrongRefCount =3D maxStrongRefCount; if (maxStrongRefCount > 0) { m_strongRefs =3D new Object[maxStrongRefCount]; } } public static Cache getInstance(Map map, int maxStrongRef) { return new SynchronizedCache(new SoftRefMemoryCache(map, = maxStrongRef)); } /** * Get the object associated to the given unique key. * *@param key the Key Object *@return requested object or null */ public Object get(Object key) { removeSoftRef(); Object object =3D null; SoftRef ref =3D (SoftRef) m_map.get(key); if (ref !=3D null) { object =3D ref.get(); } addStrongRef(object); return object; } /** * Cache the object associated to the given unique key. * *@param key the key object *@param object */ public void put(Object key, Object object) { removeSoftRef(); internalStoreObject(key, object); } private SoftRef makeValue(Object key, Object value, ReferenceQueue = queue) { return new SoftRef(key, value, queue); } // remove unused keys private void removeSoftRef() { SoftRef ref =3D (SoftRef) m_queue.poll(); while (ref !=3D null) { m_map.remove(ref.key); if (DEBUG) { System.out.println("Key " + ref.key + " removed from = Reference queue, map size is " + m_map.size()); } ref =3D (SoftRef) m_queue.poll(); } } private void addStrongRef(Object object) { if (m_strongRefs !=3D null) { m_strongRefs[(m_current++) % m_maxStrongRefCount] =3D = object; } } private void internalStoreObject(Object key, Object object) { SoftRef ref =3D makeValue(key, object, m_queue); addStrongRef(ref.get()); m_map.put(key, ref); } static class SoftRef extends SoftReference { Object key; private SoftRef(Object key, Object object, ReferenceQueue queue) = { super(object, queue); this.key =3D key; } } } ------=_NextPart_000_0087_01C20672.B47C5240 Content-Type: text/plain; charset=us-ascii -- To unsubscribe, e-mail: For additional commands, e-mail: ------=_NextPart_000_0087_01C20672.B47C5240--