-
Notifications
You must be signed in to change notification settings - Fork 175
/
collections.go
167 lines (139 loc) · 4.06 KB
/
collections.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
package badger
import (
"errors"
"fmt"
"github.com/dgraph-io/badger/v2"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/storage"
"github.com/onflow/flow-go/storage/badger/operation"
)
type Collections struct {
db *badger.DB
transactions *Transactions
}
func NewCollections(db *badger.DB, transactions *Transactions) *Collections {
c := &Collections{
db: db,
transactions: transactions,
}
return c
}
func (c *Collections) StoreLight(collection *flow.LightCollection) error {
err := operation.RetryOnConflict(c.db.Update, operation.InsertCollection(collection))
if err != nil {
return fmt.Errorf("could not insert collection: %w", err)
}
return nil
}
func (c *Collections) Store(collection *flow.Collection) error {
return operation.RetryOnConflict(c.db.Update, func(btx *badger.Txn) error {
light := collection.Light()
err := operation.SkipDuplicates(operation.InsertCollection(&light))(btx)
if err != nil {
return fmt.Errorf("could not insert collection: %w", err)
}
for _, tx := range collection.Transactions {
err = c.transactions.storeTx(tx)(btx)
if err != nil {
return fmt.Errorf("could not insert transaction: %w", err)
}
}
return nil
})
}
func (c *Collections) ByID(colID flow.Identifier) (*flow.Collection, error) {
var (
light flow.LightCollection
collection flow.Collection
)
err := c.db.View(func(btx *badger.Txn) error {
err := operation.RetrieveCollection(colID, &light)(btx)
if err != nil {
if errors.Is(err, badger.ErrKeyNotFound) {
return storage.ErrNotFound
}
return fmt.Errorf("could not retrieve collection: %w", err)
}
for _, txID := range light.Transactions {
tx, err := c.transactions.ByID(txID)
if err != nil {
if errors.Is(err, badger.ErrKeyNotFound) {
return storage.ErrNotFound
}
return fmt.Errorf("could not retrieve transaction: %w", err)
}
collection.Transactions = append(collection.Transactions, tx)
}
return nil
})
if err != nil {
return nil, err
}
return &collection, nil
}
func (c *Collections) LightByID(colID flow.Identifier) (*flow.LightCollection, error) {
var collection flow.LightCollection
err := c.db.View(func(tx *badger.Txn) error {
err := operation.RetrieveCollection(colID, &collection)(tx)
if err != nil {
if errors.Is(err, badger.ErrKeyNotFound) {
return storage.ErrNotFound
}
return fmt.Errorf("could not retrieve collection: %w", err)
}
return nil
})
if err != nil {
return nil, err
}
return &collection, nil
}
func (c *Collections) Remove(colID flow.Identifier) error {
return operation.RetryOnConflict(c.db.Update, func(btx *badger.Txn) error {
err := operation.RemoveCollection(colID)(btx)
if err != nil {
return fmt.Errorf("could not remove collection: %w", err)
}
return nil
})
}
func (c *Collections) StoreLightAndIndexByTransaction(collection *flow.LightCollection) error {
return operation.RetryOnConflict(c.db.Update, func(tx *badger.Txn) error {
err := operation.InsertCollection(collection)(tx)
if err != nil {
return fmt.Errorf("could not insert collection: %w", err)
}
for _, t := range collection.Transactions {
err = operation.IndexCollectionByTransaction(t, collection.ID())(tx)
if err != nil {
return fmt.Errorf("could not insert transaction ID: %w", err)
}
}
return nil
})
}
func (c *Collections) LightByTransactionID(txID flow.Identifier) (*flow.LightCollection, error) {
var collection flow.LightCollection
err := c.db.View(func(tx *badger.Txn) error {
collID := &flow.Identifier{}
err := operation.RetrieveCollectionID(txID, collID)(tx)
if err != nil {
if errors.Is(err, badger.ErrKeyNotFound) {
return storage.ErrNotFound
}
return fmt.Errorf("could not retrieve collection id: %w", err)
}
err = operation.RetrieveCollection(*collID, &collection)(tx)
if err != nil {
if errors.Is(err, badger.ErrKeyNotFound) {
return storage.ErrNotFound
}
return fmt.Errorf("could not retrieve collection: %w", err)
}
return nil
})
if err != nil {
return nil, err
}
return &collection, nil
}