mirror of
https://github.com/borgbackup/borg.git
synced 2024-12-25 09:19:31 +00:00
Implemented hashindex.iteritems(marker=X)
This commit is contained in:
parent
1e4fd4e18a
commit
ab1cf32071
2 changed files with 30 additions and 8 deletions
|
@ -105,17 +105,23 @@ cdef class NSIndex(IndexBase):
|
||||||
data = <int *>hashindex_get(self.index, <char *>key)
|
data = <int *>hashindex_get(self.index, <char *>key)
|
||||||
return data != NULL
|
return data != NULL
|
||||||
|
|
||||||
def iteritems(self, marker=None, limit=0):
|
def iteritems(self, marker=None):
|
||||||
|
cdef const void *key
|
||||||
iter = NSKeyIterator()
|
iter = NSKeyIterator()
|
||||||
iter.idx = self
|
iter.idx = self
|
||||||
iter.index = self.index
|
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
|
return iter
|
||||||
|
|
||||||
|
|
||||||
cdef class NSKeyIterator:
|
cdef class NSKeyIterator:
|
||||||
cdef NSIndex idx
|
cdef NSIndex idx
|
||||||
cdef HashIndex *index
|
cdef HashIndex *index
|
||||||
cdef char *key
|
cdef const void *key
|
||||||
|
|
||||||
def __cinit__(self):
|
def __cinit__(self):
|
||||||
self.key = NULL
|
self.key = NULL
|
||||||
|
@ -124,11 +130,11 @@ cdef class NSKeyIterator:
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __next__(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:
|
if not self.key:
|
||||||
raise StopIteration
|
raise StopIteration
|
||||||
cdef int *value = <int *>(self.key + 32)
|
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):
|
cdef class ChunkIndex(IndexBase):
|
||||||
|
@ -156,17 +162,23 @@ cdef class ChunkIndex(IndexBase):
|
||||||
data = <int *>hashindex_get(self.index, <char *>key)
|
data = <int *>hashindex_get(self.index, <char *>key)
|
||||||
return data != NULL
|
return data != NULL
|
||||||
|
|
||||||
def iteritems(self, marker=None, limit=0):
|
def iteritems(self, marker=None):
|
||||||
|
cdef const void *key
|
||||||
iter = ChunkKeyIterator()
|
iter = ChunkKeyIterator()
|
||||||
iter.idx = self
|
iter.idx = self
|
||||||
iter.index = self.index
|
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
|
return iter
|
||||||
|
|
||||||
|
|
||||||
cdef class ChunkKeyIterator:
|
cdef class ChunkKeyIterator:
|
||||||
cdef ChunkIndex idx
|
cdef ChunkIndex idx
|
||||||
cdef HashIndex *index
|
cdef HashIndex *index
|
||||||
cdef char *key
|
cdef const void *key
|
||||||
|
|
||||||
def __cinit__(self):
|
def __cinit__(self):
|
||||||
self.key = NULL
|
self.key = NULL
|
||||||
|
@ -175,8 +187,8 @@ cdef class ChunkKeyIterator:
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __next__(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:
|
if not self.key:
|
||||||
raise StopIteration
|
raise StopIteration
|
||||||
cdef int *value = <int *>(self.key + 32)
|
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]))
|
||||||
|
|
|
@ -76,3 +76,13 @@ def test_read_only(self):
|
||||||
idx2 = NSIndex(idx_name.name, readonly=True)
|
idx2 = NSIndex(idx_name.name, readonly=True)
|
||||||
self.assert_equal(idx2[bytes('%-0.32d' % 99, 'ascii')], (99, 99))
|
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:])
|
||||||
|
|
Loading…
Reference in a new issue