Skip to content

Commit 5ef9b3b

Browse files
author
Ralf Schlatterbeck
committed
More transitive-property support.
- Implemented transitive properties in sort and group specs. Sort/group specs can now be lists of specs. - All regression tests except for one metakit backend test related to metakit having no representation of NULL pass - Fixed more PEP 8 whitespace peeves (and probably introduced some new ones :-) - Moved Proptree from support.py to hyperdb.py due to circular import - Moved some proptree-specific methods from Class to Proptree - Added a test for sorting by ids -> should be numeric sort (which now really works for all backends) - Added "required" attribute to all property classes in hyperdb (e.g., String, Link,...), see Feature Requests [SF#539081] -> factored common stuff to _Type. Note that I also converted to a new-style class when I was at it. Bad: The repr changes for new-style classes which made some SQL backends break (!) because the repr of Multilink is used in the schema storage. Fixed the repr to be independent of the class type. - Added get_required_props to Class. Todo: should also automagically make the key property required... - Add a sort_repr method to property classes. This defines the sort-order. Individual backends may use diffent routines if the outcome is the same. This one has a special case for id properties to make the sorting numeric. Using these methods isn't mandatory in backends as long as the sort-order is correct. - Multilink sorting takes orderprop into account. It used to sort by ids. You can restore the old behaviour by specifying id as the orderprop of the Multilink if you really need that. - If somebody specified a Link or Multilink as orderprop, we sort by labelprop of that class -- not transitively by orderprop. I've resited the tempation to implement recursive orderprop here: There could even be loops if several classes specify a Link or Multilink as the orderprop... - Fixed a bug in Metakit-Backend: When sorting by Links, the backend would do a natural join to the Link class. It would rename the "id" attribute before joining but *not* all the other attributes of the joined class. So in one test-case we had a name-clash with priority.name and status.name when sorting *and* grouping by these attributes. Depending on the order of joining this would produce a name-clash with broken sort-results (and broken display if the original class has an attribute that clashes). I'm now doing the sorting of Links in the generic filter method for the metakit backend. I've left the dead code in the metakit-backend since correctly implementing this in the backend will probably be more efficient. - updated doc/design.html with the new docstring of filter.
1 parent be33b14 commit 5ef9b3b

File tree

8 files changed

+905
-428
lines changed

8 files changed

+905
-428
lines changed

CHANGES.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Feature:
1616
- update for latest version of psycopg2 (sf patch 1429391)
1717
- new "exporttables" command in roundup-admin (sf bug 1533791)
1818
- roundup-admin "export" may specify classes to exclude (sf bug 1533791)
19+
- sorting and grouping by multiple properties is now supported by the
20+
backends
21+
- sorting, grouping, and searching by transitive properties (e.g.,
22+
messages.author.supervisor) is now supported in all backends
1923

2024
Fixed:
2125
- Verbose option for import and export (sf bug 1505645)
@@ -36,7 +40,13 @@ Fixed:
3640
- fix help message for roundup-admin install (sf bug 1494990)
3741
- removed traceback with OTK is used multiple times (sf bug 1240539)
3842
- metakit backend was indexing FileClass content even when asked not to
39-
43+
- anydbm backend will finally sort numerically by ID
44+
- problem with string sorting in anydbm backend fixed: If a string was
45+
fully numeric it was sorted as a number
46+
- Multilink-sorting now sorts by orderprop not by ID and works for all
47+
backends
48+
- Bug with name-collisions in sorted classes when sorting by Link
49+
properties in metakit backend fixed
4050

4151
2006-04-27 1.1.2
4252
Feature:

doc/design.txt

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,31 @@ Here is the interface provided by the hyperdatabase::
471471
"""
472472

473473
def filter(self, search_matches, filterspec, sort, group):
474-
""" Return a list of the ids of the active items in this
475-
class that match the 'filter' spec, sorted by the group spec
476-
and then the sort spec.
474+
"""Return a list of the ids of the active nodes in this class that
475+
match the 'filter' spec, sorted by the group spec and then the
476+
sort spec.
477+
478+
"filterspec" is {propname: value(s)}
479+
480+
"sort" and "group" are [(dir, prop), ...] where dir is '+', '-'
481+
or None and prop is a prop name or None. Note that for
482+
backward-compatibility reasons a single (dir, prop) tuple is
483+
also allowed.
484+
485+
"search_matches" is {nodeid: marker}
486+
487+
The filter must match all properties specificed. If the property
488+
value to match is a list:
489+
490+
1. String properties must match all elements in the list, and
491+
2. Other properties must match any of the elements in the list.
492+
493+
Note that now the propname in filterspec and prop in a
494+
sort/group spec may be transitive, i.e., it may contain
495+
properties of the form link.link.link.name, e.g. you can search
496+
for all issues where a message was added by a certain user in
497+
the last week with a filterspec of
498+
{'messages.author' : '42', 'messages.creation' : '.-1w;'}
477499
"""
478500

479501
def list(self):

roundup/backends/back_anydbm.py

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
#$Id: back_anydbm.py,v 1.200 2006-07-08 18:28:18 schlatterbeck Exp $
18+
#$Id: back_anydbm.py,v 1.201 2006-08-21 12:19:48 schlatterbeck Exp $
1919
'''This module defines a backend that saves the hyperdatabase in a
2020
database chosen by anydbm. It is guaranteed to always be available in python
2121
versions >2.1.1 (the dumbdbm fallback in 2.1.1 and earlier has several
@@ -36,6 +36,7 @@
3636
import whichdb, os, marshal, re, weakref, string, copy, time, shutil, logging
3737

3838
from roundup import hyperdb, date, password, roundupdb, security, support
39+
from roundup.support import reversed
3940
from roundup.backends import locking
4041
from roundup.i18n import _
4142

@@ -1531,8 +1532,8 @@ def getnodeids(self, db=None, retired=None):
15311532
db.close()
15321533
return res
15331534

1534-
def _filter(self, search_matches, filterspec, sort=(None,None),
1535-
group=(None,None), num_re = re.compile('^\d+$')):
1535+
def _filter(self, search_matches, filterspec, proptree,
1536+
num_re = re.compile('^\d+$')):
15361537
"""Return a list of the ids of the active nodes in this class that
15371538
match the 'filter' spec, sorted by the group spec and then the
15381539
sort spec.
@@ -1706,18 +1707,21 @@ def _filter(self, search_matches, filterspec, sort=(None,None),
17061707
k.append(v)
17071708
matches = k
17081709

1709-
# always sort by id if no other sort is specified
1710-
if sort == (None, None):
1711-
sort = ('+', 'id')
1712-
1713-
# add sorting information to the match entries
1714-
directions = []
1710+
# add sorting information to the proptree
17151711
JPROPS = {'actor':1, 'activity':1, 'creator':1, 'creation':1}
1716-
for dir, prop in sort, group:
1717-
if dir is None or prop is None:
1718-
continue
1719-
directions.append(dir)
1712+
children = []
1713+
if proptree:
1714+
children = proptree.sortable_children()
1715+
for pt in children:
1716+
dir = pt.sort_direction
1717+
prop = pt.name
1718+
assert (dir and prop)
17201719
propclass = props[prop]
1720+
pt.sort_ids = []
1721+
is_pointer = isinstance(propclass,(hyperdb.Link,
1722+
hyperdb.Multilink))
1723+
if not is_pointer:
1724+
pt.sort_result = []
17211725
try:
17221726
# cache the opened link class db, if needed.
17231727
lcldb = None
@@ -1738,33 +1742,42 @@ def _filter(self, search_matches, filterspec, sort=(None,None),
17381742
else:
17391743
# the node doesn't have a value for this
17401744
# property
1741-
if isinstance(propclass, hyperdb.Multilink): v = []
1742-
else: v = None
1743-
entry.insert(0, v)
1745+
v = None
1746+
if isinstance(propclass, hyperdb.Multilink):
1747+
v = []
1748+
if prop == 'id':
1749+
v = int (itemid)
1750+
pt.sort_ids.append(v)
1751+
if not is_pointer:
1752+
pt.sort_result.append(v)
17441753
continue
17451754

17461755
# missing (None) values are always sorted first
17471756
if v is None:
1748-
entry.insert(0, v)
1757+
pt.sort_ids.append(v)
1758+
if not is_pointer:
1759+
pt.sort_result.append(v)
17491760
continue
17501761

1751-
if isinstance(propclass, hyperdb.String):
1752-
# it might be a string that's really an integer
1753-
try: tv = int(v)
1754-
except: v = v.lower()
1755-
else: v = tv
1756-
elif isinstance(propclass, hyperdb.Link):
1762+
if isinstance(propclass, hyperdb.Link):
17571763
lcn = propclass.classname
17581764
link = self.db.classes[lcn]
17591765
key = link.orderprop()
1766+
child = pt.propdict[key]
17601767
if key!='id':
17611768
if not lcache.has_key(v):
17621769
# open the link class db if it's not already
17631770
if lcldb is None:
17641771
lcldb = self.db.getclassdb(lcn)
17651772
lcache[v] = self.db.getnode(lcn, v, lcldb)
1766-
v = lcache[v][key]
1767-
entry.insert(0, v)
1773+
r = lcache[v][key]
1774+
child.propdict[key].sort_ids.append(r)
1775+
else:
1776+
child.propdict[key].sort_ids.append(v)
1777+
pt.sort_ids.append(v)
1778+
if not is_pointer:
1779+
r = propclass.sort_repr(pt.parent.cls, v, pt.name)
1780+
pt.sort_result.append(r)
17681781
finally:
17691782
# if we opened the link class db, close it now
17701783
if lcldb is not None:
@@ -1773,29 +1786,6 @@ def _filter(self, search_matches, filterspec, sort=(None,None),
17731786
finally:
17741787
cldb.close()
17751788

1776-
# sort vals are inserted, but directions are appended, so reverse
1777-
directions.reverse()
1778-
1779-
if '-' in directions:
1780-
# one or more of the sort specs is in reverse order, so we have
1781-
# to use this icky function to sort
1782-
def sortfun(a, b, directions=directions, n=range(len(directions))):
1783-
for i in n:
1784-
if not cmp(a[i], b[i]):
1785-
continue
1786-
if directions[i] == '+':
1787-
# compare in the usual, ascending direction
1788-
return cmp(a[i],b[i])
1789-
else:
1790-
# compare in the reverse, descending direction
1791-
return cmp(b[i],a[i])
1792-
# for consistency, sort by the id if the items are equal
1793-
return cmp(a[-2], b[-2])
1794-
matches.sort(sortfun)
1795-
else:
1796-
# sorting is in the normal, ascending direction
1797-
matches.sort()
1798-
17991789
# pull the id out of the individual entries
18001790
matches = [entry[-2] for entry in matches]
18011791
if __debug__:

roundup/backends/back_metakit.py

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $Id: back_metakit.py,v 1.111 2006-08-18 01:36:58 richard Exp $
1+
# $Id: back_metakit.py,v 1.112 2006-08-21 12:19:48 schlatterbeck Exp $
22
'''Metakit backend for Roundup, originally by Gordon McMillan.
33
44
Known Current Bugs:
@@ -42,6 +42,7 @@
4242
BACKWARDS_COMPATIBLE = 1
4343

4444
from roundup import hyperdb, date, password, roundupdb, security
45+
from roundup.support import reversed
4546
import logging
4647
import metakit
4748
from sessions_dbm import Sessions, OneTimeKeys
@@ -1155,8 +1156,7 @@ def addprop(self, **properties):
11551156
self.db.commit()
11561157
# ---- end of ping's spec
11571158

1158-
def _filter(self, search_matches, filterspec, sort=(None,None),
1159-
group=(None,None)):
1159+
def _filter(self, search_matches, filterspec, proptree):
11601160
'''Return a list of the ids of the active nodes in this class that
11611161
match the 'filter' spec, sorted by the group spec and then the
11621162
sort spec
@@ -1326,37 +1326,58 @@ def ff(row, r=regexes):
13261326
iv = v.filter(ff)
13271327
v = v.remapwith(iv)
13281328

1329-
if sort or group:
1330-
sortspec = []
1331-
rev = []
1332-
for dir, propname in group, sort:
1333-
if propname is None: continue
1334-
isreversed = 0
1335-
if dir == '-':
1336-
isreversed = 1
1337-
try:
1338-
prop = getattr(v, propname)
1339-
except AttributeError:
1329+
# Handle all the sorting we can inside Metakit. If we encounter
1330+
# transitive attributes or a Multilink on the way, we sort by
1331+
# what we have so far and defer the rest to the outer sorting
1332+
# routine. We mark the attributes for which sorting has been
1333+
# done with sort_done. Of course the whole thing works only if
1334+
# we do it backwards.
1335+
sortspec = []
1336+
rev = []
1337+
sa = []
1338+
if proptree:
1339+
sa = reversed(proptree.sortattr)
1340+
for pt in sa:
1341+
if pt.parent != proptree:
1342+
break;
1343+
propname = pt.name
1344+
dir = pt.sort_direction
1345+
assert (dir and propname)
1346+
isreversed = 0
1347+
if dir == '-':
1348+
isreversed = 1
1349+
try:
1350+
prop = getattr(v, propname)
1351+
except AttributeError:
1352+
logging.getLogger("hyperdb").error(
1353+
"MK has no property %s" % propname)
1354+
continue
1355+
propclass = self.ruprops.get(propname, None)
1356+
if propclass is None:
1357+
propclass = self.privateprops.get(propname, None)
1358+
if propclass is None:
13401359
logging.getLogger("hyperdb").error(
1341-
"MK has no property %s" % propname)
1360+
"Schema has no property %s" % propname)
13421361
continue
1343-
propclass = self.ruprops.get(propname, None)
1344-
if propclass is None:
1345-
propclass = self.privateprops.get(propname, None)
1346-
if propclass is None:
1347-
logging.getLogger("hyperdb").error(
1348-
"Schema has no property %s" % propname)
1349-
continue
1350-
if isinstance(propclass, hyperdb.Link):
1351-
linkclass = self.db.getclass(propclass.classname)
1352-
lv = linkclass.getview()
1353-
lv = lv.rename('id', propname)
1354-
v = v.join(lv, prop, 1)
1355-
prop = getattr(v, linkclass.orderprop())
1356-
if isreversed:
1357-
rev.append(prop)
1358-
sortspec.append(prop)
1359-
v = v.sortrev(sortspec, rev)[:] #XXX Metakit bug
1362+
# Dead code: We dont't find Links here (in sortattr we would
1363+
# see the order property of the link, but this is not in the
1364+
# first level of the tree). The code is left in because one
1365+
# day we might want to properly implement this. The code is
1366+
# broken because natural-joining to the Link-class can
1367+
# produce name-clashes wich result in broken sorting.
1368+
if isinstance(propclass, hyperdb.Link):
1369+
linkclass = self.db.getclass(propclass.classname)
1370+
lv = linkclass.getview()
1371+
lv = lv.rename('id', propname)
1372+
v = v.join(lv, prop, 1)
1373+
prop = getattr(v, linkclass.orderprop())
1374+
if isreversed:
1375+
rev.append(prop)
1376+
sortspec.append(prop)
1377+
pt.sort_done = True
1378+
sortspec.reverse()
1379+
rev.reverse()
1380+
v = v.sortrev(sortspec, rev)[:] #XXX Metakit bug
13601381

13611382
rslt = []
13621383
for row in v:

0 commit comments

Comments
 (0)