1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2024-12-24 08:45:13 +00:00

Implemented hashindex.iteritems(marker=X)

This commit is contained in:
Jonas Borgström 2014-02-09 22:05:33 +01:00
parent 1e4fd4e18a
commit ab1cf32071
2 changed files with 30 additions and 8 deletions

View file

@ -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]))

View file

@ -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:])