prysm-pulse/sharding/database/inmemory.go
Raul Jordan f091aa4d99 sharding: address review comments
Former-commit-id: afd2302f293fb8856c52dce5dc375372dfc310ea [formerly a2369694d644b8c76493f8ce305d80123dacd88d]
Former-commit-id: 0e13120a1c997bca6bf7a27fde6b5dc72592af5f
2018-05-22 11:50:34 -04:00

60 lines
1.4 KiB
Go

// Package database provides several constructs including a simple in-memory database.
// This should not be used for production, but would be a helpful interim
// solution for development.
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 common.Hash) (*[]byte, error) {
sb.lock.RLock()
defer sb.lock.RUnlock()
v, ok := sb.kv[k]
if !ok {
return nil, fmt.Errorf("key not found: %v", k)
}
return v, nil
}
// Has checks if the key exists in the mapping.
func (sb *ShardKV) Has(k common.Hash) bool {
sb.lock.RLock()
defer sb.lock.RUnlock()
v := sb.kv[k]
return v != nil
}
// 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
}
// 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
}