Skip to content

Commit

Permalink
moved new db from config to factory
Browse files Browse the repository at this point in the history
  • Loading branch information
ssd04 committed Jan 31, 2024
1 parent f8fcf67 commit 80d33b9
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 129 deletions.
30 changes: 30 additions & 0 deletions factory/db.go
Original file line number Diff line number Diff line change
@@ -1 +1,31 @@
package factory

import (
"github.com/multiversx/mx-chain-storage-go/common"
"github.com/multiversx/mx-chain-storage-go/leveldb"
"github.com/multiversx/mx-chain-storage-go/memorydb"
"github.com/multiversx/mx-chain-storage-go/types"
)

// ArgDB is a structure that is used to create a new storage.Persister implementation
type ArgDB struct {
DBType common.DBType
Path string
BatchDelaySeconds int
MaxBatchSize int
MaxOpenFiles int
}

// NewDB creates a new database from database config
func NewDB(argDB ArgDB) (types.Persister, error) {
switch argDB.DBType {
case common.LvlDB:
return leveldb.NewDB(argDB.Path, argDB.BatchDelaySeconds, argDB.MaxBatchSize, argDB.MaxOpenFiles)
case common.LvlDBSerial:
return leveldb.NewSerialDB(argDB.Path, argDB.BatchDelaySeconds, argDB.MaxBatchSize, argDB.MaxOpenFiles)
case common.MemoryDB:
return memorydb.New(), nil
default:
return nil, common.ErrNotSupportedDBType
}
}
63 changes: 63 additions & 0 deletions factory/db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package factory_test

import (
"testing"

"github.com/multiversx/mx-chain-storage-go/common"
"github.com/multiversx/mx-chain-storage-go/factory"
"github.com/stretchr/testify/assert"
)

func TestCreateDBFromConfWrongType(t *testing.T) {
t.Parallel()

argsDB := factory.ArgDB{
DBType: "NotLvlDB",
Path: "test",
BatchDelaySeconds: 10,
MaxBatchSize: 10,
MaxOpenFiles: 10,
}
persister, err := factory.NewDB(argsDB)

assert.NotNil(t, err, "error expected")
assert.Nil(t, persister, "persister expected to be nil, but got %s", persister)
}

func TestCreateDBFromConfWrongFileNameLvlDB(t *testing.T) {
if testing.Short() {
t.Skip("this is not a short test")
}

path := ""
argsDB := factory.ArgDB{
DBType: common.LvlDB,
Path: path,
BatchDelaySeconds: 10,
MaxBatchSize: 10,
MaxOpenFiles: 10,
}
persister, err := factory.NewDB(argsDB)
assert.NotNil(t, err, "error expected")
assert.Nil(t, persister, "persister expected to be nil, but got %s", persister)
}

func TestCreateDBFromConfLvlDBOk(t *testing.T) {
t.Parallel()

path := t.TempDir()

argsDB := factory.ArgDB{
DBType: common.LvlDB,
Path: path,
BatchDelaySeconds: 10,
MaxBatchSize: 10,
MaxOpenFiles: 10,
}
persister, err := factory.NewDB(argsDB)
assert.Nil(t, err, "no error expected")
assert.NotNil(t, persister, "valid persister expected but got nil")

err = persister.Destroy()
assert.Nil(t, err, "no error expected destroying the persister")
}
32 changes: 32 additions & 0 deletions factory/storageUnit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package factory

import (
"github.com/multiversx/mx-chain-storage-go/common"
"github.com/multiversx/mx-chain-storage-go/storageUnit"
)

// NewStorageUnitFromConf creates a new storage unit from a storage unit config
func NewStorageUnitFromConf(cacheConf common.CacheConfig, dbConf common.DBConfig) (*storageUnit.Unit, error) {
if dbConf.MaxBatchSize > int(cacheConf.Capacity) {
return nil, common.ErrCacheSizeIsLowerThanBatchSize
}

cache, err := NewCache(cacheConf)
if err != nil {
return nil, err
}

argDB := ArgDB{
DBType: dbConf.Type,
Path: dbConf.FilePath,
BatchDelaySeconds: dbConf.BatchDelaySeconds,
MaxBatchSize: dbConf.MaxBatchSize,
MaxOpenFiles: dbConf.MaxOpenFiles,
}
db, err := NewDB(argDB)
if err != nil {
return nil, err
}

return storageUnit.NewStorageUnit(cache, db)
}
105 changes: 105 additions & 0 deletions factory/storageUnit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package factory_test

import (
"testing"

"github.com/multiversx/mx-chain-storage-go/common"
"github.com/multiversx/mx-chain-storage-go/factory"
"github.com/stretchr/testify/assert"
)

