Skip to content
Permalink
Browse files

Refactored page store to support other page stores

  • Loading branch information
justinsb committed Jan 7, 2014
1 parent 16c431b commit c24a834411f786e11404fae589b3344ac5f04135
@@ -21,8 +21,8 @@

Mutable mutable;

public BranchPage(Page parent, int pageNumber, ByteBuffer buffer) {
super(parent, pageNumber, buffer);
public BranchPage(Btree btree, Page parent, int pageNumber, ByteBuffer buffer) {
super(btree, parent, pageNumber, buffer);
}

static class Mutable {
@@ -144,7 +144,7 @@ public boolean walk(Transaction txn, ByteBuffer from, EntryListener listener) {

int pageNumber = entry.pageNumber;

Page childPage = txn.getPage(page, pageNumber);
Page childPage = txn.getPage(page.btree, page, pageNumber);

boolean keepGoing = childPage.walk(txn, from, listener);
if (!keepGoing) {
@@ -166,7 +166,7 @@ public void updateLbound(Transaction txn, int pageNumber) {
}

ByteBuffer oldKey = entry.key;
Page childPage = txn.getPage(page, pageNumber);
Page childPage = txn.getPage(page.btree, page, pageNumber);

ByteBuffer newKey = childPage.getKeyLbound();
entries.set(i, new Entry(newKey, pageNumber));
@@ -363,7 +363,7 @@ public boolean walk(Transaction txn, ByteBuffer from, EntryListener listener) {
while (pos < n) {
int pageNumber = getPageNumber(pos);

Page page = txn.getPage(this, pageNumber);
Page page = txn.getPage(btree, this, pageNumber);
boolean keepGoing = page.walk(txn, from, listener);
if (!keepGoing) {
return false;
@@ -421,7 +421,7 @@ Mutable getMutable() {

int pageNumber = getPageNumber(pos);

Page childPage = txn.getPage(this, pageNumber);
Page childPage = txn.getPage(btree, this, pageNumber);

ByteBuffer oldLbound = childPage.getKeyLbound();

@@ -491,12 +491,12 @@ public void dump(PrintStream os) {
}
}

public static BranchPage createNew(Page parent, int pageNumber, Page childPage) {
public static BranchPage createNew(Btree btree, Page parent, int pageNumber, Page childPage) {
ByteBuffer empty = ByteBuffer.allocate(6);
empty.putShort((short) 0);
empty.flip();

BranchPage page = new BranchPage(parent, pageNumber, empty);
BranchPage page = new BranchPage(btree, parent, pageNumber, empty);
page.getMutable().setChildren(childPage);

return page;
@@ -9,24 +9,24 @@
public class Btree {
private static final Logger log = LoggerFactory.getLogger(Btree.class);

final PageStore pageStore;
final Database db;
final boolean uniqueKeys;

final Lock writeLock = new ReentrantLock();

public Btree(PageStore pageStore, boolean uniqueKeys) {
this.pageStore = pageStore;
public Btree(Database db, boolean uniqueKeys) {
this.db = db;
this.uniqueKeys = uniqueKeys;
}

public WriteTransaction beginReadWrite() {
writeLock.lock();
WriteTransaction txn = pageStore.beginReadWriteTransaction(writeLock);
WriteTransaction txn = db.transactionTracker.beginReadWriteTransaction(writeLock);
return txn;
}

public ReadOnlyTransaction beginReadOnly() {
ReadOnlyTransaction txn = pageStore.beginReadOnlyTransaction();
ReadOnlyTransaction txn = db.transactionTracker.beginReadOnlyTransaction();
return txn;
}

@@ -0,0 +1,14 @@
package com.cloudata.btree;

public class Database {
final PageStore pageStore;
final TransactionTracker transactionTracker;

Database(PageStore pageStore) {
this.pageStore = pageStore;

MasterPage latest = pageStore.findLatestMasterPage();
this.transactionTracker = new TransactionTracker(this, latest);
}

}
@@ -44,9 +44,9 @@

final boolean uniqueKeys;

public LeafPage(Page parent, int pageNumber, ByteBuffer buffer, boolean uniqueKeys) {
super(parent, pageNumber, buffer);
this.uniqueKeys = uniqueKeys;
public LeafPage(Btree btree, Page parent, int pageNumber, ByteBuffer buffer) {
super(btree, parent, pageNumber, buffer);
this.uniqueKeys = btree.isUniqueKeys();
}

static class Mutable {
@@ -57,6 +57,8 @@ public LeafPage(Page parent, int pageNumber, ByteBuffer buffer, boolean uniqueKe
int totalKeySize;
int totalValueSize;

final Btree btree;

int firstGTE(ByteBuffer find) {
int n = getEntryCount();
int pos;
@@ -146,6 +148,7 @@ int firstGTE(ByteBuffer find) {
}

Mutable(LeafPage page) {
this.btree = page.btree;
this.uniqueKeys = page.uniqueKeys;
int n = page.getEntryCount();

@@ -189,7 +192,7 @@ int firstGTE(ByteBuffer find) {
target = original;
} else {
int pageNumber = transaction.assignPageNumber();
target = LeafPage.createNew(original.parent, pageNumber, uniqueKeys);
target = LeafPage.createNew(btree, original.parent, pageNumber);
extraPages.add(target);
}

@@ -517,7 +520,7 @@ public byte getPageType() {
return PAGE_TYPE;
}

public static LeafPage createNew(Page parent, int pageNumber, boolean uniqueKeys) {
public static LeafPage createNew(Btree btree, Page parent, int pageNumber) {
// TODO: Reuse a shared buffer?
ByteBuffer empty = ByteBuffer.allocate(6);
empty.putShort((short) 0);
@@ -526,7 +529,7 @@ public static LeafPage createNew(Page parent, int pageNumber, boolean uniqueKeys

empty.flip();

return new LeafPage(parent, pageNumber, empty, uniqueKeys);
return new LeafPage(btree, parent, pageNumber, empty);
}

@Override

0 comments on commit c24a834

Please sign in to comment.
You can’t perform that action at this time.