Make the hasher var private in the rsync API

This commit is contained in:
Kovid Goyal 2023-07-05 22:27:16 +05:30
parent ca835cd80d
commit 6c10528cbd
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
2 changed files with 24 additions and 14 deletions

View file

@ -156,9 +156,14 @@ type RSync struct {
BlockSize int
// This must be non-nil before using any functions
UniqueHasher hash.Hash
hasher hash.Hash
hasher_constructor func() hash.Hash
buffer []byte
}
buffer []byte
func (r *RSync) SetHasher(c func() hash.Hash) {
r.hasher_constructor = c
r.hasher = c()
}
// If the target length is known the number of hashes in the
@ -525,8 +530,8 @@ func (r *RSync) CreateDiff(source io.Reader, signature []BlockHash) func() (*Ope
ans := &diff{
block_size: r.BlockSize, buffer: make([]byte, 0, (r.BlockSize * 8)),
hash_lookup: make(map[uint32][]BlockHash, len(signature)),
source: source, hasher: r.UniqueHasher,
hash_buf: make([]byte, 0, r.UniqueHasher.Size()),
source: source, hasher: r.hasher_constructor(),
hash_buf: make([]byte, 0, r.hasher.Size()),
}
for _, h := range signature {
key := h.WeakHash
@ -552,11 +557,15 @@ func (r *RSync) CreateDelta(source io.Reader, signature []BlockHash, ops Operati
// Use a more unique way to identify a set of bytes.
func (r *RSync) hash(v []byte) []byte {
r.UniqueHasher.Reset()
r.UniqueHasher.Write(v)
return r.UniqueHasher.Sum(nil)
r.hasher.Reset()
r.hasher.Write(v)
return r.hasher.Sum(nil)
}
func (r *RSync) HashSize() int { return r.hasher.Size() }
func (r *RSync) HashBlockSize() int { return r.hasher.BlockSize() }
func (r *RSync) HasHasher() bool { return r.hasher != nil }
// Searches for a given strong hash among all strong hashes in this bucket.
func find_hash(hh []BlockHash, hv []byte) (uint64, bool) {
if len(hv) == 0 {