def test_test_log_mgr_and_iter(self):
        logmgr = SimpleDB.log_mgr()
        self.assertIsInstance(logmgr, LogMgr)

        for i in range(2):
            lsn = logmgr.append(["This is a very looooooooooooo" +
                                    "ooooooooooooooooooooooooooooo" +
                                    "ooooooooooooooooooooong record "+str(i)+"."])
            logmgr.flush(lsn)

        for i in range(4):
            logmgr.append([i+1])

        self.assertEqual(SimpleDB.file_mgr().size("simpledb.log"), 3)

        self.assertEqual(logmgr._currentblk._blknum, 2)
        logiter = logmgr.iterator()
        self.assertIsInstance(logiter, LogIterator)
        for i in range(4):
            self.assertEqual(logiter.next().next_int(), 4-i)
        self.assertEqual(logiter.next().next_string(), "This is a very looooooooooooo" +
                                    "ooooooooooooooooooooooooooooo" +
                                    "ooooooooooooooooooooong record 1.")

        self.assertEqual(logmgr._currentblk._blknum, 2)
        logiter = logmgr.iterator()
        count = 0
        for record in logiter.generator():
            self.assertIsInstance(record, BasicLogRecord)
            count += 1
        self.assertEqual(count, 6)
    def test_test_log_mgr_and_iter(self):
        logmgr = SimpleDB.log_mgr()
        self.assertIsInstance(logmgr, LogMgr)

        for i in range(2):
            lsn = logmgr.append([
                "This is a very looooooooooooo" +
                "ooooooooooooooooooooooooooooo" +
                "ooooooooooooooooooooong record " + str(i) + "."
            ])
            logmgr.flush(lsn)

        for i in range(4):
            logmgr.append([i + 1])

        self.assertEqual(SimpleDB.file_mgr().size("simpledb.log"), 3)

        self.assertEqual(logmgr._currentblk._blknum, 2)
        logiter = logmgr.iterator()
        self.assertIsInstance(logiter, LogIterator)
        for i in range(4):
            self.assertEqual(logiter.next().next_int(), 4 - i)
        self.assertEqual(
            logiter.next().next_string(),
            "This is a very looooooooooooo" + "ooooooooooooooooooooooooooooo" +
            "ooooooooooooooooooooong record 1.")

        self.assertEqual(logmgr._currentblk._blknum, 2)
        logiter = logmgr.iterator()
        count = 0
        for record in logiter.generator():
            self.assertIsInstance(record, BasicLogRecord)
            count += 1
        self.assertEqual(count, 6)
Example #3
0
 def test_recordpage(self):
     SimpleDB.init_file_log_and_buffer_mgr("test")
     schema = Schema()
     schema.add_int_field("IntField")
     schema.add_string_field("StringField", 8)
     ti1 = TableInfo("table1", schema)
     blk = Block(ti1.file_name(), 0)
     tx = Transaction()
     rp = RecordPage(blk, ti1, tx)
     self.assertFalse(rp.next())
     self.assertTrue(rp.insert())
     rp.set_int("IntField", 256)
     rp.set_string("StringField", "Sample")
     ID = rp.current_id()
     self.assertFalse(rp.next())
     rp.move_to_id(ID-1)
     self.assertTrue(rp.next())
     rp.move_to_id(ID)
     self.assertEqual(rp.get_int("IntField"), 256)
     self.assertEqual(rp.get_string("StringField"), "Sample")
     tx.commit()
     #keep("table")
     #keep("simple")
     tx.pin(blk)
     rp.delete()
     rp.move_to_id(ID-1)
     self.assertFalse(rp.next())
     rp.close()
     self.assertIsNone(rp._blk)
     tx.commit()
     remove_some_start_with("table")
     remove_some_start_with("simple")
