📄 indexmgr.java
字号:
package simpledb.index.metadata;import static simpledb.metadata.TableMgr.MAX_NAME;import simpledb.tx.Transaction;import simpledb.metadata.*;import simpledb.record.*;import java.util.*;/** * The index manager. * The index manager has similar functionalty to the table manager. * @author Edward Sciore */public class IndexMgr { private static int nextIdxNum = 0; private TableInfo ti; /** * Creates the index manager. * This constructor is called during system startup. * If the database is new, then the <i>idxcat</i> table is created. * @param isnew indicates whether this is a new database * @param tx the system startup transaction */ public IndexMgr(boolean isnew, TableMgr tblmgr, Transaction tx) { if (isnew) { Schema sch = new Schema(); sch.addStringField("tablename", MAX_NAME); sch.addStringField("fieldname", MAX_NAME); sch.addIntField("indexnumber"); sch.addIntField("indextype"); tblmgr.createTable("idxcat", sch, tx); } ti = tblmgr.getTableInfo("idxcat", tx); } /** * Creates an index of the specified type for the specified field. * A unique ID is assigned to this index, and its information * is stored in the idxcat table. * @param tblname the name of the indexed table * @param fldname the name of the indexed field * @param idxtype the desired type of the index * @param tx the calling transaction */ public void createIndex(String tblname, String fldname, int idxtype, Transaction tx) { int idxnum = nextIndexNumber(); RecordFile rf = new RecordFile(ti, tx); rf.insert(); rf.setString("tablename", tblname); rf.setString("fieldname", fldname); rf.setInt("indexnumber", idxnum); rf.setInt("indextype", idxtype); rf.close(); } /** * Returns a map containing the index info for all indexes * on the specified table. * @param tblname the name of the table * @param tx the calling transaction * @return a map of IndexInfo objects, keyed by their field names */ public Map<String,IndexInfo> getIndexInfo(String tblname, Transaction tx) { Map<String,IndexInfo> result = new HashMap<String,IndexInfo>(); RecordFile rf = new RecordFile(ti, tx); while (rf.next()) if (rf.getString("tablename").equals(tblname)) { String fldname = rf.getString("fieldname"); int indexnum = rf.getInt("indexnumber"); int idxtype = rf.getInt("indextype"); IndexInfo ii = new IndexInfo(tblname, fldname, indexnum, idxtype, tx); result.put(fldname, ii); } rf.close(); return result; } private static synchronized int nextIndexNumber() { nextIdxNum++; return nextIdxNum; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -