Skip to content

Clean up store interface #823

@ethanfrey

Description

@ethanfrey

Is your feature request related to a problem? Please describe.

There have been some comments that this could be done better. It was one of the first APIs in weave, and we have learned a lot since then. I'm sure we can improve it.

For example, we now have a much simpler Iterator

type Iterator interface {
	Next() (key, value []byte, err error)
	Release()
}

Let us consider the following interfaces here:

type ReadOnlyKVStore interface {
	Get(key []byte) ([]byte, error)
	Has(key []byte) (bool, error)
	Iterator(start, end []byte) (Iterator, error)
	ReverseIterator(start, end []byte) (Iterator, error)
}

type SetDeleter interface {
	Set(key, value []byte) error
	Delete(key []byte) error
}

// Batch is used to cache writes before commiting to a lower store
type Batch interface {
	SetDeleter
	Write() error
}

type KVStore interface {
	ReadOnlyKVStore
	SetDeleter
	NewBatch() Batch
}

type CacheableKVStore interface {
	KVStore
	CacheWrap() KVCacheWrap
}

type KVCacheWrap interface {
	// CacheableKVStore allows us to use this Cache recursively
	CacheableKVStore
	Write() error
	Discard()
}

Describe the solution you'd like

Some thoughts on cleanup:

  • Can we remove NewBatch() from KVStore? What was my thinking there?
  • Can we simplify/unify CacheableKVStore and KVCacheWrap?
  • Can we combine/improve Iterator and ReverseIterator?

Describe alternatives you've considered

One concrete suggestion is to make iterator take functional parameters

// default -> ascending and no limit
type IterConfig struct {
  Descending bool
  Limit int
}

type IterOpt func(IterConfig) IterConfig

type ReadOnlyKVStore interface {
    Iterate(start, end []byte, opts ...IterOpt) (Iterator, error)
}

// example opt we expose
var Reverse IterOpt = func (cfg IterConfig) IterConfig {
  cfg.Descending = true
  return cfg
}

Additional context
Add any other context or screenshots about the feature request here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions