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/auxiliary/disk/indexed IndexedDiskElementDescriptor.java IndexedDiskCache.java
Date Wed, 14 Jul 2004 03:35:09 GMT
asmuts      2004/07/13 20:35:09

  Modified:    src/java/org/apache/jcs/auxiliary/disk/indexed
                        IndexedDiskElementDescriptor.java
                        IndexedDiskCache.java
  Log:
  Started converting the IndexedDiskCache to use the SortedPreferentialArray.  This utility
class keeps a sorted array with a configurable preference policy.  It will only add items
if they are greater than the smallest (for large preference) or less than the greatest (for
small preference).  It is designed to be used by the indexed disk cache so it can reuse empty
blocks on the file.
  
  Currently the recycle flag for the disk cache is set to false.  The prevents the disk cache
from using the unfinished utility.
  
  The IndexedDiskElementDescriptor now implements Comparable for use in the sorted array.
  
  Revision  Changes    Path
  1.4       +35 -1     jakarta-turbine-jcs/src/java/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskElementDescriptor.java
  
  Index: IndexedDiskElementDescriptor.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-jcs/src/java/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskElementDescriptor.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- IndexedDiskElementDescriptor.java	15 Apr 2004 19:22:57 -0000	1.3
  +++ IndexedDiskElementDescriptor.java	14 Jul 2004 03:35:09 -0000	1.4
  @@ -25,7 +25,7 @@
    * and loaded into memory on startup.
    *
    */
  -public class IndexedDiskElementDescriptor implements Serializable
  +public class IndexedDiskElementDescriptor implements Serializable, Comparable
   {
   
       /** Position of the cache data entry on disk. */
  @@ -45,5 +45,39 @@
   
       /** Constructor for the DiskElementDescriptor object */
       public IndexedDiskElementDescriptor() { }
  +
  +
  +    public String toString() {
  +      StringBuffer buf = new StringBuffer();
  +      buf.append( "DED: " );
  +      buf.append( " pos = " + pos );
  +      buf.append( " len = " + len );
  +      return buf.toString();
  +    }
  +
  +  /**
  +   * compareTo
  +   *
  +   * @param o Object
  +   * @return int
  +   */
  +  public int compareTo(Object o)
  +  {
  +    if ( o == null ) {
  +      return 1;
  +    }
  +
  +    int oLen = ((IndexedDiskElementDescriptor)o).len;
  +    if ( oLen == len ) {
  +      return 0;
  +    } else
  +    if ( oLen > len ) {
  +      return -1;
  +    } else
  +    if ( oLen < len ) {
  +      return 1;
  +    }
  +    return 0;
  +  }
   
   }
  
  
  
  1.13      +136 -10   jakarta-turbine-jcs/src/java/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCache.java
  
  Index: IndexedDiskCache.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-jcs/src/java/org/apache/jcs/auxiliary/disk/indexed/IndexedDiskCache.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- IndexedDiskCache.java	12 Jun 2004 02:34:13 -0000	1.12
  +++ IndexedDiskCache.java	14 Jul 2004 03:35:09 -0000	1.13
  @@ -37,9 +37,14 @@
   import org.apache.jcs.engine.control.group.GroupAttrName;
   import org.apache.jcs.engine.control.group.GroupId;
   import org.apache.jcs.utils.locking.ReadWriteLock;
  +import org.apache.jcs.utils.struct.SortedPreferentialArray;
   
   /**
  - * Disk cache that uses a RandomAccessFile with keys stored in memory
  + * Disk cache that uses a RandomAccessFile with keys stored in memory.
  + *
  + * The maximum number of keys stored in memory is configurable.
  + *
  + * The disk cache tries to recycle spots on disk to limit file expansion.
    *
    * @version $Id$
    */
  @@ -51,11 +56,14 @@
       private String fileName;
       private IndexedDisk dataFile;
       private IndexedDisk keyFile;
  -    private LRUMapJCS keyHash;
  +    private LRUMap keyHash;
       private int maxKeySize;
   
       private File rafDir;
   
  +    boolean doRecycle = false; //true;
  +    // array of empty spots
  +    private SortedPreferentialArray recycle;
   
   
       IndexedDiskCacheAttributes cattr;
  @@ -114,7 +122,7 @@
   
               else
               {
  -                keyHash = new LRUMapJCS( maxKeySize );
  +                keyHash = new LRUMap( maxKeySize );
   
                   if ( dataFile.length() > 0 )
                   {
  @@ -122,6 +130,9 @@
                   }
               }
   
  +            // TODO, make a new size parameter for this.
  +            recycle = new SortedPreferentialArray( maxKeySize );
  +
               // Initialization finished successfully, so set alive to true.
   
               alive = true;
  @@ -148,11 +159,11 @@
   
           try
           {
  -            keyHash = ( LRUMapJCS ) keyFile.readObject( 0 );
  +            keyHash = ( LRUMap ) keyFile.readObject( 0 );
   
               if ( keyHash == null )
               {
  -                keyHash = new LRUMapJCS( maxKeySize );
  +                keyHash = new LRUMap( maxKeySize );
               }
               else
               {
  @@ -265,9 +276,30 @@
                   // Try to reuse the location if possible.
                   if ( old != null && ded.len <= old.len )
                   {
  -                    ded.pos = old.pos;
  -                }
  +                  ded.pos = old.pos;
  +                } else {
  +                  if ( doRecycle )
  +                  {
  +                    IndexedDiskElementDescriptor rep =
  +                        (IndexedDiskElementDescriptor)recycle.takeNearestLargerOrEqual(ded);
  +                    if ( rep != null )
  +                    {
  +                      ded.pos = rep.pos;
  +                      if (log.isInfoEnabled())
  +                      {
  +                        log.info("using recycled ded " + ded.pos +
  +                                 " rep.len = " + rep.len + " ded.len = " +
  +                                 ded.len);
  +                      }
  +                    } else {
  +                      if (log.isInfoEnabled())
  +                      {
  +                        log.info("no ded to recycle" );
  +                      }
   
  +                    }
  +                  }
  +                }
                   dataFile.write( data, ded.pos );
               }
               finally
  @@ -419,6 +451,21 @@
                       if ( k instanceof String
                            && k.toString().startsWith( key.toString() ) )
                       {
  +
  +                      if ( doRecycle ) {
  +                        // reuse the spot
  +                        IndexedDiskElementDescriptor ded =
  +                            (IndexedDiskElementDescriptor) keyHash.get(key);
  +                        if (ded != null)
  +                        {
  +                          recycle.add(ded);
  +                          if (log.isDebugEnabled())
  +                          {
  +                            log.debug("recycling ded " + ded);
  +                          }
  +                        }
  +                      }
  +
                           iter.remove();
                           removed = true;
                       }
  @@ -437,6 +484,20 @@
                       if ( k instanceof GroupAttrName
                            && ((GroupAttrName)k).groupId.equals(key) )
                       {
  +                      if ( doRecycle ) {
  +                        // reuse the spot
  +                        IndexedDiskElementDescriptor ded =
  +                            (IndexedDiskElementDescriptor) keyHash.get(key);
  +                        if (ded != null)
  +                        {
  +                          recycle.add(ded);
  +                          if (log.isDebugEnabled())
  +                          {
  +                            log.debug("recycling ded " + ded);
  +                          }
  +                        }
  +                      }
  +
                           iter.remove();
                           removed = true;
                       }
  @@ -450,6 +511,20 @@
                       log.debug( "Disk removal: Removed from key hash, key " + key );
                   }
   
  +                if ( doRecycle ) {
  +                  // reuse the spot
  +                  IndexedDiskElementDescriptor ded =
  +                      (IndexedDiskElementDescriptor) keyHash.get(key);
  +                  if (ded != null)
  +                  {
  +                    recycle.add(ded);
  +                    if (log.isDebugEnabled())
  +                    {
  +                      log.debug("recycling ded " + ded);
  +                    }
  +                  }
  +                }
  +
                   // remove single item.
                   return keyHash.remove( key ) != null;
   
  @@ -513,7 +588,11 @@
               keyFile =
                   new IndexedDisk( new File( rafDir, fileName + ".key" ) );
   
  -            keyHash = new LRUMapJCS( this.maxKeySize );
  +            recycle = null;
  +            recycle = new SortedPreferentialArray( maxKeySize );
  +
  +            keyHash = null;
  +            keyHash = new LRUMap( this.maxKeySize );
           }
           catch ( Exception e )
           {
  @@ -591,7 +670,7 @@
           {
               // Migrate from keyHash to keyHshTemp in memory,
               // and from dataFile to dataFileTemp on disk.
  -            LRUMapJCS keyHashTemp = new LRUMapJCS( this.maxKeySize );
  +            LRUMap keyHashTemp = new LRUMap( this.maxKeySize );
   
               IndexedDisk dataFileTemp =
                   new IndexedDisk( new File( rafDir, fileName + "Temp.data" ) );
  @@ -673,6 +752,11 @@
               keyHash = keyHashTemp;
               keyFile.reset();
               saveKeys();
  +
  +            // clean up the recycle store
  +            recycle = null;
  +            recycle = new SortedPreferentialArray( maxKeySize );
  +
           }
           catch ( Exception e )
           {
  @@ -716,6 +800,48 @@
           }
       }
   
  +    /**
  +     * Inner class for recylcing and lru
  +     */
  +    public class LRUMap extends LRUMapJCS {
  +
  +      public LRUMap( int maxKeySize )
  +      {
  +        super( maxKeySize );
  +      }
  +
  +
  +      protected void processRemovedLRU( Object key, Object value )
  +       {
  +
  +         if ( doRecycle ) {
  +           // reuse the spot
  +           IndexedDiskElementDescriptor ded =
  +               (IndexedDiskElementDescriptor)value;
  +           if (ded != null)
  +           {
  +             recycle.add(ded);
  +             if (log.isInfoEnabled())
  +             {
  +               log.info("recycled ded in LRU" + ded);
  +             }
  +           }
  +         }
  +
  +
  +         if ( log.isDebugEnabled() )
  +         {
  +           log.debug( "Removing key: '" + key + "' from key store." );
  +           log.debug( "Key store size: '" + this.size() + "'." );
  +         }
  +
  +       }
  +    }
  +
  +
   }
  +
  +
  +
   
   
  
  
  

---------------------------------------------------------------------
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