changed free list implementation

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@632050 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Davies 2008-02-28 16:42:30 +00:00
parent e31ca80546
commit 03ecdb00ee
1 changed files with 11 additions and 37 deletions

View File

@ -19,6 +19,7 @@ package org.apache.activemq.kaha.impl.index.hash;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.activemq.kaha.Marshaller; import org.apache.activemq.kaha.Marshaller;
import org.apache.activemq.kaha.StoreEntry; import org.apache.activemq.kaha.StoreEntry;
@ -57,8 +58,7 @@ public class HashIndex implements Index, HashIndexMBean {
private HashBin[] bins; private HashBin[] bins;
private Marshaller keyMarshaller; private Marshaller keyMarshaller;
private long length; private long length;
private HashPage firstFree; private LinkedList<HashPage> freeList = new LinkedList<HashPage>();
private HashPage lastFree;
private AtomicBoolean loaded = new AtomicBoolean(); private AtomicBoolean loaded = new AtomicBoolean();
private LRUCache<Long, HashPage> pageCache; private LRUCache<Long, HashPage> pageCache;
private boolean enablePageCaching=true; private boolean enablePageCaching=true;
@ -207,23 +207,14 @@ public class HashIndex implements Index, HashIndexMBean {
page.setId(offset); page.setId(offset);
page.readHeader(dataIn); page.readHeader(dataIn);
if (!page.isActive()) { if (!page.isActive()) {
if (lastFree != null) { freeList.add(page);
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;
}
} else { } else {
addToBin(page); addToBin(page);
size+=page.size(); size+=page.size();
} }
offset += pageSize; offset += pageSize;
} }
System.err.println("AFTER LOAD SIZE = " + size);
length = offset; length = offset;
} catch (IOException e) { } catch (IOException e) {
LOG.error("Failed to load index ", e); LOG.error("Failed to load index ", e);
@ -237,8 +228,7 @@ public class HashIndex implements Index, HashIndexMBean {
if (indexFile != null) { if (indexFile != null) {
indexFile.close(); indexFile.close();
indexFile = null; indexFile = null;
firstFree = null; freeList.clear();
lastFree = null;
bins = new HashBin[bins.length]; bins = new HashBin[bins.length];
} }
} }
@ -249,7 +239,7 @@ public class HashIndex implements Index, HashIndexMBean {
HashEntry entry = new HashEntry(); HashEntry entry = new HashEntry();
entry.setKey((Comparable)key); entry.setKey((Comparable)key);
entry.setIndexOffset(value.getOffset()); entry.setIndexOffset(value.getOffset());
if (getBin(key).put(entry)) { if (!getBin(key).put(entry)) {
size++; size++;
} }
} }
@ -322,6 +312,7 @@ public class HashIndex implements Index, HashIndexMBean {
length += pageSize; length += pageSize;
indexFile.seek(length); indexFile.seek(length);
indexFile.write(HashEntry.NOT_SET); indexFile.write(HashEntry.NOT_SET);
System.err.println("ALLOCATED NEW PAGE size = " + size);
} }
addToCache(result); addToCache(result);
return result; return result;
@ -331,33 +322,16 @@ public class HashIndex implements Index, HashIndexMBean {
removeFromCache(page); removeFromCache(page);
page.reset(); page.reset();
page.setActive(false); page.setActive(false);
if (lastFree == null) {
firstFree = page;
lastFree = page;
} else {
lastFree.setNextFreePageId(page.getId());
writePageHeader(lastFree);
}
writePageHeader(page); writePageHeader(page);
freeList.add(page);
} }
private HashPage getNextFreePage() throws IOException { private HashPage getNextFreePage() throws IOException {
HashPage result = null; HashPage result = null;
if (firstFree != null) { if(!freeList.isEmpty()) {
if (firstFree.equals(lastFree)) { result = freeList.removeFirst();
result = firstFree;
firstFree = null;
lastFree = null;
} else {
result = firstFree;
firstFree = getPageHeader(firstFree.getNextFreePageId());
if (firstFree == null) {
lastFree = null;
}
}
result.setActive(true); result.setActive(true);
result.reset(); result.reset();
writePageHeader(result);
} }
return result; return result;
} }