-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ var ( | |
Step_02 = step_02 | ||
Step_03 = step_03 | ||
Step_04 = step_04 | ||
Step_05 = step_05 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package migration | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/ethersphere/bee/pkg/log" | ||
"github.com/ethersphere/bee/pkg/storage" | ||
"github.com/ethersphere/bee/pkg/storer/internal/upload" | ||
) | ||
|
||
// step_05 is a migration step that removes all upload items from the store. | ||
func step_05(st storage.BatchedStore) error { | ||
logger := log.NewLogger("migration-step-05", log.WithSink(os.Stdout)) | ||
logger.Info("start removing upload items") | ||
|
||
var itemsToDelete []storage.Item | ||
err := upload.IterateAll(st, func(u storage.Item) (bool, error) { | ||
itemsToDelete = append(itemsToDelete, u) | ||
return false, nil | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("iterate upload items: %w", err) | ||
} | ||
|
||
for _, item := range itemsToDelete { | ||
err := st.Delete(item) | ||
if err != nil { | ||
return fmt.Errorf("delete upload item: %w", err) | ||
} | ||
} | ||
logger.Info("finished removing upload items") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package migration_test | ||
|
||
import ( | ||
"context" | ||
"github.com/ethersphere/bee/pkg/swarm" | ||
"testing" | ||
|
||
"github.com/ethersphere/bee/pkg/storage" | ||
chunktest "github.com/ethersphere/bee/pkg/storage/testing" | ||
"github.com/ethersphere/bee/pkg/storer/internal" | ||
"github.com/ethersphere/bee/pkg/storer/internal/upload" | ||
localmigration "github.com/ethersphere/bee/pkg/storer/migration" | ||
) | ||
|
||
func Test_Step_05(t *testing.T) { | ||
t.Parallel() | ||
|
||
st, closer := internal.NewInmemStorage() | ||
t.Cleanup(func() { | ||
err := closer() | ||
if err != nil { | ||
t.Errorf("closing storage: %v", err) | ||
} | ||
}) | ||
|
||
tag, err := upload.NextTag(st.IndexStore()) | ||
if err != nil { | ||
t.Fatalf("create tag: %v", err) | ||
} | ||
|
||
putter, err := upload.NewPutter(st, tag.TagID) | ||
if err != nil { | ||
t.Fatalf("create putter: %v", err) | ||
} | ||
ctx := context.Background() | ||
chunks := chunktest.GenerateTestRandomChunks(10) | ||
b, err := st.IndexStore().Batch(ctx) | ||
if err != nil { | ||
t.Fatalf("create batch: %v", err) | ||
} | ||
|
||
for _, ch := range chunks { | ||
err := putter.Put(ctx, st, b, ch) | ||
if err != nil { | ||
t.Fatalf("put chunk: %v", err) | ||
} | ||
} | ||
err = putter.Close(st, st.IndexStore(), swarm.RandAddress(t)) | ||
if err != nil { | ||
t.Fatalf("close putter: %v", err) | ||
} | ||
err = b.Commit() | ||
if err != nil { | ||
t.Fatalf("commit batch: %v", err) | ||
} | ||
|
||
wantCount := func(t *testing.T, want int) { | ||
count := 0 | ||
err = upload.IterateAll(st.IndexStore(), func(_ storage.Item) (bool, error) { | ||
count++ | ||
return false, nil | ||
}) | ||
if err != nil { | ||
t.Fatalf("iterate upload items: %v", err) | ||
} | ||
if count != want { | ||
t.Fatalf("expected %d upload items, got %d", want, count) | ||
} | ||
} | ||
|
||
wantCount(t, 10) | ||
err = localmigration.Step_05(st.IndexStore()) | ||
if err != nil { | ||
t.Fatalf("step 05: %v", err) | ||
} | ||
wantCount(t, 0) | ||
} |