Skip to content

Commit 86f6b8e

Browse files
Merge pull request borgbackup#9370 from ThomasWaldmann/fix-hashindex-1.4
Fix hashindex .iteritems (1.4-maint)
2 parents b1aa1a4 + c5a7a3c commit 86f6b8e

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

src/borg/hashindex.pyx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ cdef class NSIndex(IndexBase):
205205
def __getitem__(self, key):
206206
assert len(key) == self.key_size
207207
data = <uint32_t *>hashindex_get(self.index, <unsigned char *>key)
208-
if not data:
208+
if data == NULL:
209209
raise KeyError(key)
210210
cdef uint32_t segment = _le32toh(data[0])
211211
assert segment <= _MAX_VALUE, "maximum number of segments reached"
@@ -237,8 +237,8 @@ cdef class NSIndex(IndexBase):
237237
iter.index = self.index
238238
if marker:
239239
key = hashindex_get(self.index, <unsigned char *>marker)
240-
if marker is None:
241-
raise IndexError
240+
if key == NULL:
241+
raise KeyError("marker not found")
242242
iter.key = key - self.key_size
243243
return iter
244244

@@ -296,7 +296,7 @@ cdef class ChunkIndex(IndexBase):
296296
def __getitem__(self, key):
297297
assert len(key) == self.key_size
298298
data = <uint32_t *>hashindex_get(self.index, <unsigned char *>key)
299-
if not data:
299+
if data == NULL:
300300
raise KeyError(key)
301301
cdef uint32_t refcount = _le32toh(data[0])
302302
assert refcount <= _MAX_VALUE, "invalid reference count"
@@ -324,7 +324,7 @@ cdef class ChunkIndex(IndexBase):
324324
"""Increase refcount for 'key', return (refcount, size, csize)."""
325325
assert len(key) == self.key_size
326326
data = <uint32_t *>hashindex_get(self.index, <unsigned char *>key)
327-
if not data:
327+
if data == NULL:
328328
raise KeyError(key)
329329
cdef uint32_t refcount = _le32toh(data[0])
330330
assert refcount <= _MAX_VALUE, "invalid reference count"
@@ -337,7 +337,7 @@ cdef class ChunkIndex(IndexBase):
337337
"""Decrease refcount for 'key', return (refcount, size, csize)."""
338338
assert len(key) == self.key_size
339339
data = <uint32_t *>hashindex_get(self.index, <unsigned char *>key)
340-
if not data:
340+
if data == NULL:
341341
raise KeyError(key)
342342
cdef uint32_t refcount = _le32toh(data[0])
343343
# Never decrease a reference count of zero
@@ -354,8 +354,8 @@ cdef class ChunkIndex(IndexBase):
354354
iter.index = self.index
355355
if marker:
356356
key = hashindex_get(self.index, <unsigned char *>marker)
357-
if marker is None:
358-
raise IndexError
357+
if key == NULL:
358+
raise KeyError("marker not found")
359359
iter.key = key - self.key_size
360360
return iter
361361

@@ -406,7 +406,7 @@ cdef class ChunkIndex(IndexBase):
406406
break
407407
our_values = <const uint32_t*> (key + self.key_size)
408408
master_values = <const uint32_t*> hashindex_get(master, key)
409-
if not master_values:
409+
if master_values == NULL:
410410
raise ValueError('stats_against: key contained in self but not in master_index.')
411411
our_refcount = _le32toh(our_values[0])
412412
chunk_size = _le32toh(master_values[1])
@@ -434,7 +434,7 @@ cdef class ChunkIndex(IndexBase):
434434
cdef _add(self, unsigned char *key, uint32_t *data):
435435
cdef uint64_t refcount1, refcount2, result64
436436
values = <uint32_t*> hashindex_get(self.index, key)
437-
if values:
437+
if values != NULL:
438438
refcount1 = _le32toh(values[0])
439439
refcount2 = _le32toh(data[0])
440440
assert refcount1 <= _MAX_VALUE, "invalid reference count"
Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,37 @@
33

44
import pytest
55

6-
from ..hashindex import NSIndex
6+
from borg.hashindex import NSIndex, ChunkIndex
7+
8+
9+
def test_nsindex_iteritems_marker():
10+
nsindex = NSIndex()
11+
nsindex[b'\xbb'*32] = (123, 456)
12+
nsindex[b'\xaa'*32] = (234, 567)
13+
14+
# marker exists
15+
items = list(nsindex.iteritems(marker=b'\xbb'*32))
16+
assert len(items) == 1
17+
assert items[0][0] == b'\xaa'*32
18+
19+
# marker does not exist
20+
with pytest.raises(KeyError, match="marker not found"):
21+
list(nsindex.iteritems(marker=b'\xcc'*32))
22+
23+
24+
def test_chunkindex_iteritems_marker():
25+
chunkindex = ChunkIndex()
26+
chunkindex[b'\xbb'*32] = (1, 100, 50)
27+
chunkindex[b'\xaa'*32] = (1, 200, 100)
28+
29+
# marker exists
30+
items = list(chunkindex.iteritems(marker=b'\xbb'*32))
31+
assert len(items) == 1
32+
assert items[0][0] == b'\xaa'*32
33+
34+
# marker does not exist
35+
with pytest.raises(KeyError, match="marker not found"):
36+
list(chunkindex.iteritems(marker=b'\xcc'*32))
737

838

939
@pytest.mark.skipif("BORG_TESTS_SLOW" not in os.environ, reason="slow tests not enabled, use BORG_TESTS_SLOW=1")

0 commit comments

Comments
 (0)