Skip to content
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
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# go-ds-flatfs

[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
[![Coverage Status](https://img.shields.io/codecov/c/github/ipfs/go-ds-flatfs.svg)](https://codecov.io/gh/ipfs/go-ds-flatfs)
[![Build Status](https://img.shields.io/github/actions/workflow/status/ipfs/go-ds-flatfs/go-test.yml?branch=master)](https://github.com/ipfs/go-ds-flatfs/actions)
[![GoDoc](https://pkg.go.dev/badge/github.com/ipfs/go-ds-flatfs)](https://pkg.go.dev/github.com/ipfs/go-ds-flatfs)
Expand Down
65 changes: 30 additions & 35 deletions batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ func TestBatchWritesToTempUntilCommit(t *testing.T) {

func testBatchWritesToTempUntilCommit(dirFunc mkShardFunc, t *testing.T) {
temp := t.TempDir()
defer checkTemp(t, temp)

ffs, err := flatfs.CreateOrOpen(temp, dirFunc(2), false)
if err != nil {
t.Fatalf("New fail: %v\n", err)
}
defer ffs.Close()

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(t.Context())
defer cancel()

// Create a batch
Expand Down Expand Up @@ -119,7 +118,7 @@ func TestBatchReadOperations(t *testing.T) {

func testBatchReadOperations(dirFunc mkShardFunc, t *testing.T) {
temp := t.TempDir()
defer checkTemp(t, temp)
ctx := t.Context()

ffs, err := flatfs.CreateOrOpen(temp, dirFunc(2), false)
if err != nil {
Expand All @@ -130,13 +129,13 @@ func testBatchReadOperations(dirFunc mkShardFunc, t *testing.T) {
// Put some initial data in the datastore
initialKey := datastore.NewKey("INITIAL")
initialData := []byte("initial data")
err = ffs.Put(context.Background(), initialKey, initialData)
err = ffs.Put(ctx, initialKey, initialData)
if err != nil {
t.Fatal(err)
}

// Create a batch
batch, err := ffs.Batch(context.Background())
batch, err := ffs.Batch(ctx)
if err != nil {
t.Fatal(err)
}
Expand All @@ -150,32 +149,32 @@ func testBatchReadOperations(dirFunc mkShardFunc, t *testing.T) {
// Put a new key in batch
batchKey := datastore.NewKey("BATCH")
batchData := []byte("batch data")
err = batch.Put(context.Background(), batchKey, batchData)
err = batch.Put(ctx, batchKey, batchData)
if err != nil {
t.Fatal(err)
}

// Overwrite initial key in batch
overwriteData := []byte("overwritten data")
err = batch.Put(context.Background(), initialKey, overwriteData)
err = batch.Put(ctx, initialKey, overwriteData)
if err != nil {
t.Fatal(err)
}

// Delete a key that will be created
deleteKey := datastore.NewKey("TODELETE")
err = ffs.Put(context.Background(), deleteKey, []byte("to be deleted"))
err = ffs.Put(ctx, deleteKey, []byte("to be deleted"))
if err != nil {
t.Fatal(err)
}
err = batch.Delete(context.Background(), deleteKey)
err = batch.Delete(ctx, deleteKey)
if err != nil {
t.Fatal(err)
}

// Test Get operations before commit
// 1. Get from batch (new key)
data, err := batchReader.Get(context.Background(), batchKey)
data, err := batchReader.Get(ctx, batchKey)
if err != nil {
t.Fatal(err)
}
Expand All @@ -184,7 +183,7 @@ func testBatchReadOperations(dirFunc mkShardFunc, t *testing.T) {
}

// 2. Get overwritten key should return new data from batch
data, err = batchReader.Get(context.Background(), initialKey)
data, err = batchReader.Get(ctx, initialKey)
if err != nil {
t.Fatal(err)
}
Expand All @@ -193,14 +192,14 @@ func testBatchReadOperations(dirFunc mkShardFunc, t *testing.T) {
}

// 3. Get deleted key should return not found
_, err = batchReader.Get(context.Background(), deleteKey)
_, err = batchReader.Get(ctx, deleteKey)
if err != datastore.ErrNotFound {
t.Errorf("expected ErrNotFound for deleted key, got %v", err)
}

// Test Has operations before commit
// 1. Has for new key in batch
has, err := batchReader.Has(context.Background(), batchKey)
has, err := batchReader.Has(ctx, batchKey)
if err != nil {
t.Fatal(err)
}
Expand All @@ -209,7 +208,7 @@ func testBatchReadOperations(dirFunc mkShardFunc, t *testing.T) {
}

// 2. Has for overwritten key
has, err = batchReader.Has(context.Background(), initialKey)
has, err = batchReader.Has(ctx, initialKey)
if err != nil {
t.Fatal(err)
}
Expand All @@ -218,7 +217,7 @@ func testBatchReadOperations(dirFunc mkShardFunc, t *testing.T) {
}

// 3. Has for deleted key should return false
has, err = batchReader.Has(context.Background(), deleteKey)
has, err = batchReader.Has(ctx, deleteKey)
if err != nil {
t.Fatal(err)
}
Expand All @@ -227,7 +226,7 @@ func testBatchReadOperations(dirFunc mkShardFunc, t *testing.T) {
}

// Test GetSize operations before commit
size, err := batchReader.GetSize(context.Background(), batchKey)
size, err := batchReader.GetSize(ctx, batchKey)
if err != nil {
t.Fatal(err)
}
Expand All @@ -236,13 +235,13 @@ func testBatchReadOperations(dirFunc mkShardFunc, t *testing.T) {
}

// GetSize for deleted key should return not found
_, err = batchReader.GetSize(context.Background(), deleteKey)
_, err = batchReader.GetSize(ctx, deleteKey)
if err != datastore.ErrNotFound {
t.Errorf("expected ErrNotFound for deleted key size, got %v", err)
}

// Main datastore should still have original data
data, err = ffs.Get(context.Background(), initialKey)
data, err = ffs.Get(ctx, initialKey)
if err != nil {
t.Fatal(err)
}
Expand All @@ -251,14 +250,14 @@ func testBatchReadOperations(dirFunc mkShardFunc, t *testing.T) {
}

// Commit the batch
err = batch.Commit(context.Background())
err = batch.Commit(ctx)
if err != nil {
t.Fatal(err)
}

// Verify final state in main datastore
// 1. New key should exist
data, err = ffs.Get(context.Background(), batchKey)
data, err = ffs.Get(ctx, batchKey)
if err != nil {
t.Fatal(err)
}
Expand All @@ -267,7 +266,7 @@ func testBatchReadOperations(dirFunc mkShardFunc, t *testing.T) {
}

// 2. Initial key should be overwritten
data, err = ffs.Get(context.Background(), initialKey)
data, err = ffs.Get(ctx, initialKey)
if err != nil {
t.Fatal(err)
}
Expand All @@ -276,7 +275,7 @@ func testBatchReadOperations(dirFunc mkShardFunc, t *testing.T) {
}

// 3. Deleted key should not exist
_, err = ffs.Get(context.Background(), deleteKey)
_, err = ffs.Get(ctx, deleteKey)
if err != datastore.ErrNotFound {
t.Errorf("expected ErrNotFound for deleted key after commit, got %v", err)
}
Expand All @@ -288,7 +287,7 @@ func TestBatchDiscard(t *testing.T) {

func testBatchDiscard(dirFunc mkShardFunc, t *testing.T) {
temp := t.TempDir()
defer checkTemp(t, temp)
ctx := t.Context()

ffs, err := flatfs.CreateOrOpen(temp, dirFunc(2), false)
if err != nil {
Expand All @@ -297,15 +296,15 @@ func testBatchDiscard(dirFunc mkShardFunc, t *testing.T) {
defer ffs.Close()

// Create a batch
batch, err := ffs.Batch(context.Background())
batch, err := ffs.Batch(ctx)
if err != nil {
t.Fatal(err)
}

// Put some keys
keys := []string{"QUUX", "QAAX", "QBBX"}
for _, key := range keys {
err = batch.Put(context.Background(), datastore.NewKey(key), []byte("testdata"))
err = batch.Put(ctx, datastore.NewKey(key), []byte("testdata"))
if err != nil {
t.Fatal(err)
}
Expand All @@ -318,14 +317,14 @@ func testBatchDiscard(dirFunc mkShardFunc, t *testing.T) {
}

// Discard the batch
err = discardable.Discard(context.Background())
err = discardable.Discard(ctx)
if err != nil {
t.Fatal(err)
}

// Check that files still don't exist in the main datastore
for _, key := range keys {
has, err := ffs.Has(context.Background(), datastore.NewKey(key))
has, err := ffs.Has(ctx, datastore.NewKey(key))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -479,15 +478,14 @@ func TestBatchQuery(t *testing.T) {

func testBatchQuery(dirFunc mkShardFunc, t *testing.T) {
temp := t.TempDir()
defer checkTemp(t, temp)

ffs, err := flatfs.CreateOrOpen(temp, dirFunc(2), false)
if err != nil {
t.Fatalf("CreateOrOpen fail: %v\n", err)
}
defer ffs.Close()

ctx := context.Background()
ctx := t.Context()

// Add some data to the main datastore
mainKeys := []string{"EXISTING1", "EXISTING2", "EXISTING3"}
Expand Down Expand Up @@ -679,15 +677,14 @@ func TestConcurrentDuplicateBatchWrites(t *testing.T) {
)

temp := t.TempDir()
defer checkTemp(t, temp)

ffs, err := flatfs.CreateOrOpen(temp, flatfs.Suffix(2), false)
if err != nil {
t.Fatalf("New fail: %v\n", err)
}
defer ffs.Close()

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
ctx, cancel := context.WithTimeout(t.Context(), time.Minute)
defer cancel()

batches := make([]datastore.Batch, 0, numBatches)
Expand Down Expand Up @@ -795,18 +792,16 @@ func TestConcurrentDuplicateBatchWrites(t *testing.T) {
// Now commit concurrently.
start = make(chan struct{})
errs = make(chan error, len(batches))
wgDone.Add(len(batches))
wgReady.Add(len(batches))
for _, batch := range batches {
go func() {
wgDone.Go(func() {
wgReady.Done()
<-start
defer wgDone.Done()
err := batch.Commit(ctx)
if err != nil {
errs <- err
}
}()
})
}

wgReady.Wait() // wait for all goroutines to be ready
Expand Down
8 changes: 5 additions & 3 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,18 @@ func populateDatastore(t *testing.T, dir string) ([]datastore.Key, [][]byte) {
}
defer ds.Close()

ctx := t.Context()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
var blocks [][]byte
var keys []datastore.Key
for i := 0; i < 256; i++ {
for i := range 256 {
blk := make([]byte, 1000)
r.Read(blk)
blocks = append(blocks, blk)

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

ctx := t.Context()
for i, key := range keys {
data, err := ds.Get(bg, key)
data, err := ds.Get(ctx, key)
if err != nil {
t.Fatalf("Get fail: %v\n", err)
}
Expand Down
Loading
Loading