Ejemplo n.º 1
0
    def main(cls, args):
        # db = SimpleDB("plannertest1")
        fm = FileMgr(File("plannertest1"), 400)
        lm = LogMgr(fm, "simpledb.log")
        bm = BufferMgr(fm, lm, 8)
        tx = Transaction(fm, lm, bm)

        isnew = fm.isNew()
        mdm = MetadataMgr(isnew, tx)
        qp = BasicQueryPlanner(mdm)
        up = BasicUpdatePlanner(mdm)
        planner = Planner(qp, up)

        cmd = "create table T1(A int, B varchar(9))"
        planner.executeUpdate(cmd, tx)
        n = 200
        print("Inserting " + str(n) + " random records.")
        for i in range(n):
            a = random.randint(0, 50)
            b = "rec" + str(a)
            cmd = "insert into T1(A,B) values(" + str(a) + ", '" + b + "')"
            planner.executeUpdate(cmd, tx)

        qry = "select B from T1 where A=10"
        p = planner.createQueryPlan(qry, tx)
        s = p.open()
        while s.next:
            print(s.getString("b"))
        s.close()
        tx.commit()
Ejemplo n.º 2
0
    def main(cls, args):
        # db = SimpleDB("studentdb")
        fm = FileMgr(File("plannertest2"), 400)
        lm = LogMgr(fm, "simpledb.log")
        bm = BufferMgr(fm, lm, 8)
        tx = Transaction(fm, lm, bm)

        isnew = fm.isNew()
        mdm = MetadataMgr(isnew, tx)
        qp = BasicQueryPlanner(mdm)
        up = BasicUpdatePlanner(mdm)
        planner = Planner(qp, up)

        #  part 1: Process a query
        qry = "select sname, gradyear from student"
        p = planner.createQueryPlan(qry, tx)
        s = p.open()
        while s.next:
            print(s.getString("sname") + " " + str(s.getInt("gradyear")))
        s.close()

        #  part 2: Process an update command
        cmd = "delete from STUDENT where MajorId = 30"
        num = planner.executeUpdate(cmd, tx)
        print(str(num) + " students were deleted")
        tx.commit()
Ejemplo n.º 3
0
class SimpleDB(object):
    BLOCK_SIZE = 400
    BUFFER_SIZE = 8
    LOG_FILE = "simpledb.log"

    def __init__(self, *args):
        if len(args) == 3:
            #
            #     * A constructor useful for debugging.
            #     * @param dirname the name of the database directory
            #     * @param blocksize the block size
            #     * @param buffsize the number of buffers
            #
            dirname, blocksize, buffsize = args
            dbDirectory = File(dirname)
            self.fm = FileMgr(dbDirectory, blocksize)
            self.lm = LogMgr(self.fm, SimpleDB.LOG_FILE)
            self.bm = BufferMgr(self.fm, self.lm, buffsize)
        else:
            #
            #     * A simpler constructor for most situations. Unlike the
            #     * 3-arg constructor, it also initializes the metadata tables.
            #     * @param dirname the name of the database directory
            #
            dirname = args[0]
            self.__init__(dirname, self.BLOCK_SIZE, self.BUFFER_SIZE)
            tx = self.newTx()
            isnew = self.fm.isNew()
            if isnew:
                print("creating new database")
            else:
                print("recovering existing database")
                tx.recover()
            self.mdm = MetadataMgr(isnew, tx)
            qp = BasicQueryPlanner(self.mdm)
            up = BasicUpdatePlanner(self.mdm)
            #     QueryPlanner qp = new HeuristicQueryPlanner(mdm);
            #     UpdatePlanner up = new IndexUpdatePlanner(mdm);
            self._planner = Planner(qp, up)
            tx.commit()

    #
    #     * A convenient way for clients to create transactions
    #     * and access the metadata.
    #
    def newTx(self):
        return Transaction(self.fm, self.lm, self.bm)

    def mdMgr(self):
        return self.mdm

    def planner(self):
        return self._planner

    #  These methods aid in debugging
    def fileMgr(self):
        return self.fm

    def logMgr(self):
        return self.lm

    def bufferMgr(self):
        return self.bm