11package cidsets
22
33import (
4+ "context"
45 "sync"
56
67 "github.com/ipfs/go-cid"
@@ -77,12 +78,13 @@ func NewCIDSet(ds datastore.Batching) *cidSet {
7778// Insert a CID into the set.
7879// Returns true if the the CID was already in the set.
7980func (s * cidSet ) Insert (c cid.Cid ) (exists bool , err error ) {
81+ ctx := context .TODO ()
8082 s .lk .Lock ()
8183 defer s .lk .Unlock ()
8284
8385 // Check if the key is in the set already
8486 k := datastore .NewKey (c .String ())
85- has , err := s .ds .Has (k )
87+ has , err := s .ds .Has (ctx , k )
8688 if err != nil {
8789 return false , err
8890 }
@@ -98,7 +100,7 @@ func (s *cidSet) Insert(c cid.Cid) (exists bool, err error) {
98100 }
99101
100102 // Add the new CID to the set
101- err = s .ds .Put (k , nil )
103+ err = s .ds .Put (ctx , k , nil )
102104 if err != nil {
103105 return false , err
104106 }
@@ -118,13 +120,15 @@ func (s *cidSet) Len() (int, error) {
118120}
119121
120122func (s * cidSet ) unlockedLen () (int , error ) {
123+ ctx := context .TODO ()
124+
121125 // If the length is already cached, return it
122126 if s .len >= 0 {
123127 return s .len , nil
124128 }
125129
126130 // 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 })
128132 if err != nil {
129133 return 0 , err
130134 }
@@ -142,10 +146,12 @@ func (s *cidSet) unlockedLen() (int, error) {
142146
143147// Get all cids in the set as an array
144148func (s * cidSet ) ToArray () ([]cid.Cid , error ) {
149+ ctx := context .TODO ()
150+
145151 s .lk .Lock ()
146152 defer s .lk .Unlock ()
147153
148- res , err := s .ds .Query (query.Query {KeysOnly : true })
154+ res , err := s .ds .Query (ctx , query.Query {KeysOnly : true })
149155 if err != nil {
150156 return nil , err
151157 }
@@ -175,11 +181,13 @@ func (s *cidSet) ToArray() ([]cid.Cid, error) {
175181
176182// Truncate removes all CIDs in the set
177183func (s * cidSet ) Truncate () error {
184+ ctx := context .TODO ()
185+
178186 s .lk .Lock ()
179187 defer s .lk .Unlock ()
180188
181189 // 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 })
183191 if err != nil {
184192 return err
185193 }
@@ -190,21 +198,21 @@ func (s *cidSet) Truncate() error {
190198 }
191199
192200 // Create a batch to perform all deletes as one operation
193- batched , err := s .ds .Batch ()
201+ batched , err := s .ds .Batch (ctx )
194202 if err != nil {
195203 return err
196204 }
197205
198206 // Add delete operations for each key to the batch
199207 for _ , entry := range entries {
200- err := batched .Delete (datastore .NewKey (entry .Key ))
208+ err := batched .Delete (ctx , datastore .NewKey (entry .Key ))
201209 if err != nil {
202210 return err
203211 }
204212 }
205213
206214 // Commit the batch
207- err = batched .Commit ()
215+ err = batched .Commit (ctx )
208216 if err != nil {
209217 return err
210218 }
0 commit comments