summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-store/kv
diff options
context:
space:
mode:
authorLibravatar tsmethurst <tobi.smethurst@protonmail.com>2022-01-16 18:52:30 +0100
committerLibravatar tsmethurst <tobi.smethurst@protonmail.com>2022-01-16 18:52:30 +0100
commit6f5ccf435585e43a00e3cc50f4bcefac36ada818 (patch)
treeba368d27464b79b1e5d010c0662fd3e340bf108e /vendor/codeberg.org/gruf/go-store/kv
parentadd go-runners to readme (diff)
downloadgotosocial-6f5ccf435585e43a00e3cc50f4bcefac36ada818.tar.xz
update dependencies
Diffstat (limited to 'vendor/codeberg.org/gruf/go-store/kv')
-rw-r--r--vendor/codeberg.org/gruf/go-store/kv/iterator.go2
-rw-r--r--vendor/codeberg.org/gruf/go-store/kv/state.go18
-rw-r--r--vendor/codeberg.org/gruf/go-store/kv/store.go82
3 files changed, 44 insertions, 58 deletions
diff --git a/vendor/codeberg.org/gruf/go-store/kv/iterator.go b/vendor/codeberg.org/gruf/go-store/kv/iterator.go
index d3999273f..ddaaf60cf 100644
--- a/vendor/codeberg.org/gruf/go-store/kv/iterator.go
+++ b/vendor/codeberg.org/gruf/go-store/kv/iterator.go
@@ -60,5 +60,5 @@ func (i *KVIterator) Value() ([]byte, error) {
}
// Attempt to fetch from store
- return i.store.get(i.key)
+ return i.store.get(i.store.mutexMap.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
index 20a3e951d..330928bce 100644
--- a/vendor/codeberg.org/gruf/go-store/kv/state.go
+++ b/vendor/codeberg.org/gruf/go-store/kv/state.go
@@ -30,7 +30,7 @@ func (st *StateRO) Get(key string) ([]byte, error) {
}
// Pass request to store
- return st.store.get(key)
+ return st.store.get(st.store.mutexMap.RLock, key)
}
func (st *StateRO) GetStream(key string) (io.ReadCloser, error) {
@@ -44,7 +44,7 @@ func (st *StateRO) GetStream(key string) (io.ReadCloser, error) {
}
// Pass request to store
- return st.store.getStream(key)
+ return st.store.getStream(st.store.mutexMap.RLock, key)
}
func (st *StateRO) Has(key string) (bool, error) {
@@ -58,7 +58,7 @@ func (st *StateRO) Has(key string) (bool, error) {
}
// Pass request to store
- return st.store.has(key)
+ return st.store.has(st.store.mutexMap.RLock, key)
}
func (st *StateRO) Release() {
@@ -94,7 +94,7 @@ func (st *StateRW) Get(key string) ([]byte, error) {
}
// Pass request to store
- return st.store.get(key)
+ return st.store.get(st.store.mutexMap.RLock, key)
}
func (st *StateRW) GetStream(key string) (io.ReadCloser, error) {
@@ -108,7 +108,7 @@ func (st *StateRW) GetStream(key string) (io.ReadCloser, error) {
}
// Pass request to store
- return st.store.getStream(key)
+ return st.store.getStream(st.store.mutexMap.RLock, key)
}
func (st *StateRW) Put(key string, value []byte) error {
@@ -122,7 +122,7 @@ func (st *StateRW) Put(key string, value []byte) error {
}
// Pass request to store
- return st.store.put(key, value)
+ return st.store.put(st.store.mutexMap.Lock, key, value)
}
func (st *StateRW) PutStream(key string, r io.Reader) error {
@@ -136,7 +136,7 @@ func (st *StateRW) PutStream(key string, r io.Reader) error {
}
// Pass request to store
- return st.store.putStream(key, r)
+ return st.store.putStream(st.store.mutexMap.Lock, key, r)
}
func (st *StateRW) Has(key string) (bool, error) {
@@ -150,7 +150,7 @@ func (st *StateRW) Has(key string) (bool, error) {
}
// Pass request to store
- return st.store.has(key)
+ return st.store.has(st.store.mutexMap.RLock, key)
}
func (st *StateRW) Delete(key string) error {
@@ -164,7 +164,7 @@ func (st *StateRW) Delete(key string) error {
}
// Pass request to store
- return st.store.delete(key)
+ return st.store.delete(st.store.mutexMap.Lock, key)
}
func (st *StateRW) Release() {
diff --git a/vendor/codeberg.org/gruf/go-store/kv/store.go b/vendor/codeberg.org/gruf/go-store/kv/store.go
index 34fe91987..4c3a31140 100644
--- a/vendor/codeberg.org/gruf/go-store/kv/store.go
+++ b/vendor/codeberg.org/gruf/go-store/kv/store.go
@@ -53,19 +53,30 @@ func OpenStorage(storage storage.Storage) (*KVStore, error) {
}, nil
}
-// Get fetches the bytes for supplied key in the store
-func (st *KVStore) Get(key string) ([]byte, error) {
- // Acquire store read lock
+// RLock acquires a read-lock on supplied key, returning unlock function.
+func (st *KVStore) RLock(key string) (runlock func()) {
st.mutex.RLock()
- defer st.mutex.RUnlock()
+ runlock = st.mutexMap.RLock(key)
+ st.mutex.RUnlock()
+ return runlock
+}
- // Pass to unprotected fn
- return st.get(key)
+// Lock acquires a write-lock on supplied key, returning unlock function.
+func (st *KVStore) Lock(key string) (unlock func()) {
+ st.mutex.Lock()
+ unlock = st.mutexMap.Lock(key)
+ st.mutex.Unlock()
+ return unlock
}
-func (st *KVStore) get(key string) ([]byte, error) {
+// 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 := st.mutexMap.RLock(key)
+ runlock := rlock(key)
defer runlock()
// Read file bytes
@@ -74,17 +85,12 @@ func (st *KVStore) get(key string) ([]byte, error) {
// GetStream fetches a ReadCloser for the bytes at the supplied key location in the store
func (st *KVStore) GetStream(key string) (io.ReadCloser, error) {
- // Acquire store read lock
- st.mutex.RLock()
- defer st.mutex.RUnlock()
-
- // Pass to unprotected fn
- return st.getStream(key)
+ return st.getStream(st.RLock, key)
}
-func (st *KVStore) getStream(key string) (io.ReadCloser, error) {
+func (st *KVStore) getStream(rlock func(string) func(), key string) (io.ReadCloser, error) {
// Acquire read lock for key
- runlock := st.mutexMap.RLock(key)
+ runlock := rlock(key)
// Attempt to open stream for read
rd, err := st.storage.ReadStream(key)
@@ -99,17 +105,12 @@ func (st *KVStore) getStream(key string) (io.ReadCloser, error) {
// Put places the bytes at the supplied key location in the store
func (st *KVStore) Put(key string, value []byte) error {
- // Acquire store write lock
- st.mutex.Lock()
- defer st.mutex.Unlock()
-
- // Pass to unprotected fn
- return st.put(key, value)
+ return st.put(st.Lock, key, value)
}
-func (st *KVStore) put(key string, value []byte) error {
+func (st *KVStore) put(lock func(string) func(), key string, value []byte) error {
// Acquire write lock for key
- unlock := st.mutexMap.Lock(key)
+ unlock := lock(key)
defer unlock()
// Write file bytes
@@ -118,17 +119,12 @@ func (st *KVStore) put(key string, value []byte) error {
// 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 {
- // Acquire store write lock
- st.mutex.Lock()
- defer st.mutex.Unlock()
-
- // Pass to unprotected fn
- return st.putStream(key, r)
+ return st.putStream(st.Lock, key, r)
}
-func (st *KVStore) putStream(key string, r io.Reader) error {
+func (st *KVStore) putStream(lock func(string) func(), key string, r io.Reader) error {
// Acquire write lock for key
- unlock := st.mutexMap.Lock(key)
+ unlock := lock(key)
defer unlock()
// Write file stream
@@ -137,17 +133,12 @@ func (st *KVStore) putStream(key string, r io.Reader) error {
// Has checks whether the supplied key exists in the store
func (st *KVStore) Has(key string) (bool, error) {
- // Acquire store read lock
- st.mutex.RLock()
- defer st.mutex.RUnlock()
-
- // Pass to unprotected fn
- return st.has(key)
+ return st.has(st.RLock, key)
}
-func (st *KVStore) has(key string) (bool, error) {
+func (st *KVStore) has(rlock func(string) func(), key string) (bool, error) {
// Acquire read lock for key
- runlock := st.mutexMap.RLock(key)
+ runlock := rlock(key)
defer runlock()
// Stat file on disk
@@ -156,17 +147,12 @@ func (st *KVStore) has(key string) (bool, error) {
// Delete removes the supplied key-value pair from the store
func (st *KVStore) Delete(key string) error {
- // Acquire store write lock
- st.mutex.Lock()
- defer st.mutex.Unlock()
-
- // Pass to unprotected fn
- return st.delete(key)
+ return st.delete(st.Lock, key)
}
-func (st *KVStore) delete(key string) error {
+func (st *KVStore) delete(lock func(string) func(), key string) error {
// Acquire write lock for key
- unlock := st.mutexMap.Lock(key)
+ unlock := lock(key)
defer unlock()
// Remove file from disk