def _read_page_data(self, page: int) -> bytes: ''' read no.x page raw binary data from the db file ''' page_start = page * self._tree_conf.page_size data = util.read_from_file(self._fd, page_start, page_start + self._tree_conf.page_size) return data
def _load_page_gc(self): ''' load all deprecated page used before into the memory ''' for offset in range(1, self.last_page): page_start = offset * self._tree_conf.page_size page_type = util.read_from_file(self._fd, page_start, constants.NODE_TYPE_LENGTH_LIMIT) if page_type == 2: # _PageType.DEPRECATED_PAGE._value==2 yield offset
def get_page(self, page: int) -> bytes: page_start = None for store in (self._uncommited_pages, self._commited_pages): page_start = store.get(page) if page_start: break if not page_start: return b'' return util.read_from_file(self._fd, page_start, page_start + self._page_size)
def checkpoint(self): ''' transfer the modified data back to the test_tree and close the WAL ''' if self._uncommited_pages: logger.warning('close WAL with uncommited data, discarding it') util.file_flush_and_sync(self._fd) for page, page_start in self._commited_pages.items(): page_data = util.read_from_file(self._fd, page_start, page_start + self._page_size) yield page, page_data self._fd.close() os.unlink(self._filename + '.xdb.wal')
def _load_next_frame(self): start = self._fd.tell() end = start + self.FRAME_HEADER_LENGTH data = util.read_from_file(self._fd, start, end) frame_type = int.from_bytes(data[0:constants.FRAME_TYPE_LENGTH_LIMIT], constants.ENDIAN) frame_type = FrameType(frame_type) if frame_type is FrameType.PAGE: self._fd.seek(end + self._page_size) page = int.from_bytes( data[constants. FRAME_TYPE_LENGTH_LIMIT:constants.FRAME_TYPE_LENGTH_LIMIT + constants.PAGE_ADDRESS_LIMIT]) self._index_frame(frame_type, page, end)
def _load_wal(self): ''' load previous WAL generated when B Tree closed accidentally. ''' self._fd.seek(0) header_data = util.read_from_file(self, 0, constants.PAGE_LENGTH_LIMIT) assert int.from_bytes(header_data, constants.ENDIAN) == self._page_size while True: try: self._load_next_frame() except util.EndOfFileError: break if self._uncommited_pages: logger.warning('WAL has uncommited data, discarding it') self._uncommited_pages = dict()