summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-store/kv
diff options
context:
space:
mode:
authorLibravatar Terin Stock <terinjokes@gmail.com>2022-04-26 20:30:25 -0700
committerLibravatar Terin Stock <terinjokes@gmail.com>2023-01-31 15:16:42 +0100
commit83b4c9ebc87d0fddf4e638f13e3af1483912e3a5 (patch)
tree47840b84c0fd3cb226eab2ecb3dbce0617163406 /vendor/codeberg.org/gruf/go-store/kv
parent[chore] update URLs to forked source (diff)
downloadgotosocial-83b4c9ebc87d0fddf4e638f13e3af1483912e3a5.tar.xz
[chore] remove vendor
Diffstat (limited to 'vendor/codeberg.org/gruf/go-store/kv')
-rw-r--r--vendor/codeberg.org/gruf/go-store/kv/iterator.go63
-rw-r--r--vendor/codeberg.org/gruf/go-store/kv/state.go130
-rw-r--r--vendor/codeberg.org/gruf/go-store/kv/store.go227
3 files changed, 0 insertions, 420 deletions
diff --git a/vendor/codeberg.org/gruf/go-store/kv/iterator.go b/vendor/codeberg.org/gruf/go-store/kv/iterator.go
deleted file mode 100644
index 2fe5dd428..000000000
--- a/vendor/codeberg.org/gruf/go-store/kv/iterator.go
+++ /dev/null
@@ -1,63 +0,0 @@
-package kv
-
-import (
- "errors"
-
- "codeberg.org/gruf/go-mutexes"
- "codeberg.org/gruf/go-store/storage"
-)
-
-var ErrIteratorClosed = errors.New("store/kv: iterator closed")
-
-// KVIterator provides a read-only iterator to all the key-value
-// pairs in a KVStore. While the iterator is open the store is read
-// locked, you MUST release the iterator when you are finished with
-// it.
-//
-// Please note:
-// - individual iterators are NOT concurrency safe, though it is safe to
-// have multiple iterators running concurrently
-type KVIterator struct {
- store *KVStore // store is the linked KVStore
- state *mutexes.LockState
- entries []storage.StorageEntry
- index int
- key string
-}
-
-// Next attempts to set the next key-value pair, the
-// return value is if there was another pair remaining
-func (i *KVIterator) Next() bool {
- next := i.index + 1
- if next >= len(i.entries) {
- i.key = ""
- return false
- }
- i.key = i.entries[next].Key()
- i.index = next
- return true
-}
-
-// Key returns the next key from the store
-func (i *KVIterator) Key() string {
- return i.key
-}
-
-// Release releases the KVIterator and KVStore's read lock
-func (i *KVIterator) Release() {
- i.state.UnlockMap()
- i.store = nil
- i.key = ""
- i.entries = nil
-}
-
-// Value returns the next value from the KVStore
-func (i *KVIterator) Value() ([]byte, error) {
- // Check store isn't closed
- if i.store == nil {
- return nil, ErrIteratorClosed
- }
-
- // Attempt to fetch from store
- return i.store.get(i.state.RLock, i.key)
-}
diff --git a/vendor/codeberg.org/gruf/go-store/kv/state.go b/vendor/codeberg.org/gruf/go-store/kv/state.go
deleted file mode 100644
index f9f789521..000000000
--- a/vendor/codeberg.org/gruf/go-store/kv/state.go
+++ /dev/null
@@ -1,130 +0,0 @@
-package kv
-
-import (
- "errors"
- "io"
-
- "codeberg.org/gruf/go-mutexes"
-)
-
-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,
-// then the state has zero guarantees
-type StateRO struct {
- store *KVStore
- state *mutexes.LockState
-}
-
-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(st.state.RLock, 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(st.state.RLock, 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(st.state.RLock, key)
-}
-
-func (st *StateRO) Release() {
- st.state.UnlockMap()
- 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
- state *mutexes.LockState
-}
-
-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(st.state.RLock, 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(st.state.RLock, 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(st.state.Lock, 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(st.state.Lock, 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(st.state.RLock, 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(st.state.Lock, key)
-}
-
-func (st *StateRW) Release() {
- st.state.UnlockMap()
- st.store = nil
-}
diff --git a/vendor/codeberg.org/gruf/go-store/kv/store.go b/vendor/codeberg.org/gruf/go-store/kv/store.go
deleted file mode 100644
index fd9935f25..000000000
--- a/vendor/codeberg.org/gruf/go-store/kv/store.go
+++ /dev/null
@@ -1,227 +0,0 @@
-package kv
-
-import (
- "io"
-
- "codeberg.org/gruf/go-mutexes"
- "codeberg.org/gruf/go-store/storage"
- "codeberg.org/gruf/go-store/util"
-)
-
-// KVStore is a very simple, yet performant key-value store
-type KVStore struct {
- mutex mutexes.MutexMap // mutex is a map of keys to mutexes to protect file access
- storage storage.Storage // storage is the underlying storage
-}
-
-func OpenFile(path string, cfg *storage.DiskConfig) (*KVStore, error) {
- // Attempt to open disk storage
- storage, err := storage.OpenFile(path, cfg)
- if err != nil {
- return nil, err
- }
-
- // Return new KVStore
- return OpenStorage(storage)
-}
-
-func OpenBlock(path string, cfg *storage.BlockConfig) (*KVStore, error) {
- // Attempt to open block storage
- storage, err := storage.OpenBlock(path, cfg)
- if err != nil {
- return nil, err
- }
-
- // Return new KVStore
- return OpenStorage(storage)
-}
-
-func OpenStorage(storage storage.Storage) (*KVStore, error) {
- // Perform initial storage clean
- err := storage.Clean()
- if err != nil {
- return nil, err
- }
-
- // Return new KVStore
- return &KVStore{
- mutex: mutexes.NewMap(-1, -1),
- storage: storage,
- }, nil
-}
-
-// RLock acquires a read-lock on supplied key, returning unlock function.
-func (st *KVStore) RLock(key string) (runlock func()) {
- return st.mutex.RLock(key)
-}
-
-// Lock acquires a write-lock on supplied key, returning unlock function.
-func (st *KVStore) Lock(key string) (unlock func()) {
- return st.mutex.Lock(key)
-}
-
-// Get fetches the bytes for supplied key in the store
-func (st *KVStore) Get(key string) ([]byte, error) {
- return st.get(st.RLock, key)
-}
-
-func (st *KVStore) get(rlock func(string) func(), key string) ([]byte, error) {
- // Acquire read lock for key
- runlock := rlock(key)
- defer runlock()
-
- // Read file bytes
- return st.storage.ReadBytes(key)
-}
-
-// GetStream fetches a ReadCloser for the bytes at the supplied key location in the store
-func (st *KVStore) GetStream(key string) (io.ReadCloser, error) {
- return st.getStream(st.RLock, key)
-}
-
-func (st *KVStore) getStream(rlock func(string) func(), key string) (io.ReadCloser, error) {
- // Acquire read lock for key
- runlock := rlock(key)
-
- // Attempt to open stream for read
- rd, err := st.storage.ReadStream(key)
- if err != nil {
- runlock()
- return nil, err
- }
-
- // Wrap readcloser in our own callback closer
- return util.ReadCloserWithCallback(rd, runlock), nil
-}
-
-// Put places the bytes at the supplied key location in the store
-func (st *KVStore) Put(key string, value []byte) error {
- return st.put(st.Lock, key, value)
-}
-
-func (st *KVStore) put(lock func(string) func(), key string, value []byte) error {
- // Acquire write lock for key
- unlock := lock(key)
- defer unlock()
-
- // Write file bytes
- return st.storage.WriteBytes(key, value)
-}
-
-// PutStream writes the bytes from the supplied Reader at the supplied key location in the store
-func (st *KVStore) PutStream(key string, r io.Reader) error {
- return st.putStream(st.Lock, key, r)
-}
-
-func (st *KVStore) putStream(lock func(string) func(), key string, r io.Reader) error {
- // Acquire write lock for key
- unlock := lock(key)
- defer unlock()
-
- // Write file stream
- return st.storage.WriteStream(key, r)
-}
-
-// Has checks whether the supplied key exists in the store
-func (st *KVStore) Has(key string) (bool, error) {
- return st.has(st.RLock, key)
-}
-
-func (st *KVStore) has(rlock func(string) func(), key string) (bool, error) {
- // Acquire read lock for key
- runlock := rlock(key)
- defer runlock()
-
- // Stat file on disk
- return st.storage.Stat(key)
-}
-
-// Delete removes the supplied key-value pair from the store
-func (st *KVStore) Delete(key string) error {
- return st.delete(st.Lock, key)
-}
-
-func (st *KVStore) delete(lock func(string) func(), key string) error {
- // Acquire write lock for key
- unlock := lock(key)
- defer unlock()
-
- // Remove file from disk
- return st.storage.Remove(key)
-}
-
-// Iterator returns an Iterator for key-value pairs in the store, using supplied match function
-func (st *KVStore) Iterator(matchFn func(string) bool) (*KVIterator, error) {
- // If no function, match all
- if matchFn == nil {
- matchFn = func(string) bool { return true }
- }
-
- // Get store read lock
- state := st.mutex.RLockMap()
-
- // Setup the walk keys function
- entries := []storage.StorageEntry{}
- walkFn := func(entry storage.StorageEntry) {
- // Ignore unmatched entries
- if !matchFn(entry.Key()) {
- return
- }
-
- // Add to entries
- entries = append(entries, entry)
- }
-
- // Walk keys in the storage
- err := st.storage.WalkKeys(storage.WalkKeysOptions{WalkFn: walkFn})
- if err != nil {
- state.UnlockMap()
- return nil, err
- }
-
- // Return new iterator
- return &KVIterator{
- store: st,
- state: state,
- entries: entries,
- index: -1,
- key: "",
- }, nil
-}
-
-// Read provides a read-only window to the store, holding it in a read-locked state until release
-func (st *KVStore) Read() *StateRO {
- state := st.mutex.RLockMap()
- return &StateRO{store: st, state: state}
-}
-
-// ReadFn provides a read-only window to the store, holding it in a read-locked state until fn return.
-func (st *KVStore) ReadFn(fn func(*StateRO)) {
- // Acquire read-only state
- state := st.Read()
- defer state.Release()
-
- // Pass to fn
- fn(state)
-}
-
-// Update provides a read-write window to the store, holding it in a write-locked state until release
-func (st *KVStore) Update() *StateRW {
- state := st.mutex.LockMap()
- return &StateRW{store: st, state: state}
-}
-
-// UpdateFn provides a read-write window to the store, holding it in a write-locked state until fn return.
-func (st *KVStore) UpdateFn(fn func(*StateRW)) {
- // Acquire read-write state
- state := st.Update()
- defer state.Release()
-
- // Pass to fn
- fn(state)
-}
-
-// Close will close the underlying storage, the mutex map locking (e.g. RLock(), Lock() will still work).
-func (st *KVStore) Close() error {
- return st.storage.Close()
-}