-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore.go
257 lines (205 loc) · 8.92 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package postgres
import (
"context"
"database/sql"
"errors"
"github.com/jmoiron/sqlx"
pgutil "github.com/code-payments/code-server/pkg/database/postgres"
"github.com/code-payments/code-server/pkg/database/query"
"github.com/code-payments/code-server/pkg/code/data/fulfillment"
)
type store struct {
db *sqlx.DB
}
func New(db *sql.DB) fulfillment.Store {
return &store{
db: sqlx.NewDb(db, "pgx"),
}
}
// Count implements fulfillment.Store.Count
func (s *store) Count(ctx context.Context) (uint64, error) {
return dbGetCount(ctx, s.db)
}
// CountByState implements fulfillment.Store.CountByState
func (s *store) CountByState(ctx context.Context, state fulfillment.State) (uint64, error) {
return dbGetCountByState(ctx, s.db, state)
}
// CountByStateGroupedByType implements fulfillment.Store.CountByStateGroupedByType
func (s *store) CountByStateGroupedByType(ctx context.Context, state fulfillment.State) (map[fulfillment.Type]uint64, error) {
return dbGetCountByStateGroupedByType(ctx, s.db, state)
}
// CountForMetrics implements fulfillment.Store.CountForMetrics
func (s *store) CountForMetrics(ctx context.Context, state fulfillment.State) (map[fulfillment.Type]uint64, error) {
return dbGetCountForMetrics(ctx, s.db, state)
}
// CountByStateAndAddress implements fulfillment.Store.CountByStateAndAddress
func (s *store) CountByStateAndAddress(ctx context.Context, state fulfillment.State, address string) (uint64, error) {
return dbGetCountByStateAndAddress(ctx, s.db, state, address)
}
// CountByTypeStateAndAddress implements fulfillment.Store.CountByTypeStateAndAddress
func (s *store) CountByTypeStateAndAddress(ctx context.Context, fulfillmentType fulfillment.Type, state fulfillment.State, address string) (uint64, error) {
return dbGetCountByTypeStateAndAddress(ctx, s.db, fulfillmentType, state, address)
}
// CountByTypeStateAndAddressAsSource implements fulfillment.Store.CountByTypeStateAndAddressAsSource
func (s *store) CountByTypeStateAndAddressAsSource(ctx context.Context, fulfillmentType fulfillment.Type, state fulfillment.State, address string) (uint64, error) {
return dbGetCountByTypeStateAndAddressAsSource(ctx, s.db, fulfillmentType, state, address)
}
// CountByIntentAndState implements fulfillment.Store.CountByIntentAndState
func (s *store) CountByIntentAndState(ctx context.Context, intent string, state fulfillment.State) (uint64, error) {
return dbGetCountByIntentAndState(ctx, s.db, intent, state)
}
// CountByIntent implements fulfillment.Store.CountByIntent
func (s *store) CountByIntent(ctx context.Context, intent string) (uint64, error) {
return dbGetCountByIntent(ctx, s.db, intent)
}
// CountByTypeActionAndState implements fulfillment.Store.CountByTypeActionAndState
func (s *store) CountByTypeActionAndState(ctx context.Context, intentId string, actionId uint32, fulfillmentType fulfillment.Type, state fulfillment.State) (uint64, error) {
return dbGetCountByTypeActionAndState(ctx, s.db, intentId, actionId, fulfillmentType, state)
}
// CountPendingByType implements fulfillment.Store.CountPendingByType
func (s *store) CountPendingByType(ctx context.Context) (map[fulfillment.Type]uint64, error) {
return dbGetCountPendingByType(ctx, s.db)
}
// PutAll implements fulfillment.Store.PutAll
func (s *store) PutAll(ctx context.Context, records ...*fulfillment.Record) error {
models := make([]*fulfillmentModel, len(records))
for i, record := range records {
model, err := toFulfillmentModel(record)
if err != nil {
return err
}
models[i] = model
}
return pgutil.ExecuteInTx(ctx, s.db, sql.LevelDefault, func(tx *sqlx.Tx) error {
updated, err := dbPutAllInTx(ctx, tx, models)
if err != nil {
return err
}
if len(updated) != len(records) {
return errors.New("unexpected count of fulfillment models returned")
}
for i, model := range updated {
converted := fromFulfillmentModel(model)
converted.CopyTo(records[i])
}
return nil
})
}
// Update implements fulfillment.Store.Update
func (s *store) Update(ctx context.Context, record *fulfillment.Record) error {
obj, err := toFulfillmentModel(record)
if err != nil {
return err
}
err = obj.dbUpdate(ctx, s.db)
if err != nil {
return err
}
res := fromFulfillmentModel(obj)
res.CopyTo(record)
return nil
}
// MarkAsActivelyScheduled implements fulfillment.Store.MarkAsActivelyScheduled
func (s *store) MarkAsActivelyScheduled(ctx context.Context, id uint64) error {
return dbMarkAsActivelyScheduled(ctx, s.db, id)
}
// ActivelyScheduleTreasuryAdvances implements fulfillment.Store.ActivelyScheduleTreasuryAdvances
func (s *store) ActivelyScheduleTreasuryAdvances(ctx context.Context, treasury string, intentOrderingIndex uint64, limit int) (uint64, error) {
return dbActivelyScheduleTreasuryAdvances(ctx, s.db, treasury, intentOrderingIndex, limit)
}
// GetById implements fulfillment.Store.GetById
func (s *store) GetById(ctx context.Context, id uint64) (*fulfillment.Record, error) {
obj, err := dbGetById(ctx, s.db, id)
if err != nil {
return nil, err
}
return fromFulfillmentModel(obj), nil
}
// GetBySignature implements fulfillment.Store.GetBySignature
func (s *store) GetBySignature(ctx context.Context, signature string) (*fulfillment.Record, error) {
obj, err := dbGetBySignature(ctx, s.db, signature)
if err != nil {
return nil, err
}
return fromFulfillmentModel(obj), nil
}
// GetAllByState implements fulfillment.Store.GetAllByState
func (s *store) GetAllByState(ctx context.Context, state fulfillment.State, includeDisabledActiveScheduling bool, cursor query.Cursor, limit uint64, direction query.Ordering) ([]*fulfillment.Record, error) {
models, err := dbGetAllByState(ctx, s.db, state, includeDisabledActiveScheduling, cursor, limit, direction)
if err != nil {
return nil, err
}
fulfillments := make([]*fulfillment.Record, len(models))
for i, model := range models {
fulfillments[i] = fromFulfillmentModel(model)
}
return fulfillments, nil
}
// GetAllByIntent implements fulfillment.Store.GetAllByIntent
func (s *store) GetAllByIntent(ctx context.Context, intent string, cursor query.Cursor, limit uint64, direction query.Ordering) ([]*fulfillment.Record, error) {
models, err := dbGetAllByIntent(ctx, s.db, intent, cursor, limit, direction)
if err != nil {
return nil, err
}
fulfillments := make([]*fulfillment.Record, len(models))
for i, model := range models {
fulfillments[i] = fromFulfillmentModel(model)
}
return fulfillments, nil
}
// GetAllByAction implements fulfillment.Store.GetAllByAction
func (s *store) GetAllByAction(ctx context.Context, intent string, actionId uint32) ([]*fulfillment.Record, error) {
models, err := dbGetAllByAction(ctx, s.db, intent, actionId)
if err != nil {
return nil, err
}
fulfillments := make([]*fulfillment.Record, len(models))
for i, model := range models {
fulfillments[i] = fromFulfillmentModel(model)
}
return fulfillments, nil
}
// GetAllByTypeAndAction implements fulfillment.Store.GetAllByTypeAndAction
func (s *store) GetAllByTypeAndAction(ctx context.Context, fulfillmentType fulfillment.Type, intentId string, actionId uint32) ([]*fulfillment.Record, error) {
models, err := dbGetAllByTypeAndAction(ctx, s.db, fulfillmentType, intentId, actionId)
if err != nil {
return nil, err
}
fulfillments := make([]*fulfillment.Record, len(models))
for i, model := range models {
fulfillments[i] = fromFulfillmentModel(model)
}
return fulfillments, nil
}
// GetFirstSchedulableByAddressAsSource implements fulfillment.Store.GetFirstSchedulableByAddressAsSource
func (s *store) GetFirstSchedulableByAddressAsSource(ctx context.Context, address string) (*fulfillment.Record, error) {
model, err := dbGetFirstSchedulableByAddressAsSource(ctx, s.db, address)
if err != nil {
return nil, err
}
return fromFulfillmentModel(model), nil
}
// GetFirstSchedulableByAddressAsDestination implements fulfillment.Store.GetFirstSchedulableByAddressAsDestination
func (s *store) GetFirstSchedulableByAddressAsDestination(ctx context.Context, address string) (*fulfillment.Record, error) {
model, err := dbGetFirstSchedulableByAddressAsDestination(ctx, s.db, address)
if err != nil {
return nil, err
}
return fromFulfillmentModel(model), nil
}
// GetFirstSchedulableByType implements fulfillment.Store.GetFirstSchedulableByType
func (s *store) GetFirstSchedulableByType(ctx context.Context, fulfillmentType fulfillment.Type) (*fulfillment.Record, error) {
model, err := dbGetFirstSchedulableByType(ctx, s.db, fulfillmentType)
if err != nil {
return nil, err
}
return fromFulfillmentModel(model), nil
}
// GetNextSchedulableByAddress implements fulfillment.Store.GetNextSchedulableByAddress
func (s *store) GetNextSchedulableByAddress(ctx context.Context, address string, intentOrderingIndex uint64, actionOrderingIndex, fulfillmentOrderingIndex uint32) (*fulfillment.Record, error) {
model, err := dbGetNextSchedulableByAddress(ctx, s.db, address, intentOrderingIndex, actionOrderingIndex, fulfillmentOrderingIndex)
if err != nil {
return nil, err
}
return fromFulfillmentModel(model), nil
}