forked from tipabu/erasurecode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.go
191 lines (154 loc) · 4.18 KB
/
buffer.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
package erasurecode
import (
"bytes"
"io"
)
type BufferMatrix struct {
b []byte
zero []byte
hdrSize, bufSize int
len int // len of input
k int
curBlock int
leftInBlock int
finished bool
}
// FragLen returns the size of a "fragment" aligned to a block size (data + header)
func (b BufferMatrix) FragLen() int {
return b.SubGroups() * (b.bufSize + b.hdrSize)
}
// SubGroups returns the number of blocks inside a single fragment
func (b BufferMatrix) SubGroups() int {
nbBlocks := (b.len + b.bufSize - 1) / b.bufSize
nbStripes := (nbBlocks + b.k - 1) / b.k
return nbStripes
}
func (b BufferMatrix) maxLen() int {
return (b.SubGroups() * b.k) * (b.bufSize + b.hdrSize)
}
// NewBufferMatrix returns a new buffer suitable for <len> data and organized
// such as it can be injected into EncodeMatrixWithBuffer without allocation/copying
// the data into shards
func NewBufferMatrix(bufSize int, len int, k int) *BufferMatrix {
var b BufferMatrix
b.Reset(bufSize, len, k)
return &b
}
// Reset serves the same purpose as NewBufferMatrix but use the existing buffer and
// tries to avoid allocation of the underlying buffer.
func (b *BufferMatrix) Reset(bufSize int, length int, k int) {
hdrSize := fragmentHeaderSize()
b.hdrSize = hdrSize
b.bufSize = bufSize
b.len = length
b.k = k
b.leftInBlock = -1
b.curBlock = 0
b.finished = false
maxLen := b.maxLen()
if cap(b.b) < maxLen {
// No internal buffer or buffer is too small, let's replace it
b.b, _ = memalign(maxLen, defaultAlign)
}
// Set proper size of buffer
b.b = b.b[:maxLen]
if len(b.zero) < bufSize {
b.zero = make([]byte, bufSize)
}
}
var emptyErasureHeader = bytes.Repeat([]byte{0}, fragmentHeaderSize())
// getOffset returns current offset in buffer and size left in the current block
// So that it is safe to copy <left> bytes at <offset>.
// If we are at a boundary, it will init the header and skip it.
func (b *BufferMatrix) getOffset() (int, int) {
realCurBlock := b.getRealBlock(b.curBlock)
blockSize := b.hdrSize + b.bufSize
blockOffset := realCurBlock * blockSize
if b.leftInBlock == -1 {
// Start of a block
copy(b.b[blockOffset:], emptyErasureHeader)
b.leftInBlock = b.bufSize
}
curOffset := blockOffset + (b.bufSize - b.leftInBlock) + b.hdrSize
return curOffset, b.leftInBlock
}
// Finish *must* be called after the final Write() *before* using the buffer
// in EncodeMatrix
// It is safe to call it multiple times.
func (b *BufferMatrix) Finish() {
if b.finished {
return
}
maxBlock := b.SubGroups() * b.k
for b.curBlock < maxBlock {
curOffset, leftToCopy := b.getOffset()
n := copy(b.b[curOffset:], b.zero[0:leftToCopy])
b.leftInBlock -= n
if b.leftInBlock == 0 {
b.curBlock++
b.leftInBlock--
}
}
b.finished = true
}
func (b BufferMatrix) getRealBlock(blockidx int) int {
subgroup := b.SubGroups()
return (blockidx%b.k)*subgroup + (blockidx / b.k)
}
func (b *BufferMatrix) Write(p []byte) (int, error) {
var dataCopied int
for len(p) > 0 {
curOffset, leftToCopy := b.getOffset()
var max int
if len(p) > leftToCopy {
max = leftToCopy
} else {
max = len(p)
}
n := copy(b.b[curOffset:], p[:max])
b.leftInBlock -= n
dataCopied += max
if b.leftInBlock == 0 {
b.curBlock++
b.leftInBlock--
}
p = p[max:]
}
return dataCopied, nil
}
func (b *BufferMatrix) ReadFrom(r io.Reader) (int64, error) {
read := int64(0)
for {
curOffset, max := b.getOffset()
n, err := r.Read(b.b[curOffset : curOffset+max])
if err != nil && err != io.EOF {
return 0, err
}
b.leftInBlock -= n
read += int64(n)
if b.leftInBlock == 0 {
b.curBlock++
b.leftInBlock--
}
if err != nil && err == io.EOF {
break
}
}
b.Finish() // Q: Mark buffer not usable anymore ?
return read, nil
}
func (b BufferMatrix) RealData() []byte {
res := make([]byte, 0, b.len)
for block := 0; len(res) < b.len; block++ {
blockSize := b.hdrSize + b.bufSize
curOffset := b.getRealBlock(block)*blockSize + b.hdrSize
res = append(res, b.b[curOffset:curOffset+b.bufSize]...)
}
return res[:b.len]
}
func (b BufferMatrix) Bytes() []byte {
return b.b
}
func (b BufferMatrix) Length() int {
return b.len
}