Example #4
0
 def test_recordpage(self):
     SimpleDB.init_file_log_and_buffer_mgr("test")
     schema = Schema()
     schema.add_int_field("IntField")
     schema.add_string_field("StringField", 8)
     ti1 = TableInfo("table1", schema)
     blk = Block(ti1.file_name(), 0)
     tx = Transaction()
     rp = RecordPage(blk, ti1, tx)
     self.assertFalse(rp.next())
     self.assertTrue(rp.insert())
     rp.set_int("IntField", 256)
     rp.set_string("StringField", "Sample")
     ID = rp.current_id()
     self.assertFalse(rp.next())
     rp.move_to_id(ID - 1)
     self.assertTrue(rp.next())
     rp.move_to_id(ID)
     self.assertEqual(rp.get_int("IntField"), 256)
     self.assertEqual(rp.get_string("StringField"), "Sample")
     tx.commit()
     #keep("table")
     #keep("simple")
     tx.pin(blk)
     rp.delete()
     rp.move_to_id(ID - 1)
     self.assertFalse(rp.next())
     rp.close()
     self.assertIsNone(rp._blk)
     tx.commit()
     remove_some_start_with("table")
     remove_some_start_with("simple")
Example #5
0
    def test_recordfile(self):
        SimpleDB.init_file_log_and_buffer_mgr("test")
        schema = Schema()
        schema.add_int_field("IntField")
        schema.add_string_field("StringField", 8)
        ti = TableInfo("table", schema)
        tx = Transaction()
        rf = RecordFile(ti, tx)

        self.assertFalse(rf.next())
        rf.before_first()
        rf.insert()
        rf.set_int("IntField", 256)
        rf.set_string("StringField", "Sample1")
        rid1 = rf.current_rid()
        self.assertEqual(rid1.block_number(), 0)
        self.assertEqual(rid1.id(), 0)
        rf.insert()
        rf.set_int("IntField", 128)
        rf.set_string("StringField", "Sample2")
        rid2 = rf.current_rid()
        self.assertTrue(rid1 != rid2)
        rf.move_to_rid(rid1)
        rf.delete()
        rf.before_first()
        count = 0
        while rf.next():
            count += 1
        self.assertEqual(count, 1)
        rf.move_to_rid(rid1)
        self.assertEqual(rf.get_int("IntField"), 256)
        self.assertEqual(rf.get_string("StringField"),
                         "Sample1")  # deleted record is still there

        rf.before_first()
        rf.insert()
        rf.set_int("IntField", 64)
        rf.set_string("StringField", "Sample3")
        rid3 = rf.current_rid()
        self.assertTrue(rid1 == rid3)
        self.assertEqual(rf.get_int("IntField"), 64)
        self.assertEqual(rf.get_string("StringField"),
                         "Sample3")  # now record1 is rewritten
        rf.insert()
        rf.set_string("StringField",
                      "Loooooooooooooooooooooooooooooooong record")
        rf.insert()
        rf.set_string("StringField",
                      "Loooooooooooooooooooooooooooooooong record")
        rid4 = rf.current_rid()
        rf.insert()
        rf.set_string("StringField",
                      "Loooooooooooooooooooooooooooooooong record")
        rid5 = rf.current_rid()
        self.assertNotEqual(rid4.block_number(), rid5.block_number())
        rf.move_to_rid(rid4)
        self.assertTrue(rf.next())
        rf.close()
        tx.commit()
Example #6
0
 def flush(self):
     """
     Writes the page to its disk block if the page is dirty.
     The method ensures that the corresponding log
     record has been written to disk prior to writing the page to disk.
     """
     if self._modified_by > 0:
         SimpleDB.log_mgr().flush(self._log_sequence_number)
         self._contents.write(self._blk)
         self._modified_by = -1
Example #7
0
 def flush(self):
     """
     Writes the page to its disk block if the page is dirty.
     The method ensures that the corresponding log
     record has been written to disk prior to writing the page to disk.
     """
     if self._modified_by > 0:
         SimpleDB.log_mgr().flush(self._log_sequence_number)
         self._contents.write(self._blk)
         self._modified_by = -1
Example #8
0
 def __init__(self, tblname, tx):
     """
     Creates a leaf node in the query tree corresponding to the specified table.
     :param tblname: the name of the table
     :param tx: the calling transaction
     """
     assert isinstance(tx, Transaction)
     self._tx = tx
     self._ti = SimpleDB.md_mgr().get_table_info(tblname, tx)
     self._si = SimpleDB.md_mgr().get_stat_info(tblname, self._ti, tx)
