def main(cls, args): # db = SimpleDB("tabletest", 400, 8) fm = FileMgr(File("tabletest"), 400) lm = LogMgr(fm, "simpledb.log") bm = BufferMgr(fm, lm, 8) tx = Transaction(fm, lm, bm) tm = TableMgr(True, tx) tcatLayout = tm.getLayout("tblcat", tx) print("Here are all the tables and their lengths.") ts = TableScan(tx, "tblcat", tcatLayout) while ts.next(): tname = ts.getString("tblname") slotsize = ts.getInt("slotsize") print(tname + " " + str(slotsize)) ts.close() print("\nHere are the fields for each table and their offsets") fcatLayout = tm.getLayout("fldcat", tx) ts = TableScan(tx, "fldcat", fcatLayout) while ts.next(): tname = ts.getString("tblname") fname = ts.getString("fldname") offset = ts.getInt("offset") print(tname + " " + fname + " " + str(offset)) ts.close()
def getViewDef(self, vname, tx): result = None layout = self.tblMgr.getLayout("viewcat", tx) ts = TableScan(tx, "viewcat", layout) while ts.next(): if ts.getString("viewname") == vname: result = ts.getString("viewdef") break ts.close() return result
def getIndexInfo(self, tblname, tx): result = {} ts = TableScan(tx, "idxcat", self.layout) while ts.next(): if ts.getString("tablename") == tblname: idxname = ts.getString("indexname") fldname = ts.getString("fieldname") tblLayout = self.tblmgr.getLayout(tblname, tx) tblsi = self.statmgr.getStatInfo(tblname, tblLayout, tx) ii = IndexInfo(idxname, fldname, tblLayout.schema(), tx, tblsi) result[fldname] = ii ts.close() return result
def main(cls, args): # db = SimpleDB("tabletest", 400, 8) fm = FileMgr(File("tabletest"), 400) lm = LogMgr(fm, "simpledb.log") bm = BufferMgr(fm, lm, 8) tx = Transaction(fm, lm, bm) sch = Schema() sch.addIntField("A") sch.addStringField("B", 9) layout = Layout(sch) for fldname in layout.schema().fields(): offset = layout.offset(fldname) print(fldname + " has offset " + str(offset)) print("Filling the table with 50 random records.") ts = TableScan(tx, "T", layout) for i in range(50): ts.insert() n = random.randint(0, 50) ts.setInt("A", n) ts.setString("B", "rec" + str(n)) print("inserting into slot " + ts.getRid().__str__() + ": {" + str(n) + ", " + "rec" + str(n) + "}") print("Deleting these records, whose A-values are less than 25.") count = 0 ts.beforeFirst() while ts.next(): a = ts.getInt("A") b = ts.getString("B") if a < 25: count += 1 print("slot " + ts.getRid().__str__() + ": {" + str(a) + ", " + b + "}") ts.delete() print(str(count) + " values under 10 were deleted.\n") print("Here are the remaining records.") ts.beforeFirst() while ts.next(): a = ts.getInt("A") b = ts.getString("B") print("slot " + ts.getRid().__str__() + ": {" + str(a) + ", " + b + "}") ts.close() tx.commit()
def getLayout(self, tblname, tx): size = -1 tcat = TableScan(tx, "tblcat", self.tcatLayout) while tcat.next(): if tcat.getString("tblname") == tblname: size = tcat.getInt("slotsize") break tcat.close() sch = Schema() offsets = {} fcat = TableScan(tx, "fldcat", self.fcatLayout) while fcat.next(): if fcat.getString("tblname") == tblname: fldname = fcat.getString("fldname") fldtype = fcat.getInt("type") fldlen = fcat.getInt("length") offset = fcat.getInt("offset") offsets[fldname] = offset sch.addField(fldname, fldtype, fldlen) fcat.close() return Layout(sch, offsets, size)
def refreshStatistics(self, tx): self.tablestats = {} self.numcalls = 0 tcatlayout = self.tblMgr.getLayout("tblcat", tx) tcat = TableScan(tx, "tblcat", tcatlayout) while tcat.next(): tblname = tcat.getString("tblname") layout = self.tblMgr.getLayout(tblname, tx) self.lock.release() si = self.calcTableStats(tblname, layout, tx) self.lock.acquire() self.tablestats[tblname] = si tcat.close()