Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

Commit

Permalink
swarm/state: refactor InmemoryStore (#18143)
Browse files Browse the repository at this point in the history
  • Loading branch information
nonsense authored Nov 21, 2018
1 parent 3fd87f2 commit 4c181e4
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 123 deletions.
4 changes: 2 additions & 2 deletions swarm/network/simulations/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ func init() {

type Simulation struct {
mtx sync.Mutex
stores map[enode.ID]*state.InmemoryStore
stores map[enode.ID]state.Store
}

func NewSimulation() *Simulation {
return &Simulation{
stores: make(map[enode.ID]*state.InmemoryStore),
stores: make(map[enode.ID]state.Store),
}
}

Expand Down
2 changes: 1 addition & 1 deletion swarm/network/stream/delivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (d *Delivery) handleRetrieveRequestMsg(ctx context.Context, sp *Peer, req *
go func() {
chunk, err := d.chunkStore.Get(ctx, req.Addr)
if err != nil {
log.Warn("ChunkStore.Get can not retrieve chunk", "err", err)
log.Warn("ChunkStore.Get can not retrieve chunk", "peer", sp.ID().String(), "addr", req.Addr, "hopcount", req.HopCount, "err", err)
return
}
if req.SkipCheck {
Expand Down
21 changes: 21 additions & 0 deletions swarm/state/dbstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"

"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/storage"
)

// ErrNotFound is returned when no results are returned from the database
Expand All @@ -30,6 +31,15 @@ var ErrNotFound = errors.New("ErrorNotFound")
// ErrInvalidArgument is returned when the argument type does not match the expected type
var ErrInvalidArgument = errors.New("ErrorInvalidArgument")

// Store defines methods required to get, set, delete values for different keys
// and close the underlying resources.
type Store interface {
Get(key string, i interface{}) (err error)
Put(key string, i interface{}) (err error)
Delete(key string) (err error)
Close() error
}

// DBStore uses LevelDB to store values.
type DBStore struct {
db *leveldb.DB
Expand All @@ -46,6 +56,17 @@ func NewDBStore(path string) (s *DBStore, err error) {
}, nil
}

// NewInmemoryStore returns a new instance of DBStore. To be used only in tests and simulations.
func NewInmemoryStore() *DBStore {
db, err := leveldb.Open(storage.NewMemStorage(), nil)
if err != nil {
panic(err)
}
return &DBStore{
db: db,
}
}

// Get retrieves a persisted value for a specific key. If there is no results
// ErrNotFound is returned. The provided parameter should be either a byte slice or
// a struct that implements the encoding.BinaryUnmarshaler interface
Expand Down
94 changes: 0 additions & 94 deletions swarm/state/inmemorystore.go

This file was deleted.

26 changes: 0 additions & 26 deletions swarm/state/store.go

This file was deleted.

0 comments on commit 4c181e4

Please sign in to comment.