def put_elts(self, elts, graph_ids=None, cache=False): """ Put graph elements in DB. :param elts: graph elements to put in DB. :type elts: dict, GraphElement or list of dict/GraphElement. :param str graph_ids: element graph id. None if elt is a graph. :param bool cache: use query cache if True (False by default). """ # ensure elts is a list if isinstance(elts, (dict, GraphElement)): elts = [elts] for elt in elts: gelt = elt if isinstance(gelt, dict): if not gelt.get(GraphElement.ID): gelt[GraphElement.ID] = GraphElement.new_id() gelt = GraphElement.new_element(**gelt) # save elt gelt.save(manager=self, cache=cache, graph_ids=graph_ids) return elts
def get_elts( self, ids=None, types=None, graph_ids=None, info=None, base_type=None, query=None, serialize=True, cls=None ): """ Get graph element(s) related to input ids, types and query. :param ids: list of ids or id of element to retrieve. If None, get all elements. If str, get one element. :type ids: list or str :param types: graph element types to retrieve. :type types: list or str :param graph_ids: graph ids from where find elts. :type graph_ids: list or str :param info: info query :param dict query: element search query. :param str base_type: elt base type. :param bool serialize: serialize result in GraphElements if True (by default). :param type cls: GraphElement type to retrieve if not None. :return: element(s) corresponding to input ids and query. :rtype: list or dict """ # check if only one element is asked unique = isinstance(ids, basestring) # init query if query is None: query = {} # put base type in query if base_type is not None: query[GraphElement.BASE_TYPE] = base_type # put types in query if not None if types is not None: if not isinstance(types, basestring): types = {"$in": types} query[GraphElement.TYPE] = types # put info if not None if info is not None: if isinstance(info, dict): for name in info: data_name = "info.{0}".format(name) query[data_name] = info[name] else: query[Vertice.DATA] = info # find ids among graphs if graph_ids is not None: result = [] graphs = self.get_elts(ids=graph_ids, serialize=False) if graphs is not None: # all graph elt ids elt_ids = set() # ensure graphs is a list of graphs if isinstance(graphs, dict): graphs = [graphs] for graph in graphs: if Graph.ELTS in graph: elts = set(graph[Graph.ELTS]) elt_ids |= elts # if ids is not given, use elt_ids if ids is None: ids = list(elt_ids) else: # else use jonction of elt_ids and ids if isinstance(ids, basestring): ids = [ids] ids = list(elt_ids & set(ids)) # get elements with ids and query result = self[GraphManager.STORAGE].get_elements(ids=ids, query=query) if result is not None and serialize: if isinstance(result, dict): result = GraphElement.new_element(**result) # ensure cls is respected if cls is not None and not isinstance(result, cls): result = None else: # save reference to new_element in order to ease its use new_element = GraphElement.new_element result = list(new_element(**elt) for elt in result) # ensure cls is respected if cls is not None: result = [elt for elt in result if isinstance(elt, cls)] if unique and isinstance(result, list): result = result[0] if result else None return result