Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/borg/hashindex.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ cdef class NSIndex(IndexBase):
def __getitem__(self, key):
assert len(key) == self.key_size
data = <uint32_t *>hashindex_get(self.index, <unsigned char *>key)
if not data:
if data == NULL:
raise KeyError(key)
cdef uint32_t segment = _le32toh(data[0])
assert segment <= _MAX_VALUE, "maximum number of segments reached"
Expand Down Expand Up @@ -237,8 +237,8 @@ cdef class NSIndex(IndexBase):
iter.index = self.index
if marker:
key = hashindex_get(self.index, <unsigned char *>marker)
if marker is None:
raise IndexError
if key == NULL:
raise KeyError("marker not found")
iter.key = key - self.key_size
return iter

Expand Down Expand Up @@ -296,7 +296,7 @@ cdef class ChunkIndex(IndexBase):
def __getitem__(self, key):
assert len(key) == self.key_size
data = <uint32_t *>hashindex_get(self.index, <unsigned char *>key)
if not data:
if data == NULL:
raise KeyError(key)
cdef uint32_t refcount = _le32toh(data[0])
assert refcount <= _MAX_VALUE, "invalid reference count"
Expand Down Expand Up @@ -324,7 +324,7 @@ cdef class ChunkIndex(IndexBase):
"""Increase refcount for 'key', return (refcount, size, csize)."""
assert len(key) == self.key_size
data = <uint32_t *>hashindex_get(self.index, <unsigned char *>key)
if not data:
if data == NULL:
raise KeyError(key)
cdef uint32_t refcount = _le32toh(data[0])
assert refcount <= _MAX_VALUE, "invalid reference count"
Expand All @@ -337,7 +337,7 @@ cdef class ChunkIndex(IndexBase):
"""Decrease refcount for 'key', return (refcount, size, csize)."""
assert len(key) == self.key_size
data = <uint32_t *>hashindex_get(self.index, <unsigned char *>key)
if not data:
if data == NULL:
raise KeyError(key)
cdef uint32_t refcount = _le32toh(data[0])
# Never decrease a reference count of zero
Expand All @@ -354,8 +354,8 @@ cdef class ChunkIndex(IndexBase):
iter.index = self.index
if marker:
key = hashindex_get(self.index, <unsigned char *>marker)
if marker is None:
raise IndexError
if key == NULL:
raise KeyError("marker not found")
iter.key = key - self.key_size
return iter

Expand Down Expand Up @@ -406,7 +406,7 @@ cdef class ChunkIndex(IndexBase):
break
our_values = <const uint32_t*> (key + self.key_size)
master_values = <const uint32_t*> hashindex_get(master, key)
if not master_values:
if master_values == NULL:
raise ValueError('stats_against: key contained in self but not in master_index.')
our_refcount = _le32toh(our_values[0])
chunk_size = _le32toh(master_values[1])
Expand Down Expand Up @@ -434,7 +434,7 @@ cdef class ChunkIndex(IndexBase):
cdef _add(self, unsigned char *key, uint32_t *data):
cdef uint64_t refcount1, refcount2, result64
values = <uint32_t*> hashindex_get(self.index, key)
if values:
if values != NULL:
refcount1 = _le32toh(values[0])
refcount2 = _le32toh(data[0])
assert refcount1 <= _MAX_VALUE, "invalid reference count"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,37 @@

import pytest

from ..hashindex import NSIndex
from borg.hashindex import NSIndex, ChunkIndex


def test_nsindex_iteritems_marker():
nsindex = NSIndex()
nsindex[b'\xbb'*32] = (123, 456)
nsindex[b'\xaa'*32] = (234, 567)

# marker exists
items = list(nsindex.iteritems(marker=b'\xbb'*32))
assert len(items) == 1
assert items[0][0] == b'\xaa'*32

# marker does not exist
with pytest.raises(KeyError, match="marker not found"):
list(nsindex.iteritems(marker=b'\xcc'*32))


def test_chunkindex_iteritems_marker():
chunkindex = ChunkIndex()
chunkindex[b'\xbb'*32] = (1, 100, 50)
chunkindex[b'\xaa'*32] = (1, 200, 100)

# marker exists
items = list(chunkindex.iteritems(marker=b'\xbb'*32))
assert len(items) == 1
assert items[0][0] == b'\xaa'*32

# marker does not exist
with pytest.raises(KeyError, match="marker not found"):
list(chunkindex.iteritems(marker=b'\xcc'*32))


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