/
raft_stable_store.go
46 lines (39 loc) · 1.1 KB
/
raft_stable_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
package rqd
import (
"encoding/binary"
"errors"
"github.com/RealFax/RedQueen/internal/rqd/store"
)
type StableStore struct {
actions store.Actions
}
func (s *StableStore) Set(key []byte, val []byte) error {
return s.actions.Set(key, val)
}
// Get returns the value for key, or an empty byte slice if key was not found.
func (s *StableStore) Get(key []byte) ([]byte, error) {
return store.UnwrapGet(s.actions.Get(key))
}
func (s *StableStore) SetUint64(key []byte, val uint64) error {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, val)
return s.actions.Set(key, buf)
}
// GetUint64 returns the uint64 value for key, or 0 if key was not found.
func (s *StableStore) GetUint64(key []byte) (uint64, error) {
val, err := s.actions.Get(key)
if err != nil {
if errors.Is(err, store.ErrKeyNotFound) {
return 0, errors.New("not found")
}
return 0, err
}
return binary.LittleEndian.Uint64(val.Data), nil
}
func NewStableStore(s store.Store) (*StableStore, error) {
namespace, err := s.Swap("_RaftStableStore")
if err != nil {
return nil, err
}
return &StableStore{actions: namespace}, nil
}