Exemple #1
0
    def availablePage(self):
        if len(self.freePages) > 0:
            freePages = [
                a for (a, b) in self.freePages if b > self.schema().size
            ]
            if len(freePages) == 0:
                return PageId(self.fileId, self.numPages())

            return freePages[0]
        else:
            return PageId(self.fileId, 0)
Exemple #2
0
 def allocatePage(self):
     pId = PageId(self.fileId, self.pageNum)
     new_page = self.pageClass()(pageId=pId,
                                 buffer=bytes(self.pageSize()),
                                 schema=self.schema())
     # update the freePages and pageNum
     self.freePages[pId] = new_page
     self.pageNum += 1
Exemple #3
0
 def makePage(self, schema, fId, f, i):
     pId = PageId(fId, i)
     p = SlottedPage(pageId=pId, buffer=bytes(f.pageSize()), schema=schema)
     for tup in [
             schema.pack(schema.instantiate(i, 2 * i + 20))
             for i in range(1000)
     ]:
         p.insertTuple(tup)
     return (pId, p)
Exemple #4
0
 def allocatePage(self):
     if self.file.read() != b'':
         print('Not at end of page!')
         return
     else:
         pageId = PageId(self.fileId, self.numPages())
         p = self.defaultPageClass(pageId=pageId,
                                   buffer=bytes(self.pageSize()),
                                   schema=self.schema())
         self.writePage(p)
     return pageId
Exemple #5
0
    def allocatePage(self):

        pId = PageId(self.fileId, self.pageNum)
        page = self.pageClass()(pageId=pId,
                                buffer=bytes(self.pageSize()),
                                schema=self.schema())
        self.file.write(page.pack())
        # increment page num
        self.pageNum += 1

        # update freepages
        self.freePages[pId] = page
Exemple #6
0
    def __init__(self, **kwargs):
        self.bufferPool = kwargs.get("bufferPool", None)
        if self.bufferPool is None:
            raise ValueError(
                "No buffer pool found when initializing a storage file")

        pageSize = kwargs.get("pageSize", io.DEFAULT_BUFFER_SIZE)
        pageClass = kwargs.get("pageClass", StorageFile.defaultPageClass)
        schema = kwargs.get("schema", None)
        mode = kwargs.get("mode", None)

        self.fileId = kwargs.get("fileId", None)
        self.filePath = kwargs.get("filePath", None)

        # will have to read in files for "update" mode
        # possibly use getother method

        ######################################################################################
        # DESIGN QUESTION: how do you initialize these?
        # The file should be opened depending on the desired mode of operation.
        # The file header may come from the file contents (i.e., if the file already exists),
        # otherwise it should be created from scratch.
        self.freePages = []

        if mode == "create":
            self.header = FileHeader(pageSize=pageSize,
                                     pageClass=pageClass,
                                     schema=schema)
            self.file = open(self.filePath, "wb+")
            self.header.toFile(self.file)
            #self.file.close()
        else:
            # self.header = FileHeader(other=)
            # read from file and pass to other

            self.file = open(self.filePath, "rb+")
            other = FileHeader.fromFile(self.file)
            self.header = FileHeader(other=other)

            self.file.seek(0)
            data = bytearray(self.file.read())

            x = self.header.size
            pageCounter = 0
            while (x + self.header.pageSize) <= len(data):
                pagebuffer = data[x:x + self.header.pageSize]
                pId = PageId(self.fileId, pageCounter)
                page = pageClass.unpack(pId, pagebuffer)
                self.freePages.append(page)
                pageCounter += 1
                x += self.header.pageSize
Exemple #7
0
    def __init__(self, **kwargs):
        self.bufferPool = kwargs.get("bufferPool", None)
        if self.bufferPool is None:
            raise ValueError(
                "No buffer pool found when initializing a storage file")

        pageSize = kwargs.get("pageSize", io.DEFAULT_BUFFER_SIZE)
        pageClass = kwargs.get("pageClass", StorageFile.defaultPageClass)
        schema = kwargs.get("schema", None)
        mode = kwargs.get("mode", None)

        self.fileId = kwargs.get("fileId", None)
        self.filePath = kwargs.get("filePath", None)

        header = kwargs.get("header", None)
        self.freePages = []  # Use a tuple of (PageID, free Space) or something

        # pageSize and pageClass and schema
        if mode == 'create':
            header = self.initializeHeader(**kwargs)
            self.header = header

            if self.filePath is not None:
                f = open(self.filePath, 'bw+')
                header.toFile(f)
                self.file = f

            else:
                raise ValueError("Appropriate filePath is needed")

        elif mode == 'update':
            with open(self.filePath, 'br+') as f:
                self.header = FileHeader.fromFile(f)
                while True:
                    pageIt = 0
                    tempPageBytes = f.read(self.header.pageSize)
                    if tempPageBytes != b'':
                        pId = PageId(self.fileId, pageIt)
                        tempPage = self.pageClass().unpack(pId, tempPageBytes)
                        self.freePages.append(
                            (tempPage.pageId, tempPage.header.freeSpace()))
                        pageIt += 1
                    else:
                        break

            self.file = open(self.filePath, 'ba+')

        else:
            raise ValueError("No mode provided..")
Exemple #8
0
    def allocatePage(self):
        pId = PageId(self.fileId, self.numPages())

        page = self.header.pageClass(pageId=pId,
                                     buffer=bytes(self.header.pageSize),
                                     schema=self.header.schema)

        #heapfile = open(self.filePath, "ab+")
        #heapfile.write(page.pack())
        #heapfile.close()
        self.file.seek(0, 2)  #should seek to the end of the file
        self.file.write(page.pack())
        self.freePages.append(page)

        return pId
Exemple #9
0
    def readPageHeader(self, pageId):
        if self.validPageId(pageId) == True:
            offset = self.pageOffset(pageId)
            self.file.seek(offset)

            tempPage = self.pageClass()(pageId=PageId(0, 0),
                                        buffer=bytes(self.pageSize()),
                                        schema=self.schema())
            headerBytes = self.file.read(tempPage.header.headerSize())
            buffer = io.BytesIO(headerBytes)

            pageHeader = self.pageClass().headerClass.unpack(
                buffer.getbuffer())
            return pageHeader
        else:
            raise ValueError("Invalid Page ID")
Exemple #10
0
 def pageId(self, pageIndex):
     return PageId(self.fileId, pageIndex)
Exemple #11
0
schema = DBSchema('employee', [('id', 'int'), ('age', 'int')])

bp = Storage.BufferPool.BufferPool()

fm = Storage.FileManager.FileManager(bufferPool=bp)

bp.setFileManager(fm)

fm.createRelation(schema.name, schema)

(fId, f) = fm.relationFile(schema.name)

f.numPages() == 0

pId  = PageId(fId, 0)
pId1 = PageId(fId, 1)

p    = Page(pageId=pId,  buffer=bytes(f.pageSize()), schema=schema)
p1   = Page(pageId=pId1, buffer=bytes(f.pageSize()), schema=schema)

for tup in [schema.pack(schema.instantiate(i, 2*i+20)) for i in range(10)]:
  _ = p.insertTuple(tup)

for tup in [schema.pack(schema.instantiate(i, i+20)) for i in range(10, 20)]:
  _ = p1.insertTuple(tup)

f.writePage(p)
f.writePage(p1)
print(p.header.usedSpace())
h1 = f.readPageHeader( pId );
Exemple #12
0
 def makeEmptyPage(self):
   schema = self.makeSchema()
   pId = PageId(FileId(1), 100)
   return pageClass(pageId=pId, buffer=bytes(4096), schema=schema)