diff roundup/support.py @ 3634:57c66056ffe4

Implemented what I'll call for now "transitive searching"... ...using the filter method. The first idea was mentioned on the roundup-users mailing list: http://article.gmane.org/gmane.comp.bug-tracking.roundup.user/6909 We can now search for items which link transitively to other classes using filter. An example is searching for all items where a certain user has added a message in the last week: db.issue.filter (None, {'messages.author' : '42', 'messages.date' : '.-1w;'}) or more readable (but not exactly semantically equivalent, if you're searching for multiple users in this way it will fail, because string searches are ANDed): {'messages.author.username':'ralf', ... We can even extend this further, look for all items that were changed by users belonging to a certain department (having the same supervisor -- a property that is not in the user class in standard roundup) in the last week, the filterspec would be: {'messages.author.supervisor' : '42', 'messages.date' : '.-1w;'} If anybody wants to suggest another name instead of transitive searching, you're welcome. I've implemented a generic method for this in hyperdb.py -- the backend now implements _filter in this case. With the generic method, anydbm and metakit should work (anydbm is tested, metakit breaks for other reasons). A backend may chose to implement the real transitive filter itself. This was done for rdbms_common.py. It now has an implementation of filter that supports transitive searching by creating one big join in the generated SQL query. I've added several new regression tests to test for the new features. All the tests (not just the new ones) run through on python2.3 and python2.4 with postgres, mysql, sqlite, anydbm -- but metakit was already broken when I started. I've generated a tag before commit called 'rsc_before_transitive_search' and will create the 'after' tag after this commit, so you can merge out my changes if you don't like them -- if you like them I can remove the tags. .-- Ralf
author Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
date Sat, 08 Jul 2006 18:28:18 +0000
parents 1be293265e61
children 53987aa153d2
line wrap: on
line diff
--- a/roundup/support.py	Fri Jul 07 15:04:28 2006 +0000
+++ b/roundup/support.py	Sat Jul 08 18:28:18 2006 +0000
@@ -5,6 +5,7 @@
 __docformat__ = 'restructuredtext'
 
 import os, time, sys
+import hyperdb
 
 class TruthDict:
     '''Returns True for valid keys, False for others.
@@ -118,4 +119,54 @@
         sys.stdout.write(s + ' '*(75-len(s)) + '\r')
         sys.stdout.flush()
 
+class Proptree :
+    ''' Simple tree data structure for optimizing searching of properties
+    '''
+
+    def __init__ (self, db, cls, name, props, parent = None, val = None) :
+        self.db        = db
+        self.name      = name
+        self.props     = props
+        self.parent    = parent
+        self.val       = val
+        self.cls       = cls
+        self.classname = None
+        self.uniqname  = None
+        self.children  = []
+        self.propnames = {}
+        if parent :
+            self.root  = parent.root
+            self.prcls = self.parent.props [name]
+        else :
+            self.root  = self
+            self.seqno = 1
+        self.id = self.root.seqno
+        self.root.seqno += 1
+        if self.cls :
+            self.classname = self.cls.classname
+            self.uniqname  = '%s%s' % (self.cls.classname, self.id)
+        if not self.parent :
+            self.uniqname  = self.cls.classname
+
+    def append (self, name) :
+        if name in self.propnames :
+            return self.propnames [name]
+        propclass = self.props [name]
+        cls   = None
+        props = None
+        if isinstance (propclass, (hyperdb.Link, hyperdb.Multilink)) :
+            cls   = self.db.getclass (propclass.classname)
+            props = cls.getprops ()
+        child = self.__class__ (self.db, cls, name, props, parent = self)
+        self.children.append (child)
+        self.propnames [name] = child
+        return child
+
+    def __iter__ (self) :
+        """ Yield nodes in depth-first order -- visited nodes first """
+        for p in self.children :
+            yield p
+            for c in p :
+                yield c
+
 # vim: set et sts=4 sw=4 :

Roundup Issue Tracker: http://roundup-tracker.org/