Make the rsync struct private

This commit is contained in:
Kovid Goyal
2023-07-05 22:32:05 +05:30
parent 6c10528cbd
commit 55f9359e1b
2 changed files with 17 additions and 17 deletions

View File

@@ -17,7 +17,7 @@ import (
"os" "os"
) )
// If no BlockSize is specified in the RSync instance, this value is used. // If no BlockSize is specified in the rsync instance, this value is used.
const DefaultBlockSize = 1024 * 6 const DefaultBlockSize = 1024 * 6
// Internal constant used in rolling checksum. // Internal constant used in rolling checksum.
@@ -150,9 +150,9 @@ type SignatureWriter func(bl BlockHash) error
type OperationWriter func(op Operation) error type OperationWriter func(op Operation) error
// Properties to use while working with the rsync algorithm. // Properties to use while working with the rsync algorithm.
// A single RSync should not be used concurrently as it may contain // A single rsync should not be used concurrently as it may contain
// internal buffers and hash sums. // internal buffers and hash sums.
type RSync struct { type rsync struct {
BlockSize int BlockSize int
// This must be non-nil before using any functions // This must be non-nil before using any functions
@@ -161,14 +161,14 @@ type RSync struct {
buffer []byte buffer []byte
} }
func (r *RSync) SetHasher(c func() hash.Hash) { func (r *rsync) SetHasher(c func() hash.Hash) {
r.hasher_constructor = c r.hasher_constructor = c
r.hasher = c() r.hasher = c()
} }
// If the target length is known the number of hashes in the // If the target length is known the number of hashes in the
// signature can be determined. // signature can be determined.
func (r *RSync) BlockHashCount(targetLength int64) (count int64) { func (r *rsync) BlockHashCount(targetLength int64) (count int64) {
bs := int64(r.BlockSize) bs := int64(r.BlockSize)
count = targetLength / bs count = targetLength / bs
if targetLength%bs != 0 { if targetLength%bs != 0 {
@@ -178,7 +178,7 @@ func (r *RSync) BlockHashCount(targetLength int64) (count int64) {
} }
// Calculate the signature of target. // Calculate the signature of target.
func (r *RSync) CreateSignature(target io.Reader, sw SignatureWriter) error { func (r *rsync) CreateSignature(target io.Reader, sw SignatureWriter) error {
var err error var err error
var n int var n int
@@ -217,7 +217,7 @@ func (r *RSync) CreateSignature(target io.Reader, sw SignatureWriter) error {
} }
// Apply the difference to the target. // Apply the difference to the target.
func (r *RSync) ApplyDelta(alignedTarget io.Writer, target io.ReadSeeker, op Operation) error { func (r *rsync) ApplyDelta(alignedTarget io.Writer, target io.ReadSeeker, op Operation) error {
var err error var err error
var n int var n int
var block []byte var block []byte
@@ -274,7 +274,7 @@ func (r *RSync) ApplyDelta(alignedTarget io.Writer, target io.ReadSeeker, op Ope
return nil return nil
} }
func (r *RSync) set_buffer_to_size(sz int) { func (r *rsync) set_buffer_to_size(sz int) {
if cap(r.buffer) < sz { if cap(r.buffer) < sz {
r.buffer = make([]byte, sz) r.buffer = make([]byte, sz)
} else { } else {
@@ -526,7 +526,7 @@ func (self *diff) read_at_least_one_operation() (err error) {
return nil return nil
} }
func (r *RSync) CreateDiff(source io.Reader, signature []BlockHash) func() (*Operation, error) { func (r *rsync) CreateDiff(source io.Reader, signature []BlockHash) func() (*Operation, error) {
ans := &diff{ ans := &diff{
block_size: r.BlockSize, buffer: make([]byte, 0, (r.BlockSize * 8)), block_size: r.BlockSize, buffer: make([]byte, 0, (r.BlockSize * 8)),
hash_lookup: make(map[uint32][]BlockHash, len(signature)), hash_lookup: make(map[uint32][]BlockHash, len(signature)),
@@ -541,7 +541,7 @@ func (r *RSync) CreateDiff(source io.Reader, signature []BlockHash) func() (*Ope
return ans.Next return ans.Next
} }
func (r *RSync) CreateDelta(source io.Reader, signature []BlockHash, ops OperationWriter) (err error) { func (r *rsync) CreateDelta(source io.Reader, signature []BlockHash, ops OperationWriter) (err error) {
diff := r.CreateDiff(source, signature) diff := r.CreateDiff(source, signature)
var op *Operation var op *Operation
for { for {
@@ -556,15 +556,15 @@ func (r *RSync) CreateDelta(source io.Reader, signature []BlockHash, ops Operati
} }
// Use a more unique way to identify a set of bytes. // Use a more unique way to identify a set of bytes.
func (r *RSync) hash(v []byte) []byte { func (r *rsync) hash(v []byte) []byte {
r.hasher.Reset() r.hasher.Reset()
r.hasher.Write(v) r.hasher.Write(v)
return r.hasher.Sum(nil) return r.hasher.Sum(nil)
} }
func (r *RSync) HashSize() int { return r.hasher.Size() } func (r *rsync) HashSize() int { return r.hasher.Size() }
func (r *RSync) HashBlockSize() int { return r.hasher.BlockSize() } func (r *rsync) HashBlockSize() int { return r.hasher.BlockSize() }
func (r *RSync) HasHasher() bool { return r.hasher != nil } func (r *rsync) HasHasher() bool { return r.hasher != nil }
// Searches for a given strong hash among all strong hashes in this bucket. // Searches for a given strong hash among all strong hashes in this bucket.
func find_hash(hh []BlockHash, hv []byte) (uint64, bool) { func find_hash(hh []BlockHash, hv []byte) (uint64, bool) {

View File

@@ -24,11 +24,11 @@ const (
XXH3 StrongHashType = iota XXH3 StrongHashType = iota
) )
const ( const (
Beta WeakHashType = iota Rsync WeakHashType = iota
) )
type Api struct { type Api struct {
rsync RSync rsync rsync
signature []BlockHash signature []BlockHash
Strong_hash_type StrongHashType Strong_hash_type StrongHashType
@@ -65,7 +65,7 @@ func (self *Api) read_signature_header(data []byte) (consumed int, err error) {
return consumed, fmt.Errorf("Invalid strong_hash in signature header: %d", strong_hash) return consumed, fmt.Errorf("Invalid strong_hash in signature header: %d", strong_hash)
} }
switch weak_hash := WeakHashType(bin.Uint16(data[6:])); weak_hash { switch weak_hash := WeakHashType(bin.Uint16(data[6:])); weak_hash {
case Beta: case Rsync:
self.Weak_hash_type = weak_hash self.Weak_hash_type = weak_hash
default: default:
return consumed, fmt.Errorf("Invalid weak_hash in signature header: %d", weak_hash) return consumed, fmt.Errorf("Invalid weak_hash in signature header: %d", weak_hash)