Skip to content
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

[#216] Removed cursor from api of get_matched*() and get_incoming_links() #217

Merged
merged 6 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
[#214] Added type alias for atom handle
[#216] Removed cursor from api of get_matched*() and get_incoming_links()
39 changes: 20 additions & 19 deletions hyperon_das_atomdb/adapters/ram_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Database: A dataclass representing the structure of the in-memory database.
InMemoryDB: A concrete implementation of the AtomDB interface using hashtables.
"""

from collections import OrderedDict
from dataclasses import dataclass
from dataclasses import field as dc_field
Expand Down Expand Up @@ -552,7 +553,11 @@ def get_matched_links(
self, link_type: str, target_handles: list[str], **kwargs
) -> MatchedLinksResultT:
if link_type != WILDCARD and WILDCARD not in target_handles:
return kwargs.get("cursor"), [self.get_link_handle(link_type, target_handles)]
try:
answer = [self.get_link_handle(link_type, target_handles)]
except AtomDoesNotExist:
answer = []
return answer

link_type_hash = (
WILDCARD if link_type == WILDCARD else ExpressionHasher.named_type_hash(link_type)
Expand All @@ -572,31 +577,31 @@ def get_matched_links(

patterns_matched = list(pattern) if (pattern := self.db.patterns.get(pattern_hash)) else []

if kwargs.get("toplevel_only"):
return kwargs.get("cursor"), self._filter_non_toplevel(patterns_matched)
if kwargs.get("toplevel_only", False):
return self._filter_non_toplevel(patterns_matched)

return kwargs.get("cursor"), patterns_matched
return patterns_matched

def get_incoming_links(self, atom_handle: str, **kwargs) -> tuple[int | None, IncomingLinksT]:
def get_incoming_links(self, atom_handle: str, **kwargs) -> IncomingLinksT:
links = self.db.incoming_set.get(atom_handle, set())
if kwargs.get("handles_only", False):
return kwargs.get("cursor"), list(links)
return kwargs.get("cursor"), [self.get_atom(handle, **kwargs) for handle in links]
return list(links)
return [self.get_atom(handle, **kwargs) for handle in links]

def get_matched_type_template(self, template: list[Any], **kwargs) -> MatchedTypesResultT:
hash_base = self._build_named_type_hash_template(template)
template_hash = ExpressionHasher.composite_hash(hash_base)
templates_matched = list(self.db.templates.get(template_hash, set()))
if kwargs.get("toplevel_only"):
return kwargs.get("cursor"), self._filter_non_toplevel(templates_matched)
return kwargs.get("cursor"), templates_matched
if kwargs.get("toplevel_only", False):
return self._filter_non_toplevel(templates_matched)
return templates_matched

def get_matched_type(self, link_type: str, **kwargs) -> MatchedTypesResultT:
link_type_hash = ExpressionHasher.named_type_hash(link_type)
templates_matched = list(self.db.templates.get(link_type_hash, set()))
if kwargs.get("toplevel_only"):
return kwargs.get("cursor"), self._filter_non_toplevel(templates_matched)
return kwargs.get("cursor"), templates_matched
if kwargs.get("toplevel_only", False):
return self._filter_non_toplevel(templates_matched)
return templates_matched

def get_atoms_by_field(
self, query: list[OrderedDict[str, str]]
Expand Down Expand Up @@ -674,12 +679,8 @@ def add_node(self, node_params: NodeParamsT) -> NodeT | None:
self._update_index(node)
return node

def add_link(self, link_params: LinkParamsT, toplevel: bool = True) -> LinkT | None:
result = self._build_link(link_params, toplevel)
# NOTE unreachable
if result is None: # pragma: no cover
return None
handle, link, _ = result
def add_link(self, link_params: LinkParamsT, toplevel: bool = True) -> LinkT:
handle, link, _ = self._build_link(link_params, toplevel)
self.db.link[handle] = link
self._update_index(link)
return link
Expand Down
Loading