Author: tubbie
Date: 2005-12-15 10:19:06 +0000 (Thu, 15 Dec 2005)
New Revision: 7711

Modified:
   branches/bdb/src/freenet/store/BerkelyDBFreenetStore.java
Log:
Don't use timestamps to determine Least Recently Used 

Modified: branches/bdb/src/freenet/store/BerkelyDBFreenetStore.java
===================================================================
--- branches/bdb/src/freenet/store/BerkelyDBFreenetStore.java   2005-12-15 
09:31:34 UTC (rev 7710)
+++ branches/bdb/src/freenet/store/BerkelyDBFreenetStore.java   2005-12-15 
10:19:06 UTC (rev 7711)
@@ -36,7 +36,6 @@
  * @author tubbie
  * 
  * TODO: Fix ugly Exception handling
- * TODO: Don't use timestamps
  */
 public class BerkelyDBFreenetStore implements FreenetStore {

@@ -53,6 +52,8 @@
        private final Database chkDB_accessTime;
        private final RandomAccessFile chkStore;

+       private long lastRecentlyUsed;
+       
        private boolean closed = false;

        /**
@@ -112,11 +113,12 @@
                if(!storeFile.exists())
                        storeFile.createNewFile();
                chkStore = new RandomAccessFile(storeFile,"rw");
+                       
+               chkBlocksInStore = countCHKBlocks();
+               lastRecentlyUsed = getMaxRecentlyUsed();

-               // Add shutdownhook
+//              Add shutdownhook
                Runtime.getRuntime().addShutdownHook(new ShutdownHook());
-               
-               chkBlocksInStore = countCHKBlocks();
        }

        /**
@@ -161,7 +163,7 @@

                        if(!dontPromote)
                        {
-                               storeBlock.updateAccessTime();
+                               storeBlock.updateRecentlyUsed();
                                DatabaseEntry updateDBE = new DatabaseEntry();
                                
storeBlockTupleBinding.objectToEntry(storeBlock, updateDBE);
                                c.putCurrent(updateDBE);
@@ -178,7 +180,7 @@

                }catch(CHKVerifyException ex){
                        Logger.normal(this, "Does not verify, setting 
accessTime to 0 for : "+chk);
-                       storeBlock.setAccessTime(0);
+                       storeBlock.setRecentlyUsedToZero();
                        DatabaseEntry updateDBE = new DatabaseEntry();
                        storeBlockTupleBinding.objectToEntry(storeBlock, 
updateDBE);
                        c.putCurrent(updateDBE);
@@ -272,31 +274,33 @@

     private class StoreBlock
     {
-       private long lastAccessed;
+       private long recentlyUsed;
        private int offset;

        public StoreBlock(int offset)
        {
-               this(offset,System.currentTimeMillis());
+               this(offset,getNewRecentlyUsed());
        }

-       public StoreBlock(int offset,long lastAccessed)
+       public StoreBlock(int offset,long recentlyUsed)
        {
                this.offset = offset;
-               this.lastAccessed = lastAccessed;
+               this.recentlyUsed = recentlyUsed;
        }

-       public void updateAccessTime() {
-               lastAccessed = System.currentTimeMillis();
+       
+       public long getRecentlyUsed() {
+               return recentlyUsed;
        }

-       public long getLastAccessed() {
-               return lastAccessed;
+       public void setRecentlyUsedToZero()
+       {
+               recentlyUsed = 0;
        }

-       public void setAccessTime(long time)
+       public void updateRecentlyUsed()
        {
-               lastAccessed = time;
+               recentlyUsed = getNewRecentlyUsed();
        }

        public int getOffset() {
@@ -314,7 +318,7 @@
                StoreBlock myData = (StoreBlock)object;

                to.writeInt(myData.getOffset());
-               to.writeLong(myData.getLastAccessed());
+               to.writeLong(myData.getRecentlyUsed());
        }

        public Object entryToObject(TupleInput ti) {
@@ -342,7 +346,7 @@
        DatabaseEntry resultEntry) {

                StoreBlock storeblock = (StoreBlock) 
theBinding.entryToObject(dataEntry);
-               Long accessTime = new Long(storeblock.getLastAccessed());
+               Long accessTime = new Long(storeblock.getRecentlyUsed());
                longTupleBinding.objectToEntry(accessTime, resultEntry);
                return true;
        }
@@ -389,4 +393,27 @@
        }
        return count;
     }
+    
+    private long getMaxRecentlyUsed()
+    {
+       long maxRecentlyUsed = 0;
+       
+       try{
+               Cursor c = chkDB_accessTime.openCursor(null,null);
+                       DatabaseEntry keyDBE = new DatabaseEntry();
+                       DatabaseEntry dataDBE = new DatabaseEntry();
+                       
if(c.getLast(keyDBE,dataDBE,null)==OperationStatus.SUCCESS) {
+                               StoreBlock storeBlock = (StoreBlock) 
storeBlockTupleBinding.entryToObject(dataDBE);
+                               maxRecentlyUsed = storeBlock.getRecentlyUsed();
+                       }
+                       c.close();
+       }catch(DatabaseException ex){ex.printStackTrace();}
+       
+       return maxRecentlyUsed;
+    }
+    
+    private synchronized long getNewRecentlyUsed() {
+       lastRecentlyUsed++;
+       return lastRecentlyUsed;
+    }
 }
\ No newline at end of file


Reply via email to