kv icon indicating copy to clipboard operation
kv copied to clipboard

Enumerator could be more convenient to use

Open rogpeppe opened this issue 10 years ago • 2 comments

A standard idiom for enumerators seems to have emerged [1],[2], which makes for quite nice looking code. Perhaps consider using that here.

Something like this perhaps?

type Enumerator struct {...}

// Next moves to the next element in the enumeration.
// It returns false when there are no more entries
// or if there's an error.
func (*Enumerator) Next() bool

// Prev moves to the previous element in the enumeration.
// It returns false when there are no more entries
// or if there's an error.
func (*Enumerator) Prev() bool

// Key returns the key at the current enumerator position.
func (*Enumerator) Key() []byte

// Value returns the value at the current enumerator position.
func (*Enumerator) Value() []byte

// Err returns any error encountered during the enumeration.
func (*Enumerator) Err() error

Example usage:

enum := db.SeekFirst()
for enum.Next() {
    fmt.Printf("%s=%s\n", enum.Key(), enum.Value())
}
if err := enum.Err(); err != nil {
    log.Fatal(err)
}

[1] http://golang.org/pkg/bufio/#Scanner [2] http://godoc.org/gopkg.in/mgo.v2#Iter

rogpeppe avatar Jul 18 '15 13:07 rogpeppe

@rogpeppe Thanks for the suggestions. Could you please checkout branch issue25 (should have been named issue26, but I errored) and test and review the changes? I would like to know your opinions before publishing it on master. Thanks in advance.

cznic avatar Jul 19 '15 11:07 cznic

I've commented directly on https://github.com/cznic/kv/commit/cf4d15f3e815ff3194eda73daf4b9f929cebfc44 (BTW, why not just make a PR?)

Seems reasonable to me but I'm not keen on the API pollution.

Thanks for considering the issue.

rogpeppe avatar Jul 20 '15 16:07 rogpeppe