Example #9
0
 def __init__(self, tblname, tx):
     """
     Creates a leaf node in the query tree corresponding to the specified table.
     :param tblname: the name of the table
     :param tx: the calling transaction
     """
     assert isinstance(tx, Transaction)
     self._tx = tx
     self._ti = SimpleDB.md_mgr().get_table_info(tblname, tx)
     self._si = SimpleDB.md_mgr().get_stat_info(tblname, self._ti, tx)
Example #10
0
    def test_recordfile(self):
        SimpleDB.init_file_log_and_buffer_mgr("test")
        schema = Schema()
        schema.add_int_field("IntField")
        schema.add_string_field("StringField", 8)
        ti = TableInfo("table", schema)
        tx = Transaction()
        rf = RecordFile(ti, tx)

        self.assertFalse(rf.next())
        rf.before_first()
        rf.insert()
        rf.set_int("IntField", 256)
        rf.set_string("StringField", "Sample1")
        rid1 = rf.current_rid()
        self.assertEqual(rid1.block_number(), 0)
        self.assertEqual(rid1.id(), 0)
        rf.insert()
        rf.set_int("IntField", 128)
        rf.set_string("StringField", "Sample2")
        rid2 = rf.current_rid()
        self.assertTrue(rid1 != rid2)
        rf.move_to_rid(rid1)
        rf.delete()
        rf.before_first()
        count = 0
        while rf.next():
            count += 1
        self.assertEqual(count, 1)
        rf.move_to_rid(rid1)
        self.assertEqual(rf.get_int("IntField"), 256)
        self.assertEqual(rf.get_string("StringField"), "Sample1")  # deleted record is still there

        rf.before_first()
        rf.insert()
        rf.set_int("IntField", 64)
        rf.set_string("StringField", "Sample3")
        rid3 = rf.current_rid()
        self.assertTrue(rid1 == rid3)
        self.assertEqual(rf.get_int("IntField"), 64)
        self.assertEqual(rf.get_string("StringField"), "Sample3")  # now record1 is rewritten
        rf.insert()
        rf.set_string("StringField", "Loooooooooooooooooooooooooooooooong record")
        rf.insert()
        rf.set_string("StringField", "Loooooooooooooooooooooooooooooooong record")
        rid4 = rf.current_rid()
        rf.insert()
        rf.set_string("StringField", "Loooooooooooooooooooooooooooooooong record")
        rid5 = rf.current_rid()
        self.assertNotEqual(rid4.block_number(), rid5.block_number())
        rf.move_to_rid(rid4)
        self.assertTrue(rf.next())
        rf.close()
        tx.commit()
Example #11
0
 def test_recordformatter(self):
     SimpleDB.init_file_log_and_buffer_mgr("test")
     schema = Schema()
     schema.add_int_field("IntField")
     schema.add_string_field("StringField", 8)
     ti1 = TableInfo("table1", schema)
     fmtr = RecordFormatter(ti1)
     page = MaxPage()
     page.read(Block(ti1.file_name(), 0))
     fmtr.format(page)
     page.write(Block(ti1.file_name(), 0))
     remove_some_start_with("table")
Example #12
0
 def test_recordformatter(self):
     SimpleDB.init_file_log_and_buffer_mgr("test")
     schema = Schema()
     schema.add_int_field("IntField")
     schema.add_string_field("StringField", 8)
     ti1 = TableInfo("table1", schema)
     fmtr = RecordFormatter(ti1)
     page = MaxPage()
     page.read(Block(ti1.file_name(), 0))
     fmtr.format(page)
     page.write(Block(ti1.file_name(), 0))
     remove_some_start_with("table")
Example #13
0
 def __init__(self, idxname, tblname, fldname, tx):
     """
     Creates an IndexInfo object for the specified index.
     :param idxname: the name of the index
     :param tblname: the name of the table
     :param fldname: the name of the indexed field
     :param tx: the calling transaction
     """
     assert isinstance(tx, Transaction)
     self._idxname = idxname
     self._fldname = fldname
     self._tx = tx
     self._ti = SimpleDB.md_mgr().get_table_info(tblname, tx)
     self._si = SimpleDB.md_mgr().get_stat_info(tblname, self._ti, tx)
