-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathwriter.go
890 lines (815 loc) · 23.4 KB
/
writer.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
package dedup
import (
"bytes"
hasher "crypto/sha1"
"encoding/binary"
"errors"
"fmt"
"io"
"math"
"math/big"
"runtime"
"sync"
"github.com/klauspost/dedup/sort"
)
type Writer interface {
io.WriteCloser
// Split content, so a new block begins with next write.
Split()
// MemUse returns an approximate maximum memory use in bytes for
// encoder (Writer) and decoder (Reader) for the given number of bytes.
MemUse(bytes int) (encoder, decoder int64)
// Returns the current number of blocks.
// Blocks may still be processing.
Blocks() int
}
// Size of the underlying hash in bytes for those interested.
const HashSize = hasher.Size
// The smallest "maximum" block size allowed.
const MinBlockSize = 512
// ErrMaxMemoryTooSmall is returned if the encoder isn't allowed to store
// even 1 block.
var ErrMaxMemoryTooSmall = errors.New("there must be at be space for 1 block")
// Deduplication mode used to determine how input is split.
type Mode int
const (
// Fixed block size
//
// This is by far the fastest mode, and checks for duplicates
// In fixed block sizes.
// It can be helpful to use the "Split" function to reset offset, which
// will reset duplication search at the position you are at.
ModeFixed Mode = 0
// Dynamic block size.
//
// This mode will create a deduplicator that will split the contents written
// to it into dynamically sized blocks.
// The size given indicates the maximum block size. Average size is usually maxSize/4.
// Minimum block size is maxSize/64.
ModeDynamic = 1
// Dynamic block size.
//
// This mode will create a deduplicator that will split the contents written
// to it into dynamically sized blocks.
// The size given indicates the maximum block size. Average size is usually maxSize/4.
// Minimum block size is maxSize/64.
ModeDynamicEntropy = 2
)
// Fragment is a file fragment.
// It is the data returned by the NewSplitter.
type Fragment struct {
Hash [HashSize]byte // Hash of the fragment
Payload []byte // Data of the fragment.
New bool // Will be true, if the data hasn't been encountered before.
N uint // Sequencially incrementing number for each segment.
}
type writer struct {
blks io.Writer // Block data writer
idx io.Writer // Index writer
frags chan<- Fragment // Fragment output
maxSize int // Maximum Block size
maxBlocks int // Maximum backreference distance
index map[[hasher.Size]byte]int // Known hashes and their index
input chan *block // Channel containing blocks to be hashed
write chan *block // Channel containing (ordered) blocks to be written
exited chan struct{} // Closed when the writer exits.
cur []byte // Current block being written
off int // Write offset in current block
buffers chan *block // Buffers ready for re-use.
vari64 []byte // Temporary buffer for writing varints
err error // Error state
mu sync.Mutex // Mutex for error state
nblocks int // Current block number. First block is 1.
writer func(*writer, []byte) (int, error) // Writes are forwarded here.
flush func(*writer) error // Called from Close *before* the writer is closed.
close func(*writer) error // Called from Close *after* the writer is closed.
split func(*writer) // Called when Split is called.
}
// block contains information about a single block
type block struct {
data []byte
sha1Hash [hasher.Size]byte
hashDone chan error
N int
}
// ErrSizeTooSmall is returned if the requested block size is smaller than
// hash size.
var ErrSizeTooSmall = errors.New("maximum block size too small. must be at least 512 bytes")
// NewWriter will create a deduplicator that will split the contents written
// to it into blocks and de-duplicate these.
//
// The output is delivered as two streams, an index stream and a block stream.
//
// The index stream will contain information about which blocks are deduplicated
// and the block stream will contain uncompressed data blocks.
//
// You can set the maximum memory for the decoder to use.
// This limits the length a match can be made.
// This is very conservative, so you can set this at the absolute limit of memory available.
// If you use dynamic blocks, also note that the average size is 1/4th of the maximum block size.
// Set maxMemory to 0 to disable decoder memory limit.
//
// This function returns data that is compatible with the NewReader function.
// The returned writer must be closed to flush the remaining data.
func NewWriter(index io.Writer, blocks io.Writer, mode Mode, maxSize, maxMemory uint) (Writer, error) {
ncpu := runtime.GOMAXPROCS(0)
// For small block sizes we need to keep a pretty big buffer to keep input fed.
// Constant below appears to be sweet spot measured with 4K blocks.
var bufmul = 256 << 10 / int(maxSize)
if bufmul < 2 {
bufmul = 2
}
w := &writer{
blks: blocks,
idx: index,
maxSize: int(maxSize),
index: make(map[[hasher.Size]byte]int),
input: make(chan *block, ncpu*bufmul),
write: make(chan *block, ncpu*bufmul),
exited: make(chan struct{}, 0),
cur: make([]byte, maxSize),
vari64: make([]byte, binary.MaxVarintLen64),
buffers: make(chan *block, ncpu*bufmul),
nblocks: 1,
maxBlocks: int(maxMemory / maxSize),
}
switch mode {
case ModeFixed:
fw := &fixedWriter{}
w.writer = fw.write
w.split = fw.split
case ModeDynamic:
zw := newZpaqWriter(maxSize)
w.writer = zw.write
w.split = zw.split
case ModeDynamicEntropy:
zw := newEntropyWriter(maxSize)
w.writer = zw.write
w.split = zw.split
default:
return nil, fmt.Errorf("dedup: unknown mode")
}
if w.maxSize < MinBlockSize {
return nil, ErrSizeTooSmall
}
w.close = idxClose
w.putUint64(1) // Format
w.putUint64(uint64(maxSize)) // Maximum block size
// Start one goroutine per core
for i := 0; i < ncpu; i++ {
go w.hasher()
}
// Insert the buffers we will use
for i := 0; i < ncpu*bufmul; i++ {
w.buffers <- &block{data: make([]byte, maxSize), hashDone: make(chan error, 1)}
}
go w.blockWriter()
return w, nil
}
// NewStreamWriter will create a deduplicator that will split the contents written
// to it into blocks and de-duplicate these.
//
// The output is delivered as a single stream, and memory use will remain stable for
// both writing and reading the stream.
//
// This function returns data that is compatible with the NewStreamReader function.
//
// You can must set the maximum memory for the decoder to use.
// This limits the length a match can be made.
// If you use dynamic blocks, also note that the average size is 1/4th of the maximum block size.
//
// The returned writer must be closed to flush the remaining data.
func NewStreamWriter(out io.Writer, mode Mode, maxSize, maxMemory uint) (Writer, error) {
ncpu := runtime.GOMAXPROCS(0)
// For small block sizes we need to keep a pretty big buffer to keep input fed.
// Constant below appears to be sweet spot measured with 4K blocks.
var bufmul = 256 << 10 / int(maxSize)
if bufmul < 2 {
bufmul = 2
}
if maxMemory < maxSize {
return nil, ErrMaxMemoryTooSmall
}
w := &writer{
idx: out,
maxSize: int(maxSize),
index: make(map[[hasher.Size]byte]int),
input: make(chan *block, ncpu*bufmul),
write: make(chan *block, ncpu*bufmul),
exited: make(chan struct{}, 0),
cur: make([]byte, maxSize),
vari64: make([]byte, binary.MaxVarintLen64),
buffers: make(chan *block, ncpu*bufmul),
nblocks: 1,
maxBlocks: int(maxMemory / maxSize),
}
switch mode {
case ModeFixed:
fw := &fixedWriter{}
w.writer = fw.write
case ModeDynamic:
zw := newZpaqWriter(maxSize)
w.writer = zw.write
case ModeDynamicEntropy:
zw := newEntropyWriter(maxSize)
w.writer = zw.write
/* case ModeDynamicSignatures:
zw := newZpaqWriter(maxSize)
w.writer = zw.writeFile
case ModeSignaturesOnly:
w.writer = fileSplitOnly
*/
default:
return nil, fmt.Errorf("dedup: unknown mode")
}
if w.maxSize < MinBlockSize {
return nil, ErrSizeTooSmall
}
w.close = streamClose
w.putUint64(2) // Format
w.putUint64(uint64(maxSize)) // Maximum block size
w.putUint64(uint64(w.maxBlocks)) // Maximum backreference length
// Start one goroutine per core
for i := 0; i < ncpu; i++ {
go w.hasher()
}
// Insert the buffers we will use
for i := 0; i < ncpu*bufmul; i++ {
w.buffers <- &block{data: make([]byte, maxSize), hashDone: make(chan error, 1)}
}
go w.blockStreamWriter()
return w, nil
}
// NewSplitter will return a writer you can write data to,
// and the file will be split into separate fragments.
//
// You must supply a fragment channel, that will output fragments for
// the data you have written. The channel must accept data while you
// write to the spliter.
//
// For each fragment the SHA-1 hash of the data section is returned,
// along with the raw data of this segment.
//
// When you call Close on the returned Writer, the final fragments
// will be sent and the channel will be closed.
func NewSplitter(fragments chan<- Fragment, mode Mode, maxSize uint) (Writer, error) {
ncpu := runtime.GOMAXPROCS(0)
// For small block sizes we need to keep a pretty big buffer to keep input fed.
// Constant below appears to be sweet spot measured with 4K blocks.
var bufmul = 256 << 10 / int(maxSize)
if bufmul < 2 {
bufmul = 2
}
w := &writer{
frags: fragments,
maxSize: int(maxSize),
index: make(map[[hasher.Size]byte]int),
input: make(chan *block, ncpu*bufmul),
write: make(chan *block, ncpu*bufmul),
exited: make(chan struct{}, 0),
cur: make([]byte, maxSize),
vari64: make([]byte, binary.MaxVarintLen64),
buffers: make(chan *block, ncpu*bufmul),
nblocks: 1,
}
switch mode {
case ModeFixed:
fw := &fixedWriter{}
w.writer = fw.write
w.split = fw.split
case ModeDynamic:
zw := newZpaqWriter(maxSize)
w.writer = zw.write
w.split = zw.split
case ModeDynamicEntropy:
zw := newEntropyWriter(maxSize)
w.writer = zw.write
w.split = zw.split
default:
return nil, fmt.Errorf("dedup: unknown mode")
}
w.flush = func(w *writer) error {
w.split(w)
return w.err
}
if w.maxSize < MinBlockSize {
return nil, ErrSizeTooSmall
}
// Start one goroutine per core
for i := 0; i < ncpu; i++ {
go w.hasher()
}
// Insert the buffers we will use
for i := 0; i < ncpu*bufmul; i++ {
w.buffers <- &block{data: make([]byte, maxSize), hashDone: make(chan error, 1)}
}
go w.fragmentWriter()
return w, nil
}
// putUint64 will Write a uint64 value to index stream.
func (w *writer) putUint64(v uint64) error {
n := binary.PutUvarint(w.vari64, v)
n2, err := w.idx.Write(w.vari64[:n])
if err != nil {
return err
}
if n2 != n {
return io.ErrShortWrite
}
return nil
}
// Split content, so a new block begins with next write
func (w *writer) Split() {
w.split(w)
}
func (w *writer) Blocks() int {
w.mu.Lock()
b := w.nblocks - 1
w.mu.Unlock()
return b
}
// Write contents to the deduplicator.
func (w *writer) Write(b []byte) (n int, err error) {
w.mu.Lock()
err = w.err
w.mu.Unlock()
if err != nil {
return 0, err
}
return w.writer(w, b)
}
// setErr will set the error state of the writer.
func (w *writer) setErr(err error) {
if err == nil {
return
}
w.mu.Lock()
w.err = err
w.mu.Unlock()
}
// idxClose will flush the remainder of an index based stream
func idxClose(w *writer) (err error) {
// Insert length of remaining data into index
w.putUint64(uint64(math.MaxUint64))
w.putUint64(uint64(w.maxSize - w.off))
w.putUint64(0) // Stream continuation possibility, should be 0.
buf := bytes.NewBuffer(w.cur[0:w.off])
n, err := io.Copy(w.blks, buf)
if err != nil {
return err
}
if int(n) != w.off {
return errors.New("idxClose: r.cur short write")
}
return nil
}
// streamClose will flush the remainder of an single stream
func streamClose(w *writer) (err error) {
// Insert length of remaining data into index
w.putUint64(uint64(math.MaxUint64))
w.putUint64(uint64(w.maxSize - w.off))
buf := bytes.NewBuffer(w.cur[0:w.off])
n, err := io.Copy(w.idx, buf)
if err != nil {
return err
}
if int(n) != w.off {
return errors.New("streamClose: r.cur short write")
}
w.putUint64(0) // Stream continuation possibility, should be 0.
return nil
}
// Close and flush the remaining data to output.
func (w *writer) Close() (err error) {
select {
case <-w.exited:
return w.err
default:
}
if w.flush != nil {
err := w.flush(w)
if err != nil {
return err
}
}
close(w.input)
close(w.write)
<-w.exited
if w.close != nil {
err := w.close(w)
if err != nil {
return err
}
}
return w.err
}
// hasher will hash incoming blocks
// and signal the writer when done.
func (w *writer) hasher() {
h := hasher.New()
for b := range w.input {
buf := bytes.NewBuffer(b.data)
h.Reset()
n, err := io.Copy(h, buf)
if err != nil {
w.setErr(err)
return
}
if int(n) != len(b.data) {
w.setErr(errors.New("short copy in hasher"))
return
}
_ = h.Sum(b.sha1Hash[:0])
b.hashDone <- nil
}
}
// blockWriter will write hashed blocks to the output
// and recycle the buffers.
func (w *writer) blockWriter() {
defer close(w.exited)
sortA := make([]int, w.maxBlocks+1)
for b := range w.write {
_ = <-b.hashDone
match, ok := w.index[b.sha1Hash]
if !ok {
buf := bytes.NewBuffer(b.data)
n, err := io.Copy(w.blks, buf)
if err != nil {
w.setErr(err)
return
}
if int(n) != len(b.data) {
// This should not be possible with io.copy without an error,
// but we test anyway.
w.setErr(errors.New("error: short write on copy"))
return
}
w.putUint64(0)
w.putUint64(uint64(w.maxSize) - uint64(n))
} else {
offset := b.N - match
if offset <= 0 {
// should be impossible, indicated an internal error
w.setErr(errors.New("internal error: negative offset"))
return
}
w.putUint64(uint64(offset))
}
// Update hash to latest match
w.index[b.sha1Hash] = b.N
// Purge the entries with the oldest matches
if w.maxBlocks > 0 && len(w.index) > w.maxBlocks {
ar := sortA[0:len(w.index)]
i := 0
for _, v := range w.index {
ar[i] = v
i++
}
sort.Asc(ar)
// Cut the oldest quarter blocks
// since this isn't free
cutoff := ar[w.maxBlocks/4]
for k, v := range w.index {
if v < cutoff {
delete(w.index, k)
}
}
}
// Done, reinsert buffer
w.buffers <- b
}
}
// blockStreamWriter will write blocks and indexes to the output stream
// and recycle the buffers.
func (w *writer) blockStreamWriter() {
defer close(w.exited)
for b := range w.write {
_ = <-b.hashDone
match, ok := w.index[b.sha1Hash]
if w.maxBlocks > 0 && (b.N-match) > w.maxBlocks {
ok = false
}
if !ok {
w.putUint64(0)
w.putUint64(uint64(w.maxSize) - uint64(len(b.data)))
buf := bytes.NewBuffer(b.data)
n, err := io.Copy(w.idx, buf)
if err != nil {
w.setErr(err)
return
}
if int(n) != len(b.data) {
// This should not be possible with io.Copy without an error,
// but we test anyway.
w.setErr(errors.New("error: short write on copy"))
return
}
} else {
offset := b.N - match
if offset <= 0 {
// should be impossible, indicated an internal error
w.setErr(errors.New("internal error: negative offset"))
return
}
w.putUint64(uint64(offset))
}
// Update hash to latest match
w.index[b.sha1Hash] = b.N
// Purge old entries once in a while
if w.maxBlocks > 0 && b.N&65535 == 65535 {
for k, v := range w.index {
if (b.N - v) > w.maxBlocks {
delete(w.index, k)
}
}
}
// Done, reinsert buffer
w.buffers <- b
}
}
// fragmentWriter will write hashed blocks to the output channel
// and recycle the buffers.
func (w *writer) fragmentWriter() {
defer close(w.exited)
defer close(w.frags)
n := uint(0)
for b := range w.write {
_ = <-b.hashDone
var f Fragment
f.N = n
copy(f.Hash[:], b.sha1Hash[:])
_, ok := w.index[b.sha1Hash]
f.Payload = make([]byte, len(b.data))
copy(f.Payload, b.data)
if !ok {
w.index[b.sha1Hash] = 0
f.New = !ok
}
w.frags <- f
// Done, reinsert buffer
w.buffers <- b
n++
}
}
type fixedWriter struct{}
// Write blocks of similar size.
func (f *fixedWriter) write(w *writer, b []byte) (n int, err error) {
written := 0
for len(b) > 0 {
n := copy(w.cur[w.off:], b)
b = b[n:]
w.off += n
written += n
// Filled the buffer? Send it off!
if w.off == w.maxSize {
b := <-w.buffers
// Swap block with current
w.cur, b.data = b.data, w.cur
w.mu.Lock()
b.N = w.nblocks
w.nblocks++
w.mu.Unlock()
w.input <- b
w.write <- b
w.off = 0
}
}
return written, nil
}
// Split content, so a new block begins with next write
func (f *fixedWriter) split(w *writer) {
if w.off == 0 {
return
}
b := <-w.buffers
// Swap block with current
w.cur, b.data = b.data[:w.maxSize], w.cur[:w.off]
w.mu.Lock()
b.N = w.nblocks
w.nblocks++
w.mu.Unlock()
w.input <- b
w.write <- b
w.off = 0
}
// MemUse returns an approximate maximum memory use in bytes for
// encoder (Writer) and decoder (Reader) for the given number of bytes.
func (w *writer) MemUse(bytes int) (encoder, decoder int64) {
blocks := (bytes + w.maxSize - 1) / w.maxSize
if w.maxBlocks > 0 {
if w.maxBlocks < blocks {
blocks = w.maxBlocks
}
}
// Data length
data := big.NewInt(int64(blocks))
data = data.Mul(data, big.NewInt(int64(w.maxSize)))
d := data.Int64()
if data.BitLen() > 63 {
d = math.MaxInt64
}
// Index length
bl := big.NewInt(int64(blocks))
perBlock := big.NewInt(int64(HashSize + 8 /*int64*/ + 24 /* map entry*/))
total := bl.Mul(bl, perBlock)
if total.BitLen() > 63 {
return math.MaxInt64, d
}
return total.Int64(), d
}
// Split blocks like ZPAQ: (public domain)
type zpaqWriter struct {
h uint32 // rolling hash for finding fragment boundaries
c1 byte // last byte
maxFragment int
minFragment int
maxHash uint32
o1 [256]byte // order 1 context -> predicted byte
}
// Split blocks. Typically block size will be maxSize / 4
// Minimum block size is maxSize/64.
//
// The break point is content dependent.
// Any insertions, deletions, or edits that occur before the start of the 32+ byte dependency window
// don't affect the break point.
// This makes it likely for two files to still have identical fragments far away from any edits.
func newZpaqWriter(maxSize uint) *zpaqWriter {
fragment := math.Log2(float64(maxSize) / (64 * 64))
mh := math.Exp2(22 - fragment)
return &zpaqWriter{
maxFragment: int(maxSize),
minFragment: int(maxSize / 64),
maxHash: uint32(mh),
}
}
// h is a 32 bit hash that depends on the last 32 bytes that were mispredicted by the order 1 model o1[].
// h < maxhash therefore occurs with probability 2^-16, giving an average fragment size of 64K.
// The variable size dependency window works because one constant is odd (correct prediction, no shift),
// and the other is even but not a multiple of 4 (missed prediction, 1 bit shift left).
// This is different from a normal Rabin filter, which uses a large fixed-sized dependency window
// and two multiply operations, one at the window entry and the inverse at the window exit.
func (z *zpaqWriter) write(w *writer, b []byte) (int, error) {
// Transfer to local variables ~30% faster.
c1 := z.c1
h := z.h
off := w.off
for _, c := range b {
if c == z.o1[c1] {
h = (h + uint32(c) + 1) * 314159265
} else {
h = (h + uint32(c) + 1) * 271828182
}
z.o1[c1] = c
c1 = c
w.cur[off] = c
off++
// At a break point? Send it off!
if (off >= z.minFragment && h < z.maxHash) || off >= z.maxFragment {
b := <-w.buffers
// Swap block with current
w.cur, b.data = b.data[:w.maxSize], w.cur[:off]
b.N = w.nblocks
w.input <- b
w.write <- b
w.nblocks++
off = 0
h = 0
c1 = 0
}
}
w.off = off
z.h = h
z.c1 = c1
return len(b), nil
}
// Split content, so a new block begins with next write
func (z *zpaqWriter) split(w *writer) {
if w.off == 0 {
return
}
b := <-w.buffers
// Swap block with current
w.cur, b.data = b.data[:w.maxSize], w.cur[:w.off]
w.mu.Lock()
b.N = w.nblocks
w.nblocks++
w.mu.Unlock()
w.input <- b
w.write <- b
w.off = 0
z.h = 0
z.c1 = 0
}
// Split blocks based on entropy distribution.
type entWriter struct {
h uint32 // rolling hash for finding fragment boundaries
maxFragment int
minFragment int
maxHash uint32
hist [256]uint16 // histogram of current accumulated
histLen int
avgHist uint16
}
// Split blocks. Typically block size will be maxSize / 4
// Minimum block size is maxSize/32.
//
// The break point is content dependent.
// Any insertions, deletions, or edits that occur before the start of the 32+ byte dependency window
// don't affect the break point.
// This makes it likely for two files to still have identical fragments far away from any edits.
func newEntropyWriter(maxSize uint) *entWriter {
fragment := math.Log2(float64(maxSize) / (64 * 64))
mh := math.Exp2(22 - fragment)
e := &entWriter{
maxFragment: int(maxSize),
minFragment: int(maxSize / 32),
maxHash: uint32(mh),
}
if e.minFragment > 65535 {
e.minFragment = 65535
}
if e.minFragment < 512 {
e.minFragment = 512
}
e.avgHist = uint16(e.minFragment / 255)
return e
}
// h is a 32 bit hash that depends on the last 32 bytes that were mispredicted by the order 1 model o1[].
// h < maxhash therefore occurs with probability 2^-16, giving an average fragment size of 64K.
// The variable size dependency window works because one constant is odd (correct prediction, no shift),
// and the other is even but not a multiple of 4 (missed prediction, 1 bit shift left).
// This is different from a normal Rabin filter, which uses a large fixed-sized dependency window
// and two multiply operations, one at the window entry and the inverse at the window exit.
func (e *entWriter) write(w *writer, b []byte) (int, error) {
inLen := len(b)
if e.histLen < e.minFragment {
b2 := b
if len(b2)+e.histLen > e.minFragment {
b2 = b2[:e.minFragment-e.histLen]
}
off := w.off
for i := range b2 {
v := b2[i]
e.hist[v]++
w.cur[off+i] = v
}
e.histLen += len(b2)
w.off += len(b2)
b = b[len(b2):]
}
if len(b) == 0 {
return inLen, nil
}
// Transfer to local variables ~30% faster.
h := e.h
off := w.off
for _, c := range b {
if e.hist[c] >= e.avgHist {
h = (h + uint32(c) + 1) * 314159265
} else {
h = (h + uint32(c) + 1) * 271828182
}
w.cur[off] = c
off++
// At a break point? Send it off!
if (off >= e.minFragment && h < e.maxHash) || off >= e.maxFragment {
b := <-w.buffers
// Swap block with current
w.cur, b.data = b.data[:w.maxSize], w.cur[:off]
b.N = w.nblocks
w.input <- b
w.write <- b
e.histLen = 0
for i := range e.hist {
e.hist[i] = 0
}
w.nblocks++
off = 0
h = 0
}
}
w.off = off
e.h = h
return inLen, nil
}
// Split content, so a new block begins with next write
func (e *entWriter) split(w *writer) {
if w.off == 0 {
return
}
b := <-w.buffers
// Swap block with current
w.cur, b.data = b.data[:w.maxSize], w.cur[:w.off]
w.mu.Lock()
b.N = w.nblocks
w.nblocks++
w.mu.Unlock()
w.input <- b
w.write <- b
w.off = 0
e.h = 0
e.histLen = 0
for i := range e.hist {
e.hist[i] = 0
}
}