Пример #1
0
    def put_elt(self, elt, graph_ids=None, cache=False):
        """
        Put an element.

        :param dict elt: element to put.
        :type elt: dict or 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 elt is a dict
        if isinstance(elt, GraphElement):
            elt = elt.to_dict()
        # get elt uuid
        if GraphElement.ID not in elt:
            elt[GraphElement.ID] = GraphElement.new_id()
        elt_id = elt[GraphElement.ID]

        # put elt value in storage
        self[GraphManager.STORAGE].put_element(_id=elt_id, element=elt, cache=cache)
        # update graph if graph_id is not None
        if graph_ids is not None:
            graphs = self.get_graphs(ids=graph_ids)
            if graphs is not None:
                # ensure graphs is a list of graphs
                if isinstance(graphs, Graph):
                    graphs = [graphs]
                for graph in graphs:
                    # if graph exists and elt_id not already present
                    if elt_id not in graph.elts:
                        # add elt_id in graph elts
                        graph.elts.append(elt_id)
                        graph.save(self, cache=cache)
Пример #2
0
    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