-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
134 lines (105 loc) · 2.8 KB
/
storage.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
package tenvis
import (
"fmt"
"github.com/go-redis/redis"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
)
const (
VotingKey = "voting"
VotingSerialKey = "voting_serial"
LedgersKey = "ledgers"
LedgerSerialKey = "ledger_serial"
)
type Storage struct {
client *redis.Client
}
func (s *Storage) SetVoting(v *Voting) error {
bytes, err := proto.Marshal(v)
if err != nil {
return errors.Wrap(err, "failed to proto marshal")
}
return s.client.Watch(func(tx *redis.Tx) error {
id, err := s.client.Incr(VotingSerialKey).Result()
if err != nil {
return errors.Wrap(err, "failed to INCR voting serial")
}
if err := s.client.HSet(VotingKey, fmt.Sprint(id), bytes).Err(); err != nil {
return errors.Wrap(err, "HSET failed")
}
return nil
})
}
func (s *Storage) UpdateVoting(v *Voting) error {
bytes, err := proto.Marshal(v)
if err != nil {
return errors.Wrap(err, "failed to proto marshal")
}
if err := s.client.HSet(VotingKey, fmt.Sprint(v.Id), bytes).Err(); err != nil {
return errors.Wrap(err, "HSET failed")
}
return nil
}
func (s *Storage) GetVoting(id uint64) (*Voting, error) {
bytes, err := s.client.HGet(VotingKey, fmt.Sprint(id)).Bytes()
if err == redis.Nil {
return nil, nil
}
if err != nil {
return nil, errors.Wrapf(err, "HGET failed")
}
voting := new(Voting)
if err := proto.Unmarshal(bytes, voting); err != nil {
return nil, errors.Wrapf(err, "failed to proto unmarshal")
}
return voting, nil
}
func (s *Storage) SetLedger(l *Ledger) error {
bytes, err := proto.Marshal(l)
if err != nil {
return errors.Wrap(err, "failed to proto marshal")
}
return s.client.Watch(func(tx *redis.Tx) error {
if err := tx.Set(LedgerSerialKey, l.Index, 0).Err(); err != nil {
return errors.Wrap(err, "failed to SET ledger index")
}
if err := tx.HSet(LedgersKey, fmt.Sprint(l.Index), bytes).Err(); err != nil {
return errors.Wrap(err, "failed to HSET ledger")
}
return nil
})
}
func (s *Storage) GetLedger(id uint64) (*Ledger, error) {
bytes, err := s.client.HGet(LedgersKey, fmt.Sprint(id)).Bytes()
if err == redis.Nil {
return nil, nil
}
if err != nil {
return nil, errors.Wrapf(err, "HGET failed")
}
ledger := new(Ledger)
if err := proto.Unmarshal(bytes, ledger); err != nil {
return nil, errors.Wrapf(err, "failed to proto unmarshal")
}
return ledger, nil
}
func (s *Storage) GetLastLedgerIndex() (uint64, error) {
index, err := s.client.Get(LedgerSerialKey).Uint64()
if err == redis.Nil {
return 0, nil
}
if err != nil {
return 0, errors.Wrap(err, "failed to GET")
}
return index, nil
}
func (s *Storage) GetLastVotingID() (uint64, error) {
id, err := s.client.Get(VotingSerialKey).Uint64()
if err == redis.Nil {
return 0, nil
}
if err != nil {
return 0, errors.Wrap(err, "failed to GET")
}
return id, nil
}