Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to context datastores #283

Merged
merged 2 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions channels/channels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ func TestMigrationsV0(t *testing.T) {
buf := new(bytes.Buffer)
err = channel.MarshalCBOR(buf)
require.NoError(t, err)
err = ds.Put(datastore.NewKey(datatransfer.ChannelID{
err = ds.Put(ctx, datastore.NewKey(datatransfer.ChannelID{
Initiator: initiators[i],
Responder: responders[i],
ID: transferIDs[i],
Expand Down Expand Up @@ -622,7 +622,7 @@ func TestMigrationsV1(t *testing.T) {
buf := new(bytes.Buffer)
err = channel.MarshalCBOR(buf)
require.NoError(t, err)
err = vds.Put(datastore.NewKey(datatransfer.ChannelID{
err = vds.Put(ctx, datastore.NewKey(datatransfer.ChannelID{
Initiator: initiators[i],
Responder: responders[i],
ID: transferIDs[i],
Expand Down
24 changes: 16 additions & 8 deletions cidsets/cidsets.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cidsets

import (
"context"
"sync"

"github.com/ipfs/go-cid"
Expand Down Expand Up @@ -77,12 +78,13 @@ func NewCIDSet(ds datastore.Batching) *cidSet {
// Insert a CID into the set.
// Returns true if the the CID was already in the set.
func (s *cidSet) Insert(c cid.Cid) (exists bool, err error) {
ctx := context.TODO()
s.lk.Lock()
defer s.lk.Unlock()

// Check if the key is in the set already
k := datastore.NewKey(c.String())
has, err := s.ds.Has(k)
has, err := s.ds.Has(ctx, k)
if err != nil {
return false, err
}
Expand All @@ -98,7 +100,7 @@ func (s *cidSet) Insert(c cid.Cid) (exists bool, err error) {
}

// Add the new CID to the set
err = s.ds.Put(k, nil)
err = s.ds.Put(ctx, k, nil)
if err != nil {
return false, err
}
Expand All @@ -118,13 +120,15 @@ func (s *cidSet) Len() (int, error) {
}

func (s *cidSet) unlockedLen() (int, error) {
ctx := context.TODO()

// If the length is already cached, return it
if s.len >= 0 {
return s.len, nil
}

// Query the datastore for all keys
res, err := s.ds.Query(query.Query{KeysOnly: true})
res, err := s.ds.Query(ctx, query.Query{KeysOnly: true})
if err != nil {
return 0, err
}
Expand All @@ -142,10 +146,12 @@ func (s *cidSet) unlockedLen() (int, error) {

// Get all cids in the set as an array
func (s *cidSet) ToArray() ([]cid.Cid, error) {
ctx := context.TODO()

s.lk.Lock()
defer s.lk.Unlock()

res, err := s.ds.Query(query.Query{KeysOnly: true})
res, err := s.ds.Query(ctx, query.Query{KeysOnly: true})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -175,11 +181,13 @@ func (s *cidSet) ToArray() ([]cid.Cid, error) {

// Truncate removes all CIDs in the set
func (s *cidSet) Truncate() error {
ctx := context.TODO()

s.lk.Lock()
defer s.lk.Unlock()

// Get all keys in the datastore
res, err := s.ds.Query(query.Query{KeysOnly: true})
res, err := s.ds.Query(ctx, query.Query{KeysOnly: true})
if err != nil {
return err
}
Expand All @@ -190,21 +198,21 @@ func (s *cidSet) Truncate() error {
}

// Create a batch to perform all deletes as one operation
batched, err := s.ds.Batch()
batched, err := s.ds.Batch(ctx)
if err != nil {
return err
}

// Add delete operations for each key to the batch
for _, entry := range entries {
err := batched.Delete(datastore.NewKey(entry.Key))
err := batched.Delete(ctx, datastore.NewKey(entry.Key))
if err != nil {
return err
}
}

// Commit the batch
err = batched.Commit()
err = batched.Commit(ctx)
if err != nil {
return err
}
Expand Down
26 changes: 13 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,37 @@ go 1.15

require (
github.com/bep/debounce v1.2.0
github.com/filecoin-project/go-ds-versioning v0.1.0
github.com/filecoin-project/go-ds-versioning v0.0.0-20211206185234-508abd7c2aff
github.com/filecoin-project/go-statemachine v0.0.0-20200925024713-05bd7c71fbfe
github.com/hannahhoward/cbor-gen-for v0.0.0-20200817222906-ea96cece81f1
github.com/hannahhoward/go-pubsub v0.0.0-20200423002714-8d62886cc36e
github.com/ipfs/go-block-format v0.0.3
github.com/ipfs/go-blockservice v0.1.3
github.com/ipfs/go-blockservice v0.2.1
github.com/ipfs/go-cid v0.0.7
github.com/ipfs/go-datastore v0.4.5
github.com/ipfs/go-ds-badger v0.2.6
github.com/ipfs/go-graphsync v0.10.4
github.com/ipfs/go-ipfs-blockstore v1.0.1
github.com/ipfs/go-datastore v0.5.1
github.com/ipfs/go-ds-badger v0.3.0
github.com/ipfs/go-graphsync v0.10.6-0.20211119000532-c416dad3bd56
github.com/ipfs/go-ipfs-blockstore v1.1.0
github.com/ipfs/go-ipfs-blocksutil v0.0.1
github.com/ipfs/go-ipfs-chunker v0.0.5
github.com/ipfs/go-ipfs-delay v0.0.1
github.com/ipfs/go-ipfs-exchange-offline v0.0.1
github.com/ipfs/go-ipfs-exchange-offline v0.1.1
github.com/ipfs/go-ipfs-files v0.0.8
github.com/ipfs/go-ipld-cbor v0.0.5
github.com/ipfs/go-ipld-format v0.2.0
github.com/ipfs/go-log/v2 v2.1.1
github.com/ipfs/go-merkledag v0.3.2
github.com/ipfs/go-log/v2 v2.3.0
github.com/ipfs/go-merkledag v0.5.1
github.com/ipfs/go-unixfs v0.2.4
github.com/ipld/go-ipld-prime v0.12.3
github.com/jbenet/go-random v0.0.0-20190219211222-123a90aedc0c
github.com/jpillora/backoff v1.0.0
github.com/libp2p/go-libp2p v0.13.0
github.com/libp2p/go-libp2p v0.14.3
github.com/libp2p/go-libp2p-core v0.8.5
github.com/libp2p/go-libp2p-record v0.1.1 // indirect
github.com/stretchr/testify v1.6.1
github.com/stretchr/testify v1.7.0
github.com/whyrusleeping/cbor-gen v0.0.0-20210219115102-f37d292932f2
go.uber.org/atomic v1.6.0
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
go.uber.org/atomic v1.7.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/tools v0.1.1-0.20210225150353-54dc8c5edb56 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
)
Loading