diff options
-rw-r--r-- | chunk.go | 17 | ||||
-rw-r--r-- | delta.go | 32 | ||||
-rw-r--r-- | repo.go | 18 |
3 files changed, 67 insertions, 0 deletions
@@ -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) +} @@ -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 |