activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rajdav...@apache.org
Subject svn commit: r632050 - /activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/index/hash/HashIndex.java
Date Thu, 28 Feb 2008 16:42:37 GMT
Author: rajdavies
Date: Thu Feb 28 08:42:30 2008
New Revision: 632050

URL: http://svn.apache.org/viewvc?rev=632050&view=rev
Log:
changed free list implementation

Modified:
    activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/index/hash/HashIndex.java

Modified: activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/index/hash/HashIndex.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/index/hash/HashIndex.java?rev=632050&r1=632049&r2=632050&view=diff
==============================================================================
--- activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/index/hash/HashIndex.java
(original)
+++ activemq/trunk/activemq-core/src/main/java/org/apache/activemq/kaha/impl/index/hash/HashIndex.java
Thu Feb 28 08:42:30 2008
@@ -19,6 +19,7 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.RandomAccessFile;
+import java.util.LinkedList;
 import java.util.concurrent.atomic.AtomicBoolean;
 import org.apache.activemq.kaha.Marshaller;
 import org.apache.activemq.kaha.StoreEntry;
@@ -57,8 +58,7 @@
     private HashBin[] bins;
     private Marshaller keyMarshaller;
     private long length;
-    private HashPage firstFree;
-    private HashPage lastFree;
+    private LinkedList<HashPage> freeList = new LinkedList<HashPage>();
     private AtomicBoolean loaded = new AtomicBoolean();
     private LRUCache<Long, HashPage> pageCache;
     private boolean enablePageCaching=true;
@@ -207,23 +207,14 @@
                     page.setId(offset);
                     page.readHeader(dataIn);
                     if (!page.isActive()) {
-                        if (lastFree != null) {
-                            lastFree.setNextFreePageId(offset);
-                            indexFile.seek(lastFree.getId());
-                            dataOut.reset();
-                            lastFree.writeHeader(dataOut);
-                            indexFile.write(dataOut.getData(), 0, HashPage.PAGE_HEADER_SIZE);
-                            lastFree = page;
-                        } else {
-                            lastFree = page;
-                            firstFree = page;
-                        }
+                        freeList.add(page);
                     } else {
                         addToBin(page);
                         size+=page.size();
                     }
                     offset += pageSize;
                 }
+                System.err.println("AFTER LOAD SIZE = " + size);
                 length = offset;
             } catch (IOException e) {
                 LOG.error("Failed to load index ", e);
@@ -237,8 +228,7 @@
             if (indexFile != null) {
                 indexFile.close();
                 indexFile = null;
-                firstFree = null;
-                lastFree = null;
+                freeList.clear();
                 bins = new HashBin[bins.length];
             }
         }
@@ -249,7 +239,7 @@
         HashEntry entry = new HashEntry();
         entry.setKey((Comparable)key);
         entry.setIndexOffset(value.getOffset());
-        if (getBin(key).put(entry)) {
+        if (!getBin(key).put(entry)) {
             size++;
         }
     }
@@ -313,7 +303,7 @@
 
     HashPage createPage(int binId) throws IOException {
         HashPage result = getNextFreePage();
-        if (result == null) {
+        if (result == null) {  
             // allocate one
             result = new HashPage(keysPerPage);
             result.setId(length);
@@ -322,6 +312,7 @@
             length += pageSize;
             indexFile.seek(length);
             indexFile.write(HashEntry.NOT_SET);
+            System.err.println("ALLOCATED NEW PAGE  size = " + size);
         }
         addToCache(result);
         return result;
@@ -331,33 +322,16 @@
         removeFromCache(page);
         page.reset();
         page.setActive(false);
-        if (lastFree == null) {
-            firstFree = page;
-            lastFree = page;
-        } else {
-            lastFree.setNextFreePageId(page.getId());
-            writePageHeader(lastFree);
-        }
         writePageHeader(page);
+        freeList.add(page);
     }
 
     private HashPage getNextFreePage() throws IOException {
         HashPage result = null;
-        if (firstFree != null) {
-            if (firstFree.equals(lastFree)) {
-                result = firstFree;
-                firstFree = null;
-                lastFree = null;
-            } else {
-                result = firstFree;
-                firstFree = getPageHeader(firstFree.getNextFreePageId());
-                if (firstFree == null) {
-                    lastFree = null;
-                }
-            }
+        if(!freeList.isEmpty()) {
+            result = freeList.removeFirst();
             result.setActive(true);
             result.reset();
-            writePageHeader(result);
         }
         return result;
     }



Mime
View raw message