From fb56fd9c768da77f4b9f78e3e562468b48d22af9 Mon Sep 17 00:00:00 2001 From: Robert Davies Date: Thu, 25 Jan 2007 13:58:02 +0000 Subject: [PATCH] fix for http://issues.apache.org/activemq/browse/AMQ-1082 git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@499797 13f79535-47bb-0310-9956-ffa450edef68 --- .../kaha/impl/index/IndexManager.java | 49 ++++++++++++++----- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/activemq-core/src/main/java/org/apache/activemq/kaha/impl/index/IndexManager.java b/activemq-core/src/main/java/org/apache/activemq/kaha/impl/index/IndexManager.java index 8f5151f652..7b55e5ac25 100644 --- a/activemq-core/src/main/java/org/apache/activemq/kaha/impl/index/IndexManager.java +++ b/activemq-core/src/main/java/org/apache/activemq/kaha/impl/index/IndexManager.java @@ -43,10 +43,12 @@ public final class IndexManager{ private RandomAccessFile indexFile; private StoreIndexReader reader; private StoreIndexWriter writer; - private LinkedList freeList=new LinkedList(); + private DataManager redoLog; private String mode; private long length=0; + private IndexItem firstFree; + private IndexItem lastFree; public IndexManager(File directory,String name,String mode,DataManager redoLog) throws IOException{ this.directory = directory; @@ -57,7 +59,7 @@ public final class IndexManager{ } public synchronized boolean isEmpty(){ - return freeList.isEmpty()&&length==0; + return lastFree == null &&length==0; } public synchronized IndexItem getIndex(long offset) throws IOException{ @@ -70,10 +72,16 @@ public final class IndexManager{ } public synchronized void freeIndex(IndexItem item) throws IOException{ - //item.reset(); + item.reset(); item.setActive(false); + if (lastFree == null) { + firstFree=lastFree=item; + } + else { + lastFree.setNextItem(item.getOffset()); + } writer.updateIndexes(item); - freeList.add(item); + } public synchronized void storeIndex(IndexItem index) throws IOException{ @@ -84,7 +92,7 @@ public final class IndexManager{ try { writer.updateIndexes(index); }catch(Throwable e) { - log.error(name + " GORT ERROR! ",e); + log.error(name + " error updating indexes ",e); } } @@ -92,7 +100,7 @@ public final class IndexManager{ writer.redoStoreItem(redo); } - public synchronized IndexItem createNewIndex(){ + public synchronized IndexItem createNewIndex() throws IOException{ IndexItem result=getNextFreeIndex(); if(result==null){ // allocate one @@ -118,7 +126,7 @@ public final class IndexManager{ public synchronized boolean delete() throws IOException{ - freeList.clear(); + firstFree = lastFree = null; if(indexFile!=null){ indexFile.close(); indexFile=null; @@ -126,12 +134,23 @@ public final class IndexManager{ return file.delete(); } - private synchronized IndexItem getNextFreeIndex(){ + private synchronized IndexItem getNextFreeIndex() throws IOException{ IndexItem result=null; - if(!freeList.isEmpty()){ - result=(IndexItem) freeList.removeLast(); + if (firstFree != null) { + if (firstFree.equals(lastFree)) { + result = firstFree; + firstFree=lastFree=null; + + }else { + result = firstFree; + firstFree = getIndex(firstFree.getNextItem()); + if (firstFree==null) { + lastFree=null; + } + } result.reset(); } + return result; } @@ -158,11 +177,19 @@ public final class IndexManager{ reader=new StoreIndexReader(indexFile); writer=new StoreIndexWriter(indexFile,name,redoLog); long offset=0; + while((offset+IndexItem.INDEX_SIZE)<=indexFile.length()){ IndexItem index=reader.readItem(offset); if(!index.isActive()){ index.reset(); - freeList.add(index); + if (lastFree != null) { + lastFree.setNextItem(index.getOffset()); + updateIndexes(lastFree); + lastFree=index; + }else { + lastFree=firstFree=index; + } + } offset+=IndexItem.INDEX_SIZE; }