]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
breakthrough, wow
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 5 Apr 2010 22:48:57 +0000 (18:48 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 5 Apr 2010 22:48:57 +0000 (18:48 -0400)
lib/sqlalchemy/orm/unitofwork.py
lib/sqlalchemy/sql/util.py
lib/sqlalchemy/topological.py
test/base/test_dependency.py

index bb3bb4fb271fe13f155b12f229152787f0cac733..16a01bdc1dcc63ad6df2a2c7cbc69721305a7d5a 100644 (file)
@@ -215,30 +215,13 @@ class UOWTransaction(object):
                             ).difference(cycles)
         
         # execute actions
-        sort = topological.sort(self.dependencies, postsort_actions)
-        print "------------------------"
-        print self.dependencies
-        print sort
-        
         if cycles:
-            # organize into a tree so that groups of nodes can be 
-            # merged together, allowing better maintenance of insert
-            # ordering and other things
-            (head, children) = topological.organize_as_tree(self.dependencies, sort)
-            stack = [(head, children)]
-            
-            head.execute(self)
-            while stack:
-                node, children = stack.pop()
-                if children:
-                    related = set([n[0] for n in children])
-                    while related:
-                        n = related.pop()
-                        n.execute_aggregate(self, related)
-                
-                    stack += children
+            for set_ in topological.sort_as_subsets(self.dependencies, postsort_actions):
+                while set_:
+                    n = set_.pop()
+                    n.execute_aggregate(self, set_)
         else:
-            for rec in sort:
+            for rec in topological.sort(self.dependencies, postsort_actions):
                 rec.execute(self)
             
 
index 4d0071978de82f36d92764a34da6139847c5dd00..dda5d2d28bc2dfdca84e0bc14caf0b425545b90a 100644 (file)
@@ -22,7 +22,7 @@ def sort_tables(tables):
         visitors.traverse(table, 
                             {'schema_visitor':True}, 
                             {'foreign_key':visit_foreign_key})
-    return topological.sort(tuples, tables)
+    return list(topological.sort(tuples, tables))
 
 def find_join_source(clauses, join_to):
     """Given a list of FROM clauses and a selectable, 
index 7253cdc4d89d1ad632e2f1faed2bef330e4b64d0..2ae631fb592f7c1152be1a68fe98a81183971615 100644 (file)
@@ -33,61 +33,48 @@ class _EdgeCollection(object):
             self.parent_to_children[parentnode].add(childnode)
             self.child_to_parents[childnode].add(parentnode)
             
-    def has_parents(self, node):
-        return node in self.child_to_parents and bool(self.child_to_parents[node])
-
     def outgoing(self, node):
-        """an iterable returning all nodes reached via node's outgoing edges"""
-        
         return self.parent_to_children[node]
+    
+    def incoming(self, node):
+        return self.child_to_parents[node]
         
-    def pop_node(self, node):
-        """Remove all edges where the given node is a parent.
-
-        Return the collection of all nodes which were children of the
-        given node, and have no further parents.
-        """
-
-        children = self.parent_to_children.pop(node, None)
-        if children is not None:
-            for child in children:
-                self.child_to_parents[child].remove(node)
-                if not self.child_to_parents[child]:
-                    yield child
-
     def __iter__(self):
-        for parent, children in self.parent_to_children.iteritems():
-            for child in children:
+        for parent in self.parent_to_children:
+            for child in self.outgoing(parent):
                 yield (parent, child)
 
     def __repr__(self):
         return repr(list(self))
 
+def sort_as_subsets(tuples, allitems):
+    output = set()
+
+    todo = set(allitems)
+    edges = _EdgeCollection(tuples)
+    while todo:
+        for node in list(todo):
+            if not todo.intersection(edges.incoming(node)):
+                output.add(node)
+
+        if not output:
+            raise CircularDependencyError(
+                    "Circular dependency detected: cycles: %r all edges: %r" % 
+                    (find_cycles(tuples, allitems), edges))
+
+        todo.difference_update(output)
+        yield output
+        output = set()
+
 def sort(tuples, allitems):
     """sort the given list of items by dependency.
 
     'tuples' is a list of tuples representing a partial ordering.
     """
 
