Skip to content

Commit

Permalink
convert DateRangeIndex/UUIDIndex uniqueValues methods into generators
Browse files Browse the repository at this point in the history
  • Loading branch information
hannosch committed Oct 17, 2013
1 parent 443798c commit cf33e0d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
30 changes: 13 additions & 17 deletions src/Products/PluginIndexes/DateRangeIndex/DateRangeIndex.py
Expand Up @@ -213,7 +213,7 @@ def uniqueValues(self, name=None, withLengths=0):
the form '(value, length)'.
"""
if not name in (self._since_field, self._until_field):
return []
raise StopIteration

if name == self._since_field:
t1 = self._since
Expand All @@ -222,26 +222,22 @@ def uniqueValues(self, name=None, withLengths=0):
t1 = self._until
t2 = self._until_only

result = []
if not withLengths:
result.extend(t1.keys())
result.extend(t2.keys())
else:
for key in t1.keys():
set = t1[key]
if isinstance(set, int):
length = 1
else:
length = len(set)
result.append((key, length))
yield key
for key in t2.keys():
set = t2[key]
if isinstance(set, int):
length = 1
yield key
else:
for key, value in t1.items():
if isinstance(value, int):
yield (key, 1)
else:
yield (key, len(value))
for key, value in t2.items():
if isinstance(value, int):
yield (key, 1)
else:
length = len(set)
result.append((key, length))
return tuple(result)
yield (key, len(value))

def _cache_key(self, catalog):
cid = catalog.getId()
Expand Down
4 changes: 2 additions & 2 deletions src/Products/PluginIndexes/DateRangeIndex/tests.py
Expand Up @@ -84,8 +84,8 @@ def test_empty(self):
self.assertTrue(empty.getEntryForObject(1234) is None)
empty.unindex_object(1234) # shouldn't throw

self.assertFalse(empty.uniqueValues('foo'))
self.assertFalse(empty.uniqueValues('foo', 1))
self.assertFalse(list(empty.uniqueValues('foo')))
self.assertFalse(list(empty.uniqueValues('foo', withLengths=True)))
self.assertTrue(empty._apply_index({'zed': 12345}) is None)

result, used = empty._apply_index({'empty': 12345})
Expand Down
11 changes: 7 additions & 4 deletions src/Products/PluginIndexes/UUIDIndex/UUIDIndex.py
Expand Up @@ -69,12 +69,15 @@ def uniqueValues(self, name=None, withLengths=0):
if name is None:
name = self.id
elif name != self.id:
return []
raise StopIteration

if not withLengths:
return tuple(self._index.keys())
# We know the length for each value is one
return [(k, 1) for k in self._index.keys()]
for key in self._index.keys():
yield key
else:
# We know the length for each value is one
for key in self._index.keys():
yield (key, 1)

def insertForwardIndexEntry(self, entry, documentId):
"""Take the entry provided and put it in the correct place
Expand Down

0 comments on commit cf33e0d

Please sign in to comment.