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
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 {

View File

@@ -4,6 +4,7 @@ package rsync
import (
"fmt"
"hash"
"io"
"math"
@@ -59,7 +60,7 @@ func (self *Api) read_signature_header(data []byte) (consumed int, err error) {
switch strong_hash := StrongHashType(bin.Uint16(data[4:])); strong_hash {
case XXH3:
self.Strong_hash_type = strong_hash
self.rsync.UniqueHasher = xxh3.New()
self.rsync.SetHasher(func() hash.Hash { return xxh3.New() })
default:
return consumed, fmt.Errorf("Invalid strong_hash in signature header: %d", strong_hash)
}
@@ -83,7 +84,7 @@ func (self *Api) read_signature_header(data []byte) (consumed int, err error) {
}
func (self *Api) read_signature_blocks(data []byte) (consumed int) {
hash_size := self.rsync.UniqueHasher.Size()
hash_size := self.rsync.HashSize()
block_hash_size := hash_size + 12
for ; len(data) >= block_hash_size; data = data[block_hash_size:] {
bl := BlockHash{}
@@ -99,7 +100,7 @@ func (self *Differ) finish_signature_data() (err error) {
return fmt.Errorf("There were %d leftover bytes in the signature data", len(self.unconsumed_signature_data))
}
self.unconsumed_signature_data = nil
if self.rsync.UniqueHasher == nil {
if !self.rsync.HasHasher() {
return fmt.Errorf("No header was found in the signature data")
}
return
@@ -211,7 +212,7 @@ func (self *Differ) CreateDelta(src io.Reader) DeltaIterator {
// Add more external signature data
func (self *Differ) AddSignatureData(data []byte) (err error) {
self.unconsumed_signature_data = append(self.unconsumed_signature_data, data...)
if self.rsync.UniqueHasher == nil {
if !self.rsync.HasHasher() {
consumed, err := self.read_signature_header(self.unconsumed_signature_data)
if err != nil {
if consumed < 0 {
@@ -240,10 +241,10 @@ func NewPatcher(expected_input_size int64) (ans *Patcher) {
}
ans = &Patcher{}
ans.rsync.BlockSize = utils.Min(bs, MaxBlockSize)
ans.rsync.UniqueHasher = xxh3.New()
ans.rsync.SetHasher(func() hash.Hash { return xxh3.New() })
if ans.rsync.UniqueHasher.BlockSize() > 0 && ans.rsync.UniqueHasher.BlockSize() < ans.rsync.BlockSize {
ans.rsync.BlockSize = (ans.rsync.BlockSize / ans.rsync.UniqueHasher.BlockSize()) * ans.rsync.UniqueHasher.BlockSize()
if ans.rsync.HashBlockSize() > 0 && ans.rsync.HashBlockSize() < ans.rsync.BlockSize {
ans.rsync.BlockSize = (ans.rsync.BlockSize / ans.rsync.HashBlockSize()) * ans.rsync.HashBlockSize()
}
ans.expected_input_size_for_signature_generation = sz