Skip to content

Commit c3c8728

Browse files
author
Peter Mrekaj
authored
feat(localstore): add migration for batch-index (ethersphere#2112)
1 parent 2c84af8 commit c3c8728

12 files changed

+589
-337
lines changed

cmd/bee/cmd/db.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func dbExportCmd(cmd *cobra.Command) {
5858

5959
path := filepath.Join(dataDir, "localstore")
6060

61-
storer, err := localstore.New(path, nil, nil, logger)
61+
storer, err := localstore.New(path, nil, nil, nil, logger)
6262
if err != nil {
6363
return fmt.Errorf("localstore: %w", err)
6464
}
@@ -118,7 +118,7 @@ func dbImportCmd(cmd *cobra.Command) {
118118

119119
path := filepath.Join(dataDir, "localstore")
120120

121-
storer, err := localstore.New(path, nil, nil, logger)
121+
storer, err := localstore.New(path, nil, nil, nil, logger)
122122
if err != nil {
123123
return fmt.Errorf("localstore: %w", err)
124124
}

pkg/localstore/gc_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ func TestDB_gcSize(t *testing.T) {
486486
t.Fatal(err)
487487
}
488488
logger := logging.New(ioutil.Discard, 0)
489-
db, err := New(dir, baseKey, nil, logger)
489+
db, err := New(dir, baseKey, nil, nil, logger)
490490
if err != nil {
491491
t.Fatal(err)
492492
}
@@ -514,7 +514,7 @@ func TestDB_gcSize(t *testing.T) {
514514
if err := db.Close(); err != nil {
515515
t.Fatal(err)
516516
}
517-
db, err = New(dir, baseKey, nil, logger)
517+
db, err = New(dir, baseKey, nil, nil, logger)
518518
if err != nil {
519519
t.Fatal(err)
520520
}

pkg/localstore/localstore.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"time"
2626

2727
"github.com/ethersphere/bee/pkg/logging"
28+
"github.com/ethersphere/bee/pkg/pinning"
2829
"github.com/ethersphere/bee/pkg/postage"
2930
"github.com/ethersphere/bee/pkg/postage/batchstore"
3031
"github.com/ethersphere/bee/pkg/shed"
@@ -62,6 +63,9 @@ type DB struct {
6263
shed *shed.DB
6364
tags *tags.Tags
6465

66+
// stateStore is needed to access the pinning Service.Pins() method.
67+
stateStore storage.StateStorer
68+
6569
// schema name of loaded data
6670
schemaName shed.StringField
6771

@@ -176,7 +180,7 @@ type Options struct {
176180
// New returns a new DB. All fields and indexes are initialized
177181
// and possible conflicts with schema from existing database is checked.
178182
// One goroutine for writing batches is created.
179-
func New(path string, baseKey []byte, o *Options, logger logging.Logger) (db *DB, err error) {
183+
func New(path string, baseKey []byte, ss storage.StateStorer, o *Options, logger logging.Logger) (db *DB, err error) {
180184
if o == nil {
181185
// default options
182186
o = &Options{
@@ -185,6 +189,7 @@ func New(path string, baseKey []byte, o *Options, logger logging.Logger) (db *DB
185189
}
186190

187191
db = &DB{
192+
stateStore: ss,
188193
cacheCapacity: o.Capacity,
189194
baseKey: baseKey,
190195
tags: o.Tags,
@@ -241,7 +246,7 @@ func New(path string, baseKey []byte, o *Options, logger logging.Logger) (db *DB
241246
}
242247
if schemaName == "" {
243248
// initial new localstore run
244-
err := db.schemaName.Put(DbSchemaCurrent)
249+
err := db.schemaName.Put(DBSchemaCurrent)
245250
if err != nil {
246251
return nil, err
247252
}
@@ -585,6 +590,16 @@ func (db *DB) DebugIndices() (indexInfo map[string]int, err error) {
585590
return indexInfo, err
586591
}
587592

593+
// stateStoreHasPins returns true if the state-store
594+
// contains any pins, otherwise false is returned.
595+
func (db *DB) stateStoreHasPins() (bool, error) {
596+
pins, err := pinning.NewService(nil, db.stateStore, nil).Pins()
597+
if err != nil {
598+
return false, err
599+
}
600+
return len(pins) > 0, nil
601+
}
602+
588603
// chunkToItem creates new Item with data provided by the Chunk.
589604
func chunkToItem(ch swarm.Chunk) shed.Item {
590605
return shed.Item{

pkg/localstore/localstore_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func newTestDB(t testing.TB, o *Options) *DB {
158158
t.Fatal(err)
159159
}
160160
logger := logging.New(ioutil.Discard, 0)
161-
db, err := New("", baseKey, o, logger)
161+
db, err := New("", baseKey, nil, o, logger)
162162
if err != nil {
163163
t.Fatal(err)
164164
}

0 commit comments

Comments
 (0)