-
Notifications
You must be signed in to change notification settings - Fork 3
/
example_test.go
59 lines (48 loc) · 1.15 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package rsync_test
import (
"fmt"
"io"
"log"
"strings"
"github.com/minio/rsync-go"
)
func ExampleRsync() {
oldReader := strings.NewReader("I am the original content")
rs := &rsync.RSync{}
// here we store the whole signature in a byte slice,
// but it could just as well be sent over a network connection for example
sig := make([]rsync.BlockHash, 0, 10)
writeSignature := func(bl rsync.BlockHash) error {
sig = append(sig, bl)
return nil
}
err := rs.CreateSignature(oldReader, writeSignature)
if err != nil {
log.Fatal(err)
}
var currentReader io.Reader
currentReader = strings.NewReader("I am the new content")
opsOut := make(chan rsync.Operation)
writeOperation := func(op rsync.Operation) error {
opsOut <- op
return nil
}
go func() {
defer close(opsOut)
err := rs.CreateDelta(currentReader, sig, writeOperation)
if err != nil {
log.Fatal(err)
}
}()
var newWriter strings.Builder
_, err = oldReader.Seek(0, io.SeekStart)
if err != nil {
log.Fatal(err)
}
err = rs.ApplyDelta(&newWriter, oldReader, opsOut)
if err != nil {
log.Fatal(err)
}
fmt.Println(newWriter.String())
// Output: I am the new content
}