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

feat: add contexts on datastore methods #98

Merged
merged 1 commit into from
Nov 10, 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
3 changes: 2 additions & 1 deletion convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package flatfs

import (
"context"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -59,7 +60,7 @@ func Move(oldPath string, newPath string, out io.Writer) error {
}
newDS.deactivate()

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move() should probably take a context eventually. Fine for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I didn't bother since it's only used by the CLI

res, err := oldDS.Query(query.Query{KeysOnly: true})
res, err := oldDS.Query(context.Background(), query.Query{KeysOnly: true})
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func populateDatastore(t *testing.T, dir string) ([]datastore.Key, [][]byte) {

key := "X" + base32.StdEncoding.EncodeToString(blk[:8])
keys = append(keys, datastore.NewKey(key))
err := ds.Put(keys[i], blocks[i])
err := ds.Put(bg, keys[i], blocks[i])
if err != nil {
t.Fatalf("Put fail: %v\n", err)
}
Expand All @@ -228,7 +228,7 @@ func checkKeys(t *testing.T, dir string, keys []datastore.Key, blocks [][]byte)
defer ds.Close()

for i, key := range keys {
data, err := ds.Get(key)
data, err := ds.Get(bg, key)
if err != nil {
t.Fatalf("Get fail: %v\n", err)
}
Expand Down
25 changes: 13 additions & 12 deletions flatfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package flatfs

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -393,7 +394,7 @@ func (fs *Datastore) renameAndUpdateDiskUsage(tmpPath, path string) error {
// one arrived slightly later than the other. In the case of a
// concurrent Put and a Delete operation, we cannot guarantee which one
// will win.
func (fs *Datastore) Put(key datastore.Key, value []byte) error {
func (fs *Datastore) Put(ctx context.Context, key datastore.Key, value []byte) error {
if !keyIsValid(key) {
return fmt.Errorf("when putting '%q': %v", key, ErrInvalidKey)
}
Expand All @@ -412,7 +413,7 @@ func (fs *Datastore) Put(key datastore.Key, value []byte) error {
return err
}

func (fs *Datastore) Sync(prefix datastore.Key) error {
func (fs *Datastore) Sync(ctx context.Context, prefix datastore.Key) error {
fs.shutdownLock.RLock()
defer fs.shutdownLock.RUnlock()
if fs.shutdown {
Expand Down Expand Up @@ -644,7 +645,7 @@ func (fs *Datastore) putMany(data map[datastore.Key][]byte) error {
return nil
}

func (fs *Datastore) Get(key datastore.Key) (value []byte, err error) {
func (fs *Datastore) Get(ctx context.Context, key datastore.Key) (value []byte, err error) {
// Can't exist in datastore.
if !keyIsValid(key) {
return nil, datastore.ErrNotFound
Expand All @@ -662,7 +663,7 @@ func (fs *Datastore) Get(key datastore.Key) (value []byte, err error) {
return data, nil
}

func (fs *Datastore) Has(key datastore.Key) (exists bool, err error) {
func (fs *Datastore) Has(ctx context.Context, key datastore.Key) (exists bool, err error) {
// Can't exist in datastore.
if !keyIsValid(key) {
return false, nil
Expand All @@ -679,7 +680,7 @@ func (fs *Datastore) Has(key datastore.Key) (exists bool, err error) {
}
}

func (fs *Datastore) GetSize(key datastore.Key) (size int, err error) {
func (fs *Datastore) GetSize(ctx context.Context, key datastore.Key) (size int, err error) {
// Can't exist in datastore.
if !keyIsValid(key) {
return -1, datastore.ErrNotFound
Expand All @@ -699,7 +700,7 @@ func (fs *Datastore) GetSize(key datastore.Key) (size int, err error) {
// Delete removes a key/value from the Datastore. Please read
// the Put() explanation about the handling of concurrent write
// operations to the same key.
func (fs *Datastore) Delete(key datastore.Key) error {
func (fs *Datastore) Delete(ctx context.Context, key datastore.Key) error {
// Can't exist in datastore.
if !keyIsValid(key) {
return nil
Expand Down Expand Up @@ -744,7 +745,7 @@ func (fs *Datastore) doDelete(key datastore.Key) error {
return err
}

func (fs *Datastore) Query(q query.Query) (query.Results, error) {
func (fs *Datastore) Query(ctx context.Context, q query.Query) (query.Results, error) {
prefix := datastore.NewKey(q.Prefix).String()
if prefix != "/" {
// This datastore can't include keys with multiple components.
Expand Down Expand Up @@ -1203,36 +1204,36 @@ type flatfsBatch struct {
ds *Datastore
}

func (fs *Datastore) Batch() (datastore.Batch, error) {
func (fs *Datastore) Batch(_ context.Context) (datastore.Batch, error) {
return &flatfsBatch{
puts: make(map[datastore.Key][]byte),
deletes: make(map[datastore.Key]struct{}),
ds: fs,
}, nil
}

func (bt *flatfsBatch) Put(key datastore.Key, val []byte) error {
func (bt *flatfsBatch) Put(ctx context.Context, key datastore.Key, val []byte) error {
if !keyIsValid(key) {
return fmt.Errorf("when putting '%q': %v", key, ErrInvalidKey)
}
bt.puts[key] = val
return nil
}

func (bt *flatfsBatch) Delete(key datastore.Key) error {
func (bt *flatfsBatch) Delete(ctx context.Context, key datastore.Key) error {
if keyIsValid(key) {
bt.deletes[key] = struct{}{}
} // otherwise, delete is a no-op anyways.
return nil
}

func (bt *flatfsBatch) Commit() error {
func (bt *flatfsBatch) Commit(ctx context.Context) error {
if err := bt.ds.putMany(bt.puts); err != nil {
return err
}

for k := range bt.deletes {
if err := bt.ds.Delete(k); err != nil {
if err := bt.ds.Delete(ctx, k); err != nil {
return err
}
}
Expand Down
Loading