Esempio n. 1
0
    def _query_create_node(self, type, properties):
        """ Create, store, and return a new Neo4j node using Gremlin.

        Keys: node_id, type, properties, edges

        Required:
        str     type        type of neo4j node to create
        dict    properties  properties to set in new neo4j node

        Returns:
        dict                properly formatted node

        """

        # add type to properties dictionary before generating script
        properties[NODE_PROPERTY.TYPE] = type

        # write a gremlin query and specify substitution fields
        # TODO: does grabbing the empty edges list take time here?
        script = "g.addVertex({0}).transform{{[it, it.outE()]}}".format(
                NODE_PROPERTY.PROPERTIES)

        # specify substitution values for the gremlin query
        parameters = {NODE_PROPERTY.PROPERTIES: properties}

        # send a request to the database
        response = self._gremlin(script, parameters)

        # format the response as a proper node
        if response is None:
            # TODO: raise DbWriteError
            return None
        else:
            return response_parser.format_node(response[0])
Esempio n. 2
0
    def _query_read_node_and_edges(self, node_id):
        """ Read and return a Neo4j node and its edges using Gremlin.

        Keys: node_id, type, properties, edges

        Required:
        id      node_id     id of node to query

        Return:
        dict                properly formatted node

        """

        # write a gremlin query and specify substitution fields
        # [ [ { v }, [ { Pipe }, { Pipe }, { Pipe } ] ] ]
        script = "g.v({0}).transform{{[it, it.outE()]}}".format(
                NODE_PROPERTY.ID)

        # specify substitution values for the gremlin query
        params = {NODE_PROPERTY.ID: node_id}

        # send a request to the database
        response = self._gremlin(script, params)

        # format response as a proper node
        if response is None:
            return None
        else:
            return response_parser.format_node(response[0])