-
Notifications
You must be signed in to change notification settings - Fork 6
/
reader.go
288 lines (209 loc) · 5.78 KB
/
reader.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
package cellar
import (
"encoding/binary"
"io"
"log"
"os"
"path"
"github.com/abdullin/mdb"
"github.com/pkg/errors"
)
type ReadFlag int
const (
RF_None ReadFlag = 0
RF_LoadBuffer ReadFlag = 1 << 1
RF_PrintChunks ReadFlag = 1 << 2
)
type Reader struct {
Folder string
Key []byte
Flags ReadFlag
StartPos int64
EndPos int64
LimitChunks int
}
func NewReader(folder string, key []byte) *Reader {
return &Reader{folder, key, RF_LoadBuffer, 0, 0, 0}
}
type ReaderInfo struct {
// can be used to convert to file name
ChunkPos int64
// global start pos
StartPos int64
// global read pos
NextPos int64
}
type ReadOp func(pos *ReaderInfo, data []byte) error
func (r *Reader) ReadDB(op mdb.TxOp) error {
var db *mdb.DB
var err error
cfg := mdb.NewConfig()
if db, err = mdb.New(r.Folder, cfg); err != nil {
return errors.Wrap(err, "mdb.New")
}
defer db.Close()
return db.Read(op)
}
func (r *Reader) Scan(op ReadOp) error {
var db *mdb.DB
var err error
cfg := mdb.NewConfig()
if db, err = mdb.New(r.Folder, cfg); err != nil {
return errors.Wrap(err, "mdb.New")
}
defer db.Close()
var b *BufferDto
// var meta *MetaDto
var chunks []*ChunkDto
loadBuffer := (r.Flags & RF_LoadBuffer) == RF_LoadBuffer
printChunks := (r.Flags & RF_PrintChunks) == RF_PrintChunks
err = db.Read(func(tx *mdb.Tx) error {
var err error
if b, err = lmdbGetBuffer(tx); err != nil {
return errors.Wrap(err, "lmdbGetBuffer")
}
if _, err = lmdbGetCellarMeta(tx); err != nil {
return errors.Wrap(err, "lmdbGetCellarMeta")
}
if chunks, err = lmdbListChunks(tx); err != nil {
return errors.Wrap(err, "lmdbListChunks")
}
return nil
})
if err != nil {
return errors.Wrap(err, "db.Read")
}
if b == nil && len(chunks) == 0 {
return nil
}
info := &ReaderInfo{}
log.Printf("Found %d chunks and limit is %d", len(chunks), r.LimitChunks)
if len(chunks) > 0 {
if r.LimitChunks > 0 && len(chunks) > r.LimitChunks {
log.Printf("Truncating input from %d to %d chunks", len(chunks), r.LimitChunks)
chunks = chunks[:r.LimitChunks]
}
for i, c := range chunks {
endPos := c.StartPos + c.UncompressedByteSize
if r.StartPos != 0 && endPos < r.StartPos {
// skip chunk if it ends before range we are interested in
continue
}
if r.EndPos != 0 && c.StartPos > r.EndPos {
// skip the chunk if it starts after the range we are interested in
continue
}
chunk := make([]byte, c.UncompressedByteSize)
var file = path.Join(r.Folder, c.FileName)
if printChunks {
log.Printf("Loading chunk %d %s with size %d", i, c.FileName, c.UncompressedByteSize)
}
if chunk, err = loadChunkIntoBuffer(file, r.Key, c.UncompressedByteSize, chunk); err != nil {
log.Panicf("Failed to load chunk %s", c.FileName)
}
info.ChunkPos = c.StartPos
chunkPos := 0
if r.StartPos != 0 && r.StartPos > c.StartPos {
// reader starts in the middle
chunkPos = int(r.StartPos - c.StartPos)
}
if err = replayChunk(info, chunk, op, chunkPos); err != nil {
return errors.Wrap(err, "Failed to read chunk")
}
}
}
if loadBuffer && b != nil && b.Pos > 0 {
if r.EndPos != 0 && b.StartPos > r.EndPos {
// if buffer starts after the end of our search interval - skip it
return nil
}
loc := path.Join(r.Folder, b.FileName)
var f *os.File
if f, err = os.Open(loc); err != nil {
log.Panicf("Failed to open buffer file %s", loc)
}
curChunk := make([]byte, b.Pos)
var n int
if n, err = f.Read(curChunk); err != nil {
log.Panicf("Failed to read %d bytes from buffer %s", b.Pos, loc)
}
if n != int(b.Pos) {
log.Panic("Failed to read bytes")
}
info.ChunkPos = b.StartPos
chunkPos := 0
if r.StartPos > b.StartPos {
chunkPos = int(r.StartPos - b.StartPos)
}
if err = replayChunk(info, curChunk, op, chunkPos); err != nil {
return errors.Wrap(err, "Failed to read chunk")
}
}
return nil
}
func readVarint(b []byte) (val int64, n int) {
val, n = binary.Varint(b)
if n <= 0 {
log.Panicf("Failed to read varint %d", n)
}
return
}
func replayChunk(info *ReaderInfo, chunk []byte, op ReadOp, pos int) error {
max := len(chunk)
var err error
// while we are not at the end,
// read first len
// then pass the bytes to the op
for pos < max {
info.StartPos = int64(pos) + info.ChunkPos
recordSize, shift := readVarint(chunk[pos:])
// move position by the header size
pos += shift
// get chunk
record := chunk[pos : pos+int(recordSize)]
// apply chunk
pos += int(recordSize)
info.NextPos = int64(pos) + info.ChunkPos
if err = op(info, record); err != nil {
return errors.Wrap(err, "Failed to execute op")
}
// shift pos
}
return nil
}
func getMaxByteSize(cs []*ChunkDto, b *BufferDto) int64 {
var bufferSize int64
for _, c := range cs {
if c.UncompressedByteSize > bufferSize {
bufferSize = c.UncompressedByteSize
}
}
if b != nil && b.MaxBytes > bufferSize {
bufferSize = b.MaxBytes
}
return bufferSize
}
func loadChunkIntoBuffer(loc string, key []byte, size int64, b []byte) ([]byte, error) {
var decryptor, zr io.Reader
var err error
var chunkFile *os.File
if chunkFile, err = os.Open(loc); err != nil {
log.Panicf("Failed to open chunk %s", loc)
}
defer chunkFile.Close()
if decryptor, err = chainDecryptor(key, chunkFile); err != nil {
log.Panicf("Failed to chain decryptor for %s: %s", loc, err)
}
if zr, err = chainDecompressor(decryptor); err != nil {
log.Panicf("Failed to chain decompressor for %s: %s", loc, err)
}
//zr.Header.CompressionLevel = 4
var readBytes int
if readBytes, err = zr.Read(b); err != nil {
log.Panicf("Failed to read from chunk %s (%d): %s", loc, size, err)
}
if int64(readBytes) != size {
log.Panicf("Read %d bytes but expected %d", readBytes, size)
}
return b[0:readBytes], nil
}