-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore.go
204 lines (164 loc) · 4.09 KB
/
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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package memory
import (
"context"
"math/rand"
"sort"
"sync"
"github.com/code-payments/code-server/pkg/database/query"
"github.com/code-payments/code-server/pkg/code/data/nonce"
)
type store struct {
mu sync.Mutex
records []*nonce.Record
last uint64
}
type ById []*nonce.Record
func (a ById) Len() int { return len(a) }
func (a ById) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ById) Less(i, j int) bool { return a[i].Id < a[j].Id }
func New() nonce.Store {
return &store{
records: make([]*nonce.Record, 0),
last: 0,
}
}
func (s *store) reset() {
s.mu.Lock()
s.records = make([]*nonce.Record, 0)
s.last = 0
s.mu.Unlock()
}
func (s *store) find(data *nonce.Record) *nonce.Record {
for _, item := range s.records {
if item.Id == data.Id {
return item
}
if item.Address == data.Address {
return item
}
}
return nil
}
func (s *store) findAddress(address string) *nonce.Record {
for _, item := range s.records {
if item.Address == address {
return item
}
}
return nil
}
func (s *store) findByState(state nonce.State) []*nonce.Record {
res := make([]*nonce.Record, 0)
for _, item := range s.records {
if item.State == state {
res = append(res, item)
}
}
return res
}
func (s *store) findByStateAndPurpose(state nonce.State, purpose nonce.Purpose) []*nonce.Record {
res := make([]*nonce.Record, 0)
for _, item := range s.records {
if item.State != state {
continue
}
if item.Purpose != purpose {
continue
}
res = append(res, item)
}
return res
}
func (s *store) filter(items []*nonce.Record, cursor query.Cursor, limit uint64, direction query.Ordering) []*nonce.Record {
var start uint64
start = 0
if direction == query.Descending {
start = s.last + 1
}
if len(cursor) > 0 {
start = cursor.ToUint64()
}
var res []*nonce.Record
for _, item := range items {
if item.Id > start && direction == query.Ascending {
res = append(res, item)
}
if item.Id < start && direction == query.Descending {
res = append(res, item)
}
}
if direction == query.Descending {
sort.Sort(sort.Reverse(ById(res)))
}
if len(res) >= int(limit) {
return res[:limit]
}
return res
}
func (s *store) Count(ctx context.Context) (uint64, error) {
s.mu.Lock()
defer s.mu.Unlock()
return uint64(len(s.records)), nil
}
func (s *store) CountByState(ctx context.Context, state nonce.State) (uint64, error) {
s.mu.Lock()
defer s.mu.Unlock()
res := s.findByState(state)
return uint64(len(res)), nil
}
func (s *store) CountByStateAndPurpose(ctx context.Context, state nonce.State, purpose nonce.Purpose) (uint64, error) {
s.mu.Lock()
defer s.mu.Unlock()
res := s.findByStateAndPurpose(state, purpose)
return uint64(len(res)), nil
}
func (s *store) Save(ctx context.Context, data *nonce.Record) error {
if err := data.Validate(); err != nil {
return err
}
s.mu.Lock()
defer s.mu.Unlock()
s.last++
if item := s.find(data); item != nil {
item.Blockhash = data.Blockhash
item.State = data.State
item.Signature = data.Signature
} else {
if data.Id == 0 {
data.Id = s.last
}
c := data.Clone()
s.records = append(s.records, &c)
}
return nil
}
func (s *store) Get(ctx context.Context, address string) (*nonce.Record, error) {
s.mu.Lock()
defer s.mu.Unlock()
if item := s.findAddress(address); item != nil {
return item, nil
}
return nil, nonce.ErrNonceNotFound
}
func (s *store) GetAllByState(ctx context.Context, state nonce.State, cursor query.Cursor, limit uint64, direction query.Ordering) ([]*nonce.Record, error) {
s.mu.Lock()
defer s.mu.Unlock()
if items := s.findByState(state); len(items) > 0 {
res := s.filter(items, cursor, limit, direction)
if len(res) == 0 {
return nil, nonce.ErrNonceNotFound
}
return res, nil
}
return nil, nonce.ErrNonceNotFound
}
func (s *store) GetRandomAvailableByPurpose(ctx context.Context, purpose nonce.Purpose) (*nonce.Record, error) {
s.mu.Lock()
defer s.mu.Unlock()
items := s.findByStateAndPurpose(nonce.StateAvailable, purpose)
if len(items) == 0 {
return nil, nonce.ErrNonceNotFound
}
index := rand.Intn(len(items))
return items[index], nil
}