func TestNewStorageUnitFromConf_WrongCacheSizeVsBatchSize(t *testing.T) {
t.Parallel()

storer, err := factory.NewStorageUnitFromConf(common.CacheConfig{
Capacity: 10,
Type: common.LRUCache,
}, common.DBConfig{
FilePath: "Blocks",
Type: common.LvlDB,
MaxBatchSize: 11,
BatchDelaySeconds: 1,
MaxOpenFiles: 10,
},
)

assert.NotNil(t, err, "error expected")
assert.Nil(t, storer, "storer expected to be nil but got %s", storer)
}

func TestNewStorageUnitFromConf_WrongCacheConfig(t *testing.T) {
t.Parallel()

storer, err := factory.NewStorageUnitFromConf(common.CacheConfig{
Capacity: 10,
Type: "NotLRU",
}, common.DBConfig{
FilePath: "Blocks",
Type: common.LvlDB,
BatchDelaySeconds: 1,
MaxBatchSize: 1,
MaxOpenFiles: 10,
},
)

assert.NotNil(t, err, "error expected")
assert.Nil(t, storer, "storer expected to be nil but got %s", storer)
}

func TestNewStorageUnitFromConf_WrongDBConfig(t *testing.T) {
t.Parallel()

storer, err := factory.NewStorageUnitFromConf(common.CacheConfig{
Capacity: 10,
Type: common.LRUCache,
}, common.DBConfig{
FilePath: "Blocks",
Type: "NotLvlDB",
},
)

assert.NotNil(t, err, "error expected")
assert.Nil(t, storer, "storer expected to be nil but got %s", storer)
}

func TestNewStorageUnitFromConf_LvlDBOk(t *testing.T) {
t.Parallel()

storer, err := factory.NewStorageUnitFromConf(common.CacheConfig{
Capacity: 10,
Type: common.LRUCache,
}, common.DBConfig{
FilePath: "Blocks",
Type: common.LvlDB,
MaxBatchSize: 1,
BatchDelaySeconds: 1,
MaxOpenFiles: 10,
},
)

assert.Nil(t, err, "no error expected but got %s", err)
assert.NotNil(t, storer, "valid storer expected but got nil")
err = storer.DestroyUnit()
assert.Nil(t, err, "no error expected destroying the persister")
}

func TestNewStorageUnitFromConf_ShouldWorkLvlDB(t *testing.T) {
t.Parallel()

storer, err := factory.NewStorageUnitFromConf(common.CacheConfig{
Capacity: 10,
Type: common.LRUCache,
}, common.DBConfig{
FilePath: "Blocks",
Type: common.LvlDB,
BatchDelaySeconds: 1,
MaxBatchSize: 1,
MaxOpenFiles: 10,
},
)

assert.Nil(t, err, "no error expected but got %s", err)
assert.NotNil(t, storer, "valid storer expected but got nil")
err = storer.DestroyUnit()
assert.Nil(t, err, "no error expected destroying the persister")
}
3 changes: 3 additions & 0 deletions storageUnit/nilStorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package storageUnit
import (
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-storage-go/common"
"github.com/multiversx/mx-chain-storage-go/types"
)

var _ types.Storer = (*NilStorer)(nil)

// NilStorer resembles a disabled implementation of the Storer interface
type NilStorer struct {
}
Expand Down
27 changes: 0 additions & 27 deletions storageUnit/storageunit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/multiversx/mx-chain-core-go/data"
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/multiversx/mx-chain-storage-go/common"
"github.com/multiversx/mx-chain-storage-go/factory"
"github.com/multiversx/mx-chain-storage-go/types"
)

Expand Down Expand Up @@ -50,32 +49,6 @@ func NewStorageUnit(c types.Cacher, p types.Persister) (*Unit, error) {
return sUnit, nil
}

// NewStorageUnitFromConf creates a new storage unit from a storage unit config
func NewStorageUnitFromConf(cacheConf common.CacheConfig, dbConf common.DBConfig, persisterFactory PersisterFactoryHandler) (*Unit, error) {
var cache types.Cacher
var db types.Persister
var err error

// TODO: if there will be a differentiation between the creation or opening of a DB, the DB could be destroyed
// in case of a failure while creating (not opening).

if dbConf.MaxBatchSize > int(cacheConf.Capacity) {
return nil, common.ErrCacheSizeIsLowerThanBatchSize
}

cache, err = factory.NewCache(cacheConf)
if err != nil {
return nil, err
}

db, err = persisterFactory.CreateWithRetries(dbConf.FilePath)
if err != nil {
return nil, err
}

return NewStorageUnit(cache, db)
}

// Put adds data to both cache and persistence medium
func (u *Unit) Put(key, data []byte) error {
u.lock.Lock()
Expand Down
Loading

0 comments on commit 80d33b9

Please sign in to comment.