@@ -5,17 +5,30 @@ import (
55)
66
77// Iterator is an interface for iterating over key-value pairs in a storage backend.
8+ // A common usage is:
9+ //
10+ // defer it.Close()
11+ //
12+ // for it.First(); it.Valid(); it.Next() {
13+ // item := it.IterItem()
14+ // }
815type Iterator interface {
916 // First seeks to the smallest key greater than or equal to the given key.
17+ // This method must be called because it's necessary for the badger implementation
18+ // to move the iteration cursor to the first key in the iteration range.
19+ // This method must be called before calling Valid, Next, IterItem, or Close.
1020 First ()
1121
1222 // Valid returns whether the iterator is positioned at a valid key-value pair.
23+ // If Valid returns false, the iterator is done and must be closed.
1324 Valid () bool
1425
1526 // Next advances the iterator to the next key-value pair.
27+ // The next key-value pair might be invalid, so you should call Valid() to check.
1628 Next ()
1729
1830 // IterItem returns the current key-value pair, or nil if done.
31+ // A best practice is always to call Valid() before calling IterItem.
1932 IterItem () IterItem
2033
2134 // Close closes the iterator. Iterator must be closed, otherwise it causes memory leak.
@@ -49,7 +62,7 @@ type Reader interface {
4962 // other errors are exceptions
5063 //
5164 // The caller should not modify the contents of the returned slice, but it is
52- // safe to modify the contents of the argument after Get returns. The
65+ // safe to modify the contents of the `key` argument after Get returns. The
5366 // returned slice will remain valid until the returned Closer is closed. On
5467 // success, the caller MUST call closer.Close() or a memory leak will occur.
5568 Get (key []byte ) (value []byte , closer io.Closer , err error )
@@ -63,7 +76,7 @@ type Reader interface {
6376}
6477
6578// Writer is an interface for batch writing to a storage backend.
66- // It cannot be used concurrently for writing .
79+ // One Writer instance cannot be used concurrently by multiple goroutines .
6780type Writer interface {
6881 // Set sets the value for the given key. It overwrites any previous value
6982 // for that key; a DB is not a multi-map.
@@ -117,18 +130,21 @@ func OnCommitSucceed(b ReaderBatchWriter, onSuccessFn func()) {
117130 })
118131}
119132
133+ // StartEndPrefixToLowerUpperBound returns the lower and upper bounds for a range of keys
134+ // specified by the start and end prefixes.
135+ // the lower and upper bounds are used for the key iteration.
136+ // The return value lowerBound specifies the smallest key to iterate and it's inclusive.
137+ // The return value upperBound specifies the largest key to iterate and it's exclusive (not inclusive)
138+ // in order to match all keys prefixed with `endPrefix`, we increment the bytes of `endPrefix` by 1,
139+ // for instance, to iterate keys between "hello" and "world",
140+ // we use "hello" as LowerBound, "worle" as UpperBound, so that "world", "world1", "worldffff...ffff"
141+ // will all be included.
120142func StartEndPrefixToLowerUpperBound (startPrefix , endPrefix []byte ) (lowerBound , upperBound []byte ) {
121- // Return value lowerBound specifies the smallest key to iterate and it's inclusive.
122- // Return value upperBound specifies the largest key to iterate and it's exclusive (not inclusive)
123- // in order to match all keys prefixed with `endPrefix`, we increment the bytes of `endPrefix` by 1,
124- // for instance, to iterate keys between "hello" and "world",
125- // we use "hello" as LowerBound, "worle" as UpperBound, so that "world", "world1", "worldffff...ffff"
126- // will all be included.
127143 return startPrefix , PrefixUpperBound (endPrefix )
128144}
129145
130146// PrefixUpperBound returns a key K such that all possible keys beginning with the input prefix
131- // sort lower than K according to the byte-wise lexicographic key ordering used by Pebble .
147+ // sort lower than K according to the byte-wise lexicographic key ordering.
132148// This is used to define an upper bound for iteration, when we want to iterate over
133149// all keys beginning with a given prefix.
134150// referred to https://pkg.go.dev/github.com/cockroachdb/pebble#example-Iterator-PrefixIteration
0 commit comments