Skip to content

Commit e607411

Browse files
committed
refactor chunk data packs storage
1 parent 0d2b34f commit e607411

12 files changed

+352
-541
lines changed

Diff for: storage/badger/all.go

-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ func InitAll(metrics module.CacheMetrics, db *badger.DB) *storage.All {
2929
transactionResults := NewTransactionResults(metrics, db, 10000)
3030
collections := NewCollections(db, transactions)
3131
events := NewEvents(metrics, db)
32-
chunkDataPacks := NewChunkDataPacks(metrics, db, collections, 1000)
3332

3433
return &storage.All{
3534
Headers: headers,
@@ -46,7 +45,6 @@ func InitAll(metrics module.CacheMetrics, db *badger.DB) *storage.All {
4645
VersionBeacons: versionBeacons,
4746
Results: results,
4847
Receipts: receipts,
49-
ChunkDataPacks: chunkDataPacks,
5048
Commits: commits,
5149
Transactions: transactions,
5250
TransactionResults: transactionResults,

Diff for: storage/badger/chunk_data_pack_test.go

-143
This file was deleted.

Diff for: storage/badger/operation/chunkDataPacks.go

-35
This file was deleted.

Diff for: storage/chunkDataPacks.go renamed to storage/chunk_data_packs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type ChunkDataPacks interface {
2121
// BatchRemove removes ChunkDataPack c keyed by its ChunkID in provided batch
2222
// No errors are expected during normal operation, even if no entries are matched.
2323
// If Badger unexpectedly fails to process the request, the error is wrapped in a generic error and returned.
24-
BatchRemove(chunkID flow.Identifier, batch BatchStorage) error
24+
BatchRemove(chunkID flow.Identifier, batch ReaderBatchWriter) error
2525
}
2626

2727
// StoredChunkDataPack is an in-storage representation of chunk data pack.

Diff for: storage/operation/chunk_data_packs.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package operation
2+
3+
import (
4+
"github.com/onflow/flow-go/model/flow"
5+
"github.com/onflow/flow-go/storage"
6+
)
7+
8+
// InsertChunkDataPack inserts a chunk data pack keyed by chunk ID.
9+
// any error are exceptions
10+
func InsertChunkDataPack(w storage.Writer, c *storage.StoredChunkDataPack) error {
11+
return UpsertByKey(w, MakePrefix(codeChunkDataPack, c.ChunkID), c)
12+
}
13+
14+
// RetrieveChunkDataPack retrieves a chunk data pack by chunk ID.
15+
// it returns storage.ErrNotFound if the chunk data pack is not found
16+
func RetrieveChunkDataPack(r storage.Reader, chunkID flow.Identifier, c *storage.StoredChunkDataPack) error {
17+
return RetrieveByKey(r, MakePrefix(codeChunkDataPack, chunkID), c)
18+
}
19+
20+
// RemoveChunkDataPack removes the chunk data pack with the given chunk ID.
21+
// any error are exceptions
22+
func RemoveChunkDataPack(w storage.Writer, chunkID flow.Identifier) error {
23+
return RemoveByKey(w, MakePrefix(codeChunkDataPack, chunkID))
24+
}

Diff for: storage/badger/operation/chunkDataPacks_test.go renamed to storage/operation/chunk_data_packs_test.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
package operation
1+
package operation_test
22

33
import (
44
"testing"
55

6-
"github.com/dgraph-io/badger/v2"
76
"github.com/stretchr/testify/assert"
87
"github.com/stretchr/testify/require"
98

109
"github.com/onflow/flow-go/storage"
10+
"github.com/onflow/flow-go/storage/operation"
11+
"github.com/onflow/flow-go/storage/operation/dbtest"
1112
"github.com/onflow/flow-go/utils/unittest"
1213
)
1314

1415
func TestChunkDataPack(t *testing.T) {
15-
unittest.RunWithBadgerDB(t, func(db *badger.DB) {
16+
dbtest.RunWithDB(t, func(t *testing.T, db storage.DB) {
1617
collectionID := unittest.IdentifierFixture()
1718
expected := &storage.StoredChunkDataPack{
1819
ChunkID: unittest.IdentifierFixture(),
@@ -23,27 +24,31 @@ func TestChunkDataPack(t *testing.T) {
2324

2425
t.Run("Retrieve non-existent", func(t *testing.T) {
2526
var actual storage.StoredChunkDataPack
26-
err := db.View(RetrieveChunkDataPack(expected.ChunkID, &actual))
27+
err := operation.RetrieveChunkDataPack(db.Reader(), expected.ChunkID, &actual)
2728
assert.Error(t, err)
2829
})
2930

3031
t.Run("Save", func(t *testing.T) {
31-
err := db.Update(InsertChunkDataPack(expected))
32+
err := db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
33+
return operation.InsertChunkDataPack(rw.Writer(), expected)
34+
})
3235
require.NoError(t, err)
3336

3437
var actual storage.StoredChunkDataPack
35-
err = db.View(RetrieveChunkDataPack(expected.ChunkID, &actual))
38+
err = operation.RetrieveChunkDataPack(db.Reader(), expected.ChunkID, &actual)
3639
assert.NoError(t, err)
3740

3841
assert.Equal(t, *expected, actual)
3942
})
4043

4144
t.Run("Remove", func(t *testing.T) {
42-
err := db.Update(RemoveChunkDataPack(expected.ChunkID))
45+
err := db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
46+
return operation.RemoveChunkDataPack(rw.Writer(), expected.ChunkID)
47+
})
4348
require.NoError(t, err)
4449

4550
var actual storage.StoredChunkDataPack
46-
err = db.View(RetrieveChunkDataPack(expected.ChunkID, &actual))
51+
err = operation.RetrieveChunkDataPack(db.Reader(), expected.ChunkID, &actual)
4752
assert.Error(t, err)
4853
})
4954
})

0 commit comments

Comments
 (0)