mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-08 10:41:19 +00:00
52cc968c57
Former-commit-id: 806e44466e6484a0028fa39f364c2ee63e828983 [formerly 89d7697ddc25156265f8f49622a775c879e3bd88] Former-commit-id: 8cf56c2760f2b9fcf1121a94967276c2a34cae58
60 lines
1.5 KiB
Go
60 lines
1.5 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 []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
|
|
}
|