Example #14
0
 def __init__(self, idxname, tblname, fldname, tx):
     """
     Creates an IndexInfo object for the specified index.
     :param idxname: the name of the index
     :param tblname: the name of the table
     :param fldname: the name of the indexed field
     :param tx: the calling transaction
     """
     assert isinstance(tx, Transaction)
     self._idxname = idxname
     self._fldname = fldname
     self._tx = tx
     self._ti = SimpleDB.md_mgr().get_table_info(tblname, tx)
     self._si = SimpleDB.md_mgr().get_stat_info(tblname, self._ti, tx)
Example #15
0
    def execute_modify(self, data: ModifyData, tx: Transaction):
        tblname = data.table_name()
        fldname = data.target_field()
        p = TablePlan(tblname, tx)
        p = SelectPlan(p, data.pred())

        ii = SimpleDB.md_mgr().get_index_info(tblname, tx)[fldname]
        assert isinstance(ii, IndexInfo)
        idx = None if ii is None else ii.open()
        assert isinstance(idx, Index)

        s = p.open()
        assert isinstance(s, UpdateScan)

        count = 0

        while s.next():
            # first, update the record
            newval = data.new_value().evaluate(s)
            oldval = s.get_val(fldname)
            s.set_val(data.target_field(), newval)

            # then update the appropriate index, if it exists
            if idx is not None:
                rid = s.get_rid()
                idx.delete(oldval, rid)
                idx.insert(newval, rid)
            count += 1

        if idx is not None:
            idx.close()
        s.close()
        return count
Example #16
0
    def execute_insert(self, data: InsertData, tx: Transaction):
        tblname = data.table_name()
        p = TablePlan(tblname, tx)

        # first, insert the record
        s = p.open()
        assert isinstance(s, UpdateScan)
        s.insert()
        rid = s.get_rid()

        # then modify each field, inserting an index record if appropriate
        indexes = SimpleDB.md_mgr().get_index_info(tblname, tx)
        assert isinstance(indexes, dict)
        val_iter = iter(data.vals())
        for fldname in data.fields():
            val = next(val_iter)
            print("Modify field " + fldname + " to val " + str(val))
            s.set_val(fldname, val)

            ii = indexes[fldname]
            if ii is not None:
                idx = ii.open()
                idx.insert(val, rid)
                idx.close()
        s.close()
        return 1
Example #17
0
    def execute_delete(self, data: DeleteData, tx: Transaction):
        tblname = data.table_name()
        p = TablePlan(tblname, tx)
        p = SelectPlan(p, data.pred())
        indexes = SimpleDB.md_mgr().get_index_info(tblname, tx)
        assert isinstance(indexes, dict)
        s = p.open()
        assert isinstance(s, UpdateScan)

        count = 0
        while s.next():
            # first, delete the record's RID from every index
            rid = s.get_rid()
            for fldname in indexes.keys():
                val = s.get_val(fldname)
                idx = indexes[fldname].open()
                assert isinstance(idx, Index)
                idx.delete(val, rid)
                idx.close()

            # then delete the record
            s.delete()
            count += 1
        s.close()
        return count
Example #18
0
    def execute_modify(self, data: ModifyData, tx: Transaction):
        tblname = data.table_name()
        fldname = data.target_field()
        p = TablePlan(tblname, tx)
        p = SelectPlan(p, data.pred())

        ii = SimpleDB.md_mgr().get_index_info(tblname, tx)[fldname]
        assert isinstance(ii, IndexInfo)
        idx = None if ii is None else ii.open()
        assert isinstance(idx, Index)

        s = p.open()
        assert isinstance(s, UpdateScan)

        count = 0

        while s.next():
            # first, update the record
            newval = data.new_value().evaluate(s)
            oldval = s.get_val(fldname)
            s.set_val(data.target_field(), newval)

            # then update the appropriate index, if it exists
            if idx is not None:
                rid = s.get_rid()
                idx.delete(oldval, rid)
                idx.insert(newval, rid)
            count += 1

        if idx is not None:
            idx.close()
        s.close()
        return count
