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/iterator.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/iterator.go')
-rw-r--r-- | vendor/git.iim.gay/grufwub/go-store/kv/iterator.go | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/vendor/git.iim.gay/grufwub/go-store/kv/iterator.go b/vendor/git.iim.gay/grufwub/go-store/kv/iterator.go new file mode 100644 index 000000000..f2c68b87c --- /dev/null +++ b/vendor/git.iim.gay/grufwub/go-store/kv/iterator.go @@ -0,0 +1,64 @@ +package kv + +import ( + "git.iim.gay/grufwub/go-errors" + "git.iim.gay/grufwub/go-store/storage" +) + +var ErrIteratorClosed = errors.Define("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 + entries []storage.StorageEntry + index int + key string + onClose func() +} + +// 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() { + // Reset key, path, entries + i.store = nil + i.key = "" + i.entries = nil + + // Perform requested callback + i.onClose() +} + +// 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.key) +} |