2021-09-11 19:12:47 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
2022-05-08 17:49:45 +00:00
|
|
|
"errors"
|
2021-09-11 19:12:47 +00:00
|
|
|
"io"
|
|
|
|
|
2022-01-24 16:35:13 +00:00
|
|
|
"codeberg.org/gruf/go-mutexes"
|
2021-09-11 19:12:47 +00:00
|
|
|
)
|
|
|
|
|
2021-11-13 11:29:08 +00:00
|
|
|
var ErrStateClosed = errors.New("store/kv: state closed")
|
2021-09-11 19:12:47 +00:00
|
|
|
|
|
|
|
// StateRO provides a read-only window to the store. While this
|
|
|
|
// state is active during the Read() function window, the entire
|
|
|
|
// store will be read-locked. The state is thread-safe for concurrent
|
|
|
|
// use UNTIL the moment that your supplied function to Read() returns,
|
|
|
|
// then the state has zero guarantees
|
|
|
|
type StateRO struct {
|
|
|
|
store *KVStore
|
2022-01-24 16:35:13 +00:00
|
|
|
state *mutexes.LockState
|
2021-09-11 19:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (st *StateRO) Get(key string) ([]byte, error) {
|
|
|
|
// Check not closed
|
|
|
|
if st.store == nil {
|
|
|
|
return nil, ErrStateClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass request to store
|
2022-01-24 16:35:13 +00:00
|
|
|
return st.store.get(st.state.RLock, key)
|
2021-09-11 19:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (st *StateRO) GetStream(key string) (io.ReadCloser, error) {
|
|
|
|
// Check not closed
|
|
|
|
if st.store == nil {
|
|
|
|
return nil, ErrStateClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass request to store
|
2022-01-24 16:35:13 +00:00
|
|
|
return st.store.getStream(st.state.RLock, key)
|
2021-09-11 19:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (st *StateRO) Has(key string) (bool, error) {
|
|
|
|
// Check not closed
|
|
|
|
if st.store == nil {
|
|
|
|
return false, ErrStateClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass request to store
|
2022-01-24 16:35:13 +00:00
|
|
|
return st.store.has(st.state.RLock, key)
|
2021-09-11 19:12:47 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 09:35:32 +00:00
|
|
|
func (st *StateRO) Release() {
|
2022-01-24 16:35:13 +00:00
|
|
|
st.state.UnlockMap()
|
|
|
|
st.store = nil
|
2021-09-11 19:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StateRW provides a read-write window to the store. While this
|
|
|
|
// state is active during the Update() function window, the entire
|
|
|
|
// store will be locked. The state is thread-safe for concurrent
|
|
|
|
// use UNTIL the moment that your supplied function to Update() returns,
|
|
|
|
// then the state has zero guarantees
|
|
|
|
type StateRW struct {
|
|
|
|
store *KVStore
|
2022-01-24 16:35:13 +00:00
|
|
|
state *mutexes.LockState
|
2021-09-11 19:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (st *StateRW) Get(key string) ([]byte, error) {
|
|
|
|
// Check not closed
|
|
|
|
if st.store == nil {
|
|
|
|
return nil, ErrStateClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass request to store
|
2022-01-24 16:35:13 +00:00
|
|
|
return st.store.get(st.state.RLock, key)
|
2021-09-11 19:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (st *StateRW) GetStream(key string) (io.ReadCloser, error) {
|
|
|
|
// Check not closed
|
|
|
|
if st.store == nil {
|
|
|
|
return nil, ErrStateClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass request to store
|
2022-01-24 16:35:13 +00:00
|
|
|
return st.store.getStream(st.state.RLock, key)
|
2021-09-11 19:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (st *StateRW) Put(key string, value []byte) error {
|
|
|
|
// Check not closed
|
|
|
|
if st.store == nil {
|
|
|
|
return ErrStateClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass request to store
|
2022-01-24 16:35:13 +00:00
|
|
|
return st.store.put(st.state.Lock, key, value)
|
2021-09-11 19:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (st *StateRW) PutStream(key string, r io.Reader) error {
|
|
|
|
// Check not closed
|
|
|
|
if st.store == nil {
|
|
|
|
return ErrStateClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass request to store
|
2022-01-24 16:35:13 +00:00
|
|
|
return st.store.putStream(st.state.Lock, key, r)
|
2021-09-11 19:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (st *StateRW) Has(key string) (bool, error) {
|
|
|
|
// Check not closed
|
|
|
|
if st.store == nil {
|
|
|
|
return false, ErrStateClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass request to store
|
2022-01-24 16:35:13 +00:00
|
|
|
return st.store.has(st.state.RLock, key)
|
2021-09-11 19:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (st *StateRW) Delete(key string) error {
|
|
|
|
// Check not closed
|
|
|
|
if st.store == nil {
|
|
|
|
return ErrStateClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass request to store
|
2022-01-24 16:35:13 +00:00
|
|
|
return st.store.delete(st.state.Lock, key)
|
2021-09-11 19:12:47 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 09:35:32 +00:00
|
|
|
func (st *StateRW) Release() {
|
2022-01-24 16:35:13 +00:00
|
|
|
st.state.UnlockMap()
|
|
|
|
st.store = nil
|
2021-09-11 19:12:47 +00:00
|
|
|
}
|