Example #19
0
    def execute_delete(self, data: DeleteData, tx: Transaction):
        tblname = data.table_name()
        p = TablePlan(tblname, tx)
        p = SelectPlan(p, data.pred())
        indexes = SimpleDB.md_mgr().get_index_info(tblname, tx)
        assert isinstance(indexes, dict)
        s = p.open()
        assert isinstance(s, UpdateScan)

        count = 0
        while s.next():
            # first, delete the record's RID from every index
            rid = s.get_rid()
            for fldname in indexes.keys():
                val = s.get_val(fldname)
                idx = indexes[fldname].open()
                assert isinstance(idx, Index)
                idx.delete(val, rid)
                idx.close()

            # then delete the record
            s.delete()
            count += 1
        s.close()
        return count
Example #20
0
    def execute_insert(self, data: InsertData, tx: Transaction):
        tblname = data.table_name()
        p = TablePlan(tblname, tx)

        # first, insert the record
        s = p.open()
        assert isinstance(s, UpdateScan)
        s.insert()
        rid = s.get_rid()

        # then modify each field, inserting an index record if appropriate
        indexes = SimpleDB.md_mgr().get_index_info(tblname, tx)
        assert isinstance(indexes, dict)
        val_iter = iter(data.vals())
        for fldname in data.fields():
            val = next(val_iter)
            print("Modify field " + fldname + " to val " + str(val))
            s.set_val(fldname, val)

            ii = indexes[fldname]
            if ii is not None:
                idx = ii.open()
                idx.insert(val, rid)
                idx.close()
        s.close()
        return 1
Example #21
0
 def __init__(self):
     """
     Creates a new page.  Although the constructor takes no arguments,
     it depends on a FileMgr object which is created during system initialization.
     Thus this constructor cannot be called until at least one of the
     initialization static functions is called first.
     """
     self._contents = bytearray(BLOCK_SIZE)
     self._file_mgr = SimpleDB.file_mgr()
Example #22
0
 def test_page(self):
     SimpleDB.init_file_mgr("test")
     page = MaxPage()
     page.set_int(0, 99999)
     page.set_string(4, "This is the sample string.")
     page.append("temp_sample_file")
     self.assertEqual(len(page._contents), 400)
     self.assertEqual(SimpleDB.fm.size("temp_sample_file"), 1)
     blk1 = Block("temp_sample_file", 1)
     page.write(blk1)
     page.clear_contents()
     page.append("temp_sample_file")
     self.assertEqual(SimpleDB.fm.size("temp_sample_file"), 3)
     blk2 = Block("temp_sample_file", 1)
     page.read(blk2)
     self.assertEqual(page.get_int(0), 99999)
     self.assertEqual(page.get_string(4), "This is the sample string.")
     self.assertEqual(page.get_int(4), 26*4)
Example #23
0
    def create_plan(self, data: QueryData, tx: Transaction):
        # Step 1: Create a plan for each mentioned table or view
        plans = []
        for tblname in data.tables():
            viewdef = SimpleDB.md_mgr().get_view_def(tblname, tx)
            if viewdef is not None:
                plans.append(SimpleDB.planner().create_query_plan(viewdef, tx))
            else:
                plans.append(TablePlan(tblname, tx))

        # Step 2: Create the product of all table plans
        p = plans[0]
        del (plans[0])
        for next_plan in plans:
            p = ProductPlan(p, next_plan)

        # Step 3: Add a selection plan for the predicate
        p = SelectPlan(p, data.pred())

        # Step 4: Project on the field names
        p = ProjectPlan(p, data.fields())
        return p
Example #24
0
    def create_plan(self, data: QueryData, tx: Transaction):
        # Step 1: Create a plan for each mentioned table or view
        plans = []
        for tblname in data.tables():
            viewdef = SimpleDB.md_mgr().get_view_def(tblname, tx)
            if viewdef is not None:
                plans.append(SimpleDB.planner().create_query_plan(viewdef, tx))
            else:
                plans.append(TablePlan(tblname, tx))

        # Step 2: Create the product of all table plans
        p = plans[0]
        del(plans[0])
        for next_plan in plans:
            p = ProductPlan(p, next_plan)

        # Step 3: Add a selection plan for the predicate
        p = SelectPlan(p, data.pred())

        # Step 4: Project on the field names
        p = ProjectPlan(p, data.fields())
        return p
