diff options
author | 2022-11-05 12:10:19 +0100 | |
---|---|---|
committer | 2022-11-05 11:10:19 +0000 | |
commit | bcb80d3ff4a669d52d63950c8830427646c05884 (patch) | |
tree | 4aa95a83545b3f87a80fe4b625cb6f2ad9c4427f /vendor/codeberg.org/gruf/go-store/v2/kv/state.go | |
parent | [bugfix] Increase field size limits when registering apps (#958) (diff) | |
download | gotosocial-bcb80d3ff4a669d52d63950c8830427646c05884.tar.xz |
[chore] bump gruf/go-store to v2 (#953)
* [chore] bump gruf/go-store to v2
* no more boobs
Diffstat (limited to 'vendor/codeberg.org/gruf/go-store/v2/kv/state.go')
-rw-r--r-- | vendor/codeberg.org/gruf/go-store/v2/kv/state.go | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/vendor/codeberg.org/gruf/go-store/v2/kv/state.go b/vendor/codeberg.org/gruf/go-store/v2/kv/state.go new file mode 100644 index 000000000..9ac8ab1bf --- /dev/null +++ b/vendor/codeberg.org/gruf/go-store/v2/kv/state.go @@ -0,0 +1,116 @@ +package kv + +import ( + "context" + "errors" + "io" + + "codeberg.org/gruf/go-mutexes" +) + +// ErrStateClosed is returned on further calls to states after calling Release(). +var ErrStateClosed = errors.New("store/kv: state closed") + +// 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. +type StateRO struct { + store *KVStore + state *mutexes.LockState +} + +// Get: see KVStore.Get(). Returns error if state already closed. +func (st *StateRO) Get(ctx context.Context, key string) ([]byte, error) { + if st.store == nil { + return nil, ErrStateClosed + } + return st.store.get(st.state.RLock, ctx, key) +} + +// GetStream: see KVStore.GetStream(). Returns error if state already closed. +func (st *StateRO) GetStream(ctx context.Context, key string) (io.ReadCloser, error) { + if st.store == nil { + return nil, ErrStateClosed + } + return st.store.getStream(st.state.RLock, ctx, key) +} + +// Has: see KVStore.Has(). Returns error if state already closed. +func (st *StateRO) Has(ctx context.Context, key string) (bool, error) { + if st.store == nil { + return false, ErrStateClosed + } + return st.store.has(st.state.RLock, ctx, key) +} + +// Release will release the store read-lock, and close this state. +func (st *StateRO) Release() { + st.state.UnlockMap() + st.state = nil + st.store = nil +} + +// 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. +type StateRW struct { + store *KVStore + state *mutexes.LockState +} + +// Get: see KVStore.Get(). Returns error if state already closed. +func (st *StateRW) Get(ctx context.Context, key string) ([]byte, error) { + if st.store == nil { + return nil, ErrStateClosed + } + return st.store.get(st.state.RLock, ctx, key) +} + +// GetStream: see KVStore.GetStream(). Returns error if state already closed. +func (st *StateRW) GetStream(ctx context.Context, key string) (io.ReadCloser, error) { + if st.store == nil { + return nil, ErrStateClosed + } + return st.store.getStream(st.state.RLock, ctx, key) +} + +// Put: see KVStore.Put(). Returns error if state already closed. +func (st *StateRW) Put(ctx context.Context, key string, value []byte) error { + if st.store == nil { + return ErrStateClosed + } + return st.store.put(st.state.Lock, ctx, key, value) +} + +// PutStream: see KVStore.PutStream(). Returns error if state already closed. +func (st *StateRW) PutStream(ctx context.Context, key string, r io.Reader) error { + if st.store == nil { + return ErrStateClosed + } + return st.store.putStream(st.state.Lock, ctx, key, r) +} + +// Has: see KVStore.Has(). Returns error if state already closed. +func (st *StateRW) Has(ctx context.Context, key string) (bool, error) { + if st.store == nil { + return false, ErrStateClosed + } + return st.store.has(st.state.RLock, ctx, key) +} + +// Delete: see KVStore.Delete(). Returns error if state already closed. +func (st *StateRW) Delete(ctx context.Context, key string) error { + if st.store == nil { + return ErrStateClosed + } + return st.store.delete(st.state.Lock, ctx, key) +} + +// Release will release the store lock, and close this state. +func (st *StateRW) Release() { + st.state.UnlockMap() + st.state = nil + st.store = nil +} |