generated from multiversx/mx-chain-ws-connector-template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
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
135 additions
and
14 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package process | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/multiversx/mx-chain-core-go/core/check" | ||
"github.com/multiversx/mx-chain-storage-go/types" | ||
) | ||
|
||
type importDBStorer struct { | ||
cacher types.Cacher | ||
} | ||
|
||
// NewImportDBStorer creates a new import db storer instancer | ||
func NewImportDBStorer(cacher types.Cacher) (*importDBStorer, error) { | ||
if check.IfNil(cacher) { | ||
return nil, ErrNilCacher | ||
} | ||
|
||
return &importDBStorer{ | ||
cacher: cacher, | ||
}, nil | ||
} | ||
|
||
// Get will get value from cache | ||
func (is *importDBStorer) Get(key []byte) ([]byte, error) { | ||
v, ok := is.cacher.Get(key) | ||
if ok { | ||
return v.([]byte), nil | ||
} | ||
|
||
return nil, fmt.Errorf("key %s not found", hex.EncodeToString(key)) | ||
} | ||
|
||
// Put will put data into cacher | ||
func (is *importDBStorer) Put(key []byte, data []byte) error { | ||
is.cacher.Put(key, data, len(data)) | ||
return nil | ||
} | ||
|
||
// Prune returns nil | ||
func (is *importDBStorer) Prune(_ uint64) error { | ||
return nil | ||
} | ||
|
||
// IsInterfaceNil returns nil if there is no value under the interface | ||
func (is *importDBStorer) IsInterfaceNil() bool { | ||
return is == nil | ||
} |
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,45 @@ | ||
package process_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/multiversx/mx-chain-ws-connector-template-go/process" | ||
"github.com/multiversx/mx-chain-ws-connector-template-go/testscommon" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewImportDBStorer(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("nil cacher", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
is, err := process.NewImportDBStorer(nil) | ||
require.Nil(t, is) | ||
require.Equal(t, process.ErrNilCacher, err) | ||
}) | ||
|
||
t.Run("should work", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
is, err := process.NewImportDBStorer(testscommon.NewCacherMock()) | ||
require.Nil(t, err) | ||
require.False(t, is.IsInterfaceNil()) | ||
|
||
key := []byte("key1") | ||
val := []byte("val1") | ||
|
||
err = is.Put(key, val) | ||
require.Nil(t, err) | ||
|
||
ret, err := is.Get(key) | ||
require.Nil(t, err) | ||
require.Equal(t, val, ret) | ||
|
||
ret, err = is.Get([]byte("not existing")) | ||
require.Nil(t, ret) | ||
require.Error(t, err) | ||
|
||
require.Nil(t, is.Prune(2)) | ||
}) | ||
} |
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