/
TopicIndex.py
245 lines (194 loc) · 7.85 KB
/
TopicIndex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
##############################################################################
#
# Copyright (c) 2002 Zope Foundation and Contributors.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
from logging import getLogger
from App.special_dtml import DTMLFile
from BTrees.IIBTree import IITreeSet
from BTrees.IIBTree import intersection
from BTrees.IIBTree import union
from BTrees.OOBTree import OOBTree
from BTrees.Length import Length
from OFS.SimpleItem import SimpleItem
from Persistence import Persistent
from zope.interface import implementer
from Products.PluginIndexes.interfaces import (
IQueryIndex,
ITopicIndex,
IIndexCounter,
)
from Products.PluginIndexes.TopicIndex.FilteredSet import factory
from Products.ZCatalog.query import IndexQuery
_marker = []
LOG = getLogger('Zope.TopicIndex')
@implementer(ITopicIndex, IQueryIndex, IIndexCounter)
class TopicIndex(Persistent, SimpleItem):
"""A TopicIndex maintains a set of FilteredSet objects.
Every FilteredSet object consists of an expression and and IISet with all
Ids of indexed objects that eval with this expression to 1.
"""
meta_type = "TopicIndex"
query_options = ('query', 'operator')
_counter = None
manage_options = (
{'label': 'FilteredSets', 'action': 'manage_main'},
)
def __init__(self, id, caller=None):
self.id = id
self.filteredSets = OOBTree()
self._counter = Length()
self.operators = ('or', 'and')
self.defaultOperator = 'or'
def getId(self):
return self.id
def clear(self):
for fs in self.filteredSets.values():
fs.clear()
self._increment_counter()
def index_object(self, docid, obj, threshold=100):
""" hook for (Z)Catalog """
res = 0
for fid, filteredSet in self.filteredSets.items():
res += filteredSet.index_object(docid, obj)
if res > 0:
self._increment_counter()
return 1
return 0
def unindex_object(self, docid):
""" hook for (Z)Catalog """
res = 0
for fs in self.filteredSets.values():
try:
fs.unindex_object(docid)
res += 1
except KeyError:
LOG.debug('Attempt to unindex document'
' with id %s failed' % docid)
if res > 0:
self._increment_counter()
return 1
return 0
def _increment_counter(self):
if self._counter is None:
self._counter = Length()
self._counter.change(1)
def getCounter(self):
"""Return a counter which is increased on index changes"""
return self._counter is not None and self._counter() or 0
def numObjects(self):
"""Return the number of indexed objects."""
return "n/a"
def indexSize(self):
"""Return the size of the index in terms of distinct values."""
return "n/a"
def search(self, filter_id):
f = self.filteredSets.get(filter_id, None)
if f is not None:
return f.getIds()
def _apply_index(self, request):
record = IndexQuery(request, self.id, self.query_options)
if record.keys is None:
return None
return (self.query_index(record), (self.id, ))
def query_index(self, record, resultset=None):
"""Hook for (Z)Catalog
'record' -- mapping type (usually {"topic": "..." }
"""
operator = record.get('operator', self.defaultOperator).lower()
if operator == 'or':
set_func = union
else:
set_func = intersection
res = None
for filter_id in record.keys:
rows = self.search(filter_id)
res = set_func(res, rows)
if res:
return res
return IITreeSet()
def uniqueValues(self, name=None, withLength=0):
""" needed to be consistent with the interface """
return self.filteredSets.keys()
def getEntryForObject(self, docid, default=_marker):
""" Takes a document ID and returns all the information we have
on that specific object.
"""
return self.filteredSets.keys()
def addFilteredSet(self, filter_id, typeFilteredSet, expr):
# Add a FilteredSet object.
if filter_id in self.filteredSets:
raise KeyError(
'A FilteredSet with this name already exists: %s' % filter_id)
self.filteredSets[filter_id] = factory(
filter_id, typeFilteredSet, expr)
def delFilteredSet(self, filter_id):
# Delete the FilteredSet object specified by 'filter_id'.
if filter_id not in self.filteredSets:
raise KeyError(
'no such FilteredSet: %s' % filter_id)
del self.filteredSets[filter_id]
def clearFilteredSet(self, filter_id):
# Clear the FilteredSet object specified by 'filter_id'.
f = self.filteredSets.get(filter_id, None)
if f is None:
raise KeyError('no such FilteredSet: %s' % filter_id)
f.clear()
def manage_addFilteredSet(self, filter_id, typeFilteredSet, expr, URL1,
REQUEST=None, RESPONSE=None):
""" add a new filtered set """
if len(filter_id) == 0:
raise RuntimeError('Length of ID too short')
if len(expr) == 0:
raise RuntimeError('Length of expression too short')
self.addFilteredSet(filter_id, typeFilteredSet, expr)
if RESPONSE:
RESPONSE.redirect(URL1 + (
'/manage_workspace?'
'manage_tabs_message=FilteredSet%20added'))
def manage_delFilteredSet(self, filter_ids=[], URL1=None,
REQUEST=None, RESPONSE=None):
""" delete a list of FilteredSets"""
for filter_id in filter_ids:
self.delFilteredSet(filter_id)
if RESPONSE:
RESPONSE.redirect(URL1 + (
'/manage_workspace?'
'manage_tabs_message=FilteredSet(s)%20deleted'))
def manage_saveFilteredSet(self, filter_id, expr, URL1=None,
REQUEST=None, RESPONSE=None):
""" save expression for a FilteredSet """
self.filteredSets[filter_id].setExpression(expr)
if RESPONSE:
RESPONSE.redirect(URL1 + (
'/manage_workspace?'
'manage_tabs_message=FilteredSet(s)%20updated'))
def getIndexSourceNames(self):
""" return names of indexed attributes """
return ('n/a', )
def getIndexQueryNames(self):
return (self.id,)
def manage_clearFilteredSet(self, filter_ids=[], URL1=None,
REQUEST=None, RESPONSE=None):
""" clear a list of FilteredSets"""
for filter_id in filter_ids:
self.clearFilteredSet(filter_id)
if RESPONSE:
RESPONSE.redirect(URL1 + (
'/manage_workspace?'
'manage_tabs_message=FilteredSet(s)%20cleared'))
manage = manage_main = DTMLFile('dtml/manageTopicIndex', globals())
manage_main._setName('manage_main')
editFilteredSet = DTMLFile('dtml/editFilteredSet', globals())
manage_addTopicIndexForm = DTMLFile('dtml/addTopicIndex', globals())
def manage_addTopicIndex(self, id, REQUEST=None, RESPONSE=None, URL3=None):
"""Add a TopicIndex"""
return self.manage_addIndex(id, 'TopicIndex', extra=None,
REQUEST=REQUEST, RESPONSE=RESPONSE, URL1=URL3)