-
Notifications
You must be signed in to change notification settings - Fork 6
/
lmdb.go
175 lines (137 loc) · 3.78 KB
/
lmdb.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
package cellar
import (
"bytes"
"encoding/binary"
"log"
"github.com/abdullin/lex-go/tuple"
"github.com/abdullin/mdb"
"github.com/bmatsuo/lmdb-go/lmdb"
"github.com/bmatsuo/lmdb-go/lmdbscan"
proto "github.com/golang/protobuf/proto"
"github.com/pkg/errors"
)
const (
ChunkTable byte = 1
MetaTable byte = 2
BufferTable byte = 3
CellarTable byte = 4
UserIndexTable byte = 5
UserCheckpointTable byte = 6
)
func lmdbPutUserCheckpoint(tx *mdb.Tx, name string, pos int64) error {
key := mdb.CreateKey(UserCheckpointTable, name)
value, err := tx.PutReserve(key, 8)
if err != nil {
return errors.Wrap(err, "PutReserve")
}
binary.LittleEndian.PutUint64(value, uint64(pos))
return nil
}
func lmdbGetUserCheckpoint(tx *mdb.Tx, name string) (int64, error) {
key := mdb.CreateKey(UserCheckpointTable, name)
value, err := tx.Get(key)
if err != nil {
return 0, errors.Wrap(err, "Get")
}
if len(value) == 0 {
return 0, nil
}
return int64(binary.LittleEndian.Uint64(value)), nil
}
func lmdbAddChunk(tx *mdb.Tx, chunkStartPos int64, dto *ChunkDto) error {
key := mdb.CreateKey(ChunkTable, chunkStartPos)
if err := tx.PutProto(key, dto); err != nil {
return errors.Wrap(err, "PutProto")
}
log.Printf("Added chunk %s with %d records and %d bytes (%d compressed)", dto.FileName, dto.Records, dto.UncompressedByteSize, dto.CompressedDiskSize)
return nil
}
func lmdbListChunks(tx *mdb.Tx) ([]*ChunkDto, error) {
tpl := mdb.CreateKey(ChunkTable)
scanner := lmdbscan.New(tx.Tx, tx.DB)
defer scanner.Close()
scanner.Set(tpl, nil, lmdb.SetRange)
var chunks []*ChunkDto
for scanner.Scan() {
key := scanner.Key()
if !bytes.HasPrefix(key, tpl) {
break
}
var chunk = &ChunkDto{}
val := scanner.Val()
if err := proto.Unmarshal(val, chunk); err != nil {
return nil, errors.Wrapf(err, "Unmarshal %x at %x", val, key)
}
chunks = append(chunks, chunk)
}
if err := scanner.Err(); err != nil {
return nil, errors.Wrap(err, "Scanner.Scan")
}
return chunks, nil
}
func lmdbPutBuffer(tx *mdb.Tx, dto *BufferDto) error {
tpl := tuple.Tuple([]tuple.Element{BufferTable})
key := tpl.Pack()
var val []byte
var err error
if val, err = proto.Marshal(dto); err != nil {
return errors.Wrap(err, "Marshal")
}
if err = tx.Put(key, val); err != nil {
return errors.Wrap(err, "tx.Put")
}
return nil
}
func lmdbGetBuffer(tx *mdb.Tx) (*BufferDto, error) {
tpl := tuple.Tuple([]tuple.Element{BufferTable})
key := tpl.Pack()
var data []byte
var err error
if data, err = tx.Get(key); err != nil {
return nil, errors.Wrap(err, "tx.Get")
}
if data == nil {
return nil, nil
}
dto := &BufferDto{}
if err = proto.Unmarshal(data, dto); err != nil {
return nil, errors.Wrap(err, "Unmarshal")
}
return dto, nil
}
func lmdbIndexPosition(tx *mdb.Tx, stream string, k uint64, pos int64) error {
tpl := tuple.Tuple([]tuple.Element{MetaTable, stream, k})
key := tpl.Pack()
var err error
buf := make([]byte, binary.MaxVarintLen64)
n := binary.PutVarint(buf, pos)
if err = tx.Put(key, buf[0:n]); err != nil {
return errors.Wrap(err, "tx.Put")
}
return nil
}
func lmdbLookupPosition(tx *mdb.Tx, stream string, k uint64) (int64, error) {
tpl := tuple.Tuple([]tuple.Element{MetaTable, stream, k})
key := tpl.Pack()
var err error
var val []byte
if val, err = tx.Get(key); err != nil {
return 0, errors.Wrap(err, "tx.Get")
}
var pos int64
pos, _ = binary.Varint(val)
return pos, nil
}
func lmdbSetCellarMeta(tx *mdb.Tx, m *MetaDto) error {
key := mdb.CreateKey(CellarTable)
return tx.PutProto(key, m)
}
func lmdbGetCellarMeta(tx *mdb.Tx) (*MetaDto, error) {
key := mdb.CreateKey(CellarTable)
dto := &MetaDto{}
var err error
if err = tx.ReadProto(key, dto); err != nil {
return nil, errors.Wrap(err, "ReadProto")
}
return dto, nil
}