/
entryfile.go
444 lines (392 loc) · 11.4 KB
/
entryfile.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
package datatree
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/coinexchain/onvakv/types"
)
type Entry = types.Entry
const MaxEntryBytes int = (1 << 24) - 1
var MagicBytes = [8]byte{255, 254, 253, 252, 252, 253, 254, 255}
var dbg bool
func DummyEntry(sn int64) *Entry {
return &Entry{
Key: []byte("dummy"),
Value: []byte("dummy"),
NextKey: []byte("dummy"),
Height: -2,
LastHeight: -2,
SerialNum: sn,
}
}
func NullEntry() Entry {
return Entry{
Key: []byte{},
Value: []byte{},
NextKey: []byte{},
Height: -1,
LastHeight: -1,
SerialNum: -1,
}
}
// Entry serialization format:
// magicBytes 8-bytes
// 8b snList length
// 24b-totalLength (this length does not include padding, snList and this field itself)
// magicBytesPos(list of 32b-int, -1 for ending), posistions are relative to the end of 32b-totalLength
// normalPayload
// DeactivedSerialNumList (list of 64b-int)
// padding-zero-bytes
const (
MSB32 = uint32(1<<31)
)
func PutUint24(b []byte, n uint32) {
//!! if n == 0 {
//!! panic("here PutUint24")
//!! }
b[0] = byte(n)
b[1] = byte(n>>8)
b[2] = byte(n>>16)
}
func GetUint24(b []byte) (n uint32) {
n = uint32(b[0])
n |= uint32(b[1]) << 8
n |= uint32(b[2]) << 16
//!! if n == 0 {
//!! panic("here GetUint24")
//!! }
return
}
//!! func SkipPosList(bz []byte) []byte {
//!! for i := 0; i + 4 < len(bz); i+=4 {
//!! if (bz[i]&bz[i+1]&bz[i+2]&bz[i+3]) == 0xFF {
//!! return bz[i+4:]
//!! }
//!! }
//!! return nil
//!! }
func ExtractKeyFromRawBytes(b []byte) []byte {
bb := b[4:]
if (bb[0]&bb[1]&bb[2]&bb[3]) == 0xFF { // No MagicBytes to recover
length := int(binary.LittleEndian.Uint32(bb[4:8]))
return append([]byte{}, bb[8:8+length]...)
}
bb = append([]byte{}, b[4:]...)
n := recoverMagicBytes(bb)
bb = bb[n:]
length := int(binary.LittleEndian.Uint32(bb[:4]))
return append([]byte{}, bb[4:4+length]...)
}
func EntryFromRawBytes(b []byte) *Entry {
bb := b[4:]
if (bb[0]&bb[1]&bb[2]&bb[3]) == 0xFF { // No MagicBytes to recover
e, _ := EntryFromBytes(bb[4:], 0)
return e
}
bb = append([]byte{}, b[4:]...)
n := recoverMagicBytes(bb)
e, _ := EntryFromBytes(bb[n+4:], 0)
return e
}
func ExtractSerialNum(entryBz []byte) int64 {
return int64(binary.LittleEndian.Uint64(entryBz[len(entryBz)-8:]))
}
func UpdateSerialNum(entryBz []byte, sn int64) {
binary.LittleEndian.PutUint64(entryBz[len(entryBz)-8:], uint64(sn))
}
func SNListToBytes(deactivedSerialNumList []int64) []byte {
res := make([]byte, len(deactivedSerialNumList) * 8)
i := 0
for _, sn := range deactivedSerialNumList {
binary.LittleEndian.PutUint64(res[i:i+8], uint64(sn))
i += 8
}
return res
}
func EntryToBytes(entry Entry, deactivedSerialNumList []int64) []byte {
length := 4 + 4 // 32b-length and empty magicBytesPos
length += 4*3 + len(entry.Key) + len(entry.Value) + len(entry.NextKey) // Three strings
length += 8 * 3 // Three int64
length += len(deactivedSerialNumList) * 8
b := make([]byte, length)
b[0] = byte(len(deactivedSerialNumList))
const start = 8
writeEntryPayload(b[start:], entry, deactivedSerialNumList)
// MagicBytes can not lay in or overlap with these 64b integers
stop := len(b) - len(deactivedSerialNumList)*8 - 3*8
magicBytesPosList := getAllPos(b[start:stop], MagicBytes[:])
if len(magicBytesPosList) == 0 {
//!! if dbg {
//!! fmt.Printf("here-length %d %d\n", length, length-4-len(deactivedSerialNumList)*8)
//!! }
PutUint24(b[1:4], uint32(length-4-len(deactivedSerialNumList)*8))
binary.LittleEndian.PutUint32(b[4:8], ^uint32(0))
return b
}
// if magicBytesPosList is not empty:
var zeroBuf [8]byte
for _, pos := range magicBytesPosList {
copy(b[start+pos:start+pos+8], zeroBuf[:]) // over-write the occurrence of magic bytes with zeros
}
length += 4 * len(magicBytesPosList)
buf := make([]byte, length)
bytesAdded := 4 * len(magicBytesPosList)
var i int
for i = 0; i < len(magicBytesPosList); i++ {
pos := magicBytesPosList[i] + bytesAdded /*32b-length*/
binary.LittleEndian.PutUint32(buf[i*4+4:i*4+8], uint32(pos))
}
binary.LittleEndian.PutUint32(buf[i*4+4:i*4+8], ^uint32(0))
copy(buf[i*4+8:], b[8:])
// Re-write the new length. minus 4 because the first 4 bytes of length isn't included
buf[0] = byte(len(deactivedSerialNumList))
PutUint24(buf[1:4], uint32(length-4-len(deactivedSerialNumList)*8))
//!! if dbg {
//!! fmt.Printf("there-length %d %d\n", length, length-4-len(deactivedSerialNumList)*8)
//!! }
return buf
}
func writeEntryPayload(b []byte, entry Entry, deactivedSerialNumList []int64) {
i := 0
binary.LittleEndian.PutUint32(b[i:i+4], uint32(len(entry.Key)))
i += 4
copy(b[i:], entry.Key)
i += len(entry.Key)
binary.LittleEndian.PutUint32(b[i:i+4], uint32(len(entry.Value)))
i += 4
copy(b[i:], entry.Value)
i += len(entry.Value)
binary.LittleEndian.PutUint32(b[i:i+4], uint32(len(entry.NextKey)))
i += 4
copy(b[i:], entry.NextKey)
i += len(entry.NextKey)
binary.LittleEndian.PutUint64(b[i:i+8], uint64(entry.Height))
i += 8
binary.LittleEndian.PutUint64(b[i:i+8], uint64(entry.LastHeight))
i += 8
binary.LittleEndian.PutUint64(b[i:i+8], uint64(entry.SerialNum))
i += 8
for _, sn := range deactivedSerialNumList {
binary.LittleEndian.PutUint64(b[i:i+8], uint64(sn))
i += 8
}
}
func getAllPos(s, sep []byte) (allpos []int) {
for start, pos := 0, 0; start + len(sep) < len(s); start += pos + len(sep) {
pos = bytes.Index(s[start:], sep)
if pos == -1 {
return
}
allpos = append(allpos, pos+start)
}
return
}
func EntryFromBytes(b []byte, numberOfSN int) (*Entry, []int64) {
entry := &Entry{}
i := 0
length := int(binary.LittleEndian.Uint32(b[i : i+4]))
i += 4
entry.Key = b[i:i+length]
i += length
length = int(binary.LittleEndian.Uint32(b[i : i+4]))
i += 4
entry.Value = b[i:i+length]
i += length
length = int(binary.LittleEndian.Uint32(b[i : i+4]))
i += 4
entry.NextKey = b[i:i+length]
i += length
entry.Height = int64(binary.LittleEndian.Uint64(b[i : i+8]))
i += 8
entry.LastHeight = int64(binary.LittleEndian.Uint64(b[i : i+8]))
i += 8
entry.SerialNum = int64(binary.LittleEndian.Uint64(b[i : i+8]))
i += 8
if numberOfSN == 0 {
return entry, nil
}
deactivedSerialNumList := make([]int64, numberOfSN)
for j := range deactivedSerialNumList {
deactivedSerialNumList[j] = int64(binary.LittleEndian.Uint64(b[i : i+8]))
i += 8
}
return entry, deactivedSerialNumList
}
type EntryFile struct {
HPFile
}
func getPaddingSize(length int) int {
rem := length % 8
if rem == 0 {
return 0
} else {
return 8 - rem
}
}
func (ef *EntryFile) readMagicBytesAndLength(off int64, withBuf bool) (length int64, numberOfSN int) {
var buf [12]byte
err := ef.HPFile.ReadAt(buf[:], off, withBuf)
if err != nil {
panic(err)
}
if !bytes.Equal(buf[:8], MagicBytes[:]) {
fmt.Printf("Now off %d %x\n", off, off)
panic("Invalid MagicBytes")
}
length = int64(GetUint24(buf[9:12]))
if int(length) >= MaxEntryBytes {
panic("Entry to long")
}
return length, int(buf[8])
}
func getNextPos(off, length int64) int64 {
length += 8 /*magicbytes*/ + 4 /*length*/
paddingSize := getPaddingSize(int(length))
paddedLen := length + int64(paddingSize)
nextPos := off + paddedLen
//fmt.Printf("off %d length %d paddingSize %d paddedLen %d nextPos %d\n", off, length, paddingSize, paddedLen, nextPos)
return nextPos
}
func (ef *EntryFile) ReadEntryAndSNList(off int64) (entry *Entry, deactivedSerialNumList []int64, nextPos int64) {
entryBz, numberOfSN, nextPos := ef.readEntry(off, true, false, true)
entry, deactivedSerialNumList = EntryFromBytes(entryBz, numberOfSN)
return
}
func (ef *EntryFile) ReadEntry(off int64) (entry *Entry, nextPos int64) {
entryBz, numberOfSN, nextPos := ef.readEntry(off, false, false, false)
entry, _ = EntryFromBytes(entryBz, numberOfSN)
return
}
func (ef *EntryFile) ReadEntryRawBytes(off int64) (entryBz []byte, nextPos int64) {
entryBz, _, nextPos = ef.readEntry(off, false, true, true)
return
}
func recoverMagicBytes(b []byte) (n int) {
for n = 0; n + 4 < len(b); n += 4 { // recover magic bytes in payload
pos := binary.LittleEndian.Uint32(b[n : n+4])
if pos == ^(uint32(0)) {
n += 4
break
}
if int(pos) >= MaxEntryBytes {
panic("Position to large")
}
copy(b[int(pos)+4:int(pos)+12], MagicBytes[:])
}
return
}
func (ef *EntryFile) readEntry(off int64, withSNList, useRaw, withBuf bool) (entrybz []byte, numberOfSN int, nextPos int64) {
length, numberOfSN := ef.readMagicBytesAndLength(off, withBuf)
nextPos = getNextPos(off, int64(length)+8*int64(numberOfSN))
if withSNList {
length += 8 * int64(numberOfSN) // ignore snlist
} else {
numberOfSN = 0
}
b := make([]byte, 12+int(length)) // include 12 (magicbytes and length)
err := ef.HPFile.ReadAt(b, off, withBuf)
origB := b
b = b[12:] // ignore magicbytes and length
if err != nil {
panic(err)
}
if useRaw {
return origB[8:], numberOfSN, nextPos
}
n := recoverMagicBytes(b)
return b[n:length], numberOfSN, nextPos
}
func NewEntryFile(bufferSize, blockSize int, dirName string) (res EntryFile, err error) {
res.HPFile, err = NewHPFile(bufferSize, blockSize, dirName)
res.HPFile.InitPreReader()
return
}
func (ef *EntryFile) Size() int64 {
return ef.HPFile.Size()
}
func (ef *EntryFile) Truncate(size int64) {
err := ef.HPFile.Truncate(size)
if err != nil {
panic(err)
}
}
func (ef *EntryFile) Flush() {
ef.HPFile.Flush()
}
func (ef *EntryFile) FlushAsync() {
ef.HPFile.FlushAsync()
}
func (ef *EntryFile) Close() {
err := ef.HPFile.Close()
if err != nil {
panic(err)
}
}
func (ef *EntryFile) PruneHead(off int64) {
err := ef.HPFile.PruneHead(off)
if err != nil {
panic(err)
}
}
func (ef *EntryFile) Append(b [2][]byte) (pos int64) {
//!! if b[0][1] == 0 && b[0][2] == 0 && b[0][3] == 0 {
//!! fmt.Printf("%#v\n", b)
//!! panic("here in Append")
//!! }
var bb [4][]byte
bb[0] = MagicBytes[:]
bb[1] = b[0]
bb[2] = b[1]
paddingSize := getPaddingSize(len(b[0])+len(b[1]))
bb[3] = make([]byte, paddingSize) // padding zero bytes
pos, err := ef.HPFile.Append(bb[:])
//!! if pos > 108996000 {
//!! dbg = true
//!! fmt.Printf("Append pos %d %#v len(bb[1]) %d padding %d\n", pos, bb[:], len(bb[1]), paddingSize)
//!! }
if pos%8 != 0 {
panic("Entries are not aligned")
}
if err != nil {
panic(err)
}
//fmt.Printf("Now Append At: %d len: %d\n", pos, len(b))
return
}
func (ef *EntryFile) GetActiveEntriesInTwig(twig *Twig) chan []byte {
res := make(chan []byte, 100)
go func() {
start := twig.FirstEntryPos
for i := 0; i < LeafCountInTwig; i++ {
if twig.getBit(i) {
entryBz, next := ef.ReadEntryRawBytes(start)
//!! fmt.Printf("Why start %d entryBz %#v\n", start, entryBz)
start = next
res <- entryBz
} else { // skip an inactive entry
length, numberOfSN := ef.readMagicBytesAndLength(start, true)
start = getNextPos(start, length+8*int64(numberOfSN))
}
}
close(res)
}()
return res
}
//!! func (ef *EntryFile) GetActiveEntriesInTwigOld(twig *Twig) chan *Entry {
//!! res := make(chan *Entry, 100)
//!! go func() {
//!! start := twig.FirstEntryPos
//!! for i := 0; i < LeafCountInTwig; i++ {
//!! if twig.getBit(i) {
//!! entry, next := ef.ReadEntry(start)
//!! start = next
//!! res <- entry
//!! } else { // skip an inactive entry
//!! length, numberOfSN := ef.readMagicBytesAndLength(start, true)
//!! start = getNextPos(start, length+8*int64(numberOfSN))
//!! }
//!! }
//!! close(res)
//!! }()
//!! return res
//!! }