This commit is contained in:
unknown
2011-07-19 18:52:14 +04:00
parent b74670bd22
commit 6e60ee82fd
2 changed files with 26 additions and 20 deletions
@@ -27,7 +27,7 @@ abstract class IntToIntBtree {
private final TIntObjectHashMap<byte[]> pagesCache = new TIntObjectHashMap<byte[]>(100);
private boolean isLarge = true;
public IntToIntBtree(int _pageSize, int rootAdress) {
public IntToIntBtree(int _pageSize, int rootAdress, boolean initial) {
pageSize = _pageSize;
buffer = new byte[_pageSize];
root = new BtreeIndexNodeView(this);
@@ -37,19 +37,24 @@ abstract class IntToIntBtree {
assert i < Short.MAX_VALUE && i % 2 == 0;
maxInteriorNodes = (short)i;
pagesCount = 1;
if (initial) {
root.setIndexLeaf(true);
root.sync();
}
}
protected abstract int allocPage();
protected abstract int allocEmptyPage();
private int nextPage() {
int address = allocPage();
int address = allocEmptyPage();
++pagesCount;
return address;
}
protected abstract void saveBytes(int address, byte[] buffer, int offset, int length);
protected abstract void savePage(int address, byte[] pageBuffer);
protected abstract void loadBytes(int address, byte[] buffer, int offset, int length);
protected abstract void loadPageContent(int address, byte[] pageBuffer);
public int get(int key) {
BtreeIndexNodeView currentIndexNode = new BtreeIndexNodeView(this);
@@ -173,7 +178,7 @@ abstract class IntToIntBtree {
buffer = bytes;
} else {
buffer = new byte[btree.pageSize];
btree.loadBytes(address, buffer, 0, btree.pageSize);
btree.loadPageContent(address, buffer);
btree.pagesCache.put(address, buffer);
}
}
@@ -188,7 +193,7 @@ abstract class IntToIntBtree {
void sync() {
if (buffer != null) {
btree.saveBytes(address, buffer, 0, buffer.length);
btree.savePage(address, buffer);
}
}
@@ -45,25 +45,27 @@ public class PersistentBTreeEnumerator<Data> extends PersistentEnumeratorBase<Da
super(file, new MappedFileEnumeratorStorage(file, initialSize), dataDescriptor, initialSize);
storeVars(false);
if (btree == null) initBtree();
if (btree == null) initBtree(false);
}
private void initBtree() {
btree = new IntToIntBtree(PAGE_SIZE, myRootNodeStart) {
private void initBtree(boolean initial) {
btree = new IntToIntBtree(PAGE_SIZE, myRootNodeStart, initial) {
@Override
protected int allocPage() {
protected int allocEmptyPage() {
return PersistentBTreeEnumerator.this.allocPage();
}
@Override
protected void saveBytes(int address, byte[] buffer, int offset, int length) {
myStorage.put(address, buffer, offset, length);
protected void savePage(int address, byte[] pageBuffer) {
if (doSanityCheck) myAssert(pageBuffer.length == PAGE_SIZE);
myStorage.put(address, pageBuffer, 0, pageBuffer.length);
}
@Override
protected void loadBytes(int address, byte[] buffer, int offset, int length) {
myStorage.get(address, buffer, offset, length);
protected void loadPageContent(int address, byte[] pageBuffer) {
if (doSanityCheck) myAssert(pageBuffer.length == PAGE_SIZE);
myStorage.get(address, pageBuffer, 0, pageBuffer.length);
}
@Override
@@ -103,14 +105,13 @@ public class PersistentBTreeEnumerator<Data> extends PersistentEnumeratorBase<Da
myLogicalFileLength = PAGE_SIZE;
myRootNodeStart = (DATA_START / PAGE_SIZE + 1) * PAGE_SIZE;
allocPage(myRootNodeStart);
allocEmptyPage(myRootNodeStart);
myFirstPageStart = myDataPageStart = -1;
myDuplicatedValuesPageStart = -1;
storeVars(true);
initBtree();
btree.root.setIndexLeaf(true);
initBtree(true);
btree.root.sync();
}
@@ -120,7 +121,7 @@ public class PersistentBTreeEnumerator<Data> extends PersistentEnumeratorBase<Da
super.doClose();
}
private int allocPage(int pageStart) {
private int allocEmptyPage(int pageStart) {
if (myStorage instanceof MappedFileEnumeratorStorage) {
myStorage.putInt(pageStart + PAGE_SIZE - 4, 0);
} else {
@@ -132,7 +133,7 @@ public class PersistentBTreeEnumerator<Data> extends PersistentEnumeratorBase<Da
}
private int allocPage() {
return allocPage(myLogicalFileLength);
return allocEmptyPage(myLogicalFileLength);
}
@Override