-
Notifications
You must be signed in to change notification settings - Fork 3
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
[#201] Implement fetch() in the DAS API #207
Changes from 10 commits
5883e92
c049f2d
b09e1e7
f8d6fe1
c3a99c8
dec20df
6a565cd
86919f5
ec77412
fbf303c
cd7709d
18bde9d
81ea33c
4f4c2d9
f6e850f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
|
||
[#201] Implement fetch() in the DAS API |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
from hyperon_das_atomdb.adapters import InMemoryDB, RedisMongoDB | ||
from hyperon_das_atomdb.exceptions import InvalidAtomDB | ||
|
||
from hyperon_das.cache import QueryAnswerIterator | ||
from hyperon_das.cache import CacheManager, QueryAnswerIterator | ||
from hyperon_das.exceptions import ( | ||
GetTraversalCursorException, | ||
InvalidDASParameters, | ||
|
@@ -32,6 +32,8 @@ def __init__(self, **kwargs: Optional[Dict[str, Any]]) -> None: | |
else: | ||
raise InvalidAtomDB(message="Invalid AtomDB type. Choose either 'ram' or 'redis_mongo'") | ||
|
||
kwargs.update({'cache_manager': CacheManager(self.backend)}) | ||
|
||
if query_engine_parameter == 'local': | ||
self.query_engine = LocalQueryEngine(self.backend, kwargs) | ||
logger().info('Initialized local Das') | ||
|
@@ -575,3 +577,44 @@ def create_field_index( | |
return self.query_engine.create_field_index( | ||
atom_type, field, type=type, composite_type=composite_type | ||
) | ||
|
||
def fetch( | ||
self, | ||
query: Union[List[dict], dict], | ||
host: Optional[str] = None, | ||
port: Optional[int] = None, | ||
**kwargs, | ||
) -> Any: | ||
"""Fetch data from the remote server using the a query as input and load it locally. | ||
If it is a local DAS, the host and port must be sent. | ||
|
||
The input dict is a link, used as a pattern to make the query. | ||
Variables can be used as link targets as well as nodes. Nested links are | ||
allowed as well. | ||
|
||
Args: | ||
query (Union[List[dict], dict]): A pattern described as a link (possibly with nested links) | ||
with nodes and variables used to query the knowledge base. | ||
host (Optional[str], optional): Address to remote server. Defaults to None. | ||
port (Optional[int], optional): Port to remote server. Defaults to None. | ||
|
||
Raises: | ||
ValueError: If the 'host' and 'port' parameters are not sent to DAS local | ||
|
||
Examples: | ||
>>> query = { | ||
"atom_type": "link", | ||
"type": "Expression", | ||
"targets": [ | ||
{"atom_type": "node", "type": "Symbol", "name": "Inheritance"}, | ||
{"atom_type": "variable", "name": "v1"}, | ||
{"atom_type": "node", "type": "Symbol", "name": '"mammal"'}, | ||
], | ||
} | ||
das = DistributedAtomSpace() | ||
das.fetch(query, host='123.4.5.6', port=8080) | ||
""" | ||
if not kwargs.get('running_on_server') and not host and not port: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You shouldn't need to have a KW parameter to check if it's a local or a remote DAS. There should be a more elegant way to identify if it's running on a local or a remote DAS. This is a method in DAS public API so you are expecting that the user will pass this parameter... This seems like bad UX. |
||
raise ValueError("The 'host' and 'port' parameters must be sent to DAS local") | ||
|
||
return self.query_engine.fetch(query, host, port, **kwargs) |
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.
This check seems to be wrong. If it's not running on server then it's running in a local DAS. But if it's running in a local DAS, fetch will need both
host
andport
. However, your check will not raise if one of them is None but the other is OK.