-
Notifications
You must be signed in to change notification settings - Fork 6
/
writer.go
273 lines (206 loc) · 5.17 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
package cellar
import (
"encoding/binary"
fmt "fmt"
"log"
"os"
"path"
"github.com/abdullin/mdb"
"github.com/pkg/errors"
)
type Writer struct {
db *mdb.DB
b *Buffer
maxKeySize int64
maxValSize int64
folder string
maxBufferSize int64
key []byte
encodingBuf []byte
}
func NewWriter(folder string, maxBufferSize int64, key []byte) (*Writer, error) {
ensureFolder(folder)
var db *mdb.DB
var err error
cfg := mdb.NewConfig()
// make sure we are writing sync
cfg.EnvFlags = 0
if db, err = mdb.New(folder, cfg); err != nil {
return nil, errors.Wrap(err, "mdb.New")
}
var meta *MetaDto
var b *Buffer
err = db.Update(func(tx *mdb.Tx) error {
var err error
var dto *BufferDto
if dto, err = lmdbGetBuffer(tx); err != nil {
return errors.Wrap(err, "lmdbGetBuffer")
}
if dto == nil {
if b, err = createBuffer(tx, 0, maxBufferSize, folder); err != nil {
return errors.Wrap(err, "SetNewBuffer")
}
return nil
} else if b, err = openBuffer(dto, folder); err != nil {
return errors.Wrap(err, "openBuffer")
}
if meta, err = lmdbGetCellarMeta(tx); err != nil {
return errors.Wrap(err, "lmdbGetCellarMeta")
}
return nil
})
if err != nil {
return nil, errors.Wrap(err, "Update")
}
wr := &Writer{
folder: folder,
maxBufferSize: maxBufferSize,
key: key,
encodingBuf: make([]byte, binary.MaxVarintLen64),
db: db,
b: b,
}
if meta != nil {
wr.maxKeySize = meta.MaxKeySize
wr.maxValSize = meta.MaxValSize
}
return wr, nil
}
func (w *Writer) VolatilePos() int64 {
if w.b != nil {
return w.b.startPos + w.b.pos
}
return 0
}
func (w *Writer) Append(data []byte) (pos int64, err error) {
dataLen := int64(len(data))
n := binary.PutVarint(w.encodingBuf, dataLen)
totalSize := n + len(data)
if !w.b.fits(int64(totalSize)) {
if err = w.SealTheBuffer(); err != nil {
return 0, errors.Wrap(err, "SealTheBuffer")
}
}
if err = w.b.writeBytes(w.encodingBuf[0:n]); err != nil {
return 0, errors.Wrap(err, "write len prefix")
}
if err = w.b.writeBytes(data); err != nil {
return 0, errors.Wrap(err, "write body")
}
w.b.endRecord()
// update statistics
if dataLen > w.maxValSize {
w.maxValSize = dataLen
}
pos = w.b.startPos + w.b.pos
return pos, nil
}
func createBuffer(tx *mdb.Tx, startPos int64, maxSize int64, folder string) (*Buffer, error) {
name := fmt.Sprintf("%012d", startPos)
dto := &BufferDto{
Pos: 0,
StartPos: startPos,
MaxBytes: maxSize,
Records: 0,
FileName: name,
}
var err error
var buf *Buffer
if buf, err = openBuffer(dto, folder); err != nil {
return nil, errors.Wrapf(err, "openBuffer %s", folder)
}
if err = lmdbPutBuffer(tx, dto); err != nil {
return nil, errors.Wrap(err, "lmdbPutBuffer")
}
return buf, nil
}
func (w *Writer) SealTheBuffer() error {
var err error
oldBuffer := w.b
var newBuffer *Buffer
if err = oldBuffer.flush(); err != nil {
return errors.Wrap(err, "buffer.Flush")
}
var dto *ChunkDto
if dto, err = oldBuffer.compress(w.key); err != nil {
return errors.Wrap(err, "compress")
}
newStartPos := dto.StartPos + dto.UncompressedByteSize
err = w.db.Update(func(tx *mdb.Tx) error {
if err = lmdbAddChunk(tx, dto.StartPos, dto); err != nil {
return errors.Wrap(err, "lmdbAddChunk")
}
if newBuffer, err = createBuffer(tx, newStartPos, w.maxBufferSize, w.folder); err != nil {
return errors.Wrap(err, "createBuffer")
}
return nil
})
if err != nil {
return errors.Wrap(err, "w.db.Update")
}
w.b = newBuffer
oldBufferPath := path.Join(w.folder, oldBuffer.fileName)
if err = os.Remove(oldBufferPath); err != nil {
log.Printf("Can't remove old buffer %s: %s", oldBufferPath, err)
}
return nil
}
// Close disposes all resources
func (w *Writer) Close() error {
// TODO: flush, checkpoint and close current buffer
return w.db.Close()
}
// ReadDB allows to execute read transaction against
// the meta database
func (w *Writer) ReadDB(op mdb.TxOp) error {
return w.db.Read(op)
}
// Write DB allows to execute write transaction against
// the meta database
func (w *Writer) UpdateDB(op mdb.TxOp) error {
return w.db.Update(op)
}
func (w *Writer) PutUserCheckpoint(name string, pos int64) error {
return w.db.Update(func(tx *mdb.Tx) error {
return lmdbPutUserCheckpoint(tx, name, pos)
})
}
func (w *Writer) GetUserCheckpoint(name string) (int64, error) {
var pos int64
err := w.db.Read(func(tx *mdb.Tx) error {
p, e := lmdbGetUserCheckpoint(tx, name)
if e != nil {
return e
}
pos = p
return nil
})
if err != nil {
return 0, err
}
return pos, nil
}
func (w *Writer) Checkpoint() (int64, error) {
w.b.flush()
var err error
dto := w.b.getState()
current := dto.StartPos + dto.Pos
err = w.db.Update(func(tx *mdb.Tx) error {
var err error
if err = lmdbPutBuffer(tx, dto); err != nil {
return errors.Wrap(err, "lmdbPutBuffer")
}
meta := &MetaDto{
MaxKeySize: w.maxKeySize,
MaxValSize: w.maxValSize,
}
if err = lmdbSetCellarMeta(tx, meta); err != nil {
return errors.Wrap(err, "lmdbSetCellarMeta")
}
return nil
})
if err != nil {
return 0, errors.Wrap(err, "txn.Update")
}
return current, nil
}