|
15 | 15 | # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, |
16 | 16 | # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
17 | 17 | # |
18 | | -#$Id: back_anydbm.py,v 1.145 2004-05-05 00:17:13 richard Exp $ |
| 18 | +#$Id: back_anydbm.py,v 1.146 2004-05-05 01:52:34 richard Exp $ |
19 | 19 | '''This module defines a backend that saves the hyperdatabase in a |
20 | 20 | database chosen by anydbm. It is guaranteed to always be available in python |
21 | 21 | versions >2.1.1 (the dumbdbm fallback in 2.1.1 and earlier has several |
@@ -1664,6 +1664,7 @@ def filter(self, search_matches, filterspec, sort=(None,None), |
1664 | 1664 | # now, find all the nodes that are active and pass filtering |
1665 | 1665 | matches = [] |
1666 | 1666 | cldb = self.db.getclassdb(cn) |
| 1667 | + t = 0 |
1667 | 1668 | try: |
1668 | 1669 | # TODO: only full-scan once (use items()) |
1669 | 1670 | for nodeid in self.getnodeids(cldb): |
@@ -1725,55 +1726,74 @@ def filter(self, search_matches, filterspec, sort=(None,None), |
1725 | 1726 | break |
1726 | 1727 | else: |
1727 | 1728 | matches.append([nodeid, node]) |
1728 | | - finally: |
1729 | | - cldb.close() |
1730 | 1729 |
|
1731 | | - # filter based on full text search |
1732 | | - if search_matches is not None: |
1733 | | - k = [] |
1734 | | - for v in matches: |
1735 | | - if search_matches.has_key(v[0]): |
1736 | | - k.append(v) |
1737 | | - matches = k |
1738 | | - |
1739 | | - # always sort by id if no other sort is specified |
1740 | | - if sort == (None, None): |
1741 | | - sort = ('+', 'id') |
1742 | | - |
1743 | | - # add sorting information to the match entries |
1744 | | - directions = [] |
1745 | | - for dir, prop in sort, group: |
1746 | | - if dir is None or prop is None: |
1747 | | - continue |
1748 | | - directions.append(dir) |
1749 | | - propclass = props[prop] |
1750 | | - for entry in matches: |
1751 | | - itemid = entry[-2] |
1752 | | - item = entry[-1] |
1753 | | - # handle the properties that might be "faked" |
1754 | | - # also, handle possible missing properties |
1755 | | - try: |
1756 | | - v = self.get(itemid, prop) |
1757 | | - except KeyError: |
1758 | | - # the node doesn't have a value for this property |
1759 | | - if isinstance(propclass, Multilink): v = [] |
1760 | | - else: v = None |
1761 | | - s.append((v, itemid, item)) |
| 1730 | + # filter based on full text search |
| 1731 | + if search_matches is not None: |
| 1732 | + k = [] |
| 1733 | + for v in matches: |
| 1734 | + if search_matches.has_key(v[0]): |
| 1735 | + k.append(v) |
| 1736 | + matches = k |
| 1737 | + |
| 1738 | + # always sort by id if no other sort is specified |
| 1739 | + if sort == (None, None): |
| 1740 | + sort = ('+', 'id') |
| 1741 | + |
| 1742 | + # add sorting information to the match entries |
| 1743 | + directions = [] |
| 1744 | + for dir, prop in sort, group: |
| 1745 | + if dir is None or prop is None: |
1762 | 1746 | continue |
| 1747 | + directions.append(dir) |
| 1748 | + propclass = props[prop] |
| 1749 | + try: |
| 1750 | + # cache the opened link class db, if needed. |
| 1751 | + lcldb = None |
| 1752 | + # cache the linked class items too |
| 1753 | + lcache = {} |
| 1754 | + |
| 1755 | + for entry in matches: |
| 1756 | + itemid = entry[-2] |
| 1757 | + item = entry[-1] |
| 1758 | + # handle the properties that might be "faked" |
| 1759 | + # also, handle possible missing properties |
| 1760 | + try: |
| 1761 | + v = item[prop] |
| 1762 | + except KeyError: |
| 1763 | + # the node doesn't have a value for this property |
| 1764 | + if isinstance(propclass, Multilink): v = [] |
| 1765 | + else: v = None |
| 1766 | + entry.insert(0, v) |
| 1767 | + continue |
1763 | 1768 |
|
1764 | | - if isinstance(propclass, String): |
1765 | | - # it might be a string that's really an integer |
1766 | | - try: tv = int(v) |
1767 | | - except: v = v.lower() |
1768 | | - else: v = tv |
1769 | | - elif isinstance(propclass, Link): |
1770 | | - link = self.db.classes[propclass.classname] |
1771 | | - if link.getprops().has_key('order'): |
1772 | | - v = link.get(v, 'order') |
1773 | | - elif link.getkey(): |
1774 | | - key = link.getkey() |
1775 | | - v = link.get(v, key) |
1776 | | - entry.insert(0, v) |
| 1769 | + if isinstance(propclass, String): |
| 1770 | + # it might be a string that's really an integer |
| 1771 | + try: tv = int(v) |
| 1772 | + except: v = v.lower() |
| 1773 | + else: v = tv |
| 1774 | + elif isinstance(propclass, Link): |
| 1775 | + lcn = propclass.classname |
| 1776 | + link = self.db.classes[lcn] |
| 1777 | + key = None |
| 1778 | + if link.getprops().has_key('order'): |
| 1779 | + key = 'order' |
| 1780 | + elif link.getkey(): |
| 1781 | + key = link.getkey() |
| 1782 | + if key: |
| 1783 | + if not lcache.has_key(v): |
| 1784 | + # open the link class db if it's not already |
| 1785 | + if lcldb is None: |
| 1786 | + lcldb = self.db.getclassdb(lcn) |
| 1787 | + lcache[v] = self.db.getnode(lcn, v, lcldb) |
| 1788 | + v = lcache[v][key] |
| 1789 | + entry.insert(0, v) |
| 1790 | + finally: |
| 1791 | + # if we opened the link class db, close it now |
| 1792 | + if lcldb is not None: |
| 1793 | + lcldb.close() |
| 1794 | + del lcache |
| 1795 | + finally: |
| 1796 | + cldb.close() |
1777 | 1797 |
|
1778 | 1798 | if '-' in directions: |
1779 | 1799 | # one or more of the sort specs is in reverse order, so we have |
|
0 commit comments