Skip to content

Commit

Permalink
integrate factory from storage
Browse files Browse the repository at this point in the history
  • Loading branch information
ssd04 committed Jan 31, 2024
1 parent 7bfe861 commit 922d528
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 71 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/multiversx/mx-chain-es-indexer-go v1.4.18-0.20231228064619-e3b0caf29058
github.com/multiversx/mx-chain-logger-go v1.0.14-0.20231215125130-a3bed6e76040
github.com/multiversx/mx-chain-scenario-go v1.2.2-0.20231129113427-ad3056f45296
github.com/multiversx/mx-chain-storage-go v1.0.15-0.20240103193554-5ad54212812d
github.com/multiversx/mx-chain-storage-go v1.0.15-0.20240131142608-5c126467749c
github.com/multiversx/mx-chain-vm-common-go v1.5.10-0.20231228070003-ae14e1e0adfa
github.com/multiversx/mx-chain-vm-go v1.5.23-0.20231228064104-964359cb8dd3
github.com/multiversx/mx-chain-vm-v1_2-go v1.2.65-0.20231228071026-eed2cb19c216
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ github.com/multiversx/mx-chain-storage-go v1.0.15-0.20231213110622-e222ba96a9f4
github.com/multiversx/mx-chain-storage-go v1.0.15-0.20231213110622-e222ba96a9f4/go.mod h1:ioCT2oHQ+TyHQYpgjxzlUdy7dCdv56+w5HnBg9z96eY=
github.com/multiversx/mx-chain-storage-go v1.0.15-0.20240103193554-5ad54212812d h1:mNf2qlDGSNp6yd4rSJBT93vGseuqraj8/jWWXm1ro+k=
github.com/multiversx/mx-chain-storage-go v1.0.15-0.20240103193554-5ad54212812d/go.mod h1:ioCT2oHQ+TyHQYpgjxzlUdy7dCdv56+w5HnBg9z96eY=
github.com/multiversx/mx-chain-storage-go v1.0.15-0.20240131142608-5c126467749c h1:Fr0PM4Kh33QqTHyIqzRQqx049zNvmeKKSCxCFfB/JK4=
github.com/multiversx/mx-chain-storage-go v1.0.15-0.20240131142608-5c126467749c/go.mod h1:ioCT2oHQ+TyHQYpgjxzlUdy7dCdv56+w5HnBg9z96eY=
github.com/multiversx/mx-chain-vm-common-go v1.5.10-0.20231228070003-ae14e1e0adfa h1:xdDeUC4yOfiUwctkYioYMjjigBZoZo5RZq1e5WoCVRs=
github.com/multiversx/mx-chain-vm-common-go v1.5.10-0.20231228070003-ae14e1e0adfa/go.mod h1:7jjGRykSfLeMs6iQdszlE0lGK2xp9/cctiVdeKbQLLM=
github.com/multiversx/mx-chain-vm-go v1.5.23-0.20231228064104-964359cb8dd3 h1:qfzeTPI2oSgxnw52KiVWc2fHMem6FZIkX1Azwy64098=
Expand Down
6 changes: 3 additions & 3 deletions storage/constants.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package storage

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

// MaxRetriesToCreateDB represents the maximum number of times to try to create DB if it failed
const MaxRetriesToCreateDB = storageUnit.MaxRetriesToCreateDB
const MaxRetriesToCreateDB = common.MaxRetriesToCreateDB

// SleepTimeBetweenCreateDBRetries represents the number of seconds to sleep between DB creates
const SleepTimeBetweenCreateDBRetries = storageUnit.SleepTimeBetweenCreateDBRetries
const SleepTimeBetweenCreateDBRetries = common.SleepTimeBetweenCreateDBRetries

// PathShardPlaceholder represents the placeholder for the shard ID in paths
const PathShardPlaceholder = "[S]"
Expand Down
19 changes: 10 additions & 9 deletions storage/factory/persisterCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/multiversx/mx-chain-go/storage"
"github.com/multiversx/mx-chain-go/storage/database"
"github.com/multiversx/mx-chain-go/storage/storageunit"
"github.com/multiversx/mx-chain-storage-go/factory"
)

const minNumShards = 2
Expand Down Expand Up @@ -51,16 +52,16 @@ func (pc *persisterCreator) Create(path string) (storage.Persister, error) {
// CreateBasePersister will create base the persister for the provided path
func (pc *persisterCreator) CreateBasePersister(path string) (storage.Persister, error) {
var dbType = storageunit.DBType(pc.dbType)
switch dbType {
case storageunit.LvlDB:
return database.NewLevelDB(path, pc.batchDelaySeconds, pc.maxBatchSize, pc.maxOpenFiles)
case storageunit.LvlDBSerial:
return database.NewSerialDB(path, pc.batchDelaySeconds, pc.maxBatchSize, pc.maxOpenFiles)
case storageunit.MemoryDB:
return database.NewMemDB(), nil
default:
return nil, storage.ErrNotSupportedDBType

argsDB := factory.ArgDB{
DBType: dbType,
Path: path,
BatchDelaySeconds: pc.batchDelaySeconds,
MaxBatchSize: pc.maxBatchSize,
MaxOpenFiles: pc.maxOpenFiles,
}

return storageunit.NewDB(argsDB)
}

func (pc *persisterCreator) createShardIDProvider() (storage.ShardIDProvider, error) {
Expand Down
38 changes: 38 additions & 0 deletions storage/factory/persisterFactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package factory_test
import (
"fmt"
"os"
"path"
"testing"

"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-go/storage"
"github.com/multiversx/mx-chain-go/storage/factory"
"github.com/multiversx/mx-chain-go/storage/storageunit"
"github.com/multiversx/mx-chain-storage-go/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -46,6 +49,41 @@ func TestPersisterFactory_Create(t *testing.T) {
})
}

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

t.Run("wrong config should error", func(t *testing.T) {
t.Parallel()

path := "TEST"
dbConfig := createDefaultDBConfig()
dbConfig.Type = "invalid type"

persisterFactory, err := factory.NewPersisterFactory(dbConfig)
assert.Nil(t, err)

db, err := persisterFactory.CreateWithRetries(path)
assert.True(t, check.IfNil(db))
assert.Equal(t, common.ErrNotSupportedDBType, err)
})

t.Run("should work", func(t *testing.T) {
t.Parallel()

path := path.Join(t.TempDir(), "TEST")
dbConfig := createDefaultDBConfig()
dbConfig.FilePath = path

persisterFactory, err := factory.NewPersisterFactory(dbConfig)
assert.Nil(t, err)

db, err := persisterFactory.CreateWithRetries(path)
assert.False(t, check.IfNil(db))
assert.Nil(t, err)
_ = db.Close()
})
}

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

