summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-store/kv/iterator.go
blob: d3999273fff5548b32c2489842327331eaf36d62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package kv

import (
	"codeberg.org/gruf/go-errors"
	"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
	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)
}