-
Notifications
You must be signed in to change notification settings - Fork 10
Add ESE #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Add ESE #4
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from dissect.database.bsd.db import DB | ||
| from dissect.database.ese.ese import ESE | ||
| from dissect.database.exception import Error | ||
| from dissect.database.sqlite3.sqlite3 import SQLite3 | ||
|
|
||
| __all__ = [ | ||
| "DB", | ||
| "ESE", | ||
| "Error", | ||
| "SQLite3", | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from dissect.database.ese.ese import ESE | ||
| from dissect.database.ese.exception import ( | ||
| InvalidDatabase, | ||
| KeyNotFoundError, | ||
| NoNeighbourPageError, | ||
| ) | ||
| from dissect.database.ese.index import Index | ||
| from dissect.database.ese.page import Page | ||
| from dissect.database.ese.record import Record | ||
| from dissect.database.ese.table import Table | ||
|
|
||
| __all__ = [ | ||
| "ESE", | ||
| "CompressedTaggedDataError", | ||
| "Index", | ||
| "InvalidDatabase", | ||
| "KeyNotFoundError", | ||
| "NoNeighbourPageError", | ||
| "Page", | ||
| "Record", | ||
| "Table", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from dissect.database.ese.exception import KeyNotFoundError, NoNeighbourPageError | ||
|
|
||
| if TYPE_CHECKING: | ||
| from dissect.database.ese.ese import ESE | ||
| from dissect.database.ese.page import Node, Page | ||
|
|
||
|
|
||
| class BTree: | ||
| """A simple implementation for searching the ESE B+Trees. | ||
|
|
||
| This is a stateful interactive class that moves an internal cursor to a position within the BTree. | ||
|
|
||
| Args: | ||
| db: An instance of :class:`~dissect.database.ese.ese.ESE`. | ||
| page: The page to open the :class:`BTree` on. | ||
| """ | ||
|
|
||
| def __init__(self, db: ESE, root: int | Page): | ||
| self.db = db | ||
|
|
||
| if isinstance(root, int): | ||
| page_num = root | ||
| root = db.page(page_num) | ||
| else: | ||
| page_num = root.num | ||
|
|
||
| self.root = root | ||
|
|
||
| self._page = root | ||
| self._page_num = page_num | ||
| self._node_num = 0 | ||
|
|
||
| def reset(self) -> None: | ||
| """Reset the internal state to the root of the BTree.""" | ||
| self._page = self.root | ||
| self._page_num = self._page.num | ||
| self._node_num = 0 | ||
|
|
||
| def node(self) -> Node: | ||
| """Return the node the BTree is currently on. | ||
|
|
||
| Returns: | ||
| A :class:`~dissect.database.ese.page.Node` object of the current node. | ||
| """ | ||
| return self._page.node(self._node_num) | ||
|
|
||
| def next(self) -> Node: | ||
| """Move the BTree to the next node and return it. | ||
|
|
||
| Can move the BTree to the next page as a side effect. | ||
|
|
||
| Returns: | ||
| A :class:`~dissect.database.ese.page.Node` object of the next node. | ||
| """ | ||
| if self._node_num + 1 > self._page.node_count - 1: | ||
| self.next_page() | ||
| else: | ||
| self._node_num += 1 | ||
|
|
||
| return self.node() | ||
|
|
||
| def next_page(self) -> None: | ||
| """Move the BTree to the next page in the tree. | ||
|
|
||
| Raises: | ||
| NoNeighbourPageError: If the current page has no next page. | ||
| """ | ||
| if self._page.next_page: | ||
| self._page = self.db.page(self._page.next_page) | ||
| self._node_num = 0 | ||
| else: | ||
| raise NoNeighbourPageError(f"{self._page} has no next page") | ||
|
|
||
| def prev(self) -> Node: | ||
| """Move the BTree to the previous node and return it. | ||
|
|
||
| Can move the BTree to the previous page as a side effect. | ||
|
|
||
| Returns: | ||
| A :class:`~dissect.database.ese.page.Node` object of the previous node. | ||
| """ | ||
| if self._node_num - 1 < 0: | ||
| self.prev_page() | ||
| else: | ||
| self._node_num -= 1 | ||
|
|
||
| return self.node() | ||
|
|
||
| def prev_page(self) -> None: | ||
| """Move the BTree to the previous page in the tree. | ||
|
|
||
| Raises: | ||
| NoNeighbourPageError: If the current page has no previous page. | ||
| """ | ||
| if self._page.previous_page: | ||
| self._page = self.db.page(self._page.previous_page) | ||
| self._node_num = self._page.node_count - 1 | ||
| else: | ||
| raise NoNeighbourPageError(f"{self._page} has no previous page") | ||
|
|
||
| def search(self, key: bytes, exact: bool = True) -> Node: | ||
| """Search the tree for the given ``key``. | ||
|
|
||
| Moves the BTree to the matching node, or on the last node that is less than the requested key. | ||
|
|
||
| Args: | ||
| key: The key to search for. | ||
| exact: Whether to only return successfully on an exact match. | ||
|
|
||
| Raises: | ||
| KeyNotFoundError: If an ``exact`` match was requested but not found. | ||
| """ | ||
| page = self._page | ||
| while True: | ||
| node = find_node(page, key) | ||
|
|
||
| if page.is_branch: | ||
| page = self.db.page(node.child) | ||
| else: | ||
| self._page = page | ||
| self._page_num = page.num | ||
| self._node_num = node.num | ||
| break | ||
|
|
||
| if exact and key != node.key: | ||
| raise KeyNotFoundError(f"Can't find key: {key}") | ||
|
|
||
| return self.node() | ||
|
|
||
|
|
||
| def find_node(page: Page, key: bytes) -> Node: | ||
| """Search a page for a node matching ``key``. | ||
|
|
||
| Args: | ||
| page: The page to search. | ||
| key: The key to search. | ||
| """ | ||
| first_node_idx = 0 | ||
| last_node_idx = page.node_count - 1 | ||
|
|
||
| node = None | ||
| while first_node_idx < last_node_idx: | ||
| node_idx = (first_node_idx + last_node_idx) // 2 | ||
| node = page.node(node_idx) | ||
|
|
||
| # It turns out that the way BTree keys are compared matches 1:1 with how Python compares bytes | ||
| # First compare data, then length | ||
| if key < node.key: | ||
| last_node_idx = node_idx | ||
| elif key == node.key: | ||
| if page.is_branch: | ||
| # If there's an exact match on a key on a branch page, the actual leaf nodes are in the next branch | ||
| # Page keys for branch pages appear to be non-inclusive upper bounds | ||
| node_idx = min(node_idx + 1, page.node_count - 1) | ||
| node = page.node(node_idx) | ||
|
|
||
| return node | ||
| else: | ||
| first_node_idx = node_idx + 1 | ||
|
|
||
| # We're at the last node | ||
| return page.node(first_node_idx) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, in hindsight. Maybe you want to rename
DBtoBerkleyDBor something along those lines? For instance,from dissect.database import DBfeels a bit ambiguous now.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about that too, but it's just a convenience import. The recommended import path is either
from dissect.database.bsd.db import DBorfrom dissect.database.bsd import DB.I don't have super strong opinion either way, but
DBis (sadly) more in line with the Berkeley DB nomenclature.