prysm-pulse/sharding/database/inmemory.go
Raul Jordan 3861c0cf06 sharding: ensure godoc for every package
Former-commit-id: 0ecc597de035e61ca219f4f30695cb8db59c129b [formerly da9312c1c9d69010083f94f387fe6a52aa817683]
Former-commit-id: 8c8c218e16ef248ae8954168cf7bf5aa6ed6839d
2018-06-05 17:28:57 -04:00

57 lines
1.3 KiB
Go

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
lock sync.RWMutex
}
// NewShardKV initializes a keyval store in memory.
func NewShardKV() *ShardKV {
return &ShardKV{kv: make(map[common.Hash][]byte)}
}
// Get fetches a val from the mappping by key.
func (sb *ShardKV) Get(k []byte) ([]byte, error) {
sb.lock.RLock()
defer sb.lock.RUnlock()
v, ok := sb.kv[common.BytesToHash(k)]
if !ok {
return []byte{}, fmt.Errorf("key not found: %v", k)
}
return v, nil
}
// Has checks if the key exists in the mapping.
func (sb *ShardKV) Has(k []byte) (bool, error) {
sb.lock.RLock()
defer sb.lock.RUnlock()
v := sb.kv[common.BytesToHash(k)]
return v != nil, nil
}
// Put updates a key's value in the mapping.
func (sb *ShardKV) Put(k []byte, 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[common.BytesToHash(k)] = v
return nil
}
// Delete removes the key and value from the mapping.
func (sb *ShardKV) Delete(k []byte) 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, common.BytesToHash(k))
return nil
}