62 lines
1.0 KiB
Go
62 lines
1.0 KiB
Go
package storage
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"git.maze.io/maze/conduit/storage/codec"
|
|
)
|
|
|
|
// KV implements a key-value store.
|
|
type KV interface {
|
|
// Has checks if the key is present.
|
|
Has(key string) bool
|
|
|
|
// Get a value by key.
|
|
Get(key string) (value []byte, ok bool)
|
|
}
|
|
|
|
// WritableKV can store key-value items.
|
|
type WritableKV interface {
|
|
KV
|
|
|
|
// Close the key-value storage.
|
|
Close() error
|
|
|
|
// DeleteKey removes a key.
|
|
Delete(key string) error
|
|
|
|
// Set a value by key.
|
|
Set(key string, value []byte) error
|
|
}
|
|
|
|
type EncodedKV interface {
|
|
Has(key string) bool
|
|
Get(key string, value any) (ok bool, err error)
|
|
}
|
|
|
|
type EncodedWritableKV interface {
|
|
EncodedKV
|
|
|
|
Close() error
|
|
|
|
Delete(key string) error
|
|
|
|
Set(key string, value []byte) error
|
|
}
|
|
|
|
type encodedKV struct {
|
|
kv KV
|
|
encode func(any) ([]byte, error)
|
|
decode func([]byte, any) error
|
|
}
|
|
|
|
func (kv encodedKV) Has(key)
|
|
|
|
func NewEncodedKV(kv KV, codec codec.Codec) EncodedKV {
|
|
return &encodedKV{
|
|
kv: kv,
|
|
encode: json.Marshal,
|
|
decode: json.Unmarshal,
|
|
}
|
|
}
|