Skip to content

Commit

Permalink
Add getCounter logic
Browse files Browse the repository at this point in the history
  • Loading branch information
andbag committed Mar 31, 2016
1 parent b9041a6 commit 937d82f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/Products/PluginIndexes/common/UnIndex.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class UnIndex(SimpleItem):
"""Simple forward and reverse index.
"""
implements(ILimitedResultIndex, IUniqueValueIndex, ISortIndex)
_counter = None

def __init__(
self, id, ignore_ex=None, call_methods=None, extra=None, caller=None):
Expand Down Expand Up @@ -117,6 +118,7 @@ def clear(self):
self._length = Length()
self._index = OOBTree()
self._unindex = IOBTree()
self._counter = Length()

def __nonzero__(self):
return not not self._unindex
Expand Down Expand Up @@ -175,14 +177,16 @@ def removeForwardIndexEntry(self, entry, documentId):
except Exception:
LOG.error('%s: unindex_object could not remove '
'documentId %s from index %s. This '
'should not happen.' % (self.__class__.__name__,
'should not happen.' %
(self.__class__.__name__,
str(documentId), str(self.id)),
exc_info=sys.exc_info())
exc_info=sys.exc_info())
else:
LOG.error('%s: unindex_object tried to retrieve set %s '
'from index %s but couldn\'t. This '
'should not happen.' % (self.__class__.__name__,
repr(entry), str(self.id)))
'should not happen.' %
(self.__class__.__name__,
repr(entry), str(self.id)))

def insertForwardIndexEntry(self, entry, documentId):
"""Take the entry provided and put it in the correct place
Expand Down Expand Up @@ -210,6 +214,7 @@ def insertForwardIndexEntry(self, entry, documentId):

def index_object(self, documentId, obj, threshold=None):
""" wrapper to handle indexing of multiple attributes """
self._increment_counter()
fields = self.getIndexSourceNames()
res = 0
for attr in fields:
Expand Down Expand Up @@ -242,7 +247,7 @@ def _index_object(self, documentId, obj, threshold=None, attr=''):
raise
except Exception:
LOG.error('Should not happen: oldDatum was there, '
'now its not, for document: %s' % documentId)
'now its not, for document: %s' % documentId)

if datum is not _marker:
self.insertForwardIndexEntry(datum, documentId)
Expand All @@ -264,6 +269,14 @@ def _get_object_datum(self, obj, attr):
datum = _marker
return datum

def _increment_counter(self):
if self._counter is None:
self._counter = Length()
self._counter.change(1)

def getCounter(self):
return self._counter is not None and self._counter() or 0

def numObjects(self):
"""Return the number of indexed objects."""
return len(self._unindex)
Expand All @@ -276,6 +289,7 @@ def unindex_object(self, documentId):
""" Unindex the object with integer id 'documentId' and don't
raise an exception if we fail
"""
self._increment_counter()
unindexRecord = self._unindex.get(documentId, _marker)
if unindexRecord is _marker:
return None
Expand Down
18 changes: 18 additions & 0 deletions src/Products/PluginIndexes/common/tests/test_UnIndex.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,21 @@ def interesting(self):

dummy.exc = TypeError
self.assertEquals(idx._get_object_datum(dummy, 'interesting'), _marker)

def test_getCounter(self):
idx = self._makeOne('counter')

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

class DummyContent(object):
counter = 'first'
dummy = DummyContent()

idx.index_object(dummy)
self.assertEqual(idx.getCounter(), 1)

idx.unindex_object(dummy)
self.assertEqual(idx.getCounter(), 2)

idx.clear()
self.assertEqual(idx.getCounter(), 0)

0 comments on commit 937d82f

Please sign in to comment.