Skip to content

Commit

Permalink
trie: introduce delete_node
Browse files Browse the repository at this point in the history
  • Loading branch information
efiop committed Jun 9, 2023
1 parent 2ea009b commit 49403d5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/sqltrie/pygtrie.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def __len__(self):
def has_node(self, key):
return bool(self._trie.has_node(key))

def delete_node(self, key):
raise NotImplementedError

def items(self, prefix=None, shallow=False):
kwargs = {"shallow": shallow}
if prefix is not None:
Expand Down
3 changes: 3 additions & 0 deletions src/sqltrie/serialized.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ def diff(self, *args, **kwargs):
def has_node(self, key):
return self._trie.has_node(key)

def delete_node(self, key):
return self._trie.delete_node(key)

def shortest_prefix(self, key):
sprefix = self._trie.shortest_prefix(key)
if sprefix is None:
Expand Down
20 changes: 10 additions & 10 deletions src/sqltrie/sqlite/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,6 @@ def _get_children(self, key, limit=None):
(node["id"],),
).fetchall()

def _delete_node(self, key):
node = self._get_node(key)
del self._ids[key]
self._conn.execute(
"""
DELETE FROM nodes WHERE id = ?
""",
(node["id"],),
)

def __setitem__(self, key, value):
pid = self._create_node(key[:-1])

Expand Down Expand Up @@ -290,6 +280,16 @@ def has_node(self, key: TrieKey) -> bool:
except KeyError:
return False

def delete_node(self, key: TrieKey):
node = self._get_node(key)
del self._ids[key]
self._conn.execute(
"""
DELETE FROM nodes WHERE id = ?
""",
(node["id"],),
)

def ls(
self, key: TrieKey, with_values: Optional[bool] = False
) -> Iterator[Union[TrieKey, TrieNode]]:
Expand Down
5 changes: 5 additions & 0 deletions src/sqltrie/trie.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ def view(self, key: Optional[TrieKey] = None) -> "AbstractTrie":
def has_node(self, key: TrieKey) -> bool:
pass

@abstractmethod
def delete_node(self, key: TrieKey) -> bool:
# NOTE: this will leave orphans down the tree
pass

@abstractmethod
def prefixes(self, key: TrieKey) -> Iterator[TrieStep]:
pass
Expand Down

0 comments on commit 49403d5

Please sign in to comment.