-
Notifications
You must be signed in to change notification settings - Fork 14
/
persistence.go
67 lines (58 loc) · 1.36 KB
/
persistence.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
package internal
import (
"bytes"
"encoding/gob"
"github.com/zeebo/xxh3"
)
const BlockBufferSize = 4 * 1024 * 1024
type DataBlock[V any] struct {
Type uint8
SecondaryType uint8
CheckSum uint64
Index uint64 // helper filed, usage depends on Type/SecondaryType
Data []byte
clean bool
buffer *bytes.Buffer // used in entryDecoder
// datablock should share single blockEncoder
// but use separate entryEncoder
blockEncoder *gob.Encoder
entryEncoder *gob.Encoder
}
func NewBlock[V any](tp uint8, buffer *bytes.Buffer, blockEncoder *gob.Encoder) *DataBlock[V] {
return &DataBlock[V]{
Type: tp,
buffer: buffer,
blockEncoder: blockEncoder,
entryEncoder: gob.NewEncoder(buffer),
clean: true,
}
}
func (b *DataBlock[V]) Save() error {
if b.clean {
return nil
}
b.clean = true
data := b.buffer.Bytes()
b.CheckSum = xxh3.Hash(data)
b.Data = data
return b.blockEncoder.Encode(b)
}
func (b *DataBlock[V]) Write(item V) (full bool, err error) {
err = b.entryEncoder.Encode(item)
if err != nil {
return false, err
}
b.clean = false
if b.buffer.Len() >= BlockBufferSize {
b.clean = true
data := b.buffer.Bytes()
b.CheckSum = xxh3.Hash(data)
b.Data = data
err = b.blockEncoder.Encode(b)
return true, err
}
return false, nil
}
func (b *DataBlock[V]) MarkDirty() {
b.clean = false
}