📄 indexinfo.java
字号:
package simpledb.index.metadata;import static simpledb.sql.Types.INTEGER;import static simpledb.file.Page.BLOCK_SIZE;import simpledb.server.SimpleDB;import simpledb.tx.Transaction;import simpledb.record.*;import simpledb.index.Index;import simpledb.index.hash.HashIndex;import simpledb.index.btree.BTreeIndex;import simpledb.metadata.StatInfo;/** * The information about an index. * This information is used by the query planner in order to * estimate the costs of using the index, * and to obtain the schema of the index records. * It's methods are essentially the same as those of Plan. * @author Edward Sciore */public class IndexInfo { public static int HASH = 0; public static int BTREE = 1; private String fldname; private int idxnum, idxtype; private Transaction tx; private TableInfo ti; private StatInfo si; /** * Creates an IndexInfo object for the specified index. * @param tblname the name of the table * @param fldname the name of the indexed field * @param idxnum the ID of the index * @param idxtype the type of the index (i.e. hash or B-tree) * @param tx the calling transaction */ public IndexInfo(String tblname, String fldname, int idxnum, int idxtype, Transaction tx) { this.fldname = fldname; this.idxnum = idxnum; this.idxtype = idxtype; this.tx = tx; ti = SimpleDB.mdMgr().getTableInfo(tblname, tx); si = SimpleDB.mdMgr().getStatInfo(tblname, ti, tx); } /** * Opens the index described by this object. * @return the Index object associated with this information */ public Index open() { Schema sch = schema(); if (idxtype == HASH) return new HashIndex(idxnum, sch, tx); else return new BTreeIndex(idxnum, sch, tx); } /** * Estimates the number of block accesses required to * find all index records having a particular search key. * The method uses the table's metadata to estimate the * size of the index file and the number of index records * per block. * It then passes this information to the traversalCost * method of the appropriate index type, * which provides the estimate. * @return the number of block accesses required to traverse the index */ public int blocksAccessed() { int rpb = BLOCK_SIZE / ti.recordLength(); int numblocks = si.recordsOutput() / rpb; if (idxtype == HASH) return HashIndex.searchCost(numblocks, rpb); else return BTreeIndex.searchCost(numblocks, rpb); } /** * Returns the estimated number of records having a * search key. This value is the same as doing a select * query; that is, it is the number of records in the table * divided by the number of distinct values of the indexed field. * @return the estimated number of records having a search key */ public int recordsOutput() { return si.recordsOutput() / si.distinctValues(fldname); } /** * Returns the schema of the index records. * The schema consists of the dataRID (which is * represented as two integers, the block number and the * record ID) and the dataval (which is the indexed field). * Schema information about the indexed field is obtained * via the table's metadata. * @return the schema of the index records */ private Schema schema() { Schema sch = new Schema(); sch.addIntField("block"); sch.addIntField("id"); if (ti.schema().type(fldname) == INTEGER) sch.addIntField("dataval"); else { int fldlen = ti.schema().length(fldname); sch.addStringField("dataval", fldlen); } return sch; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -