forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
helpers.go
179 lines (149 loc) · 3.67 KB
/
helpers.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package database
import (
"encoding/binary"
"errors"
"fmt"
"time"
"github.com/MetalBlockchain/metalgo/ids"
)
const (
Uint64Size = 8 // bytes
// kvPairOverhead is an estimated overhead for a kv pair in a database.
kvPairOverhead = 8 // bytes
)
var errWrongSize = errors.New("value has unexpected size")
func PutID(db KeyValueWriter, key []byte, val ids.ID) error {
return db.Put(key, val[:])
}
func GetID(db KeyValueReader, key []byte) (ids.ID, error) {
b, err := db.Get(key)
if err != nil {
return ids.ID{}, err
}
return ids.ToID(b)
}
func ParseID(b []byte) (ids.ID, error) {
return ids.ToID(b)
}
func PutUInt64(db KeyValueWriter, key []byte, val uint64) error {
b := PackUInt64(val)
return db.Put(key, b)
}
func GetUInt64(db KeyValueReader, key []byte) (uint64, error) {
b, err := db.Get(key)
if err != nil {
return 0, err
}
return ParseUInt64(b)
}
func PackUInt64(val uint64) []byte {
bytes := make([]byte, Uint64Size)
binary.BigEndian.PutUint64(bytes, val)
return bytes
}
func ParseUInt64(b []byte) (uint64, error) {
if len(b) != Uint64Size {
return 0, errWrongSize
}
return binary.BigEndian.Uint64(b), nil
}
func PutUInt32(db KeyValueWriter, key []byte, val uint32) error {
b := PackUInt32(val)
return db.Put(key, b)
}
func GetUInt32(db KeyValueReader, key []byte) (uint32, error) {
b, err := db.Get(key)
if err != nil {
return 0, err
}
return ParseUInt32(b)
}
func PackUInt32(val uint32) []byte {
bytes := make([]byte, 4)
binary.BigEndian.PutUint32(bytes, val)
return bytes
}
func ParseUInt32(b []byte) (uint32, error) {
if len(b) != 4 {
return 0, errWrongSize
}
return binary.BigEndian.Uint32(b), nil
}
func PutTimestamp(db KeyValueWriter, key []byte, val time.Time) error {
valBytes, err := val.MarshalBinary()
if err != nil {
return err
}
return db.Put(key, valBytes)
}
func GetTimestamp(db KeyValueReader, key []byte) (time.Time, error) {
b, err := db.Get(key)
if err != nil {
return time.Time{}, err
}
return ParseTimestamp(b)
}
func ParseTimestamp(b []byte) (time.Time, error) {
val := time.Time{}
if err := val.UnmarshalBinary(b); err != nil {
return time.Time{}, err
}
return val, nil
}
func PutBool(db KeyValueWriter, key []byte, b bool) error {
if b {
return db.Put(key, []byte{1})
}
return db.Put(key, []byte{0})
}
func GetBool(db KeyValueReader, key []byte) (bool, error) {
b, err := db.Get(key)
switch {
case err != nil:
return false, err
case len(b) != 1:
return false, fmt.Errorf("length should be 1 but is %d", len(b))
case b[0] != 0 && b[0] != 1:
return false, fmt.Errorf("should be 0 or 1 but is %v", b[0])
}
return b[0] == 1, nil
}
func Count(db Iteratee) (int, error) {
iterator := db.NewIterator()
defer iterator.Release()
count := 0
for iterator.Next() {
count++
}
return count, iterator.Error()
}
func Size(db Iteratee) (int, error) {
iterator := db.NewIterator()
defer iterator.Release()
size := 0
for iterator.Next() {
size += len(iterator.Key()) + len(iterator.Value()) + kvPairOverhead
}
return size, iterator.Error()
}
func IsEmpty(db Iteratee) (bool, error) {
iterator := db.NewIterator()
defer iterator.Release()
return !iterator.Next(), iterator.Error()
}
func Clear(readerDB Iteratee, deleterDB KeyValueDeleter) error {
return ClearPrefix(readerDB, deleterDB, nil)
}
func ClearPrefix(readerDB Iteratee, deleterDB KeyValueDeleter, prefix []byte) error {
iterator := readerDB.NewIteratorWithPrefix(prefix)
defer iterator.Release()
for iterator.Next() {
key := iterator.Key()
if err := deleterDB.Delete(key); err != nil {
return err
}
}
return iterator.Error()
}