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

wire a context in most of the Datastore methods #85

Closed
wants to merge 1 commit into from
Closed
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()

res, err := oldDS.Query(query.Query{KeysOnly: true})
res, err := oldDS.Query(context.TODO(), query.Query{KeysOnly: true})
if err != nil {
return err
}
Expand Down
33 changes: 20 additions & 13 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package flatfs_test

import (
"bytes"
"context"
"encoding/base32"
"io/ioutil"
"math/rand"
Expand All @@ -16,6 +17,8 @@ import (
)

func TestMove(t *testing.T) {
ctx := context.Background()

tempdir, cleanup := tempdir(t)
defer cleanup()

Expand All @@ -27,7 +30,7 @@ func TestMove(t *testing.T) {
t.Fatalf("WriteFile fail: %v\n", err)
}

keys, blocks := populateDatastore(t, v1dir)
keys, blocks := populateDatastore(t, ctx, v1dir)

v2dir := filepath.Join(tempdir, "v2")
createDatastore(t, v2dir, flatfs.NextToLast(2))
Expand All @@ -47,7 +50,7 @@ func TestMove(t *testing.T) {
}

// check that all keys are available
checkKeys(t, v2dir, keys, blocks)
checkKeys(t, ctx, v2dir, keys, blocks)

// check that a key is in the correct format
shard := filepath.Join(v2dir, flatfs.NextToLast(2).Func()(keys[0].String()))
Expand All @@ -58,6 +61,8 @@ func TestMove(t *testing.T) {
}

func TestMoveRestart(t *testing.T) {
ctx := context.Background()

if runtime.GOOS == "windows" {
t.Skip()
}
Expand All @@ -71,8 +76,8 @@ func TestMoveRestart(t *testing.T) {

createDatastore(t, v2dir, flatfs.NextToLast(5))

keys, blocks := populateDatastore(t, v1dir)
checkKeys(t, v1dir, keys, blocks)
keys, blocks := populateDatastore(t, ctx, v1dir)
checkKeys(t, ctx, v1dir, keys, blocks)

// get a directory in the datastore
noslash := keys[0].String()[1:]
Expand All @@ -95,7 +100,7 @@ func TestMoveRestart(t *testing.T) {
if err != nil {
t.Fatal("Could not undo the move.", err)
}
checkKeys(t, v1dir, keys, blocks)
checkKeys(t, ctx, v1dir, keys, blocks)

// there should be nothing left in the new datastore
rmEmptyDatastore(t, v2dir)
Expand Down Expand Up @@ -123,7 +128,7 @@ func TestMoveRestart(t *testing.T) {
rmEmptyDatastore(t, v1dir)

// make sure everything moved by checking all keys
checkKeys(t, v2dir, keys, blocks)
checkKeys(t, ctx, v2dir, keys, blocks)

// check that a key is in the correct format
shard := filepath.Join(v2dir, flatfs.NextToLast(2).Func()(keys[0].String()))
Expand All @@ -134,13 +139,15 @@ func TestMoveRestart(t *testing.T) {
}

func TestUpgradeDownload(t *testing.T) {
ctx := context.Background()

tempdir, cleanup := tempdir(t)
defer cleanup()

createDatastore(t, tempdir, flatfs.Prefix(3))

keys, blocks := populateDatastore(t, tempdir)
checkKeys(t, tempdir, keys, blocks)
keys, blocks := populateDatastore(t, ctx, tempdir)
checkKeys(t, ctx, tempdir, keys, blocks)

err := flatfs.UpgradeV0toV1(tempdir, 3)
if err == nil {
Expand All @@ -165,7 +172,7 @@ func TestUpgradeDownload(t *testing.T) {
}

// This will fail unless the repository is in the new version
checkKeys(t, tempdir, keys, blocks)
checkKeys(t, ctx, tempdir, keys, blocks)
}

func TestDownloadNonPrefix(t *testing.T) {
Expand Down Expand Up @@ -194,7 +201,7 @@ func rmEmptyDatastore(t *testing.T, dir string) {
}
}

func populateDatastore(t *testing.T, dir string) ([]datastore.Key, [][]byte) {
func populateDatastore(t *testing.T, ctx context.Context, dir string) ([]datastore.Key, [][]byte) {
ds, err := flatfs.Open(dir, false)
if err != nil {
t.Fatalf("Open fail: %v\n", err)
Expand All @@ -211,7 +218,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(ctx, keys[i], blocks[i])
if err != nil {
t.Fatalf("Put fail: %v\n", err)
}
Expand All @@ -220,15 +227,15 @@ func populateDatastore(t *testing.T, dir string) ([]datastore.Key, [][]byte) {
return keys, blocks
}

func checkKeys(t *testing.T, dir string, keys []datastore.Key, blocks [][]byte) {
func checkKeys(t *testing.T, ctx context.Context, dir string, keys []datastore.Key, blocks [][]byte) {
ds, err := flatfs.Open(dir, false)
if err != nil {
t.Fatalf("Open fail: %v\n", err)
}
defer ds.Close()

for i, key := range keys {
data, err := ds.Get(key)
data, err := ds.Get(ctx, key)
if err != nil {
t.Fatalf("Get fail: %v\n", err)
}
Expand Down
23 changes: 12 additions & 11 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 @@ -1211,28 +1212,28 @@ func (fs *Datastore) Batch() (datastore.Batch, error) {
}, 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