66from aperturedb .Constraints import Constraints
77from aperturedb .Connector import Connector
88from aperturedb .ParallelQuery import execute_batch
9+ from aperturedb .Query import QueryBuilder
910import pandas as pd
1011
1112
@@ -21,13 +22,13 @@ class Entities(Subscriptable):
2122 update_command = f"Update{ db_object } "
2223
2324 @classmethod
24- def retrieve (cls ,
25- db : Connector ,
26- spec : Query ,
27- with_adjacent : Dict [str , Query ] = None
28- ) -> List [Entities ]:
25+ def retrieve_entities (cls ,
26+ db : Connector ,
27+ spec : Query ,
28+ with_adjacent : Dict [str , Query ] = None
29+ ) -> List [Entities ]:
2930 """
30- Using the Entities.retrieve method, is a simpple layer, with typical native queries converted
31+ Using the Entities.retrieve method, is a simple layer, with typical native queries converted
3132 using :class:`~aperturedb.Query.Query`
3233
3334 Args:
@@ -85,8 +86,7 @@ def retrieve(cls,
8586 entities = cls .known_entities [wc ](
8687 db = db , response = subresponse , type = wc )
8788 entities .blobs = blobs
88- if wc [0 ] == "_" :
89- entities .find_command = f"Find{ wc [1 :]} "
89+
9090 results .append (entities )
9191 except Exception as e :
9292 print (e )
@@ -96,8 +96,25 @@ def retrieve(cls,
9696 cls .__postprocess__ (entities = results [- 1 ], with_adjacent = with_adjacent )
9797 return results
9898
99+ @classmethod
100+ def retrieve (cls ,
101+ db : Connector ,
102+ spec : Query ,
103+ with_adjacent : Dict [str , Query ] = None ) -> Entities :
104+ spec .db_object = cls .db_object
105+
106+ results = Entities .retrieve_entities (
107+ db = db , spec = spec , with_adjacent = with_adjacent )
108+
109+ # This is a very naive assumption, we will stop querying once
110+ # the object of interest is the in the resopnses.
111+ objects = results [- 1 ]
112+
113+ return objects
114+
99115 # This needs to be defined so that the application can access the adjacent items,
100116 # with every item of this iterable.
117+
101118 @classmethod
102119 def __decorator (cls , index , adjacent ):
103120 item = {}
@@ -180,27 +197,28 @@ def get_connected_entities(self, etype: Union[ObjectType, str], constraints: Co
180197 result = []
181198 entity_class = etype .value if isinstance (etype , ObjectType ) else etype
182199 for entity in self :
183- query = [
184- {
185- self .find_command : {
186- "_ref" : 1 ,
187- "unique" : False ,
188- "constraints" : {
189- "_uniqueid" : ["==" , entity ["_uniqueid" ]]
190- }
191- }
192- }, {
193- "FindEntity" : {
194- "is_connected_to" : {
195- "ref" : 1
196- },
197- "with_class" : entity_class ,
198- "constraints" : constraints .constraints ,
199- "results" : {
200- "all_properties" : True
201- }
202- }
200+ params_src = {
201+ "_ref" : 1 ,
202+ "unique" : False ,
203+ "constraints" : {
204+ "_uniqueid" : ["==" , entity ["_uniqueid" ]]
205+ }
206+ }
207+ params_dst = {
208+ "is_connected_to" : {
209+ "ref" : 1
210+ },
211+ "with_class" : entity_class ,
212+ "constraints" : constraints .constraints ,
213+ "results" : {
214+ "all_properties" : True
203215 }
216+ }
217+
218+ query = [
219+ QueryBuilder .find_command (self .db_object , params = params_src ),
220+ QueryBuilder .find_command (
221+ oclass = entity_class , params = params_dst )
204222 ]
205223 res , r , b = execute_batch (query , [], self .db )
206224 cl = Entities
@@ -214,19 +232,18 @@ def get_blob(self, entity) -> Any:
214232 """
215233 Helper to get blobs for FindImage, FindVideo and FindBlob commands.
216234 """
217- query = [
218- {
219- self .find_command : {
220- "constraints" : {
221- "_uniqueid" : ["==" , entity ["_uniqueid" ]]
222- },
223- "blobs" : True ,
224- "uniqueids" : True ,
225- "results" : {
226- "count" : True
227- }
228- }
235+ cmd_params = {
236+ "constraints" : {
237+ "_uniqueid" : ["==" , entity ["_uniqueid" ]]
238+ },
239+ "blobs" : True ,
240+ "uniqueids" : True ,
241+ "results" : {
242+ "count" : True
229243 }
244+ }
245+ query = [
246+ QueryBuilder ().find_command (self .db_object , params = cmd_params )
230247 ]
231248 res , r , b = execute_batch (query , [], self .db )
232249 return b [0 ]
0 commit comments