/*$Id$*/
package at.jku.sii.sqlitereader.btree;
import at.jku.sii.sqlitereader.SqliteDataBase;
import at.jku.sii.sqlitereader.io.ArrayDataInput;
public final class BTreePages {
private BTreePages() {
}
public static BTreePage<TableCell> readMaster(SqliteDataBase db) {
final ArrayDataInput block = db.getPageBlock(1);
block.seek(SqliteDataBase.HEADER_SIZE); // as we have the first block skip the header
@SuppressWarnings("unchecked")
BTreePage<TableCell> tree = (BTreePage<TableCell>) read(db, 1, block);
db.resolvePage(1, tree);
return tree;
}
public static BTreePage<? extends Cell> read(SqliteDataBase db, int page) {
ArrayDataInput block = db.getPageBlock(page);
BTreePage<? extends Cell> tree = read(db, page, block);
db.resolvePage(page, tree);
return tree;
}
private static BTreePage<? extends Cell> read(SqliteDataBase db, int page, ArrayDataInput block) {
Type type = readType(block);
block.annotateLastByte("BTreeType", type);
switch (type) {
case TABLE_INTERIOR:
return new TableInteriorBTreePage(page, db, block);
case TABLE_LEAF:
return new TableLeafBTreePage(page, db, block);
case INDEX_LEAF:
return new IndexLeafBTreePage(page, db, block);
case INDEX_INTERIOR:
return new IndexInteriorBTreePage(page, db, block);
default:
throw new IllegalStateException();
}
}
private static Type readType(ArrayDataInput block) {
byte type = block.readByte();
switch (type) {
case 2:
return Type.INDEX_INTERIOR;
case 5:
return Type.TABLE_INTERIOR;
case 10:
return Type.INDEX_LEAF;
case 13:
return Type.TABLE_LEAF;
}
return null;
}
}