sharding: made inmemory db concurrency-safe

Former-commit-id: 479801ca87e72986b2e635839c2e178cd08e7c8d [formerly 154c4c2fb0fa8dbaf24be0b8daf428c7f0c061b0]
Former-commit-id: 6489659f940b3737c663c40ff3f5385d001f5b97
This commit is contained in:
Raul Jordan
2018-05-22 11:42:31 -04:00
2 changed files with 356 additions and 657 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -5,13 +5,15 @@ package database
import (
"fmt"
"sync"
"github.com/ethereum/go-ethereum/common"
)
// ShardKV is an in-memory mapping of hashes to RLP encoded values.
type ShardKV struct {
kv map[common.Hash]*[]byte
kv map[common.Hash]*[]byte
lock sync.RWMutex
}
// NewShardKV initializes a keyval store in memory.
@@ -36,6 +38,8 @@ func (sb *ShardKV) Has(k common.Hash) bool {
// Put updates a key's value in the mapping.
func (sb *ShardKV) Put(k common.Hash, v []byte) error {
sb.lock.Lock()
defer sb.lock.Unlock()
// there is no error in a simple setting of a value in a go map.
sb.kv[k] = &v
return nil
@@ -43,6 +47,8 @@ func (sb *ShardKV) Put(k common.Hash, v []byte) error {
// Delete removes the key and value from the mapping.
func (sb *ShardKV) Delete(k common.Hash) error {
sb.lock.Lock()
defer sb.lock.Unlock()
// There is no return value for deleting a simple key in a go map.
delete(sb.kv, k)
return nil