@@ -6,19 +6,20 @@ import (
66
77// Iterator is an interface for iterating over key-value pairs in a storage backend.
88type Iterator interface {
9- // SeekGE seeks to the smallest key greater than or equal to the given key.
10- SeekGE ()
9+ // First seeks to the smallest key greater than or equal to the given key.
10+ First ()
1111
1212 // Valid returns whether the iterator is positioned at a valid key-value pair.
1313 Valid () bool
1414
1515 // Next advances the iterator to the next key-value pair.
1616 Next ()
1717
18- // Key returns the key of the current key-value pair, or nil if done.
18+ // IterItem returns the current key-value pair, or nil if done.
1919 IterItem () IterItem
2020
2121 // Close closes the iterator. Iterator must be closed, otherwise it causes memory leak.
22+ // No errors expected during normal operation
2223 Close () error
2324}
2425
@@ -28,6 +29,7 @@ type IterItem interface {
2829
2930 // Value returns the value of the current key-value pair
3031 // The reason it takes a function is to follow badgerDB's API pattern
32+ // No errors expected during normal operation
3133 Value (func (val []byte ) error ) error
3234}
3335
@@ -44,14 +46,19 @@ func DefaultIteratorOptions() IteratorOption {
4446type Reader interface {
4547 // Get gets the value for the given key. It returns ErrNotFound if the DB
4648 // does not contain the key.
49+ // other errors are exceptions
4750 //
4851 // The caller should not modify the contents of the returned slice, but it is
4952 // safe to modify the contents of the argument after Get returns. The
5053 // returned slice will remain valid until the returned Closer is closed. On
5154 // success, the caller MUST call closer.Close() or a memory leak will occur.
5255 Get (key []byte ) (value []byte , closer io.Closer , err error )
5356
54- // NewIter returns a new Iterator for the given key range [startPrefix, endPrefix], both inclusive.
57+ // NewIter returns a new Iterator for the given key prefix range [startPrefix, endPrefix], both inclusive.
58+ // Specifically, all keys that meet ANY of the following conditions are included in the iteration:
59+ // - have a prefix equal to startPrefix OR
60+ // - have a prefix equal to the endPrefix OR
61+ // - have a prefix that is lexicographically between startPrefix and endPrefix
5562 NewIter (startPrefix , endPrefix []byte , ops IteratorOption ) (Iterator , error )
5663}
5764
@@ -61,20 +68,26 @@ type Writer interface {
6168 // for that key; a DB is not a multi-map.
6269 //
6370 // It is safe to modify the contents of the arguments after Set returns.
71+ // No errors expected during normal operation
6472 Set (k , v []byte ) error
6573
6674 // Delete deletes the value for the given key. Deletes are blind all will
6775 // succeed even if the given key does not exist.
6876 //
6977 // It is safe to modify the contents of the arguments after Delete returns.
78+ // No errors expected during normal operation
7079 Delete (key []byte ) error
7180
7281 // DeleteByRange removes all keys with a prefix that falls within the
7382 // range [start, end], both inclusive.
83+ // No errors expected during normal operation
7484 DeleteByRange (globalReader Reader , startPrefix , endPrefix []byte ) error
7585}
7686
7787// ReaderBatchWriter is an interface for reading and writing to a storage backend.
88+ // It is useful for performing a related sequence of reads and writes, after which you would like
89+ // to modify some non-database state if the sequence completed successfully (via AddCallback).
90+ // If you are not using AddCallback, avoid using ReaderBatchWriter: use Reader and Writer directly.
7891type ReaderBatchWriter interface {
7992 // GlobalReader returns a database-backed reader which reads the latest committed global database state ("read-committed isolation").
8093 // This reader will not read writes written to ReaderBatchWriter.Writer until the write batch is committed.
@@ -104,21 +117,21 @@ func OnCommitSucceed(b ReaderBatchWriter, onSuccessFn func()) {
104117}
105118
106119func StartEndPrefixToLowerUpperBound (startPrefix , endPrefix []byte ) (lowerBound , upperBound []byte ) {
107- // LowerBound specifies the smallest key to iterate and it's inclusive.
108- // UpperBound specifies the largest key to iterate and it's exclusive (not inclusive)
109- // in order to match all keys prefixed with the `end` bytes , we increment the bytes of end by 1,
120+ // Return value lowerBound specifies the smallest key to iterate and it's inclusive.
121+ // Return value upperBound specifies the largest key to iterate and it's exclusive (not inclusive)
122+ // in order to match all keys prefixed with `endPrefix` , we increment the bytes of `endPrefix` by 1,
110123 // for instance, to iterate keys between "hello" and "world",
111124 // we use "hello" as LowerBound, "worle" as UpperBound, so that "world", "world1", "worldffff...ffff"
112125 // will all be included.
113- return startPrefix , prefixUpperBound (endPrefix )
126+ return startPrefix , PrefixUpperBound (endPrefix )
114127}
115128
116- // prefixUpperBound returns a key K such that all possible keys beginning with the input prefix
129+ // PrefixUpperBound returns a key K such that all possible keys beginning with the input prefix
117130// sort lower than K according to the byte-wise lexicographic key ordering used by Pebble.
118131// This is used to define an upper bound for iteration, when we want to iterate over
119132// all keys beginning with a given prefix.
120133// referred to https://pkg.go.dev/github.com/cockroachdb/pebble#example-Iterator-PrefixIteration
121- func prefixUpperBound (prefix []byte ) []byte {
134+ func PrefixUpperBound (prefix []byte ) []byte {
122135 end := make ([]byte , len (prefix ))
123136 copy (end , prefix )
124137 for i := len (end ) - 1 ; i >= 0 ; i -- {
0 commit comments