1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2025-02-23 22:51:35 +00:00

cached_hash is only used in archive, move it there

This commit is contained in:
Thomas Waldmann 2021-01-14 20:41:57 +01:00
parent e41dc6e96f
commit 8162e2e67b
2 changed files with 28 additions and 27 deletions

View file

@ -19,7 +19,7 @@
logger = create_logger() logger = create_logger()
from . import xattr from . import xattr
from .chunker import get_chunker, Chunk, cached_hash from .chunker import get_chunker, Chunk
from .cache import ChunkListEntry from .cache import ChunkListEntry
from .crypto.key import key_factory from .crypto.key import key_factory
from .compress import Compressor, CompressionSpec from .compress import Compressor, CompressionSpec
@ -41,6 +41,7 @@
from .helpers import os_open, flags_normal, flags_dir from .helpers import os_open, flags_normal, flags_dir
from .helpers import msgpack from .helpers import msgpack
from .helpers import sig_int from .helpers import sig_int
from .lrucache import LRUCache
from .patterns import PathPrefixPattern, FnmatchPattern, IECommand from .patterns import PathPrefixPattern, FnmatchPattern, IECommand
from .item import Item, ArchiveItem, ItemDiff from .item import Item, ArchiveItem, ItemDiff
from .platform import acl_get, acl_set, set_flags, get_flags, swidth, hostname from .platform import acl_get, acl_set, set_flags, get_flags, swidth, hostname
@ -1088,6 +1089,32 @@ def stat_attrs(self, st, path, fd=None):
return attrs return attrs
# remember a few recently used all-zero chunk hashes in this mapping.
# (hash_func, chunk_length) -> chunk_hash
# we play safe and have the hash_func in the mapping key, in case we
# have different hash_funcs within the same borg run.
zero_chunk_ids = LRUCache(10, dispose=lambda _: None)
def cached_hash(chunk, id_hash):
allocation = chunk.meta['allocation']
if allocation == CH_DATA:
data = chunk.data
chunk_id = id_hash(data)
elif allocation in (CH_HOLE, CH_ALLOC):
size = chunk.meta['size']
assert size <= len(zeros)
data = memoryview(zeros)[:size]
try:
chunk_id = zero_chunk_ids[(id_hash, size)]
except KeyError:
chunk_id = id_hash(data)
zero_chunk_ids[(id_hash, size)] = chunk_id
else:
raise ValueError('unexpected allocation type')
return chunk_id, data
class ChunksProcessor: class ChunksProcessor:
# Processes an iterator of chunks for an Item # Processes an iterator of chunks for an Item

View file

@ -7,7 +7,6 @@ import os
from collections import namedtuple from collections import namedtuple
from .constants import CH_DATA, CH_ALLOC, CH_HOLE, MAX_DATA_SIZE, zeros from .constants import CH_DATA, CH_ALLOC, CH_HOLE, MAX_DATA_SIZE, zeros
from .lrucache import LRUCache
from libc.stdlib cimport free from libc.stdlib cimport free
@ -53,31 +52,6 @@ def Chunk(data, **meta):
return _Chunk(meta, data) return _Chunk(meta, data)
# remember a few recently used all-zero chunk hashes in this mapping.
# (hash_func, chunk_length) -> chunk_hash
# we play safe and have the hash_func in the mapping key, in case we
# have different hash_funcs within the same borg run.
zero_chunk_ids = LRUCache(10, dispose=lambda _: None)
def cached_hash(chunk, id_hash):
allocation = chunk.meta['allocation']
if allocation == CH_DATA:
data = chunk.data
chunk_id = id_hash(data)
elif allocation in (CH_HOLE, CH_ALLOC):
size = chunk.meta['size']
assert size <= len(zeros)
data = memoryview(zeros)[:size]
try:
chunk_id = zero_chunk_ids[(id_hash, size)]
except KeyError:
chunk_id = id_hash(data)
zero_chunk_ids[(id_hash, size)] = chunk_id
else:
raise ValueError('unexpected allocation type')
return chunk_id, data
def dread(offset, size, fd=None, fh=-1): def dread(offset, size, fd=None, fh=-1):
use_fh = fh >= 0 use_fh = fh >= 0
if use_fh: if use_fh: