1
1
package cidsets
2
2
3
3
import (
4
+ "context"
4
5
"sync"
5
6
6
7
"github.com/ipfs/go-cid"
@@ -77,12 +78,13 @@ func NewCIDSet(ds datastore.Batching) *cidSet {
77
78
// Insert a CID into the set.
78
79
// Returns true if the the CID was already in the set.
79
80
func (s * cidSet ) Insert (c cid.Cid ) (exists bool , err error ) {
81
+ ctx := context .TODO ()
80
82
s .lk .Lock ()
81
83
defer s .lk .Unlock ()
82
84
83
85
// Check if the key is in the set already
84
86
k := datastore .NewKey (c .String ())
85
- has , err := s .ds .Has (k )
87
+ has , err := s .ds .Has (ctx , k )
86
88
if err != nil {
87
89
return false , err
88
90
}
@@ -98,7 +100,7 @@ func (s *cidSet) Insert(c cid.Cid) (exists bool, err error) {
98
100
}
99
101
100
102
// Add the new CID to the set
101
- err = s .ds .Put (k , nil )
103
+ err = s .ds .Put (ctx , k , nil )
102
104
if err != nil {
103
105
return false , err
104
106
}
@@ -118,13 +120,15 @@ func (s *cidSet) Len() (int, error) {
118
120
}
119
121
120
122
func (s * cidSet ) unlockedLen () (int , error ) {
123
+ ctx := context .TODO ()
124
+
121
125
// If the length is already cached, return it
122
126
if s .len >= 0 {
123
127
return s .len , nil
124
128
}
125
129
126
130
// Query the datastore for all keys
127
- res , err := s .ds .Query (query.Query {KeysOnly : true })
131
+ res , err := s .ds .Query (ctx , query.Query {KeysOnly : true })
128
132
if err != nil {
129
133
return 0 , err
130
134
}
@@ -142,10 +146,12 @@ func (s *cidSet) unlockedLen() (int, error) {
142
146
143
147
// Get all cids in the set as an array
144
148
func (s * cidSet ) ToArray () ([]cid.Cid , error ) {
149
+ ctx := context .TODO ()
150
+
145
151
s .lk .Lock ()
146
152
defer s .lk .Unlock ()
147
153
148
- res , err := s .ds .Query (query.Query {KeysOnly : true })
154
+ res , err := s .ds .Query (ctx , query.Query {KeysOnly : true })
149
155
if err != nil {
150
156
return nil , err
151
157
}
@@ -175,11 +181,13 @@ func (s *cidSet) ToArray() ([]cid.Cid, error) {
175
181
176
182
// Truncate removes all CIDs in the set
177
183
func (s * cidSet ) Truncate () error {
184
+ ctx := context .TODO ()
185
+
178
186
s .lk .Lock ()
179
187
defer s .lk .Unlock ()
180
188
181
189
// Get all keys in the datastore
182
- res , err := s .ds .Query (query.Query {KeysOnly : true })
190
+ res , err := s .ds .Query (ctx , query.Query {KeysOnly : true })
183
191
if err != nil {
184
192
return err
185
193
}
@@ -190,21 +198,21 @@ func (s *cidSet) Truncate() error {
190
198
}
191
199
192
200
// Create a batch to perform all deletes as one operation
193
- batched , err := s .ds .Batch ()
201
+ batched , err := s .ds .Batch (ctx )
194
202
if err != nil {
195
203
return err
196
204
}
197
205
198
206
// Add delete operations for each key to the batch
199
207
for _ , entry := range entries {
200
- err := batched .Delete (datastore .NewKey (entry .Key ))
208
+ err := batched .Delete (ctx , datastore .NewKey (entry .Key ))
201
209
if err != nil {
202
210
return err
203
211
}
204
212
}
205
213
206
214
// Commit the batch
207
- err = batched .Commit ()
215
+ err = batched .Commit (ctx )
208
216
if err != nil {
209
217
return err
210
218
}
0 commit comments