-
Notifications
You must be signed in to change notification settings - Fork 4
/
store.go
62 lines (53 loc) · 1.03 KB
/
store.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
package server
import (
"encoding/binary"
"github.com/dgraph-io/badger"
)
const (
LOG_ENTRIES = 0
GROUP_PEERS = 1
GROUP_META = 2
NODES_LIST = 3
GROUP_LAST_IDX = 4
CLIENT = 5
SERVER_CONF = 100
)
const (
NODE_LIST_GROUP = 1
CLIENT_LIST_GROUP = 2
CONFIG_GROUP = 0
)
const (
UINT32_LEN = 4
UINT64_LEN = 8
)
func U32Bytes(t uint32) []byte {
bs := make([]byte, UINT32_LEN)
binary.BigEndian.PutUint32(bs, t)
return bs
}
func U64Bytes(n uint64) []byte {
bs := make([]byte, UINT64_LEN)
binary.BigEndian.PutUint64(bs, n)
return bs
}
func BytesU64(bs []byte, offset int) uint64 {
return binary.BigEndian.Uint64(bs[offset : offset+UINT64_LEN])
}
func ComposeKeyPrefix(group uint64, t uint32) []byte {
return append(U32Bytes(t), U64Bytes(group)...)
}
func ItemValue(item *badger.KVItem) *[]byte {
var val *[]byte
item.Value(func(bytes []byte) error {
if bytes != nil {
lval := make([]byte, len(bytes))
copy(lval, bytes)
val = &lval
} else {
val = nil
}
return nil
})
return val
}