From 1dec2cdc84a6497c893d84485c6f94589f997215 Mon Sep 17 00:00:00 2001 From: n-peugnet Date: Wed, 1 Sep 2021 19:07:49 +0200 Subject: Prepare for delta encoding --- chunk.go | 17 +++++++++++++++++ delta.go | 32 ++++++++++++++++++++++++++++++++ repo.go | 18 ++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 delta.go diff --git a/chunk.go b/chunk.go index 9927c94..3fc9057 100644 --- a/chunk.go +++ b/chunk.go @@ -115,3 +115,20 @@ func (c *TempChunk) AppendFrom(r io.Reader) { } c.value = append(c.value, buff...) } + +type DeltaChunk struct { + repo *Repo + source *ChunkId + patch []byte + size int +} + +func (c *DeltaChunk) Reader() ChunkReader { + var buff bytes.Buffer + c.repo.Patcher().Patch(c.source.Reader(c.repo), &buff, bytes.NewReader(c.patch)) + return &buff +} + +func (c *DeltaChunk) Len() int { + return c.size +} diff --git a/delta.go b/delta.go new file mode 100644 index 0000000..423fb07 --- /dev/null +++ b/delta.go @@ -0,0 +1,32 @@ +package main + +import ( + "io" + + "github.com/gabstv/go-bsdiff/pkg/bsdiff" + "github.com/gabstv/go-bsdiff/pkg/bspatch" +) + +type DeltaCodec interface { + Differ + Patcher +} + +type Differ interface { + Diff(source io.Reader, target io.Reader, patch io.Writer) error +} + +type Patcher interface { + Patch(source io.Reader, target io.Writer, patch io.Reader) error +} + +// TODO: maybe move this in it own file ? +type Bsdiff struct{} + +func (*Bsdiff) Diff(source io.Reader, target io.Reader, patch io.Writer) error { + return bsdiff.Reader(source, target, patch) +} + +func (*Bsdiff) Patch(source io.Reader, target io.Writer, patch io.Reader) error { + return bspatch.Reader(source, target, patch) +} diff --git a/repo.go b/repo.go index 6f177c2..32666f4 100644 --- a/repo.go +++ b/repo.go @@ -48,6 +48,8 @@ type Repo struct { sketchWSize int sketchSfCount int sketchFCount int + differ Differ + patcher Patcher } type File struct { @@ -63,9 +65,19 @@ func NewRepo(path string) *Repo { sketchWSize: 32, sketchSfCount: 3, sketchFCount: 4, + differ: &Bsdiff{}, + patcher: &Bsdiff{}, } } +func (r *Repo) Differ() Differ { + return r.differ +} + +func (r *Repo) Patcher() Patcher { + return r.patcher +} + func (r *Repo) Commit(source string) { versions := r.loadVersions() newVersion := len(versions) @@ -275,6 +287,12 @@ func (r *Repo) findSimilarChunk(chunk Chunk, sketches SketchMap) (*ChunkId, bool return similarChunk, similarChunk != nil } +// TODO: encode stream +func (r *Repo) encodeStream(stream io.Reader, fingerprints FingerprintMap, sketches SketchMap) []Chunk { + chunks := r.matchStream(stream, fingerprints) + return chunks +} + func (r *Repo) matchStream(stream io.Reader, fingerprints FingerprintMap) []Chunk { var b byte var chunks []Chunk -- cgit v1.2.3