Skip to content

Commit

Permalink
feat(kv): define forward cursor interface
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeMac committed Dec 12, 2019
1 parent 3daaa4d commit 9016a48
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions kv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,64 @@ type Bucket interface {
Put(key, value []byte) error
// Delete should error if the transaction it was called in is not writable.
Delete(key []byte) error
// ForwardCursor returns a forward cursor from the seek position provided.
// Other options can be supplied to provide direction and hints.
ForwardCursor(seek []byte, opts ...CursorOption) (ForwardCursor, error)
}

// Cursor is an abstraction for iterating/ranging through data. A concrete implementation
// of a cursor can be found in cursor.go.
type Cursor interface {
ForwardCursor
// Seek moves the cursor forward until reaching prefix in the key name.
Seek(prefix []byte) (k []byte, v []byte)
// First moves the cursor to the first key in the bucket.
First() (k []byte, v []byte)
// Last moves the cursor to the last key in the bucket.
Last() (k []byte, v []byte)
// Next moves the cursor to the next key in the bucket.
Next() (k []byte, v []byte)
// Prev moves the cursor to the prev key in the bucket.
Prev() (k []byte, v []byte)
}

// ForwardCursor is an abstraction for interating/ranging through data in one direction.
type ForwardCursor interface {
// Next moves the cursor to the next key in the bucket.
Next() (k, v []byte)
}

// CursorDirection is an integer used to define the direction
// a request cursor operates in.
type CursorDirection int

const (
// CursorAscending directs a cursor to range in ascending order
CursorAscending CursorDirection = iota
// CursorAscending directs a cursor to range in descending order
CursorDescending
)

// CursorConfig is a type used to configure a new forward cursor.
// It includes a direction and a set of hints
type CursorConfig struct {
Direction CursorDirection
Hints CursorHints
}

// CursorOption is a functional option for configuring a forward cursor
type CursorOption func(*CursorConfig)

// WithCursorDirection sets the cursor direction on a provided cursor config
func WithCursorDirection(direction CursorDirection) CursorOption {
return func(c *CursorConfig) {
c.Direction = direction
}
}

// WithCursorHints configs the provided hints on the cursor config
func WithCursorHints(hints ...CursorHint) CursorOption {
return func(c *CursorConfig) {
for _, hint := range hints {
hint(&c.Hints)
}
}
}

0 comments on commit 9016a48

Please sign in to comment.