[12920] | 1 | Cataloging Support |
---|
| 2 | ****************** |
---|
[5092] | 3 | |
---|
[7811] | 4 | .. module:: waeup.kofa.catalog |
---|
[5092] | 5 | |
---|
| 6 | Components that support cataloging and searching objects inside a |
---|
[7819] | 7 | Kofa site. |
---|
[5092] | 8 | |
---|
| 9 | .. :doctest: |
---|
[7819] | 10 | .. :layer: waeup.kofa.testing.KofaUnitTestLayer |
---|
[5092] | 11 | |
---|
| 12 | Getting a general query object |
---|
[12951] | 13 | ============================== |
---|
[5092] | 14 | |
---|
[7819] | 15 | We can get a KofaQuery object by asking for an unnamed global utility |
---|
[5092] | 16 | implementing `hurry.query.interfaces.IQuery`: |
---|
| 17 | |
---|
| 18 | >>> from hurry.query.interfaces import IQuery |
---|
| 19 | >>> from zope.component import getUtility |
---|
| 20 | >>> q = getUtility(IQuery) |
---|
| 21 | >>> q |
---|
[7819] | 22 | <waeup.kofa.catalog.KofaQuery object at 0x...> |
---|
[5092] | 23 | |
---|
| 24 | This query can get 'subqueries' and delivers the objects found or |
---|
| 25 | their ids. To show this we have to setup a catalog with some entries. |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | Setting up a catalog and feeding it |
---|
[12951] | 29 | =================================== |
---|
[5092] | 30 | |
---|
| 31 | >>> from zope.catalog.interfaces import ICatalog |
---|
| 32 | >>> from zope.catalog.catalog import Catalog |
---|
| 33 | >>> mycat = Catalog() |
---|
| 34 | |
---|
| 35 | We register this catalog with the component architechture as a utility |
---|
| 36 | named 'mycatalog': |
---|
| 37 | |
---|
| 38 | >>> from zope.component import provideUtility |
---|
| 39 | >>> provideUtility(mycat, ICatalog, 'mycatalog') |
---|
| 40 | |
---|
| 41 | We setup a special content type whose instances we will catalog later: |
---|
| 42 | |
---|
| 43 | >>> from zope.interface import Interface, Attribute, implements |
---|
| 44 | >>> from zope.container.contained import Contained |
---|
| 45 | >>> class IMammoth(Interface): |
---|
| 46 | ... name = Attribute('name') |
---|
| 47 | ... age = Attribute('age') |
---|
| 48 | |
---|
| 49 | >>> class Mammoth(Contained): |
---|
| 50 | ... implements(IMammoth) |
---|
| 51 | ... def __init__(self, name, age): |
---|
| 52 | ... self.name = name |
---|
| 53 | ... self.age = age |
---|
| 54 | ... def __cmp__(self, other): |
---|
| 55 | ... return cmp(self.name, other.name) |
---|
| 56 | |
---|
| 57 | By including the __cmp__ method we make sure search results can be |
---|
| 58 | stably sorted. |
---|
| 59 | |
---|
| 60 | We also setup a `zope.intid.interfaces.IIntIds` utility. This is not |
---|
[7819] | 61 | necessary for plain catalogs, but when we want to use KofaQuery (or |
---|
[5092] | 62 | `hurry.query.query.Query` objects), as to get a unique mapping from |
---|
| 63 | objects (stored in ZODB) to integer numbers (stored in catalogs), |
---|
| 64 | these query objects lookup a global IIntIds utiliy: |
---|
| 65 | |
---|
| 66 | >>> from zope import interface |
---|
| 67 | >>> import zope.intid.interfaces |
---|
| 68 | >>> class DummyIntId(object): |
---|
| 69 | ... interface.implements(zope.intid.interfaces.IIntIds) |
---|
| 70 | ... MARKER = '__dummy_int_id__' |
---|
| 71 | ... def __init__(self): |
---|
| 72 | ... self.counter = 0 |
---|
| 73 | ... self.data = {} |
---|
| 74 | ... def register(self, obj): |
---|
| 75 | ... intid = getattr(obj, self.MARKER, None) |
---|
| 76 | ... if intid is None: |
---|
| 77 | ... setattr(obj, self.MARKER, self.counter) |
---|
| 78 | ... self.data[self.counter] = obj |
---|
| 79 | ... intid = self.counter |
---|
| 80 | ... self.counter += 1 |
---|
| 81 | ... return intid |
---|
| 82 | ... def getObject(self, intid): |
---|
| 83 | ... return self.data[intid] |
---|
| 84 | ... def __iter__(self): |
---|
| 85 | ... return iter(self.data) |
---|
| 86 | >>> intid = DummyIntId() |
---|
| 87 | >>> from zope.component import provideUtility |
---|
| 88 | >>> provideUtility(intid, zope.intid.interfaces.IIntIds) |
---|
| 89 | |
---|
| 90 | Now we can catalog some mammoths. Here we create a herd and catalog |
---|
| 91 | each item of it: |
---|
| 92 | |
---|
| 93 | >>> from zope.catalog.field import FieldIndex |
---|
| 94 | >>> mycat['mammoth_name'] = FieldIndex('name', IMammoth) |
---|
| 95 | >>> mycat['mammoth_age'] = FieldIndex('age', IMammoth) |
---|
| 96 | |
---|
| 97 | >>> herd = [ |
---|
| 98 | ... Mammoth(name='Fred', age=33), |
---|
| 99 | ... Mammoth(name='Hank', age=30), |
---|
| 100 | ... Mammoth(name='Wilma', age=28), |
---|
| 101 | ... ] |
---|
| 102 | |
---|
| 103 | >>> for mammoth in herd: |
---|
| 104 | ... mycat.index_doc(intid.register(mammoth), mammoth) |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | Searching for result sets |
---|
[12951] | 108 | ========================= |
---|
[5092] | 109 | |
---|
| 110 | Finally we can perform queries: |
---|
| 111 | |
---|
| 112 | >>> from hurry.query import Eq |
---|
| 113 | >>> from zope.component import getUtility |
---|
| 114 | >>> subquery1 = Eq(('mycatalog', 'mammoth_name'), 'Fred') |
---|
| 115 | |
---|
| 116 | The latter means: search for objects whose name is ``'Fred'`` in the |
---|
| 117 | ``mammoth_name`` index of a catalog registered as a utility named |
---|
| 118 | ``mycatalog``. |
---|
| 119 | |
---|
| 120 | >>> from hurry.query import Between |
---|
| 121 | >>> subquery2 = Between(('mycatalog', 'mammoth_age'), 30, 33) |
---|
| 122 | |
---|
| 123 | This means: ask for objects cataloged in an index named 'mammoth_age', |
---|
| 124 | whose cataloged value is between 30 and 33 (including this values). |
---|
| 125 | |
---|
| 126 | >>> r1 = q.apply(subquery2) |
---|
| 127 | >>> r1 |
---|
| 128 | IFSet([0, 1]) |
---|
| 129 | |
---|
| 130 | Using ``apply()`` above, we get a set of values stored in an |
---|
| 131 | ``IFBTree``: |
---|
| 132 | |
---|
| 133 | >>> type(r1) |
---|
| 134 | <type 'BTrees.IFBTree.IFSet'> |
---|
| 135 | |
---|
| 136 | ``IFBTree`` objects implement a rather efficient integer to float |
---|
| 137 | mapping where also integers are allowed as values. For each object |
---|
| 138 | found (i.e. mammoths whose age is between 30 and 33), we get the |
---|
| 139 | number of its entry. |
---|
| 140 | |
---|
| 141 | To get the real object, we can use intids here, because we setup an |
---|
| 142 | appropriate IIntIds utility before: |
---|
| 143 | |
---|
| 144 | >>> [intid.getObject(x).name for x in r1] |
---|
| 145 | ['Fred', 'Hank'] |
---|
| 146 | |
---|
| 147 | We can (and should) also use the `searchResults()` method explained |
---|
| 148 | below to do that. |
---|
| 149 | |
---|
| 150 | Retrieving BTree sets can, however, make sense, if you want to know |
---|
| 151 | only the number of results for a particular query or whether there are |
---|
| 152 | results at all in a more efficient way: |
---|
| 153 | |
---|
| 154 | >>> len(r1) |
---|
| 155 | 2 |
---|
| 156 | |
---|
| 157 | Searching for objects |
---|
[12951] | 158 | ===================== |
---|
[5092] | 159 | |
---|
| 160 | Very often we don't want to know the catalog-internal 'ids' of |
---|
| 161 | searched objects but the objects themselves. |
---|
| 162 | |
---|
| 163 | This can be done by using the ``searchResults`` method of |
---|
[7819] | 164 | ``KofaQuery``: |
---|
[5092] | 165 | |
---|
| 166 | >>> r2 = q.searchResults(subquery1) |
---|
| 167 | >>> r2 |
---|
| 168 | <zope.catalog.catalog.ResultSet instance at 0x...> |
---|
| 169 | |
---|
| 170 | >>> list(r2) |
---|
| 171 | [<Mammoth object at 0x...>] |
---|
| 172 | |
---|
| 173 | We got one result item, we can immediately ask for further infos. To |
---|
| 174 | access a result item by its index number, we have to turn the |
---|
| 175 | ResultSet into an ordinary list before: |
---|
| 176 | |
---|
| 177 | >>> entry = list(r2)[0] |
---|
| 178 | >>> entry.name, entry.age |
---|
| 179 | ('Fred', 33) |
---|
| 180 | |
---|
| 181 | We can also use ``subquery2`` as above: |
---|
| 182 | |
---|
| 183 | >>> r3 = q.searchResults(subquery2) |
---|
| 184 | >>> [(x.name, x.age) for x in r3] |
---|
| 185 | [('Fred', 33), ('Hank', 30)] |
---|
[5094] | 186 | |
---|
| 187 | or use both queries at once: |
---|
| 188 | |
---|
| 189 | >>> r4 = q.searchResults(subquery1 & subquery2) |
---|
| 190 | >>> [(x.name, x.age) for x in r4] |
---|
| 191 | [('Fred', 33)] |
---|
| 192 | |
---|
| 193 | which will give us, of course, the same result set as with subquery1. |
---|