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

[#188] WIP - Implementation of get_atom() using the cache #196

Merged
merged 7 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
10 changes: 5 additions & 5 deletions hyperon_das_atomdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
from .exceptions import AtomDoesNotExist

__all__ = [
'AtomDB',
'WILDCARD',
'UNORDERED_LINK_TYPES',
'AtomDoesNotExist',
"AtomDB",
"WILDCARD",
"UNORDERED_LINK_TYPES",
"AtomDoesNotExist",
]

__version__ = '0.8.0'
__version__ = "0.8.0"
2 changes: 1 addition & 1 deletion hyperon_das_atomdb/adapters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
from .ram_only import InMemoryDB
from .redis_mongo_db import RedisMongoDB

__all__ = ['RedisMongoDB', 'InMemoryDB']
__all__ = ["RedisMongoDB", "InMemoryDB"]
44 changes: 30 additions & 14 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.
"""
import copy
from collections import OrderedDict
from dataclasses import dataclass
from typing import Any, Iterable
Expand Down Expand Up @@ -235,7 +236,11 @@ def _delete_incoming_set(self, link_handle: str, atoms_handle: list[str]) -> Non
handles.remove(link_handle)

def _add_templates(
self, composite_type_hash: str, named_type_hash: str, key: str, targets_hash: list[str]
self,
composite_type_hash: str,
named_type_hash: str,
key: str,
targets_hash: list[str],
) -> None:
"""
Add templates to the database.
Expand Down Expand Up @@ -488,7 +493,7 @@ def get_all_links(self, link_type: str, **kwargs) -> tuple[int | None, list[str]
for _, link in self.db.link.items():
if link[FieldNames.TYPE_NAME] == link_type:
answer.append(link[FieldNames.ID_HASH])
return kwargs.get('cursor'), answer
return kwargs.get("cursor"), answer

def get_link_handle(self, link_type: str, target_handles: list[str]) -> str:
link_handle = self.link_handle(link_type, target_handles)
Expand Down Expand Up @@ -543,7 +548,7 @@ def get_matched_links(
) -> MatchedLinksResultT:
if link_type != WILDCARD and WILDCARD not in target_handles:
link_handle = self.get_link_handle(link_type, target_handles)
return kwargs.get('cursor'), [link_handle]
return kwargs.get("cursor"), [link_handle]

if link_type == WILDCARD:
link_type_hash = WILDCARD
Expand All @@ -565,30 +570,30 @@ def get_matched_links(
patterns_matched = list(self.db.patterns.get(pattern_hash, set()))

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

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

def get_incoming_links(self, atom_handle: str, **kwargs) -> tuple[int | None, 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 kwargs.get("cursor"), list(links)
return kwargs.get("cursor"), [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
return kwargs.get("cursor"), self._filter_non_toplevel(templates_matched)
return kwargs.get("cursor"), 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
return kwargs.get("cursor"), self._filter_non_toplevel(templates_matched)
return kwargs.get("cursor"), templates_matched

def get_atoms_by_field(self, query: list[OrderedDict[str, str]]) -> list[str]:
raise NotImplementedError()
Expand All @@ -603,15 +608,22 @@ def get_atoms_by_index(
raise NotImplementedError()

def get_atoms_by_text_field(
self, text_value: str, field: str | None = None, text_index_id: str | None = None
self,
text_value: str,
field: str | None = None,
text_index_id: str | None = None,
) -> list[str]:
raise NotImplementedError()

def get_node_by_name_starting_with(self, node_type: str, startswith: str) -> list[str]:
raise NotImplementedError()

def _get_atom(self, handle: str) -> AtomT | None:
return self.db.node.get(handle) or self._get_link(handle)
document = self.db.node.get(handle) or self._get_link(handle)
if document is None:
return None
else:
return copy.deepcopy(document)

def get_atom_type(self, handle: str) -> str | None:
atom = self.db.node.get(handle)
Expand Down Expand Up @@ -649,7 +661,11 @@ def count_atoms(self, parameters: dict[str, Any] | None = None) -> dict[str, int
node_count = len(self.db.node)
link_count = len(self.db.link)
atom_count = node_count + link_count
return {'atom_count': atom_count, 'node_count': node_count, 'link_count': link_count}
return {
"atom_count": atom_count,
"node_count": node_count,
"link_count": link_count,
}

def clear_database(self) -> None:
self.named_type_table = {}
Expand Down
Loading
Loading