Expand Down
16 changes: 9 additions & 7 deletions storage/storageunit/constants.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
package storageunit

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

const (
// LRUCache defines a cache identifier with least-recently-used eviction mechanism
LRUCache = storageUnit.LRUCache
LRUCache = common.LRUCache
// SizeLRUCache defines a cache identifier with least-recently-used eviction mechanism and fixed size in bytes
SizeLRUCache = storageUnit.SizeLRUCache
SizeLRUCache = common.SizeLRUCache
)

// DB types that are currently supported
const (
// LvlDB represents a levelDB storage identifier
LvlDB = storageUnit.LvlDB
LvlDB = common.LvlDB
// LvlDBSerial represents a levelDB storage with serialized operations identifier
LvlDBSerial = storageUnit.LvlDBSerial
LvlDBSerial = common.LvlDBSerial
// MemoryDB represents an in memory storage identifier
MemoryDB = storageUnit.MemoryDB
MemoryDB = common.MemoryDB
)

// Shard id provider types that are currently supported
const (
BinarySplit = storageUnit.BinarySplit
BinarySplit = common.BinarySplit
)
38 changes: 31 additions & 7 deletions storage/storageunit/storageunit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package storageunit
import (
"github.com/multiversx/mx-chain-core-go/marshal"
"github.com/multiversx/mx-chain-go/storage"
"github.com/multiversx/mx-chain-storage-go/common"
"github.com/multiversx/mx-chain-storage-go/factory"
"github.com/multiversx/mx-chain-storage-go/storageCacherAdapter"
"github.com/multiversx/mx-chain-storage-go/storageUnit"
)
Expand All @@ -12,22 +14,25 @@ import (
type Unit = storageUnit.Unit

// CacheConfig holds the configurable elements of a cache
type CacheConfig = storageUnit.CacheConfig
type CacheConfig = common.CacheConfig

// DBConfig holds the configurable elements of a database
type DBConfig = storageUnit.DBConfig
type DBConfig = common.DBConfig

// NilStorer resembles a disabled implementation of the Storer interface
type NilStorer = storageUnit.NilStorer

// CacheType represents the type of the supported caches
type CacheType = storageUnit.CacheType
type CacheType = common.CacheType

// DBType represents the type of the supported databases
type DBType = storageUnit.DBType
type DBType = common.DBType

// ShardIDProviderType represents the type of the supported shard id providers
type ShardIDProviderType = storageUnit.ShardIDProviderType
type ShardIDProviderType = common.ShardIDProviderType

// ArgDB is a structure that is used to create a new storage.Persister implementation
type ArgDB = factory.ArgDB

// NewStorageUnit is the constructor for the storage unit, creating a new storage unit
// from the given cacher and persister.
Expand All @@ -37,12 +42,31 @@ func NewStorageUnit(c storage.Cacher, p storage.Persister) (*Unit, error) {

// NewCache creates a new cache from a cache config
func NewCache(config CacheConfig) (storage.Cacher, error) {
return storageUnit.NewCache(config)
return factory.NewCache(config)
}

// NewDB creates a new database from database config
func NewDB(args ArgDB) (storage.Persister, error) {
return factory.NewDB(args)
}

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

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

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

return NewStorageUnit(cache, db)
}

// NewNilStorer will return a nil storer
Expand Down
44 changes: 0 additions & 44 deletions storage/storageunit/storageunit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,50 +72,6 @@ func TestNewCache(t *testing.T) {
})
}

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

t.Run("wrong config should error", func(t *testing.T) {
t.Parallel()

path := "TEST"
dbConfig := config.DBConfig{
FilePath: path,
Type: "invalid type",
BatchDelaySeconds: 5,
MaxBatchSize: 10,
MaxOpenFiles: 10,
}

persisterFactory, err := factory.NewPersisterFactory(dbConfig)
assert.Nil(t, err)

db, err := persisterFactory.CreateWithRetries(path)
assert.True(t, check.IfNil(db))
assert.Equal(t, common.ErrNotSupportedDBType, err)
})
t.Run("should work", func(t *testing.T) {
t.Parallel()

path := path.Join(t.TempDir(), "TEST")
dbConfig := config.DBConfig{
FilePath: path,
Type: "LvlDBSerial",
BatchDelaySeconds: 5,
MaxBatchSize: 10,
MaxOpenFiles: 10,
}

persisterFactory, err := factory.NewPersisterFactory(dbConfig)
assert.Nil(t, err)

db, err := persisterFactory.CreateWithRetries(path)
assert.False(t, check.IfNil(db))
assert.Nil(t, err)
_ = db.Close()
})
}

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

Expand Down

0 comments on commit 922d528

Please sign in to comment.