From ab1cf32071fb3bc6f933cd88e0c56ae80370a487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Borgstr=C3=B6m?= Date: Sun, 9 Feb 2014 22:05:33 +0100 Subject: [PATCH] Implemented hashindex.iteritems(marker=X) --- attic/hashindex.pyx | 28 ++++++++++++++++++++-------- attic/testsuite/hashindex.py | 10 ++++++++++ 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/attic/hashindex.pyx b/attic/hashindex.pyx index e5c8aede0..6a359f2cf 100644 --- a/attic/hashindex.pyx +++ b/attic/hashindex.pyx @@ -105,17 +105,23 @@ cdef class NSIndex(IndexBase): data = hashindex_get(self.index, key) return data != NULL - def iteritems(self, marker=None, limit=0): + def iteritems(self, marker=None): + cdef const void *key iter = NSKeyIterator() iter.idx = self iter.index = self.index + if marker: + key = hashindex_get(self.index, marker) + if marker is None: + raise IndexError + iter.key = key - 32 return iter cdef class NSKeyIterator: cdef NSIndex idx cdef HashIndex *index - cdef char *key + cdef const void *key def __cinit__(self): self.key = NULL @@ -124,11 +130,11 @@ cdef class NSKeyIterator: return self def __next__(self): - self.key = hashindex_next_key(self.index, self.key) + self.key = hashindex_next_key(self.index, self.key) if not self.key: raise StopIteration cdef int *value = (self.key + 32) - return self.key[:32], (_le32toh(value[0]), _le32toh(value[1])) + return (self.key)[:32], (_le32toh(value[0]), _le32toh(value[1])) cdef class ChunkIndex(IndexBase): @@ -156,17 +162,23 @@ cdef class ChunkIndex(IndexBase): data = hashindex_get(self.index, key) return data != NULL - def iteritems(self, marker=None, limit=0): + def iteritems(self, marker=None): + cdef const void *key iter = ChunkKeyIterator() iter.idx = self iter.index = self.index + if marker: + key = hashindex_get(self.index, marker) + if marker is None: + raise IndexError + iter.key = key - 32 return iter cdef class ChunkKeyIterator: cdef ChunkIndex idx cdef HashIndex *index - cdef char *key + cdef const void *key def __cinit__(self): self.key = NULL @@ -175,8 +187,8 @@ cdef class ChunkKeyIterator: return self def __next__(self): - self.key = hashindex_next_key(self.index, self.key) + self.key = hashindex_next_key(self.index, self.key) if not self.key: raise StopIteration cdef int *value = (self.key + 32) - return self.key[:32], (_le32toh(value[0]), _le32toh(value[1]), _le32toh(value[2])) + return (self.key)[:32], (_le32toh(value[0]), _le32toh(value[1]), _le32toh(value[2])) diff --git a/attic/testsuite/hashindex.py b/attic/testsuite/hashindex.py index d14354774..b33ab9930 100644 --- a/attic/testsuite/hashindex.py +++ b/attic/testsuite/hashindex.py @@ -76,3 +76,13 @@ def test_read_only(self): idx2 = NSIndex(idx_name.name, readonly=True) self.assert_equal(idx2[bytes('%-0.32d' % 99, 'ascii')], (99, 99)) + def test_iteritems(self): + idx_name = tempfile.NamedTemporaryFile() + idx = NSIndex.create(idx_name.name) + for x in range(100): + idx[bytes('%-0.32d' % x, 'ascii')] = x, x + all = list(idx.iteritems()) + self.assert_equal(len(all), 100) + second_half = list(idx.iteritems(marker=all[49][0])) + self.assert_equal(len(second_half), 50) + self.assert_equal(second_half, all[50:])