Skip to content

Commit

Permalink
Add Length object for index entries (enhancement of uniqueValues)
Browse files Browse the repository at this point in the history
  • Loading branch information
andbag committed Apr 11, 2019
1 parent 800f45d commit 879091f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/Products/PluginIndexes/DateIndex/DateIndex.py
Expand Up @@ -107,6 +107,7 @@ def clear(self):
self._index = IOBTree()
self._unindex = IIBTree()
self._length = Length()
self._entry_lengths = IOBTree()
if self._counter is None:
self._counter = Length()
else:
Expand Down
25 changes: 13 additions & 12 deletions src/Products/PluginIndexes/unindex.py
Expand Up @@ -129,6 +129,7 @@ def getId(self):

def clear(self):
self._length = Length()
self._entry_lengths = OOBTree()
self._index = OOBTree()
self._unindex = IOBTree()

Expand All @@ -145,12 +146,8 @@ def histogram(self):
elements found at each point in the index.
"""
histogram = {}
for item in self._index.items():
if isinstance(item, int):
entry = 1 # "set" length is 1
else:
key, value = item
entry = len(value)
for key in self._index.keys():
entry = self._entry_lengths[key].value
histogram[entry] = histogram.get(entry, 0) + 1
return histogram

Expand All @@ -176,13 +173,15 @@ def removeForwardIndexEntry(self, entry, documentId):
indexRow.remove(documentId)
if not indexRow:
del self._index[entry]
del self._entry_lengths[entry]
self._length.change(-1)
except ConflictError:
raise
except AttributeError:
# index row is an int
try:
del self._index[entry]
del self._entry_lengths[entry]
except KeyError:
# swallow KeyError because it was probably
# removed and then _length AttributeError raised
Expand Down Expand Up @@ -221,15 +220,19 @@ def insertForwardIndexEntry(self, entry, documentId):
# We always use a set to avoid getting conflict errors on
# multiple threads adding a new row at the same time
self._index[entry] = IITreeSet((documentId, ))
self._entry_lengths[entry] = Length(1)
self._length.change(1)
else:
try:
indexRow.insert(documentId)
success = indexRow.insert(documentId)
if success:
self._entry_lengths[entry].change(1)
except AttributeError:
# Inline migration: index row with one element was an int at
# first (before Zope 2.13).
indexRow = IITreeSet((indexRow, documentId))
self._index[entry] = indexRow
self._entry_lengths[entry] = Length(2)

def index_object(self, documentId, obj, threshold=None):
""" wrapper to handle indexing of multiple attributes """
Expand Down Expand Up @@ -682,11 +685,9 @@ def uniqueValues(self, name=None, withLengths=0):
for key in self._index.iterkeys():
yield key
else:
for key, value in self._index.iteritems():
if isinstance(value, int):
yield (key, 1)
else:
yield (key, len(value))
for key in self._index.iterkeys():
length = self._entry_lengths[key].value
yield (key, length)

def keyForDocument(self, id):
# This method is superseded by documentToKeyMap
Expand Down
2 changes: 1 addition & 1 deletion src/Products/ZCatalog/plan.py
Expand Up @@ -322,7 +322,7 @@ def stop_split(self, name, result=None, limit=False):
duration, hits, limit = benchmark[name]
duration = ((duration * hits) + dt) / float(hits + 1)
# reset adaption
if hits % REFRESH_RATE == 0:
if int(hits) % REFRESH_RATE == 0:
hits = 0
hits += 1
benchmark[name] = Benchmark(duration, hits, limit)
Expand Down

0 comments on commit 879091f

Please sign in to comment.