jakarta-jcs-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From asm...@apache.org
Subject cvs commit: jakarta-turbine-jcs/src/java/org/apache/jcs/engine/memory/lru LRUMemoryCache.java LHMLRUMemoryCache.java
Date Tue, 13 Jul 2004 02:11:45 GMT
asmuts      2004/07/12 19:11:45

  Modified:    src/java/org/apache/jcs/engine/memory/lru
                        LRUMemoryCache.java LHMLRUMemoryCache.java
  Log:
  Converted to use the new linked list.
  
  Fixed removeAll problem.  The AbstractMemoryCache was only clearing the map.  Any subclasses
that didn't implement removeAll would have full lists and empty maps.  This solves a major
memory problem.
  
  Revision  Changes    Path
  1.26      +226 -319  jakarta-turbine-jcs/src/java/org/apache/jcs/engine/memory/lru/LRUMemoryCache.java
  
  Index: LRUMemoryCache.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-jcs/src/java/org/apache/jcs/engine/memory/lru/LRUMemoryCache.java,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- LRUMemoryCache.java	8 Jul 2004 00:48:45 -0000	1.25
  +++ LRUMemoryCache.java	13 Jul 2004 02:11:45 -0000	1.26
  @@ -32,6 +32,9 @@
   import org.apache.jcs.engine.control.group.GroupId;
   import org.apache.jcs.engine.control.group.GroupAttrName;
   
  +import org.apache.jcs.engine.memory.util.DoubleLinkedList;
  +import org.apache.jcs.engine.memory.util.MemoryElementDescriptor;
  +
   /**
    *  A fast reference management system. The least recently used items move to
    *  the end of the list and get spooled to disk if the cache hub is configured
  @@ -49,21 +52,25 @@
       extends AbstractMemoryCache
   {
     private final static Log log =
  -      LogFactory.getLog( LRUMemoryCache.class );
  +      LogFactory.getLog(LRUMemoryCache.class);
  +
  +  // double linked list for lru
  +  private DoubleLinkedList list;
   
  -  // LRU double linked list head/tail nodes
  -  private MemoryElementDescriptor first;
  -  private MemoryElementDescriptor last;
  +  int hitCnt = 0;
  +  int missCnt = 0;
  +  int putCnt = 0;
   
     /**
      *  For post reflection creation initialization
      *
      *@param  hub
      */
  -  public synchronized void initialize( CompositeCache hub )
  +  public synchronized void initialize(CompositeCache hub)
     {
  -    super.initialize( hub );
  -    log.info( "initialized LRUMemoryCache for " + cacheName );
  +    super.initialize(hub);
  +    list = new DoubleLinkedList();
  +    log.info("initialized LRUMemoryCache for " + cacheName);
     }
   
     /**
  @@ -72,117 +79,107 @@
      *@param  ce               Description of the Parameter
      *@exception  IOException
      */
  -  public void update( ICacheElement ce ) throws IOException
  +  public void update(ICacheElement ce) throws IOException
     {
  +    putCnt++;
  +
       // Asynchronisly create a MemoryElement
   
       ce.getElementAttributes().setLastAccessTimeNow();
       MemoryElementDescriptor old = null;
  -    synchronized ( this )
  +    synchronized (this)
       {
         // TODO address double synchronization of addFirst, use write lock
  -      addFirst( ce );
  +      addFirst(ce);
         // this must be synchronized
  -      old = ( MemoryElementDescriptor ) map.put( first.ce.getKey(), first );
  +      old = (MemoryElementDescriptor) map.put( ( (MemoryElementDescriptor) list.
  +                                                getFirst()).ce.getKey(),
  +                                              (MemoryElementDescriptor) list.
  +                                              getFirst());
       }
   
       // If the node was the same as an existing node, remove it.
  -
  -    if ( old != null && first.ce.getKey().equals( old.ce.getKey() ) )
  +    if (old != null &&
  +        ( (MemoryElementDescriptor) list.getFirst()).ce.getKey().
  +        equals(old.ce.getKey()))
       {
  -      removeNode( old );
  +      list.remove(old);
       }
   
       int size = map.size();
       // If the element limit is reached, we need to spool
   
  -    if ( size < this.cattr.getMaxObjects() )
  +    if (size < this.cattr.getMaxObjects())
       {
         return;
       }
  -    log.debug( "In memory limit reached, spooling" );
  +    log.debug("In memory limit reached, spooling");
   
       // Write the last 'chunkSize' items to disk.
  +    int chunkSizeCorrected = Math.min(size, chunkSize);
   
  -    int chunkSizeCorrected = Math.min( size, chunkSize );
  -
  -    if ( log.isDebugEnabled() )
  +    if (log.isDebugEnabled())
       {
  -      log.debug( "About to spool to disk cache, map size: " + size
  -                 + ", max objects: " + this.cattr.getMaxObjects()
  -                 + ", items to spool: " + chunkSizeCorrected );
  +      log.debug("About to spool to disk cache, map size: " + size
  +                + ", max objects: " + this.cattr.getMaxObjects()
  +                + ", items to spool: " + chunkSizeCorrected);
       }
   
       // The spool will put them in a disk event queue, so there is no
       // need to pre-queue the queuing.  This would be a bit wasteful
       // and wouldn't save much time in this synchronous call.
   
  -    for ( int i = 0; i < chunkSizeCorrected; i++ )
  +    for (int i = 0; i < chunkSizeCorrected; i++)
       {
  -      synchronized ( this )
  +      synchronized (this)
         {
  -        if ( last != null )
  +        if (list.getLast() != null)
           {
  -          if ( last.ce != null )
  +          if ( ( (MemoryElementDescriptor) list.getLast()).ce != null)
             {
  -            cache.spoolToDisk( last.ce );
  -            if ( !map.containsKey( last.ce.getKey() ) )
  +            cache.spoolToDisk( ( (MemoryElementDescriptor) list.getLast()).ce);
  +            if (!map.containsKey( ( (MemoryElementDescriptor) list.getLast()).
  +                                 ce.getKey()))
               {
  -              log.error( "update: map does not contain key: " + last.ce.getKey() );
  +              log.error("update: map does not contain key: " +
  +                        ( (MemoryElementDescriptor) list.getLast()).ce.getKey());
                 verifyCache();
               }
  -            if ( map.remove( last.ce.getKey() ) == null )
  +            if (map.remove( ( (MemoryElementDescriptor) list.getLast()).ce.
  +                           getKey()) == null)
               {
  -              log.warn( "update: remove failed for key: " + last.ce.getKey() );
  +              log.warn("update: remove failed for key: " +
  +                       ( (MemoryElementDescriptor) list.getLast()).ce.getKey());
                 verifyCache();
               }
             }
             else
             {
  -            throw new Error( "update: last.ce is null!" );
  +            throw new Error("update: last.ce is null!");
             }
  -          removeNode( last );
  +          list.removeLast();
           }
           else
           {
             verifyCache();
  -          throw new Error( "update: last is null!" );
  +          throw new Error("update: last is null!");
           }
         }
       }
   
  -    if ( log.isDebugEnabled() )
  +    if (log.isDebugEnabled())
       {
  -      log.debug( "update: After spool map size: " + map.size() );
  +      log.debug("update: After spool map size: " + map.size());
       }
  -    if ( map.size() != dumpCacheSize() )
  +    if (map.size() != dumpCacheSize())
       {
  -      log.error( "update: After spool, size mismatch: map.size() = "
  -                 + map.size() + ", linked list size = " +
  -                 dumpCacheSize() );
  +      log.error("update: After spool, size mismatch: map.size() = "
  +                + map.size() + ", linked list size = " +
  +                dumpCacheSize());
       }
     }
   
     /**
  -   * Remove all of the elements from both the Map and the linked
  -   * list implementation. Overrides base class.
  -   */
  -  public synchronized void removeAll() throws IOException
  -  {
  -    map.clear();
  -    for ( MemoryElementDescriptor me = first; me != null; )
  -    {
  -      if ( me.prev != null )
  -      {
  -        me.prev = null;
  -      }
  -      MemoryElementDescriptor next = me.next;
  -      me = next;
  -    }
  -    first = last = null;
  -  }
  -
  -  /**
      *  Get an item from the cache without affecting its last access time or
      *  position.
      *
  @@ -190,23 +187,23 @@
      *@return                  Element mathinh key if found, or null
      *@exception  IOException
      */
  -  public ICacheElement getQuiet( Serializable key ) throws IOException
  +  public ICacheElement getQuiet(Serializable key) throws IOException
     {
       ICacheElement ce = null;
   
  -    MemoryElementDescriptor me = ( MemoryElementDescriptor ) map.get( key );
  -    if ( me != null )
  +    MemoryElementDescriptor me = (MemoryElementDescriptor) map.get(key);
  +    if (me != null)
       {
  -      if ( log.isDebugEnabled() )
  +      if (log.isDebugEnabled())
         {
  -        log.debug( cacheName + ": LRUMemoryCache quiet hit for " + key );
  +        log.debug(cacheName + ": LRUMemoryCache quiet hit for " + key);
         }
   
         ce = me.ce;
       }
  -    else if ( log.isDebugEnabled() )
  +    else if (log.isDebugEnabled())
       {
  -      log.debug( cacheName + ": LRUMemoryCache quiet miss for " + key );
  +      log.debug(cacheName + ": LRUMemoryCache quiet miss for " + key);
       }
   
       return ce;
  @@ -219,33 +216,35 @@
      *@return                  ICacheElement if found, else null
      *@exception  IOException
      */
  -  public synchronized ICacheElement get( Serializable key ) throws IOException
  +  public synchronized ICacheElement get(Serializable key) throws IOException
     {
       ICacheElement ce = null;
   
  -    if ( log.isDebugEnabled() )
  +    if (log.isDebugEnabled())
       {
  -      log.debug( "getting item from cache " + cacheName + " for key " +
  -                 key );
  +      log.debug("getting item from cache " + cacheName + " for key " +
  +                key);
       }
   
  -    MemoryElementDescriptor me = ( MemoryElementDescriptor ) map.get( key );
  +    MemoryElementDescriptor me = (MemoryElementDescriptor) map.get(key);
   
  -    if ( me != null )
  +    if (me != null)
       {
  -      if ( log.isDebugEnabled() )
  +      hitCnt++;
  +      if (log.isDebugEnabled())
         {
  -        log.debug( cacheName + ": LRUMemoryCache hit for " + key );
  +        log.debug(cacheName + ": LRUMemoryCache hit for " + key);
         }
   
         ce = me.ce;
   
         ce.getElementAttributes().setLastAccessTimeNow();
  -      makeFirst( me );
  +      list.makeFirst(me);
       }
       else
       {
  -      log.debug( cacheName + ": LRUMemoryCache miss for " + key );
  +      missCnt++;
  +      log.debug(cacheName + ": LRUMemoryCache miss for " + key);
       }
   
       verifyCache();
  @@ -262,32 +261,32 @@
      *@return
      *@exception  IOException
      */
  -  public synchronized boolean remove( Serializable key ) throws IOException
  +  public synchronized boolean remove(Serializable key) throws IOException
     {
  -    if ( log.isDebugEnabled() )
  +    if (log.isDebugEnabled())
       {
  -      log.debug( "removing item for key: " + key );
  +      log.debug("removing item for key: " + key);
       }
   
       boolean removed = false;
   
       // handle partial removal
  -    if ( key instanceof String && ( ( String ) key )
  -         .endsWith( CacheConstants.NAME_COMPONENT_DELIMITER ) )
  +    if (key instanceof String && ( (String) key)
  +        .endsWith(CacheConstants.NAME_COMPONENT_DELIMITER))
       {
         // remove all keys of the same name hierarchy.
  -      synchronized ( map )
  +      synchronized (map)
         {
  -        for ( Iterator itr = map.entrySet().iterator(); itr.hasNext(); )
  +        for (Iterator itr = map.entrySet().iterator(); itr.hasNext(); )
           {
  -          Map.Entry entry = ( Map.Entry ) itr.next();
  +          Map.Entry entry = (Map.Entry) itr.next();
             Object k = entry.getKey();
   
  -          if ( k instanceof String
  -               && ( ( String ) k ).startsWith( key.toString() ) )
  +          if (k instanceof String
  +              && ( (String) k).startsWith(key.toString()))
             {
  -            removeNode( ( MemoryElementDescriptor )
  -                        entry.getValue() );
  +            list.remove( (MemoryElementDescriptor)
  +                        entry.getValue());
   
               itr.remove();
   
  @@ -296,23 +295,23 @@
           }
         }
       }
  -    else if ( key instanceof GroupId )
  +    else if (key instanceof GroupId)
       {
         // remove all keys of the same name hierarchy.
  -      synchronized ( map )
  +      synchronized (map)
         {
  -        for ( Iterator itr = map.entrySet().iterator(); itr.hasNext(); )
  +        for (Iterator itr = map.entrySet().iterator(); itr.hasNext(); )
           {
  -          Map.Entry entry = ( Map.Entry ) itr.next();
  +          Map.Entry entry = (Map.Entry) itr.next();
             Object k = entry.getKey();
   
  -          if ( k instanceof GroupAttrName
  -               && ( ( GroupAttrName ) k ).groupId.equals( key ) )
  +          if (k instanceof GroupAttrName
  +              && ( (GroupAttrName) k).groupId.equals(key))
             {
               itr.remove();
   
  -            removeNode( ( MemoryElementDescriptor )
  -                        entry.getValue() );
  +            list.remove( (MemoryElementDescriptor)
  +                        entry.getValue());
   
               removed = true;
             }
  @@ -323,11 +322,11 @@
       {
         // remove single item.
         MemoryElementDescriptor me =
  -          ( MemoryElementDescriptor ) map.remove( key );
  +          (MemoryElementDescriptor) map.remove(key);
   
  -      if ( me != null )
  +      if (me != null)
         {
  -        removeNode( me );
  +        list.remove(me);
           removed = true;
         }
       }
  @@ -335,13 +334,25 @@
       return removed;
     }
   
  +  /**
  +   * Remove all of the elements from both the Map and the linked
  +   * list implementation. Overrides base class.
  +   */
  +  public synchronized void removeAll() throws IOException
  +  {
  +    map.clear();
  +    list.removeAll();
  +  }
  +
  +
  +  // --------------------------- iteration mehods (iteration helpers)
     public class IteratorWrapper
         implements Iterator
     {
  -    private final Log log = LogFactory.getLog( LRUMemoryCache.class );
  +    private final Log log = LogFactory.getLog(LRUMemoryCache.class);
       private final Iterator i;
   
  -    private IteratorWrapper( Map m )
  +    private IteratorWrapper(Map m)
       {
         i = m.entrySet().iterator();
       }
  @@ -353,7 +364,7 @@
   
       public Object next()
       {
  -      return new MapEntryWrapper( ( Map.Entry ) i.next() );
  +      return new MapEntryWrapper( (Map.Entry) i.next());
       }
   
       public void remove()
  @@ -361,9 +372,9 @@
         i.remove();
       }
   
  -    public boolean equals( Object o )
  +    public boolean equals(Object o)
       {
  -      return i.equals( o );
  +      return i.equals(o);
       }
   
       public int hashCode()
  @@ -376,14 +387,14 @@
         implements Map.Entry
     {
       private final Map.Entry e;
  -    private MapEntryWrapper( Map.Entry e )
  +    private MapEntryWrapper(Map.Entry e)
       {
         this.e = e;
       }
   
  -    public boolean equals( Object o )
  +    public boolean equals(Object o)
       {
  -      return e.equals( o );
  +      return e.equals(o);
       }
   
       public Object getKey()
  @@ -393,7 +404,7 @@
   
       public Object getValue()
       {
  -      return ( ( MemoryElementDescriptor ) e.getValue() ).ce;
  +      return ( (MemoryElementDescriptor) e.getValue()).ce;
       }
   
       public int hashCode()
  @@ -401,11 +412,11 @@
         return e.hashCode();
       }
   
  -    public Object setValue( Object value )
  +    public Object setValue(Object value)
       {
  -      throw new UnsupportedOperationException( "Use normal cache methods"
  -                                               +
  -          " to alter the contents of the cache." );
  +      throw new UnsupportedOperationException("Use normal cache methods"
  +                                              +
  +                                              " to alter the contents of the cache.");
       }
     }
   
  @@ -416,7 +427,7 @@
      */
     public Iterator getIterator()
     {
  -    return new IteratorWrapper( map );
  +    return new IteratorWrapper(map);
     }
   
     /**
  @@ -427,7 +438,7 @@
     public Object[] getKeyArray()
     {
       // need a better locking strategy here.
  -    synchronized ( this )
  +    synchronized (this)
       {
         // may need to lock to map here?
         return map.keySet().toArray();
  @@ -435,79 +446,16 @@
     }
   
     // --------------------------- internal mehods (linked list implementation)
  -
  -  /**
  -   *  Removes the specified node from the link list.
  -   *
  -   *@param  me  Description of the Parameter
  -   */
  -  private synchronized void removeNode( MemoryElementDescriptor me )
  -  {
  -    if ( log.isDebugEnabled() )
  -    {
  -      log.debug( "removing node " + me.ce.getKey() + " from cache " +
  -                 cacheName );
  -    }
  -
  -    if ( me.next == null )
  -    {
  -      if ( me.prev == null )
  -      {
  -        // Make sure it really is the only node before setting head and
  -        // tail to null. It is possible that we will be passed a node
  -        // which has already been removed from the list, in which case
  -        // we should ignore it
  -
  -        if ( me == first && me == last )
  -        {
  -          first = last = null;
  -        }
  -      }
  -      else
  -      {
  -        // last but not the first.
  -        last = me.prev;
  -        last.next = null;
  -        me.prev = null;
  -      }
  -    }
  -    else if ( me.prev == null )
  -    {
  -      // first but not the last.
  -      first = me.next;
  -      first.prev = null;
  -      me.next = null;
  -    }
  -    else
  -    {
  -      // neither the first nor the last.
  -      me.prev.next = me.next;
  -      me.next.prev = me.prev;
  -      me.prev = me.next = null;
  -    }
  -  }
  -
     /**
      *  Adds a new node to the end of the link list. Currently not used.
      *
      *@param  ce  The feature to be added to the Last
      */
  -  private void addLast( CacheElement ce )
  +  private void addLast(CacheElement ce)
     {
  -    MemoryElementDescriptor me = new MemoryElementDescriptor( ce );
  -
  -    if ( first == null )
  -    {
  -      // empty list.
  -      first = me;
  -    }
  -    else
  -    {
  -      last.next = me;
  -      me.prev = last;
  -    }
  -    last = me;
  -    verifyCache( ce.getKey() );
  +    MemoryElementDescriptor me = new MemoryElementDescriptor(ce);
  +    list.addLast(me);
  +    verifyCache(ce.getKey());
     }
   
     /**
  @@ -515,66 +463,14 @@
      *
      *@param  ce  The feature to be added to the First
      */
  -  private synchronized void addFirst( ICacheElement ce )
  +  private synchronized void addFirst(ICacheElement ce)
     {
   
  -    MemoryElementDescriptor me = new MemoryElementDescriptor( ce );
  -
  -    if ( last == null )
  -    {
  -      // empty list.
  -      last = me;
  -    }
  -    else
  -    {
  -      first.prev = me;
  -      me.next = first;
  -    }
  -    first = me;
  +    MemoryElementDescriptor me = new MemoryElementDescriptor(ce);
  +    list.addFirst(me);
       return;
     }
   
  -  /**
  -   *  Moves an existing node to the start of the link list.
  -   *
  -   *@param  ce  Description of the Parameter
  -   */
  -  public void makeFirst( ICacheElement ce )
  -  {
  -    makeFirst( new MemoryElementDescriptor( ce ) );
  -  }
  -
  -  /**
  -   *  Moves an existing node to the start of the link list.
  -   *
  -   *@param  me  Description of the Parameter
  -   */
  -  public synchronized void makeFirst( MemoryElementDescriptor me )
  -  {
  -    if ( me.prev == null )
  -    {
  -      // already the first node.
  -      return;
  -    }
  -    me.prev.next = me.next;
  -
  -    if ( me.next == null )
  -    {
  -      // last but not the first.
  -      last = me.prev;
  -      last.next = null;
  -    }
  -    else
  -    {
  -      // neither the last nor the first.
  -      me.next.prev = me.prev;
  -    }
  -    first.prev = me;
  -    me.next = first;
  -    me.prev = null;
  -    first = me;
  -  }
  -
     // ---------------------------------------------------------- debug methods
   
     /**
  @@ -582,12 +478,12 @@
      */
     public void dumpMap()
     {
  -    log.debug( "dumpingMap" );
  -    for ( Iterator itr = map.entrySet().iterator(); itr.hasNext(); )
  +    log.debug("dumpingMap");
  +    for (Iterator itr = map.entrySet().iterator(); itr.hasNext(); )
       {
  -      Map.Entry e = ( Map.Entry ) itr.next();
  -      MemoryElementDescriptor me = ( MemoryElementDescriptor ) e.getValue();
  -      log.debug( "dumpMap> key=" + e.getKey() + ", val=" + me.ce.getVal() );
  +      Map.Entry e = (Map.Entry) itr.next();
  +      MemoryElementDescriptor me = (MemoryElementDescriptor) e.getValue();
  +      log.debug("dumpMap> key=" + e.getKey() + ", val=" + me.ce.getVal());
       }
     }
   
  @@ -596,118 +492,117 @@
      */
     public void dumpCacheEntries()
     {
  -    log.debug( "dumpingCacheEntries" );
  -    for ( MemoryElementDescriptor me = first; me != null; me = me.next )
  +    log.debug("dumpingCacheEntries");
  +    for (MemoryElementDescriptor me = (MemoryElementDescriptor) list.getFirst();
  +         me != null; me = (MemoryElementDescriptor) me.next)
       {
  -      log.debug( "dumpCacheEntries> key="
  -                 + me.ce.getKey() + ", val=" + me.ce.getVal() );
  +      log.debug("dumpCacheEntries> key="
  +                + me.ce.getKey() + ", val=" + me.ce.getVal());
       }
     }
   
     private int dumpCacheSize()
     {
  -    int size = 0;
  -    for ( MemoryElementDescriptor me = first; me != null; me = me.next )
  -    {
  -      size++;
  -    }
  -    return size;
  +    return list.size();
     }
   
     private void verifyCache()
     {
  -    if ( !log.isDebugEnabled() )
  +    if (!log.isDebugEnabled())
       {
         return;
       }
   
       boolean found = false;
  -    log.debug( "verifycache[" + cacheName + "]: mapContains " + map.size() +
  -               " elements, linked list contains "
  -               + dumpCacheSize() + " elements" );
  -    log.debug( "verifycache: checking linked list by key " );
  -    for ( MemoryElementDescriptor li = first; li != null; li = li.next )
  +    log.debug("verifycache[" + cacheName + "]: mapContains " + map.size() +
  +              " elements, linked list contains "
  +              + dumpCacheSize() + " elements");
  +    log.debug("verifycache: checking linked list by key ");
  +    for (MemoryElementDescriptor li = (MemoryElementDescriptor) list.getFirst();
  +         li != null; li = (MemoryElementDescriptor) li.next)
       {
         Object key = li.ce.getKey();
  -      if ( !map.containsKey( key ) )
  +      if (!map.containsKey(key))
         {
  -        log.error( "verifycache[" + cacheName +
  -                   "]: map does not contain key : " + li.ce.getKey() );
  -        log.error( "li.hashcode=" + li.ce.getKey().hashCode() );
  -        log.error( "key class=" + key.getClass() );
  -        log.error( "key hashcode=" + key.hashCode() );
  -        log.error( "key toString=" + key.toString() );
  -        if ( key instanceof GroupAttrName )
  +        log.error("verifycache[" + cacheName +
  +                  "]: map does not contain key : " + li.ce.getKey());
  +        log.error("li.hashcode=" + li.ce.getKey().hashCode());
  +        log.error("key class=" + key.getClass());
  +        log.error("key hashcode=" + key.hashCode());
  +        log.error("key toString=" + key.toString());
  +        if (key instanceof GroupAttrName)
           {
  -          GroupAttrName name = ( GroupAttrName ) key;
  -          log.error( "GroupID hashcode=" + name.groupId.hashCode() );
  -          log.error( "GroupID.class=" + name.groupId.getClass() );
  -          log.error( "AttrName hashcode=" + name.attrName.hashCode() );
  -          log.error( "AttrName.class=" + name.attrName.getClass() );
  +          GroupAttrName name = (GroupAttrName) key;
  +          log.error("GroupID hashcode=" + name.groupId.hashCode());
  +          log.error("GroupID.class=" + name.groupId.getClass());
  +          log.error("AttrName hashcode=" + name.attrName.hashCode());
  +          log.error("AttrName.class=" + name.attrName.getClass());
           }
           dumpMap();
         }
  -      else if ( map.get( li.ce.getKey() ) == null )
  +      else if (map.get(li.ce.getKey()) == null)
         {
  -        log.error( "verifycache[" + cacheName +
  -                   "]: linked list retrieval returned null for key: " +
  -                   li.ce.getKey() );
  +        log.error("verifycache[" + cacheName +
  +                  "]: linked list retrieval returned null for key: " +
  +                  li.ce.getKey());
         }
       }
   
  -    log.debug( "verifycache: checking linked list by value " );
  -    for ( MemoryElementDescriptor li3 = first; li3 != null; li3 = li3.next )
  +    log.debug("verifycache: checking linked list by value ");
  +    for (MemoryElementDescriptor li3 = (MemoryElementDescriptor) list.getFirst();
  +         li3 != null; li3 = (MemoryElementDescriptor) li3.next)
       {
  -      if ( map.containsValue( li3 ) == false )
  +      if (map.containsValue(li3) == false)
         {
  -        log.error( "verifycache[" + cacheName +
  -                   "]: map does not contain value : " + li3 );
  +        log.error("verifycache[" + cacheName +
  +                  "]: map does not contain value : " + li3);
           dumpMap();
         }
       }
   
  -    log.debug( "verifycache: checking via keysets!" );
  -    for ( Iterator itr2 = map.keySet().iterator(); itr2.hasNext(); )
  +    log.debug("verifycache: checking via keysets!");
  +    for (Iterator itr2 = map.keySet().iterator(); itr2.hasNext(); )
       {
         found = false;
         Serializable val = null;
         try
         {
  -        val = ( Serializable ) itr2.next();
  +        val = (Serializable) itr2.next();
         }
  -      catch ( NoSuchElementException nse )
  +      catch (NoSuchElementException nse)
         {
  -        log.error( "verifycache: no such element exception" );
  +        log.error("verifycache: no such element exception");
         }
   
  -      for ( MemoryElementDescriptor li2 = first; li2 != null; li2 = li2.next )
  +      for (MemoryElementDescriptor li2 = (MemoryElementDescriptor) list.
  +           getFirst(); li2 != null; li2 = (MemoryElementDescriptor) li2.next)
         {
  -        if ( val.equals( li2.ce.getKey() ) )
  +        if (val.equals(li2.ce.getKey()))
           {
             found = true;
             break;
           }
         }
  -      if ( !found )
  +      if (!found)
         {
  -        log.error( "verifycache[" + cacheName + "]: key not found in list : " +
  -                   val );
  +        log.error("verifycache[" + cacheName + "]: key not found in list : " +
  +                  val);
           dumpCacheEntries();
  -        if ( map.containsKey( val ) )
  +        if (map.containsKey(val))
           {
  -          log.error( "verifycache: map contains key" );
  +          log.error("verifycache: map contains key");
           }
           else
           {
  -          log.error( "verifycache: map does NOT contain key, what the HECK!" );
  +          log.error("verifycache: map does NOT contain key, what the HECK!");
           }
         }
       }
     }
   
  -  private void verifyCache( Serializable key )
  +  private void verifyCache(Serializable key)
     {
  -    if ( !log.isDebugEnabled() )
  +    if (!log.isDebugEnabled())
       {
         return;
       }
  @@ -715,42 +610,54 @@
       boolean found = false;
   
       // go through the linked list looking for the key
  -    for ( MemoryElementDescriptor li = first; li != null; li = li.next )
  +    for (MemoryElementDescriptor li = (MemoryElementDescriptor) list.getFirst();
  +         li != null; li = (MemoryElementDescriptor) li.next)
       {
  -      if ( li.ce.getKey() == key )
  +      if (li.ce.getKey() == key)
         {
           found = true;
  -        log.debug( "verifycache(key) key match: " + key );
  +        log.debug("verifycache(key) key match: " + key);
           break;
         }
       }
  -    if ( !found )
  +    if (!found)
       {
  -      log.error( "verifycache(key)[" + cacheName + "], couldn't find key! : " +
  -                 key );
  +      log.error("verifycache(key)[" + cacheName + "], couldn't find key! : " +
  +                key);
       }
     }
  -}
   
  -/**
  - * needed for memory cache element LRU linked lisk
  - */
  -class MemoryElementDescriptor
  -    implements Serializable
  -{
  -  /** Description of the Field */
  -  public MemoryElementDescriptor prev, next;
  +  /////////////////////////////////////////////////////////////////////////
  +  public String getStats()
  +  {
  +    StringBuffer buf = new StringBuffer(0);
  +    buf.append(getInfo());
  +    buf.append(getCnts());
  +    return buf.toString();
  +  }
   
  -  /** Description of the Field */
  -  public ICacheElement ce;
  +  public String getCnts()
  +  {
  +    StringBuffer buf = new StringBuffer(0);
  +    buf.append("\n putCnt = " + putCnt);
  +    buf.append("\n hitCnt = " + hitCnt);
  +    buf.append("\n missCnt = " + missCnt);
  +    buf.append( "\n -------------------------" );
  +    if (hitCnt != 0)
  +    {
  +      // int rate = ((hitCnt + missCnt) * 100) / (hitCnt * 100) * 100;
  +      //buf.append("\n Hit Rate = " + rate + " %" );
  +    }
  +    return buf.toString();
  +  }
   
  -  /**
  -   * Constructor for the MemoryElementDescriptor object
  -   *
  -   * @param ce
  -   */
  -  public MemoryElementDescriptor( ICacheElement ce )
  +  public String getInfo()
     {
  -    this.ce = ce;
  +    StringBuffer buf = new StringBuffer();
  +    buf.append("\n list.size() = " + list.size());
  +    buf.append("\n map.size() = " + map.size());
  +    buf.append( "\n -------------------------" );
  +    return buf.toString();
     }
  +
   }
  
  
  
  1.2       +65 -63    jakarta-turbine-jcs/src/java/org/apache/jcs/engine/memory/lru/LHMLRUMemoryCache.java
  
  Index: LHMLRUMemoryCache.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-jcs/src/java/org/apache/jcs/engine/memory/lru/LHMLRUMemoryCache.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- LHMLRUMemoryCache.java	8 Jul 2004 00:48:20 -0000	1.1
  +++ LHMLRUMemoryCache.java	13 Jul 2004 02:11:45 -0000	1.2
  @@ -31,6 +31,8 @@
   import org.apache.jcs.engine.control.group.GroupId;
   import org.apache.jcs.engine.control.group.GroupAttrName;
   
  +import org.apache.jcs.engine.memory.util.MemoryElementDescriptor;
  +
   /**
    *  This is a test memory manager using the jdk1.4 LinkedHashMap.
    *  There may be some thread safety issues.
  @@ -42,7 +44,7 @@
   public class LHMLRUMemoryCache
       extends AbstractMemoryCache
   {
  -  private final static Log log = LogFactory.getLog( LRUMemoryCache.class );
  +  private final static Log log = LogFactory.getLog(LRUMemoryCache.class);
   
     // the extended LinkedHashMap
     private Map map;
  @@ -52,13 +54,13 @@
      *
      *@param  hub
      */
  -  public synchronized void initialize( CompositeCache hub )
  +  public synchronized void initialize(CompositeCache hub)
     {
  -    super.initialize( hub );
  +    super.initialize(hub);
   
       map = new LHMSpooler();
   
  -    log.info( "initialized LHMLRUMemoryCache for " + cacheName );
  +    log.info("initialized LHMLRUMemoryCache for " + cacheName);
     }
   
     /**
  @@ -67,11 +69,11 @@
      *@param  ce               Description of the Parameter
      *@exception  IOException
      */
  -  public void update( ICacheElement ce ) throws IOException
  +  public void update(ICacheElement ce) throws IOException
     {
       // Asynchronisly create a MemoryElement
       ce.getElementAttributes().setLastAccessTimeNow();
  -    map.put( ce.getKey(), ce );
  +    map.put(ce.getKey(), ce);
     }
   
     /**
  @@ -91,23 +93,23 @@
      *@return                  Element mathinh key if found, or null
      *@exception  IOException
      */
  -  public ICacheElement getQuiet( Serializable key ) throws IOException
  +  public ICacheElement getQuiet(Serializable key) throws IOException
     {
       ICacheElement ce = null;
   
  -    ce = ( ICacheElement ) map.get( key );
  +    ce = (ICacheElement) map.get(key);
   
  -    if ( ce != null )
  +    if (ce != null)
       {
  -      if ( log.isDebugEnabled() )
  +      if (log.isDebugEnabled())
         {
  -        log.debug( cacheName + ": LRUMemoryCache quiet hit for " + key );
  +        log.debug(cacheName + ": LRUMemoryCache quiet hit for " + key);
         }
   
       }
  -    else if ( log.isDebugEnabled() )
  +    else if (log.isDebugEnabled())
       {
  -      log.debug( cacheName + ": LRUMemoryCache quiet miss for " + key );
  +      log.debug(cacheName + ": LRUMemoryCache quiet miss for " + key);
       }
   
       return ce;
  @@ -120,29 +122,29 @@
      *@return                  ICacheElement if found, else null
      *@exception  IOException
      */
  -  public synchronized ICacheElement get( Serializable key ) throws IOException
  +  public synchronized ICacheElement get(Serializable key) throws IOException
     {
       ICacheElement ce = null;
   
  -    if ( log.isDebugEnabled() )
  +    if (log.isDebugEnabled())
       {
  -      log.debug( "getting item from cache " + cacheName + " for key " +
  -                 key );
  +      log.debug("getting item from cache " + cacheName + " for key " +
  +                key);
       }
   
  -    ce = ( ICacheElement ) map.get( key );
  +    ce = (ICacheElement) map.get(key);
   
  -    if ( ce != null )
  +    if (ce != null)
       {
  -      if ( log.isDebugEnabled() )
  +      if (log.isDebugEnabled())
         {
  -        log.debug( cacheName + ": LRUMemoryCache hit for " + key );
  +        log.debug(cacheName + ": LRUMemoryCache hit for " + key);
         }
   
       }
       else
       {
  -      log.debug( cacheName + ": LRUMemoryCache miss for " + key );
  +      log.debug(cacheName + ": LRUMemoryCache miss for " + key);
       }
   
       return ce;
  @@ -158,29 +160,29 @@
      *@return
      *@exception  IOException
      */
  -  public synchronized boolean remove( Serializable key ) throws IOException
  +  public synchronized boolean remove(Serializable key) throws IOException
     {
  -    if ( log.isDebugEnabled() )
  +    if (log.isDebugEnabled())
       {
  -      log.debug( "removing item for key: " + key );
  +      log.debug("removing item for key: " + key);
       }
   
       boolean removed = false;
   
       // handle partial removal
  -    if ( key instanceof String && ( ( String ) key )
  -         .endsWith( CacheConstants.NAME_COMPONENT_DELIMITER ) )
  +    if (key instanceof String && ( (String) key)
  +        .endsWith(CacheConstants.NAME_COMPONENT_DELIMITER))
       {
         // remove all keys of the same name hierarchy.
  -      synchronized ( map )
  +      synchronized (map)
         {
  -        for ( Iterator itr = map.entrySet().iterator(); itr.hasNext(); )
  +        for (Iterator itr = map.entrySet().iterator(); itr.hasNext(); )
           {
  -          Map.Entry entry = ( Map.Entry ) itr.next();
  +          Map.Entry entry = (Map.Entry) itr.next();
             Object k = entry.getKey();
   
  -          if ( k instanceof String
  -               && ( ( String ) k ).startsWith( key.toString() ) )
  +          if (k instanceof String
  +              && ( (String) k).startsWith(key.toString()))
             {
               itr.remove();
   
  @@ -189,18 +191,18 @@
           }
         }
       }
  -    else if ( key instanceof GroupId )
  +    else if (key instanceof GroupId)
       {
         // remove all keys of the same name hierarchy.
  -      synchronized ( map )
  +      synchronized (map)
         {
  -        for ( Iterator itr = map.entrySet().iterator(); itr.hasNext(); )
  +        for (Iterator itr = map.entrySet().iterator(); itr.hasNext(); )
           {
  -          Map.Entry entry = ( Map.Entry ) itr.next();
  +          Map.Entry entry = (Map.Entry) itr.next();
             Object k = entry.getKey();
   
  -          if ( k instanceof GroupAttrName
  -               && ( ( GroupAttrName ) k ).groupId.equals( key ) )
  +          if (k instanceof GroupAttrName
  +              && ( (GroupAttrName) k).groupId.equals(key))
             {
               itr.remove();
   
  @@ -212,7 +214,7 @@
       else
       {
         // remove single item.
  -      ICacheElement ce = ( ICacheElement ) map.remove( key );
  +      ICacheElement ce = (ICacheElement) map.remove(key);
         removed = true;
       }
   
  @@ -227,7 +229,7 @@
     public Object[] getKeyArray()
     {
       // need a better locking strategy here.
  -    synchronized ( this )
  +    synchronized (this)
       {
         // may need to lock to map here?
         return map.keySet().toArray();
  @@ -241,13 +243,13 @@
      */
     public void dumpMap()
     {
  -    log.debug( "dumpingMap" );
  +    log.debug("dumpingMap");
   
  -    for ( Iterator itr = map.entrySet().iterator(); itr.hasNext(); )
  +    for (Iterator itr = map.entrySet().iterator(); itr.hasNext(); )
       {
  -      Map.Entry e = ( Map.Entry ) itr.next();
  -      MemoryElementDescriptor me = ( MemoryElementDescriptor ) e.getValue();
  -      log.debug( "dumpMap> key=" + e.getKey() + ", val=" + me.ce.getVal() );
  +      Map.Entry e = (Map.Entry) itr.next();
  +      MemoryElementDescriptor me = (MemoryElementDescriptor) e.getValue();
  +      log.debug("dumpMap> key=" + e.getKey() + ", val=" + me.ce.getVal());
       }
     }
   
  @@ -256,7 +258,7 @@
      */
     public void dumpCacheEntries()
     {
  -    log.debug( "dumpingCacheEntries" );
  +    log.debug("dumpingCacheEntries");
       //Map.Entry e = map.
       //for (  )
       //{
  @@ -288,35 +290,35 @@
        */
       public LHMSpooler()
       {
  -      super( ( int ) ( cache.getCacheAttributes().getMaxObjects() * .5 ), .75F, true );
  +      super( (int) (cache.getCacheAttributes().getMaxObjects() * .5), .75F, true);
       }
   
       /**
        * Remove eldest.  Automatically called by LinkedHashMap.
        */
  -    protected boolean removeEldestEntry( Map.Entry eldest )
  +    protected boolean removeEldestEntry(Map.Entry eldest)
       {
   
  -      CacheElement element = ( CacheElement ) eldest.getValue();
  +      CacheElement element = (CacheElement) eldest.getValue();
   
  -      if ( size() <= cache.getCacheAttributes().getMaxObjects() )
  +      if (size() <= cache.getCacheAttributes().getMaxObjects())
         {
           return false;
         }
         else
         {
   
  -        if ( log.isDebugEnabled() )
  +        if (log.isDebugEnabled())
           {
  -          log.debug( "LHMLRU max size: " +
  -                     cache.getCacheAttributes().getMaxObjects()
  -                     + ".  Spooling element, key: " + element.getKey() );
  +          log.debug("LHMLRU max size: " +
  +                    cache.getCacheAttributes().getMaxObjects()
  +                    + ".  Spooling element, key: " + element.getKey());
           }
  -        spoolToDisk( element );
  +        spoolToDisk(element);
   
  -        if ( log.isDebugEnabled() )
  +        if (log.isDebugEnabled())
           {
  -          log.debug( "LHMLRU size: " + map.size() );
  +          log.debug("LHMLRU size: " + map.size());
           }
         }
         return true;
  @@ -326,14 +328,14 @@
        * Puts the element in the DiskStore
        * @param  element  The CacheElement
        */
  -    private void spoolToDisk( CacheElement element )
  +    private void spoolToDisk(CacheElement element)
       {
  -      cache.spoolToDisk( element );
  +      cache.spoolToDisk(element);
   
  -      if ( log.isDebugEnabled() )
  +      if (log.isDebugEnabled())
         {
  -        log.debug( cache.getCacheName() + "Spoolled element to disk: " +
  -                   element.getKey() );
  +        log.debug(cache.getCacheName() + "Spoolled element to disk: " +
  +                  element.getKey());
         }
       }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-jcs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-jcs-dev-help@jakarta.apache.org


Mime
View raw message