Пример #1
0
    def createEdge(self,
                   sourceNode,
                   targetNode,
                   type="",
                   basicAttrs=None,
                   save=True):
        """
        Create a directed edge between two nodes.
        
        If you use node IDs instead of nodes as parameters,
        the nodes will not be saved automatically and will not contain
        any basic attributes.
        You can save the nodes
        
        @param sourceNode A source node identifier or a Node instance that acts as source
        @param targetNode A target node identifier or a Node instance that acts as target
        @param type The type of the edge
        @return The edge instance
        """
        #Check if both nodes are from the same graph.
        if isinstance(sourceNode, Node) and isinstance(targetNode, Node):
            if sourceNode.graph() != targetNode.graph():
                raise ConsistencyException(
                    "Source and target node are not from the same graph")
        #If the arguments are strings, ensure they're proper identifiers

        #Ensure we have strings because the edge constructor takes them
        if isinstance(sourceNode, Node):
            sourceNode = sourceNode.id
        if isinstance(targetNode, Node):
            targetNode = targetNode.id
        #Create the edge and save it to the database
        edge = Edge(sourceNode, targetNode, self, type, basicAttrs)
        if save: edge.save()
        return edge
Пример #2
0
 def _scanEdges(self, startKey, endKey, limit=None):
     """
     Do a scan over the edge table.
     Internally used by the Node class.
     @param startKey The edge table start key
     @param endKey The edge table start key
     @return a list of edge objects
     """
     scanResult = self.conn.scan(self.edgeTableId, startKey, endKey, limit)
     edges = []
     for (key, value) in scanResult.iteritems():
         #Deserialize key and value
         edgeTuple = Edge._deserializeEdge(key)
         basicAttrs = BasicAttributes._parseAttributeSet(value)
         edge = Edge(edgeTuple[0], edgeTuple[1], self, edgeTuple[2],
                     basicAttrs)
         edges.append(edge)
     return edges