prysm-pulse/sharding/database/inmemory.go
Raul Jordan 5dc498f402 sharding: shardDB put, delete can return error now
Former-commit-id: 8d96f95dbe6419d804d86b05dca8f1e90e2b56ed [formerly c42c9e84b82dcc29d50739e0b8eac5bd65f93024]
Former-commit-id: 527cb5170e3eb156bf7c0bb2ec69ef9f32cc8320
2018-05-10 22:00:54 -04:00

50 lines
1.2 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"
"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
}
// MakeShardKV initializes a keyval store in memory.
func MakeShardKV() *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) {
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 {
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 {
// 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 {
// There is no return value for deleting a simple key in a go map.
delete(sb.kv, k)
return nil
}