From e207ff4759e9e2aba9b4938f54ed2f31d362f7ee Mon Sep 17 00:00:00 2001 From: n-peugnet Date: Tue, 31 Aug 2021 17:45:07 +0200 Subject: fix matchStream if first chunk is not matched --- repo.go | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) (limited to 'repo.go') diff --git a/repo.go b/repo.go index d32d437..a987dc6 100644 --- a/repo.go +++ b/repo.go @@ -265,24 +265,18 @@ func findSimilarChunk(chunk Chunk, sketches SketchMap) (*ChunkId, bool) { return similarChunk, similarChunk != nil } -func readChunk(stream io.Reader) ([]byte, error) { - buff := make([]byte, chunkSize) - _, err := io.ReadFull(stream, buff) - return buff, err -} - func (r *Repo) matchStream(stream io.Reader, fingerprints FingerprintMap) []Chunk { var b byte var chunks []Chunk bufStream := bufio.NewReaderSize(stream, chunkSize) - buff, err := readChunk(bufStream) - if err == io.EOF { - chunks = append(chunks, NewTempChunk(buff)) + buff := make([]byte, 0, chunkSize*2) + n, err := io.ReadFull(stream, buff[:chunkSize]) + if n < chunkSize { + chunks = append(chunks, NewTempChunk(buff[:n])) return chunks } hasher := rabinkarp64.New() - hasher.Write(buff) - buff = make([]byte, 0, chunkSize*2) + hasher.Write(buff[:n]) for err != io.EOF { h := hasher.Sum64() chunkId, exists := fingerprints[h] @@ -297,8 +291,8 @@ func (r *Repo) matchStream(stream io.Reader, fingerprints FingerprintMap) []Chun buff = make([]byte, 0, chunkSize*2) for i := 0; i < chunkSize && err == nil; i++ { b, err = bufStream.ReadByte() - buff = append(buff, b) hasher.Roll(b) + buff = append(buff, b) } continue } -- cgit v1.2.3