Skip to content

Commit

Permalink
Merge pull request #279 from singnet/imp/issue-276-query-engine-new-a…
Browse files Browse the repository at this point in the history
…tom-api-support

[#276] API support to AtomDB new queries
  • Loading branch information
eddiebrissow authored Jul 18, 2024
2 parents a13f1fb + f06aca9 commit 885b0c0
Show file tree
Hide file tree
Showing 9 changed files with 481 additions and 87 deletions.
8 changes: 6 additions & 2 deletions hyperon_das/cache/iterators.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,13 @@ def get_fetch_data_kwargs(self) -> Dict[str, Any]:
def get_fetch_data(self, **kwargs) -> tuple:
if self.backend:
if self.is_remote:
return self.backend.custom_query(self.index_id, **kwargs)
return self.backend.custom_query(
self.index_id, query=kwargs.get('query', []), **kwargs
)
else:
return self.backend.get_atoms_by_index(self.index_id, **kwargs)
return self.backend.get_atoms_by_index(
self.index_id, query=kwargs.get('query', []), **kwargs
)


class TraverseLinksIterator(QueryAnswerIterator):
Expand Down
65 changes: 56 additions & 9 deletions hyperon_das/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ def query(
return self._send_request(payload)
except HTTPError as e:
if e.status_code == 400:
raise ValueError("Your query couldn't be processed due to an invalid format. Review the way the query is written and try again.", str(e))
raise ValueError(
"Your query couldn't be processed due to an invalid format. Review the way the query "
"is written and try again.",
str(e),
)
elif e.status_code == 404:
raise Exception("Your query couldn't be processed because Atom nonexistent", str(e))
raise e
Expand Down Expand Up @@ -184,7 +188,7 @@ def get_incoming_links(
payload = {
'action': 'get_incoming_links',
'input': {'atom_handle': atom_handle, 'kwargs': kwargs},
}
}
try:
return self._send_request(payload)
except HTTPError as e:
Expand All @@ -194,17 +198,19 @@ def get_incoming_links(
def create_field_index(
self,
atom_type: str,
field: str,
type: Optional[str] = None,
fields: List[str],
named_type: Optional[str] = None,
composite_type: Optional[List[Any]] = None,
index_type: Optional[str] = None,
) -> str:
payload = {
'action': 'create_field_index',
'input': {
'atom_type': atom_type,
'field': field,
'type': type,
'fields': fields,
'named_type': named_type,
'composite_type': composite_type,
'index_type': index_type,
},
}
try:
Expand All @@ -215,10 +221,10 @@ def create_field_index(
else:
raise e

def custom_query(self, index_id: str, **kwargs) -> List[Dict[str, Any]]:
def custom_query(self, index_id: str, query: Query, **kwargs) -> List[Dict[str, Any]]:
payload = {
'action': 'custom_query',
'input': {'index_id': index_id, 'kwargs': kwargs},
'input': {'index_id': index_id, 'query': query, 'kwargs': kwargs},
}
try:
return self._send_request(payload)
Expand Down Expand Up @@ -254,4 +260,45 @@ def create_context(self, name: str, queries: Optional[List[Query]]) -> Any:
elif e.status_code == 400:
raise ValueError(str(e))
else:
raise e
raise e

def get_atoms_by_field(self, query: Query) -> List[str]:
payload = {'action': 'get_atoms_by_field', 'input': {'query': query}}
try:
return self._send_request(payload)
except HTTPError as e:
if e.status_code == 400:
raise ValueError(str(e))
else:
raise e

def get_atoms_by_text_field(
self, text_value: str, field: Optional[str] = None, text_index_id: Optional[str] = None
) -> List[str]:
payload = {
'action': 'get_atoms_by_text_field',
'input': {'text_value': text_value, 'field': field, 'text_index_id': text_index_id},
}
try:
return self._send_request(payload)
except HTTPError as e:
if e.status_code == 400:
raise ValueError(str(e))
else:
raise e

def get_node_by_name_starting_with(self, node_type: str, startswith: str) -> List[str]:
payload = {
'action': 'get_node_by_name_starting_with',
'input': {
'node_type': node_type,
'startswith': startswith,
},
}
try:
return self._send_request(payload)
except HTTPError as e:
if e.status_code == 400:
raise ValueError(str(e))
else:
raise e
Loading

0 comments on commit 885b0c0

Please sign in to comment.