-
Notifications
You must be signed in to change notification settings - Fork 1
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
7 changed files
with
233 additions
and
129 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
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 | ||
} | ||
} |
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,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") | ||
} |
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,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) | ||
} |
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,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") | ||
} |
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
Oops, something went wrong.