public void createTable(string tblname, Schema sch, Transaction tx) {//创建一个新表,有指定的表名称、模式信息 TableInfo ti = new TableInfo(tblname, sch); //往tblcat表中插入一条记录 RecordFile tcatfile = new RecordFile(tcatInfo, tx); tcatfile.insert(); tcatfile.setString("tblname", tblname); tcatfile.setInt("reclength", ti.recordLength()); tcatfile.close(); //往fldcat表中插入记录:每个字段的信息 RecordFile fcatfile = new RecordFile(fcatInfo, tx); foreach (string fldname in sch.fields()) { fcatfile.insert(); fcatfile.setString("tblname", tblname); fcatfile.setString("fldname", fldname); fcatfile.setInt("type", sch.type(fldname)); fcatfile.setInt("length", sch.length(fldname)); fcatfile.setInt("offset", ti.offset(fldname)); } fcatfile.close(); }
public TableInfo getTableInfo(string tblname, Transaction tx) { RecordFile tcatfile = new RecordFile(tcatInfo, tx); int reclen = -1; while (tcatfile.next()) { if (tcatfile.getString("tblname").Equals(tblname)) {//获取到指定表名称的一条记录长度 reclen = tcatfile.getInt("reclength"); break; } } tcatfile.close(); RecordFile fcatfile = new RecordFile(fcatInfo, tx); Schema sch = new Schema(); Dictionary <string, int> offsets = new Dictionary <string, int>(); while (fcatfile.next()) {//每次取fcatfile中的一条记录 if (fcatfile.getString("tblname").Equals(tblname)) { string fldname = fcatfile.getString("fldname"); int fldtype = fcatfile.getInt("type"); int fldlen = fcatfile.getInt("length"); int offset = fcatfile.getInt("offset"); offsets.Add(fldname, offset); //获取一张表中每个字段名称的偏移量 sch.addField(fldname, fldtype, fldlen); //获取一张表的模式信息 } } fcatfile.close(); return(new TableInfo(tblname, sch, offsets, reclen)); }
public void createIndex(string idxname, string tblname, string fldname, Transaction tx) {//在指定表、指定字段上创造一个指定名称的索引 RecordFile rf = new RecordFile(ti, tx); rf.insert(); rf.setString("indexname", idxname); rf.setString("tablename", tblname); rf.setString("fieldname", fldname); rf.close(); }
public void createView(string vname, string vdef, Transaction tx) {//创建视图,在内存中创建RecordFile读取类往里面写信息 TableInfo ti = tblMgr.getTableInfo("viewcat", tx); RecordFile rf = new RecordFile(ti, tx); rf.insert(); rf.setString("viewname", vname); rf.setString("viewdef", vdef); rf.close(); }
private StatInfo calcTableStats(TableInfo ti, Transaction tx) { lock (threadLock) { int numRecs = 0; RecordFile rf = new RecordFile(ti, tx); int numblocks = 0; while (rf.next()) { numRecs++; numblocks = rf.currentRid().blockNumber() + 1; } rf.close(); return(new StatInfo(numblocks, numRecs)); } }
public string getViewDef(string vname, Transaction tx) { string result = null; TableInfo ti = tblMgr.getTableInfo("viewcat", tx); RecordFile rf = new RecordFile(ti, tx); while (rf.next()) { if (rf.getString("viewdef").Equals(vname)) { result = rf.getString("viewdef"); break; } } rf.close(); return(result); }
private void refreshStatistics(Transaction tx) { lock (threadLock) { tablestats = new Dictionary <string, StatInfo>(); numcalls = 0; TableInfo tcatmd = tblMgr.getTableInfo("tblcat", tx); //获取tblcat表中的信息 RecordFile tcatfile = new RecordFile(tcatmd, tx); //利用这些信息新建一个RecordFile读取类 while (tcatfile.next()) { //获取每一张表的字段信息,计算出块数和记录数,添加到统计信息管理器中去 string tblname = tcatfile.getString("tblname"); TableInfo md = tblMgr.getTableInfo(tblname, tx); StatInfo si = calcTableStats(md, tx); tablestats.Add(tblname, si); } tcatfile.close(); } }
public Dictionary <string, IndexInfo> getIndexInfo(string tblname, Transaction tx) {//返回一个映射,包含了指定表中所有索引的信息 Dictionary <string, IndexInfo> result = new Dictionary <string, IndexInfo>(); RecordFile rf = new RecordFile(ti, tx); while (rf.next()) { if (rf.getString("tablename").Equals(tblname)) { string idxname = rf.getString("indexname"); string fldname = rf.getString("fieldname"); IndexInfo ii = new IndexInfo(idxname, tblname, fldname, tx); result.Add(fldname, ii); } } rf.close(); return(result); }
public void close() { rf.close(); }