📄 btree.java
字号:
public IPersistent[] toPersistentArray(IPersistent[] arr) {
if (arr.length < nElems) {
arr = (IPersistent[])Array.newInstance(arr.getClass().getComponentType(), nElems);
}
if (root != 0) {
BtreePage.traverseForward((StorageImpl)getStorage(), root, type, height, arr, 0);
}
if (arr.length > nElems) {
arr[nElems] = null;
}
return arr;
}
public void deallocate() {
if (root != 0) {
BtreePage.purge((StorageImpl)getStorage(), root, type, height);
}
super.deallocate();
}
public int markTree()
{
if (root != 0) {
return BtreePage.markPage((StorageImpl)getStorage(), root, type, height);
}
return 0;
}
public void export(XMLExporter exporter) throws java.io.IOException
{
if (root != 0) {
BtreePage.exportPage((StorageImpl)getStorage(), exporter, root, type, height);
}
}
static class BtreeEntry implements Map.Entry {
public Object getKey() {
return key;
}
public Object getValue() {
return db.lookupObject(oid, null);
}
public Object setValue(Object value) {
throw new UnsupportedOperationException();
}
public boolean equals(Object o) {
if (!(o instanceof Map.Entry)) {
return false;
}
Map.Entry e = (Map.Entry)o;
return (getKey()==null
? e.getKey()==null : getKey().equals(e.getKey()))
&& (getValue()==null
? getValue()==null : getValue().equals(e.getValue()));
}
BtreeEntry(StorageImpl db, Object key, int oid) {
this.db = db;
this.key = key;
this.oid = oid;
}
private Object key;
private StorageImpl db;
private int oid;
}
Object unpackKey(StorageImpl db, Page pg, int pos) {
byte[] data = pg.data;
int offs = BtreePage.firstKeyOffs + pos*ClassDescriptor.sizeof[type];
switch (type) {
case ClassDescriptor.tpBoolean:
return Boolean.valueOf(data[offs] != 0);
case ClassDescriptor.tpByte:
return new Byte(data[offs]);
case ClassDescriptor.tpShort:
return new Short(Bytes.unpack2(data, offs));
case ClassDescriptor.tpChar:
return new Character((char)Bytes.unpack2(data, offs));
case ClassDescriptor.tpInt:
return new Integer(Bytes.unpack4(data, offs));
case ClassDescriptor.tpObject:
return db.lookupObject(Bytes.unpack4(data, offs), null);
case ClassDescriptor.tpLong:
return new Long(Bytes.unpack8(data, offs));
case ClassDescriptor.tpDate:
return new Date(Bytes.unpack8(data, offs));
case ClassDescriptor.tpFloat:
return new Float(Float.intBitsToFloat(Bytes.unpack4(data, offs)));
case ClassDescriptor.tpDouble:
return new Double(Double.longBitsToDouble(Bytes.unpack8(data, offs)));
case ClassDescriptor.tpString:
return unpackStrKey(pg, pos);
case ClassDescriptor.tpArrayOfByte:
return unpackByteArrayKey(pg, pos);
default:
Assert.failed("Invalid type");
}
return null;
}
static String unpackStrKey(Page pg, int pos) {
int len = BtreePage.getKeyStrSize(pg, pos);
int offs = BtreePage.firstKeyOffs + BtreePage.getKeyStrOffs(pg, pos);
byte[] data = pg.data;
char[] sval = new char[len];
for (int j = 0; j < len; j++) {
sval[j] = (char)Bytes.unpack2(data, offs);
offs += 2;
}
return new String(sval);
}
Object unpackByteArrayKey(Page pg, int pos) {
int len = BtreePage.getKeyStrSize(pg, pos);
int offs = BtreePage.firstKeyOffs + BtreePage.getKeyStrOffs(pg, pos);
byte[] val = new byte[len];
System.arraycopy(pg.data, offs, val, 0, len);
return val;
}
public Iterator iterator() {
return iterator(null, null, ASCENT_ORDER);
}
public Iterator entryIterator() {
return entryIterator(null, null, ASCENT_ORDER);
}
final int compareByteArrays(Key key, Page pg, int i) {
return compareByteArrays((byte[])key.oval,
pg.data,
BtreePage.getKeyStrOffs(pg, i) + BtreePage.firstKeyOffs,
BtreePage.getKeyStrSize(pg, i));
}
class BtreeSelectionIterator implements PersistentIterator {
BtreeSelectionIterator(Key from, Key till, int order) {
this.from = from;
this.till = till;
this.order = order;
reset();
}
void reset() {
int i, l, r;
sp = 0;
counter = updateCounter;
if (height == 0) {
return;
}
int pageId = root;
StorageImpl db = (StorageImpl)getStorage();
if (db == null) {
throw new StorageError(StorageError.DELETED_OBJECT);
}
int h = height;
pageStack = new int[h];
posStack = new int[h];
if (type == ClassDescriptor.tpString) {
if (order == ASCENT_ORDER) {
if (from == null) {
while (--h >= 0) {
posStack[sp] = 0;
pageStack[sp] = pageId;
Page pg = db.getPage(pageId);
pageId = BtreePage.getKeyStrOid(pg, 0);
end = BtreePage.getnItems(pg);
db.pool.unfix(pg);
sp += 1;
}
} else {
while (--h > 0) {
pageStack[sp] = pageId;
Page pg = db.getPage(pageId);
l = 0;
r = BtreePage.getnItems(pg);
while (l < r) {
i = (l+r) >> 1;
if (BtreePage.compareStr(from, pg, i) >= from.inclusion) {
l = i + 1;
} else {
r = i;
}
}
Assert.that(r == l);
posStack[sp] = r;
pageId = BtreePage.getKeyStrOid(pg, r);
db.pool.unfix(pg);
sp += 1;
}
pageStack[sp] = pageId;
Page pg = db.getPage(pageId);
l = 0;
end = r = BtreePage.getnItems(pg);
while (l < r) {
i = (l+r) >> 1;
if (BtreePage.compareStr(from, pg, i) >= from.inclusion) {
l = i + 1;
} else {
r = i;
}
}
Assert.that(r == l);
if (r == end) {
sp += 1;
gotoNextItem(pg, r-1);
} else {
posStack[sp++] = r;
db.pool.unfix(pg);
}
}
if (sp != 0 && till != null) {
Page pg = db.getPage(pageStack[sp-1]);
if (-BtreePage.compareStr(till, pg, posStack[sp-1]) >= till.inclusion) {
sp = 0;
}
db.pool.unfix(pg);
}
} else { // descent order
if (till == null) {
while (--h > 0) {
pageStack[sp] = pageId;
Page pg = db.getPage(pageId);
posStack[sp] = BtreePage.getnItems(pg);
pageId = BtreePage.getKeyStrOid(pg, posStack[sp]);
db.pool.unfix(pg);
sp += 1;
}
pageStack[sp] = pageId;
Page pg = db.getPage(pageId);
posStack[sp++] = BtreePage.getnItems(pg)-1;
db.pool.unfix(pg);
} else {
while (--h > 0) {
pageStack[sp] = pageId;
Page pg = db.getPage(pageId);
l = 0;
r = BtreePage.getnItems(pg);
while (l < r) {
i = (l+r) >> 1;
if (BtreePage.compareStr(till, pg, i) >= 1-till.inclusion) {
l = i + 1;
} else {
r = i;
}
}
Assert.that(r == l);
posStack[sp] = r;
pageId = BtreePage.getKeyStrOid(pg, r);
db.pool.unfix(pg);
sp += 1;
}
pageStack[sp] = pageId;
Page pg = db.getPage(pageId);
l = 0;
r = BtreePage.getnItems(pg);
while (l < r) {
i = (l+r) >> 1;
if (BtreePage.compareStr(till, pg, i) >= 1-till.inclusion) {
l = i + 1;
} else {
r = i;
}
}
Assert.that(r == l);
if (r == 0) {
sp += 1;
gotoNextItem(pg, r);
} else {
posStack[sp++] = r-1;
db.pool.unfix(pg);
}
}
if (sp != 0 && from != null) {
Page pg = db.getPage(pageStack[sp-1]);
if (BtreePage.compareStr(from, pg, posStack[sp-1]) >= from.inclusion) {
sp = 0;
}
db.pool.unfix(pg);
}
}
} else if (type == ClassDescriptor.tpArrayOfByte) {
if (order == ASCENT_ORDER) {
if (from == null) {
while (--h >= 0) {
posStack[sp] = 0;
pageStack[sp] = pageId;
Page pg = db.getPage(pageId);
pageId = BtreePage.getKeyStrOid(pg, 0);
end = BtreePage.getnItems(pg);
db.pool.unfix(pg);
sp += 1;
}
} else {
while (--h > 0) {
pageStack[sp] = pageId;
Page pg = db.getPage(pageId);
l = 0;
r = BtreePage.getnItems(pg);
while (l < r) {
i = (l+r) >> 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -