Skip to content

Commit

Permalink
Add getCounter logic to PathIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
andbag committed Apr 3, 2018
1 parent 627b238 commit 90ac2c0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Products/PluginIndexes/PathIndex/PathIndex.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@
IQueryIndex,
ISortIndex,
IUniqueValueIndex,
IIndexCounter,
)
from Products.PluginIndexes.util import safe_callable
from Products.ZCatalog.query import IndexQuery

LOG = getLogger('Zope.PathIndex')


@implementer(IPathIndex, IQueryIndex, IUniqueValueIndex, ISortIndex)
@implementer(IPathIndex, IQueryIndex, IUniqueValueIndex,
ISortIndex, IIndexCounter)
class PathIndex(Persistent, SimpleItem):

"""Index for paths returned by getPhysicalPath.
Expand All @@ -60,6 +62,7 @@ class PathIndex(Persistent, SimpleItem):
operators = ('or', 'and')
useOperator = 'or'
query_options = ('query', 'level', 'operator')
_counter = None

manage_options = (
{'label': 'Settings', 'action': 'manage_main'},
Expand Down Expand Up @@ -128,6 +131,7 @@ def index_object(self, docid, obj, threshold=100):
for i in range(len(comps)):
self.insertEntry(comps[i], docid, i)
self._unindex[docid] = path
self._increment_counter()
return 1

def unindex_object(self, docid):
Expand All @@ -154,6 +158,7 @@ def unindex_object(self, docid):
% docid)

self._length.change(-1)
self._increment_counter()
del self._unindex[docid]

def _apply_index(self, request):
Expand Down Expand Up @@ -186,6 +191,15 @@ def query_index(self, record, resultset=None):
return res
return IISet()

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):
""" See IPluggableIndex.
"""
Expand All @@ -203,6 +217,10 @@ def clear(self):
self._index = OOBTree()
self._unindex = IOBTree()
self._length = Length(0)
if self._counter is None:
self._counter = Length(0)
else:
self._increment_counter()

# IUniqueValueIndex implementation

Expand Down
20 changes: 20 additions & 0 deletions src/Products/PluginIndexes/PathIndex/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,3 +533,23 @@ def test__search_w_level_0(self):
self.assertEqual(list(index._search('bb', 1)), [1])
self.assertEqual(list(index._search('aa/bb', 0)), [1])
self.assertEqual(list(index._search('aa/bb', 1)), [])

def test_getCounter(self):
index = self._makeOne()

self.assertEqual(index.getCounter(), 0)

doc = Dummy('/aa/bb')
index.index_object(1, doc)
self.assertEqual(index.getCounter(), 1)

index.unindex_object(1)
self.assertEqual(index.getCounter(), 2)

# unknown id
index.unindex_object(1)
self.assertEqual(index.getCounter(), 2)

# clear changes the index
index.clear()
self.assertEqual(index.getCounter(), 3)

0 comments on commit 90ac2c0

Please sign in to comment.