aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--chunk.go17
-rw-r--r--delta.go32
-rw-r--r--repo.go18
3 files changed, 67 insertions, 0 deletions
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