def join(self, table1, table2, selection1, selection2, cond): ctlg1 = catalogCore() ctlg1.loadCatalog(table1) ctlg2 = catalogCore() ctlg2.loadCatalog(table2) pgmg = pageManager() io_s = general() cond1pos = -1 cond2pos = -1 for x in range(len(ctlg1.catalog)): if ctlg1.catalog[x][0] == cond: cond1pos = x for x in range(len(ctlg2.catalog)): if ctlg2.catalog[x][0] == cond: cond2pos = x joinedVals = [] for x in selection1: for y in selection2: if x[1][cond1pos] == y[1][cond2pos]: newv = [] newv.extend(x[1]) newv.extend(y[1]) joinedVals.append(newv) print(newv) return joinedVals
def selection(self, table, values, function=None, newValues=None): ctlg = catalogCore() ctlg.loadCatalog(table) pgmg = pageManager() io_s = general() print("Values not verified.") vals = self.buildValues(ctlg) newVals = None if newValues is not None: newVals = self.buildValues(ctlg) for x in range(len(newValues)): for y in range(len(ctlg.catalog)): val1 = newValues[x][0] val2 = ctlg.catalog[y][0] if val1 == val2: newVals[y] = newValues[x][1] for x in range(len(values)): for y in range(len(ctlg.catalog)): val1 = values[x][0] val2 = ctlg.catalog[y][0] if val1 == val2: vals[y] = values[x][1] x = pgmg.readValues(table, vals, function, newVals) return x print(len(x))
def selection(self, table, values, function = None, newValues = None): ctlg = catalogCore() ctlg.loadCatalog(table) pgmg = pageManager() io_s = general() print("Values not verified.") vals = self.buildValues(ctlg) newVals = None if newValues is not None: newVals = self.buildValues(ctlg) for x in range(len(newValues)): for y in range(len(ctlg.catalog)): val1 = newValues[x][0] val2 = ctlg.catalog[y][0] if val1 == val2: newVals[y] = newValues[x][1] for x in range(len(values)): for y in range(len(ctlg.catalog)): print("VER: " + str(ctlg.catalog[y][0])) val1 = str(values[x][0]).replace("'", "") val2 = str(ctlg.catalog[y][0]).replace("'", "") if values[x][1] == '=': if val1 == val2: vals[y] = values[x][2].replace("'", "") else: print ("NO OPERATOR #322") print("VALS: " + str(vals)) x = pgmg.readValues(table, vals, function, newVals) return x print(len(x))
def writeValue(self, table, values): io_s = general() if(table not in self.catalog): print("not in") self.catalog[table] = [0,0] self.catalog[table] = [self.catalog[table][0],self.catalog[table][1]+1] pageid = table + str(self.catalog[table][__numberOfPages_IDX__]) if(self.catalog[table][__numberOfPages_IDX__] == 0): self.catalog[table][__numberOfPages_IDX__] += 1 pageid = table + str(self.catalog[table][__numberOfPages_IDX__]) io_s.initPage(pageid) self.commit() strToSVX = "" for x in values: strToSVX += x + "$" strToSVX = strToSVX[0:len(strToSVX)-1] while(True): if(io_s.hasEmptySpace(pageid)): io_s.writeValue(self.catalog[table][1], strToSVX, pageid) break else: self.catalog[table][__numberOfPages_IDX__] += 1 self.commit() pageid = table + str(self.catalog[table][__numberOfPages_IDX__]) io_s.initPage(pageid) print("Another page created")
def selection(self, table, values, function = None, newValues = None): ctlg = catalogCore() ctlg.loadCatalog(table) pgmg = pageManager() io_s = general() print("Values not verified.") vals = self.buildValues(ctlg) newVals = None if newValues is not None: newVals = self.buildValues(ctlg) for x in range(len(newValues)): for y in range(len(ctlg.catalog)): val1 = newValues[x][0] val2 = ctlg.catalog[y][0] if val1 == val2: newVals[y] = newValues[x][1] for x in range(len(values)): for y in range(len(ctlg.catalog)): val1 = values[x][0] val2 = ctlg.catalog[y][0] if val1 == val2: vals[y] = values[x][1] x = pgmg.readValues(table, vals, function, newVals) return x print(len(x))
def addToPool(self, pid): ios = general() newb = buffer() newb.pid = pid newb.timestamp = calendar.timegm(time.gmtime()) if pid is not None: newb.page = ios.readPage(pid) buffer_pool.pool.append(newb)
def selection(self, table, values): ctlg = catalogCore() ctlg.loadCatalog(table) pgmg = pageManager() io_s = general() print("Values not verified.") pgmg.readValues(table)
def main(): print ("dsad") io = general() #io.initPage(2) io.write() eval = evaluator() eval.execute( """ create table table_name ( column_name1 data_type(size), column_name2 data_type(size), column_name3 data_type(size), ); """ ) tblm = manager() tblm.createTable("students2", [["id", "integer"], ["phone", "string"]]) ctlg2 = catalogCore() ctlg2.loadCatalog("students2") ctlg2.printCtlg() ins = insert() #ins.insertRecord("students2", ["26", "terce32441iro"]) #for x in range(25, 82): # ins.insertRecord("students3", [str(x), "terce3PG32323o" + str(x)]) sel = select() upd = update() #sel.selection("students2", []) upd.update("students2", [['phone', 'terceiro2221see']], [['phone', 'terceiro2221seesss']]) sel.selection("students2", [['phone', 'terce32441iro']]) print("321-----") #sel.selection("students2", []) #sel.selection("students3", []) #sel.join("students2", "students3", sel.selection("students2", []), sel.selection("students3", []), "id") main_pool.forceBuffer() print(main_pool.pool) from query.parser.sqlparse import parser pt = parser() #print(pt.parse("select * from x where x = 3 and y = 9")) #eval.execQuery("insert into students2 values (\"83\", \"gravado\")") print("select") eval.execQuery("select * from students2 where phone = 'gravado'") #where phone = terce32441iro
def main(): print ("dsad") io = general() #io.initPage(2) io.write() eval = evaluator() eval.execute( """ create table table_name ( column_name1 data_type(size), column_name2 data_type(size), column_name3 data_type(size), ); """ ) tblm = manager() tblm.createTable("students2", [["id", "integer"], ["phone", "string"]]) ctlg2 = catalogCore() ctlg2.loadCatalog("students2") ctlg2.printCtlg() ins = insert() #ins.insertRecord("students2", ["26", "terce32441iro"]) #for x in range(25, 82): # ins.insertRecord("students3", [str(x), "terce3PG32323o" + str(x)]) sel = select() upd = update() #sel.selection("students2", []) upd.update("students2", [['phone', 'terceiro2221see']], [['phone', 'terceiro2221seesss']]) sel.selection("students2", [['phone', 'terce32441iro']]) print("321-----") #sel.selection("students2", []) #sel.selection("students3", []) #sel.join("students2", "students3", sel.selection("students2", []), sel.selection("students3", []), "id") main_pool.forceBuffer() print(main_pool.pool) from query.parser.sqlparse import parser pt = parser() #print(pt.parse("select * from x where x = 3 and y = 9")) #eval.execQuery("insert into students2 values (\"83\", \"gravado\")") print("select") eval.execQuery("select * from students2 where id = '26'") #where phone = terce32441iro
def insertRecord(self, table, values): ctlg = catalogCore() ctlg.loadCatalog(table) io_s = general() print("Values not verified.") if len(ctlg.catalog) == len(values): pgmg = pageManager() pgmg.writeValue(table, values) else: print("Number of values does not match table columns")
def readValues(self, table, cond=None, function=None, newValues=None): bfm = buffer_pool() print("match") io_s = general() if (len(self.catalog) == 0): print("WARNING: IT SHOULD BE LOADED HERE") self.load() if (table not in self.catalog): print("Table does not exists -- attention") print(self.catalog) print(self.catalog[table][__numberOfPages_IDX__]) values = [] for x in range(1, self.catalog[table][__numberOfPages_IDX__] + 1): #readvals = io_s.readValues(table + str(x)) page = table + str(x) victimPage = self.replaceControl(page) for victimRecord in range(len(bfm.pool[victimPage].page)): y = bfm.pool[victimPage].page[victimRecord] row = y.split(chr(0))[0].split("$") if len(row) == 1 and len(row[0]) == 0: continue #empty row elif len(row) > 0: if cond is None: #not a filtered selection values.append( [bfm.pool[victimPage].rids[victimRecord], row]) else: if len(cond) != len(row): print("Length of values does not match table") else: valid = True for x in range(len(cond)): if cond[x] is None: continue else: if cond[x] != row[x]: valid = False break if valid and bfm.pool[victimPage].rids[ victimRecord] != storage.io.__CONS_EMPTY_SLOT__: if newValues is not None: function(row, victimPage, victimRecord, newValues) values.append([ bfm.pool[victimPage].rids[victimRecord], row ]) print(values) return values
def writeValue(self, table, values): io_s = general() bfm = buffer_pool() if (table not in self.catalog): print("not in") self.catalog[table] = [0, 0] self.catalog[table] = [ self.catalog[table][0], self.catalog[table][1] + 1 ] pageid = table + str(self.catalog[table][__numberOfPages_IDX__]) if (self.catalog[table][__numberOfPages_IDX__] == 0): self.catalog[table][__numberOfPages_IDX__] += 1 pageid = table + str(self.catalog[table][__numberOfPages_IDX__]) io_s.initPage(pageid) self.commit() strToSVX = "" for x in values: strToSVX += x.replace('""', '') + "$" strToSVX = strToSVX[0:len(strToSVX) - 1] while (True): if (io_s.hasEmptySpace(pageid)): #io_s.writeValue(self.catalog[table][1], strToSVX, pageid) #victim = bfm.replacePage(pageid) readvals = bfm.findPage(pageid) if readvals == -1: readvals = bfm.replacePage(pageid) for rec in range(len(bfm.pool[readvals].rids)): if bfm.pool[readvals].rids[ rec] == storage.io.__CONS_EMPTY_SLOT__: bfm.pool[readvals].rids[rec] = self.catalog[table][1] bfm.pool[readvals].page[rec] = strToSVX bfm.pool[readvals].dirty = True break print("Values written") break else: self.catalog[table][__numberOfPages_IDX__] += 1 self.commit() pageid = table + str( self.catalog[table][__numberOfPages_IDX__]) io_s.initPage(pageid) print("Another page created")
def readValues(self, table, cond = None, function = None, newValues = None): bfm = buffer_pool() print("match") io_s = general() if(len(self.catalog) == 0): print("WARNING: IT SHOULD BE LOADED HERE") self.load() if(table not in self.catalog): print("Table does not exists -- attention") print(self.catalog) print (self.catalog[table][__numberOfPages_IDX__]) values = [] for x in range(1, self.catalog[table][__numberOfPages_IDX__]+1): #readvals = io_s.readValues(table + str(x)) page = table + str(x) victimPage = self.replaceControl(page) for victimRecord in range(len(bfm.pool[victimPage].page)): y = bfm.pool[victimPage].page[victimRecord] row = y.split(chr(0))[0].split("$") if len(row) == 1 and len(row[0]) == 0: continue#empty row elif len(row) > 0: if cond is None: #not a filtered selection values.append([bfm.pool[victimPage].rids[victimRecord], row]) else: if len(cond) != len(row): print("Length of values does not match table") else: valid = True for x in range(len(cond)): if cond[x] is None: continue else: if str(cond[x]) != str(row[x]): #print ( str(cond[x]) +"-VER-"+ str(row[x])) valid = False break if valid and bfm.pool[victimPage].rids[victimRecord] != storage.io.__CONS_EMPTY_SLOT__: if newValues is not None: function(row, victimPage, victimRecord, newValues) values.append([bfm.pool[victimPage].rids[victimRecord], row]) print (values) return values
def writeValue(self, table, values): io_s = general() bfm = buffer_pool() if(table not in self.catalog): print("not in") self.catalog[table] = [0,0] self.catalog[table] = [self.catalog[table][0],self.catalog[table][1]+1] pageid = table + str(self.catalog[table][__numberOfPages_IDX__]) if(self.catalog[table][__numberOfPages_IDX__] == 0): self.catalog[table][__numberOfPages_IDX__] += 1 pageid = table + str(self.catalog[table][__numberOfPages_IDX__]) io_s.initPage(pageid) self.commit() strToSVX = "" for x in values: strToSVX += x + "$" strToSVX = strToSVX[0:len(strToSVX)-1] while(True): if(io_s.hasEmptySpace(pageid)): #io_s.writeValue(self.catalog[table][1], strToSVX, pageid) #victim = bfm.replacePage(pageid) readvals = bfm.findPage(pageid) if readvals == -1: readvals = bfm.replacePage(pageid) for rec in range(len(bfm.pool[readvals].rids)): if bfm.pool[readvals].rids[rec] == storage.io.__CONS_EMPTY_SLOT__: bfm.pool[readvals].rids[rec] = self.catalog[table][1] bfm.pool[readvals].page[rec] =strToSVX bfm.pool[readvals].dirty = True break print("Values written") break else: self.catalog[table][__numberOfPages_IDX__] += 1 self.commit() pageid = table + str(self.catalog[table][__numberOfPages_IDX__]) io_s.initPage(pageid) print("Another page created")
def readValues(self, table): print("match") io_s = general() if(table not in self.catalog): print("Table does not exists -- attention") print (self.catalog[table][__numberOfPages_IDX__]) values = [] for x in range(1, self.catalog[table][__numberOfPages_IDX__]+1): readvals = io_s.readValues(table + str(x)) for y in readvals: row = y.split(chr(0))[0].split("$") if len(row) == 1: if len(row[0]) == 0: continue if len(row) > 0: values.append(row) print (values)
def replacePage(self, pid): ios = general() newb = buffer() newb.pid = pid page = ios.readValues(pid) print(page[0]) newb.page = page[0]#.split(chr(0))[0].split("$") newb.rids = page[1] victim = self.findVictimPage() buffer_pool.pool[victim].forcePage() #Forces all data to disk before replacing #time.sleep(1) newb.timestamp = time.time() print("Replacing Page: " + str(buffer_pool.pool[victim].pid) + " -> " + str(newb.pid) + " on slot " + str(victim)) buffer_pool.pool[victim] = newb print(buffer_pool.pool[victim].rids) print(buffer_pool.pool[victim].page) return victim
def replacePage(self, pid): ios = general() newb = buffer() newb.pid = pid page = ios.readValues(pid) print(page[0]) newb.page = page[0] #.split(chr(0))[0].split("$") newb.rids = page[1] victim = self.findVictimPage() buffer_pool.pool[victim].forcePage( ) #Forces all data to disk before replacing #time.sleep(1) newb.timestamp = time.time() print("Replacing Page: " + str(buffer_pool.pool[victim].pid) + " -> " + str(newb.pid) + " on slot " + str(victim)) buffer_pool.pool[victim] = newb print(buffer_pool.pool[victim].rids) print(buffer_pool.pool[victim].page) return victim
def forcePage(self): if self.pid is None or self.dirty is False: return ios = general() ios.writePage(self.rids, self.page, self.pid)
__author__ = 'Bernardo Augusto Godinho de Oliveira - @bernardogo' from storage.io import general from query.evaluator import evaluator from catalog.core import catalogCore from storage.tablemgr import manager def main(): print("dsad") io = general() #io.initPage(2) io.write() eval = evaluator() eval.execute(""" create table table_name ( column_name1 data_type(size), column_name2 data_type(size), column_name3 data_type(size), ); """) tblm = manager() tblm.createTable("students2", [["id", "integer"], ["phone", "string"]]) ctlg2 = catalogCore() ctlg2.loadCatalog("students2")
__author__ = 'Bernardo Augusto Godinho de Oliveira - @bernardogo' from storage.io import general from query.evaluator import evaluator from catalog.core import catalogCore from storage.tablemgr import manager def main(): print ("dsad") io = general() #io.initPage(2) io.write() eval = evaluator() eval.execute( """ create table table_name ( column_name1 data_type(size), column_name2 data_type(size), column_name3 data_type(size), ); """ ) tblm = manager() tblm.createTable("students2", [["id", "integer"], ["phone", "string"]])