Example #25
0
 def execute_query(self, qry: str):
     """
     Executes the specified SQL query string.
     The method calls the query planner to create a plan
     for the query. It then sends the plan to the
     RemoteResultSetImpl constructor for processing.
     """
     try:
         tx = self._rconn.get_transaction()
         pln = SimpleDB.planner().create_query_plan(qry, tx)
         return Pyro4.core.Proxy(SimpleDB.server_daemon.register(RemoteResultSetImpl(pln, self._rconn)))
     except RuntimeError as e:
         self._rconn.rollback()
         raise e
Example #26
0
 def execute_update(self, cmd: str):
     """
     Executes the specified SQL update command.
     The method sends the command to the update planner,
     which executes it.
     """
     try:
         tx = self._rconn.get_transaction()
         result = SimpleDB.planner().execute_update(cmd, tx)
         self._rconn.commit()
         return result
     except RuntimeError as e:
         self._rconn.rollback()
         raise e
Example #27
0
 def __init__(self, logfile):
     """
     Creates the manager for the specified log file.
     If the log file does not yet exist, it is created
     with an empty first block.
     This constructor depends on a FileMgr object
     that it gets from the method simpledb.server.SimpleDB.fileMgr().
     That object is created during system initialization.
     Thus this constructor cannot be called until
     simpledb.server.SimpleDB.initFileMgr(String)
     is called first.
     :param logfile: the name of the log file
     """
     print(SimpleDB)
     self._mypage = MaxPage()
     assert isinstance(logfile, str)
     self._logfile = logfile
     logsize = SimpleDB.file_mgr().size(logfile)
     if logsize == 0:
         self.__append_new_block()
     else:
         self._currentblk = Block(logfile, logsize - 1)
         self._mypage.read(self._currentblk)
         self._currentpos = self.__get_last_record_position() + MaxPage.INT_SIZE
Example #28
0
 def execute_create_view(self, data: CreateViewData, tx: Transaction):
     SimpleDB.md_mgr().create_view(data.view_name(), data.view_def(), tx)
     return 0
Example #29
0
 def execute_create_table(self, data: CreateTableData, tx: Transaction):
     SimpleDB.md_mgr().create_table(data.table_name(), data.new_schema(),
                                    tx)
 def setUp(self):
     SimpleDB.init_file_mgr("test")
     self.pg = MaxPage()
     self.pg.set_string(4, "Sample string")
     self.pg.set_int(0, 99999)
     self.basic_log_record = BasicLogRecord(self.pg, 0)
Example #31
0
 def execute_create_table(self, data: CreateTableData, tx: Transaction):
     SimpleDB.md_mgr().create_table(data.table_name(), data.new_schema(), tx)
Example #32
0
 def execute_create_view(self, data: CreateViewData, tx: Transaction):
     SimpleDB.md_mgr().create_view(data.view_name(), data.view_def(), tx)
     return 0
Example #33
0
 def execute_create_index(self, data: CreateIndexData, tx: Transaction):
     SimpleDB.md_mgr().create_index(data.index_name(), data.table_name(), data.field_name(), tx)
     return 0
 def setUp(self):
     remove_some_start_with("simple")
     SimpleDB.init_file_and_log_mgr("test")
     self.page = MaxPage()
Example #35
0
 def setUp(self):
     SimpleDB.init_file_mgr("test")
     self.pg = MaxPage()
     self.pg.set_string(4, "Sample string")
     self.pg.set_int(0, 99999)
     self.basic_log_record = BasicLogRecord(self.pg, 0)
Example #36
0
 def setUp(self):
     remove_some_start_with("simple")
     SimpleDB.init_file_and_log_mgr("test")
     self.page = MaxPage()
Example #37
0
 def execute_create_index(self, data: CreateIndexData, tx: Transaction):
     SimpleDB.md_mgr().create_index(data.index_name(), data.table_name(),
                                    data.field_name(), tx)
     return 0