-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
47 lines (38 loc) · 893 Bytes
/
util.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
package raftbadger
import (
"bytes"
"encoding/binary"
"encoding/gob"
"sync"
)
var bufPool = sync.Pool{
New: func() interface{} { return new(bytes.Buffer) },
}
var rdrPool = sync.Pool{
New: func() interface{} { return new(bytes.Reader) },
}
// Converts bytes to an integer
func bytesToUint64(b []byte) uint64 {
return binary.BigEndian.Uint64(b)
}
// Converts a uint to a byte slice
func uint64ToBytes(u uint64) []byte {
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, u)
return buf
}
func gobEncode(v interface{}) (*bytes.Buffer, error) {
buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()
if err := gob.NewEncoder(buf).Encode(v); err != nil {
bufPool.Put(buf)
return nil, err
}
return buf, nil
}
func gobDecode(p []byte, v interface{}) error {
rdr := rdrPool.Get().(*bytes.Reader)
rdr.Reset(p)
defer rdrPool.Put(rdr)
return gob.NewDecoder(rdr).Decode(v)
}