-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdata.go
241 lines (200 loc) · 6.29 KB
/
data.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2015 Monmohan Singh. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package xferspdy provides the basic interfaces around binary diff and patching process
package xferspdy
import (
"crypto/sha256"
"fmt"
"hash/adler32"
"io"
"os"
"sync"
"github.com/golang/glog"
)
var (
DEFAULT_GENERATOR = &FingerprintGenerator{ConcurrentMode: true, NumWorkers: 8}
)
type FingerprintGenerator struct {
Source io.Reader
BlockSize uint32
ConcurrentMode bool
NumWorkers int
}
// Block represent a byte slice from the file. For each block, following are computed.
//
// * Adler-32 and SHA256 checksum,
//
// * Start and End byte pos of the block,
//
// * Whether or not its a data block -If this is a data block, RawBytes will capture the byte data represented by this block
type Block struct {
Start, End int64
Checksum32 uint32
Sha256hash [sha256.Size]byte
HasData bool
RawBytes []byte
}
// Fingerprint of a given File, encapsulates the following mapping -
// Adler-32 hash of Block --> SHA256 hash of Block -->Block
// Also stores the block size and the source
type Fingerprint struct {
Blocksz uint32
BlockMap map[uint32]map[[sha256.Size]byte]Block
Source string
}
func (b Block) String() string {
return fmt.Sprintf("Start %d End %d adler %d HasData %v \n", b.Start, b.End, b.Checksum32, b.HasData)
}
func (f Fingerprint) String() string {
buf := fmt.Sprintf("Block size=%d, Source=%s\n", f.Blocksz, f.Source)
for k, v := range f.BlockMap {
buf += fmt.Sprintf("Checksum32=%d\n", k)
for sha, blk := range v {
buf += fmt.Sprintf("\tSHA Hash=%d,Block=%v\n", sha, blk)
}
}
return buf
}
// Generate creates a finger print using the FingerprintGenerator.
// Processing i.e. concurrent or sequential depends on the generator field ConcurrentMode
func (g *FingerprintGenerator) Generate() *Fingerprint {
if g.ConcurrentMode {
return g.genConcurrent()
} else {
return g.genSequential()
}
}
// NewFingerprintFromReader creates a Fingerprint for a given reader and blocksize.
// By default it does concurrent processing of blocks to generate fingerprint.
// However if the number of blocks is small <50 , then caller should use sequential generation,
// since the concurrent processing would not add much value.
// Or use the function NewFingerrprint(file, blocksize) when dealing with files, which switches
// mode based on the number of blocks.
// Number of blocks can be calculated as file size/block size
func NewFingerprintFromReader(r io.Reader, blocksz uint32) *Fingerprint {
DEFAULT_GENERATOR.Source = r
DEFAULT_GENERATOR.BlockSize = blocksz
return DEFAULT_GENERATOR.Generate()
}
func (g *FingerprintGenerator) genSequential() *Fingerprint {
bufz := make([]byte, g.BlockSize)
n, start := 0, int64(0)
var (
err error
block Block
)
fngprt := Fingerprint{
Blocksz: g.BlockSize, BlockMap: make(map[uint32]map[[sha256.Size]byte]Block)}
for err == nil {
n, err = g.Source.Read(bufz)
if err == nil {
block = Block{Start: start, End: start + int64(n),
Checksum32: adler32.Checksum(bufz[0:n]),
Sha256hash: sha256.Sum256(bufz[0:n])}
addBlock(&fngprt, &block)
start = block.End
} else {
if err == io.EOF {
glog.V(2).Infoln("Fingerprint generation: Reader read complete")
} else {
glog.Fatal(err)
}
}
}
return &fngprt
}
// NewFingerprint creates a Fingerprint for a given reader and blocksize.
func (g *FingerprintGenerator) genConcurrent() *Fingerprint {
fngprt := Fingerprint{
Blocksz: g.BlockSize, BlockMap: make(map[uint32]map[[sha256.Size]byte]Block)}
blkin := readBlocks(g.Source, g.BlockSize, g.NumWorkers)
blkout := fillBlocks(blkin, g.NumWorkers)
for b := range blkout {
addBlock(&fngprt, b)
}
return &fngprt
}
// NewFingerprint creates a Fingerprint for a given file and blocksize.
// By default it does concurrent processing of blocks to generate fingerprint.
// The generation is switched to sequential mode if the number of blocks is less than 50.
func NewFingerprint(filename string, blocksize uint32) *Fingerprint {
file, e := os.Open(filename)
defer file.Close()
if e != nil {
glog.Fatalf("Unable to open file %s %s", filename, e)
}
fileInfo, _ := file.Stat()
numblocks := (fileInfo.Size() / int64(blocksize))
var f *Fingerprint
if numblocks < 50 {
//switch to sequential mode
g := &FingerprintGenerator{Source: file, ConcurrentMode: false, BlockSize: blocksize}
f = g.Generate()
} else {
//use default generator
f = NewFingerprintFromReader(file, blocksize)
}
f.Source = filename
return f
}
// addBlock adds the hashed block to the Fingerprint struct
func addBlock(f *Fingerprint, b *Block) {
glog.V(3).Infof("Adding Block %v ", *b)
if sha2blk := f.BlockMap[b.Checksum32]; sha2blk == nil {
f.BlockMap[b.Checksum32] = make(map[[sha256.Size]byte]Block)
}
f.BlockMap[b.Checksum32][b.Sha256hash] = *b
}
// readBlocks reads blocksize bytes from the reader into memory
// numhashers determines the buffer size of the output channel where the method places the blocks which
// been read in
func readBlocks(r io.Reader, blocksize uint32, numhashers int) chan *Block {
blkin := make(chan *Block, numhashers)
n, start := 0, int64(0)
go func() {
var err error
defer close(blkin)
for err == nil {
bufz := make([]byte, blocksize)
n, err = r.Read(bufz)
if err == nil {
block := Block{Start: start, End: start + int64(n), RawBytes: bufz, HasData: true}
blkin <- &block
start += int64(n)
} else {
if err == io.EOF {
glog.V(2).Infoln("Fingerprint generation: Reader read complete")
} else {
glog.Fatal(err)
}
}
}
}()
return blkin
}
// fillBlocks takes an input channel with the bytes read from disk and creates the Checksum and SHAHashes
// numworkers is the number of go routines used for processing
func fillBlocks(in chan *Block, numhashers int) chan *Block {
out := make(chan *Block)
var wg sync.WaitGroup
wg.Add(numhashers)
for i := 0; i < numhashers; i++ {
go func() {
for blkptr := range in {
buf := blkptr.RawBytes[0:(blkptr.End - blkptr.Start)]
blkptr.Checksum32 = adler32.Checksum(buf)
blkptr.Sha256hash = sha256.Sum256(buf)
blkptr.RawBytes = nil
blkptr.HasData = false
out <- blkptr
}
wg.Done()
}()
}
go func() {
wg.Wait()
close(out)
}()
return out
}