| 1 |
package raftboltdb |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/binary" |
| 6 |
|
| 7 |
"github.com/hashicorp/go-msgpack/codec" |
| 8 |
) |
| 9 |
|
| 10 |
// Decode reverses the encode operation on a byte slice input |
| 11 |
func decodeMsgPack(buf []byte, out interface{}) error { |
| 12 |
r := bytes.NewBuffer(buf) |
| 13 |
hd := codec.MsgpackHandle{} |
| 14 |
dec := codec.NewDecoder(r, &hd) |
| 15 |
return dec.Decode(out) |
| 16 |
} |
| 17 |
|
| 18 |
// Encode writes an encoded object to a new bytes buffer |
| 19 |
func encodeMsgPack(in interface{}) (*bytes.Buffer, error) { |
| 20 |
buf := bytes.NewBuffer(nil) |
| 21 |
hd := codec.MsgpackHandle{} |
| 22 |
enc := codec.NewEncoder(buf, &hd) |
| 23 |
err := enc.Encode(in) |
| 24 |
return buf, err |
| 25 |
} |
| 26 |
|
| 27 |
// Converts bytes to an integer |
| 28 |
func bytesToUint64(b []byte) uint64 { |
| 29 |
return binary.BigEndian.Uint64(b) |
| 30 |
} |
| 31 |
|
| 32 |
// Converts a uint to a byte slice |
| 33 |
func uint64ToBytes(u uint64) []byte { |
| 34 |
buf := make([]byte, 8) |
| 35 |
binary.BigEndian.PutUint64(buf, u) |
| 36 |
return buf |
| 37 |
} |