-    edges = _EdgeCollection(tuples)
-    nodes = set(allitems)
-
-    queue = []
-    for n in nodes:
-        if not edges.has_parents(n):
-            queue.append(n)
-
-    output = []
-    while nodes:
-        if not queue:
-            raise CircularDependencyError("Circular dependency detected: cycles: %r all edges: %r" % 
-                                                    (find_cycles(tuples, allitems), edges))
-        node = queue.pop()
-        output.append(node)
-        nodes.remove(node)
-        for childnode in edges.pop_node(node):
-            queue.append(childnode)
-    return output
+    for set_ in sort_as_subsets(tuples, allitems):
+        for s in set_:
+            yield s
 
 def find_cycles(tuples, allitems):
     # straight from gvr with some mods
@@ -115,49 +102,3 @@ def find_cycles(tuples, allitems):
                 node = stack.pop()
     return output
 
-
-def organize_as_tree(tuples, allitems):
-    """Given a list of sorted nodes from a topological sort, organize the
-    nodes into a tree structure, with as many non-dependent nodes
-    set as siblings to each other as possible.
-
-    returns nodes as tuples (item, children).
-    """
-
-    nodes = allitems
-    edges = _EdgeCollection(tuples)
-    children = util.defaultdict(list)
-    
-    if not nodes:
-        return None
-    # a list of all currently independent subtrees as a tuple of
-    # (root_node, set_of_all_tree_nodes, set_of_all_cycle_nodes_in_tree)
-    # order of the list has no semantics for the algorithmic
-    independents = []
-    # in reverse topological order
-    for node in reversed(nodes):
-        # nodes subtree and cycles contain the node itself
-        subtree = set([node])
-        # get a set of dependent nodes of node and its cycles
-        nodealldeps = edges.outgoing(node)
-        if nodealldeps:
-            # iterate over independent node indexes in reverse order so we can efficiently remove them
-            for index in xrange(len(independents) - 1, -1, -1):
-                child, childsubtree = independents[index]
-                # if there is a dependency between this node and an independent node
-                if (childsubtree.intersection(nodealldeps)):
-                    # prepend child to nodes children
-                    # (append should be fine, but previous implemetation used prepend)
-                    children[node][0:0] = [(child, children[child])]
-                    # merge childs subtree and cycles
-                    subtree.update(childsubtree)
-                    # remove the child from list of independent subtrees
-                    independents[index:index+1] = []
-        # add node as a new independent subtree
-        independents.append((node, subtree))
-    # choose an arbitrary node from list of all independent subtrees
-    head = independents.pop()[0]
-    # add all other independent subtrees as a child of the chosen root
-    # used prepend [0:0] instead of extend to maintain exact behaviour of previous implementation
-    children[head][0:0] = [(i[0], children[i[0]]) for i in independents]
-    return (head, children[head])
index 462e923f1b34fbdb29a5d7d266f162b0a95bd288..bef0ef25293b83c269b73364cb2872a7e94fc602 100644 (file)
@@ -12,7 +12,7 @@ class DependencySortTest(TestBase):
         else:
             allitems = self._nodes_from_tuples(tuples).union(allitems)
             
-        result = topological.sort(tuples, allitems)
+        result = list(topological.sort(tuples, allitems))
         
         deps = collections.defaultdict(set)
         for parent, child in tuples:
@@ -96,7 +96,7 @@ class DependencySortTest(TestBase):
             (node4, node1)
         ]
         allitems = self._nodes_from_tuples(tuples)
-        assert_raises(exc.CircularDependencyError, topological.sort, tuples, allitems)
+        assert_raises(exc.CircularDependencyError, list, topological.sort(tuples, allitems))
 
         # TODO: test find_cycles
 
@@ -115,7 +115,7 @@ class DependencySortTest(TestBase):
             (node2, node3)
         ]
         allitems = self._nodes_from_tuples(tuples)
-        assert_raises(exc.CircularDependencyError, topological.sort, tuples, allitems)
+        assert_raises(exc.CircularDependencyError, list, topological.sort(tuples, allitems))
 
         # TODO: test find_cycles
 
@@ -127,7 +127,7 @@ class DependencySortTest(TestBase):
                     (provider, providerservice), (question, answer), (issue, question)]
 
         allitems = self._nodes_from_tuples(tuples)
-        assert_raises(exc.CircularDependencyError, topological.sort, tuples, allitems)
+        assert_raises(exc.CircularDependencyError, list, topological.sort(tuples, allitems))
         
         # TODO: test find_cycles