Ver código fonte

Implemented hashindex.iteritems(marker=X)

Jonas Borgström 11 anos atrás
pai
commit
ab1cf32071
2 arquivos alterados com 30 adições e 8 exclusões
  1. 20 8
      attic/hashindex.pyx
  2. 10 0
      attic/testsuite/hashindex.py

+ 20 - 8
attic/hashindex.pyx

@@ -105,17 +105,23 @@ cdef class NSIndex(IndexBase):
         data = <int *>hashindex_get(self.index, <char *>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, <char *>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 = <char *>hashindex_next_key(self.index, <char *>self.key)
+        self.key = hashindex_next_key(self.index, <char *>self.key)
         if not self.key:
             raise StopIteration
         cdef int *value = <int *>(self.key + 32)
-        return self.key[:32], (_le32toh(value[0]), _le32toh(value[1]))
+        return (<char *>self.key)[:32], (_le32toh(value[0]), _le32toh(value[1]))
 
 
 cdef class ChunkIndex(IndexBase):
@@ -156,17 +162,23 @@ cdef class ChunkIndex(IndexBase):
         data = <int *>hashindex_get(self.index, <char *>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, <char *>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 = <char *>hashindex_next_key(self.index, <char *>self.key)
+        self.key = hashindex_next_key(self.index, <char *>self.key)
         if not self.key:
             raise StopIteration
         cdef int *value = <int *>(self.key + 32)
-        return self.key[:32], (_le32toh(value[0]), _le32toh(value[1]), _le32toh(value[2]))
+        return (<char *>self.key)[:32], (_le32toh(value[0]), _le32toh(value[1]), _le32toh(value[2]))

+ 10 - 0
attic/testsuite/hashindex.py

@@ -76,3 +76,13 @@ class HashIndexTestCase(AtticTestCase):
         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:])