diff options
author | 2021-09-12 10:10:24 +0100 | |
---|---|---|
committer | 2021-09-12 10:10:24 +0100 | |
commit | f6492d12d948507021bbe934de94e87e20464c01 (patch) | |
tree | 6705d6ef6f3c4d70f3b3ebc77c2960d8e508cf37 /vendor/git.iim.gay/grufwub/go-store/kv/state.go | |
parent | Merge pull request #213 from superseriousbusiness/alpine+node_upstep (diff) | |
parent | fix keys used to access storage items (diff) | |
download | gotosocial-f6492d12d948507021bbe934de94e87e20464c01.tar.xz |
Merge pull request #214 from NyaaaWhatsUpDoc/improvement/update-storage-library
add git.iim.gay/grufwub/go-store for storage backend, replacing blob.Storage
Diffstat (limited to 'vendor/git.iim.gay/grufwub/go-store/kv/state.go')
-rw-r--r-- | vendor/git.iim.gay/grufwub/go-store/kv/state.go | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/vendor/git.iim.gay/grufwub/go-store/kv/state.go b/vendor/git.iim.gay/grufwub/go-store/kv/state.go new file mode 100644 index 000000000..a8bc64637 --- /dev/null +++ b/vendor/git.iim.gay/grufwub/go-store/kv/state.go @@ -0,0 +1,125 @@ +package kv + +import ( + "io" + + "git.iim.gay/grufwub/go-errors" +) + +var ErrStateClosed = errors.Define("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, +// then the state has zero guarantees +type StateRO struct { + store *KVStore +} + +func (st *StateRO) Get(key string) ([]byte, error) { + // Check not closed + if st.store == nil { + return nil, ErrStateClosed + } + + // Pass request to store + return st.store.get(key) +} + +func (st *StateRO) GetStream(key string) (io.ReadCloser, error) { + // Check not closed + if st.store == nil { + return nil, ErrStateClosed + } + + // Pass request to store + return st.store.getStream(key) +} + +func (st *StateRO) Has(key string) (bool, error) { + // Check not closed + if st.store == nil { + return false, ErrStateClosed + } + + // Pass request to store + return st.store.has(key) +} + +func (st *StateRO) close() { + 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, +// then the state has zero guarantees +type StateRW struct { + store *KVStore +} + +func (st *StateRW) Get(key string) ([]byte, error) { + // Check not closed + if st.store == nil { + return nil, ErrStateClosed + } + + // Pass request to store + return st.store.get(key) +} + +func (st *StateRW) GetStream(key string) (io.ReadCloser, error) { + // Check not closed + if st.store == nil { + return nil, ErrStateClosed + } + + // Pass request to store + return st.store.getStream(key) +} + +func (st *StateRW) Put(key string, value []byte) error { + // Check not closed + if st.store == nil { + return ErrStateClosed + } + + // Pass request to store + return st.store.put(key, value) +} + +func (st *StateRW) PutStream(key string, r io.Reader) error { + // Check not closed + if st.store == nil { + return ErrStateClosed + } + + // Pass request to store + return st.store.putStream(key, r) +} + +func (st *StateRW) Has(key string) (bool, error) { + // Check not closed + if st.store == nil { + return false, ErrStateClosed + } + + // Pass request to store + return st.store.has(key) +} + +func (st *StateRW) Delete(key string) error { + // Check not closed + if st.store == nil { + return ErrStateClosed + } + + // Pass request to store + return st.store.delete(key) +} + +func (st *StateRW) close() { + st.store = nil +} |