Skip to content

Commit

Permalink
Fix support large numb of mesh lamps
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Dec 4, 2020
1 parent 6959178 commit 53b9827
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions custom_components/xiaomi_gateway3/core/unqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,17 @@ def read_db_header(self):
def read_page(self, page_num: int):
self.pos = 100 if page_num == 0 else self.page_size * page_num

# B-tree Page Header Format
page_type = self.read(1)
# check if page is leaf table b-tree page
assert page_type == b'\x0D'

if page_type == b'\x0D':
return self._read_leaf_table(page_num)
elif page_type == b'\x05':
return self._read_interior_table(page_num)
else:
raise NotImplemented

def _read_leaf_table(self, page_num: int):
first_block = self.read_int(2)
cells_num = self.read_int(2)
cells_pos = self.read_int(2)
Expand Down Expand Up @@ -172,6 +179,24 @@ def read_page(self, page_num: int):

return rows

def _read_interior_table(self, page_num: int):
first_block = self.read_int(2)
cells_num = self.read_int(2)
cells_pos = self.read_int(2)
fragmented_free_bytes = self.read_int(1)
last_page_num = self.read_int(4)

cells_pos = [self.read_int(2) for _ in range(cells_num)]
rows = []

for cell_pos in cells_pos:
self.pos = self.page_size * page_num + cell_pos
child_page_num = self.read_int(4)
rowid = self.read_varint()
rows += self.read_page(child_page_num - 1)

return rows + self.read_page(last_page_num - 1)

def read_table(self, name: str):
page = next(t[3] - 1 for t in self.tables if t[1] == name)
return self.read_page(page)

0 comments on commit 53b9827

Please sign in to comment.