git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@499797 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Davies 2007-01-25 13:58:02 +00:00
parent b036d4d2fa
commit fb56fd9c76
1 changed files with 38 additions and 11 deletions

View File

@ -43,10 +43,12 @@ public final class IndexManager{
private RandomAccessFile indexFile; private RandomAccessFile indexFile;
private StoreIndexReader reader; private StoreIndexReader reader;
private StoreIndexWriter writer; private StoreIndexWriter writer;
private LinkedList freeList=new LinkedList();
private DataManager redoLog; private DataManager redoLog;
private String mode; private String mode;
private long length=0; private long length=0;
private IndexItem firstFree;
private IndexItem lastFree;
public IndexManager(File directory,String name,String mode,DataManager redoLog) throws IOException{ public IndexManager(File directory,String name,String mode,DataManager redoLog) throws IOException{
this.directory = directory; this.directory = directory;
@ -57,7 +59,7 @@ public final class IndexManager{
} }
public synchronized boolean isEmpty(){ public synchronized boolean isEmpty(){
return freeList.isEmpty()&&length==0; return lastFree == null &&length==0;
} }
public synchronized IndexItem getIndex(long offset) throws IOException{ public synchronized IndexItem getIndex(long offset) throws IOException{
@ -70,10 +72,16 @@ public final class IndexManager{
} }
public synchronized void freeIndex(IndexItem item) throws IOException{ public synchronized void freeIndex(IndexItem item) throws IOException{
//item.reset(); item.reset();
item.setActive(false); item.setActive(false);
if (lastFree == null) {
firstFree=lastFree=item;
}
else {
lastFree.setNextItem(item.getOffset());
}
writer.updateIndexes(item); writer.updateIndexes(item);
freeList.add(item);
} }
public synchronized void storeIndex(IndexItem index) throws IOException{ public synchronized void storeIndex(IndexItem index) throws IOException{
@ -84,7 +92,7 @@ public final class IndexManager{
try { try {
writer.updateIndexes(index); writer.updateIndexes(index);
}catch(Throwable e) { }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); writer.redoStoreItem(redo);
} }
public synchronized IndexItem createNewIndex(){ public synchronized IndexItem createNewIndex() throws IOException{
IndexItem result=getNextFreeIndex(); IndexItem result=getNextFreeIndex();
if(result==null){ if(result==null){
// allocate one // allocate one
@ -118,7 +126,7 @@ public final class IndexManager{
public synchronized boolean delete() throws IOException{ public synchronized boolean delete() throws IOException{
freeList.clear(); firstFree = lastFree = null;
if(indexFile!=null){ if(indexFile!=null){
indexFile.close(); indexFile.close();
indexFile=null; indexFile=null;
@ -126,12 +134,23 @@ public final class IndexManager{
return file.delete(); return file.delete();
} }
private synchronized IndexItem getNextFreeIndex(){ private synchronized IndexItem getNextFreeIndex() throws IOException{
IndexItem result=null; IndexItem result=null;
if(!freeList.isEmpty()){ if (firstFree != null) {
result=(IndexItem) freeList.removeLast(); if (firstFree.equals(lastFree)) {
result = firstFree;
firstFree=lastFree=null;
}else {
result = firstFree;
firstFree = getIndex(firstFree.getNextItem());
if (firstFree==null) {
lastFree=null;
}
}
result.reset(); result.reset();
} }
return result; return result;
} }
@ -158,11 +177,19 @@ public final class IndexManager{
reader=new StoreIndexReader(indexFile); reader=new StoreIndexReader(indexFile);
writer=new StoreIndexWriter(indexFile,name,redoLog); writer=new StoreIndexWriter(indexFile,name,redoLog);
long offset=0; long offset=0;
while((offset+IndexItem.INDEX_SIZE)<=indexFile.length()){ while((offset+IndexItem.INDEX_SIZE)<=indexFile.length()){
IndexItem index=reader.readItem(offset); IndexItem index=reader.readItem(offset);
if(!index.isActive()){ if(!index.isActive()){
index.reset(); index.reset();
freeList.add(index); if (lastFree != null) {
lastFree.setNextItem(index.getOffset());
updateIndexes(lastFree);
lastFree=index;
}else {
lastFree=firstFree=index;
}
} }
offset+=IndexItem.INDEX_SIZE; offset+=IndexItem.INDEX_SIZE;
} }