ooni-probe-cli/internal/engine/kvstore/kvstore.go
Simone Basso 4eeadd06a5
refactor: move more commands to internal/cmd (#207)
* refactor: move more commands to internal/cmd

Part of https://github.com/ooni/probe/issues/1335.

We would like all commands to be at the same level of engine
rather than inside engine (now that we can do it).

* fix: update .gitignore

* refactor: also move jafar outside engine

* We should be good now?
2021-02-03 12:23:15 +01:00

45 lines
886 B
Go

// Package kvstore contains key-value stores
package kvstore
import (
"errors"
"sync"
)
// MemoryKeyValueStore is an in-memory key-value store
type MemoryKeyValueStore struct {
m map[string][]byte
mu sync.Mutex
}
// NewMemoryKeyValueStore creates a new in-memory key-value store
func NewMemoryKeyValueStore() *MemoryKeyValueStore {
return &MemoryKeyValueStore{
m: make(map[string][]byte),
}
}
// Get returns a key from the key value store
func (kvs *MemoryKeyValueStore) Get(key string) ([]byte, error) {
var (
err error
ok bool
value []byte
)
kvs.mu.Lock()
defer kvs.mu.Unlock()
value, ok = kvs.m[key]
if !ok {
err = errors.New("no such key")
}
return value, err
}
// Set sets a key into the key value store
func (kvs *MemoryKeyValueStore) Set(key string, value []byte) error {
kvs.mu.Lock()
defer kvs.mu.Unlock()
kvs.m[key] = value
return nil
}