📄 btree.java
字号:
super.deallocate();
}
static class BtreeEntry implements Map.Entry {
public Object getKey() {
return key;
}
public Object getValue() {
return db.lookupObject(oid);
}
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 Types.Boolean:
return (data[offs] != 0) ? Boolean.TRUE : Boolean.FALSE;
case Types.Byte:
return new Byte(data[offs]);
case Types.Short:
return new Short(Bytes.unpack2(data, offs));
case Types.Char:
return new Character((char)Bytes.unpack2(data, offs));
case Types.Int:
return new Integer(Bytes.unpack4(data, offs));
case Types.Object:
return db.lookupObject(Bytes.unpack4(data, offs));
case Types.Long:
return new Long(Bytes.unpack8(data, offs));
case Types.Date:
return new Date(Bytes.unpack8(data, offs));
case Types.Float:
return new Float(Float.intBitsToFloat(Bytes.unpack4(data, offs)));
case Types.Double:
return new Double(Double.longBitsToDouble(Bytes.unpack8(data, offs)));
case Types.String:
return unpackStrKey(pg, pos);
case Types.ArrayOfByte:
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 extends Iterator {
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 == Types.String) {
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 == Types.ArrayOfByte) {
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 (compareByteArrays(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 (compareByteArrays(from, pg, i) >= from.inclusion) {
l = i + 1;
} else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -