2020-03-12 13:41:30 +00:00
// Package store is an interface for distributed data storage.
// The design document is located at https://github.com/micro/development/blob/master/design/store.md
2019-06-12 07:46:20 +01:00
package store
2019-05-31 00:43:23 +01:00
import (
"errors"
"time"
)
var (
2020-03-12 13:41:30 +00:00
// ErrNotFound is returned when a key doesn't exist
2019-05-31 00:43:23 +01:00
ErrNotFound = errors . New ( "not found" )
2020-03-12 13:41:30 +00:00
// DefaultStore is the memory store.
DefaultStore Store = new ( noopStore )
2019-05-31 00:43:23 +01:00
)
2019-06-12 07:46:20 +01:00
// Store is a data storage interface
type Store interface {
2020-03-12 13:41:30 +00:00
// Init initialises the store. It must perform any required setup on the backing storage implementation and check that it is ready for use, returning any errors.
2020-01-08 12:11:31 +00:00
Init ( ... Option ) error
2020-03-12 13:41:30 +00:00
// Options allows you to view the current options.
Options ( ) Options
// Read takes a single key name and optional ReadOptions. It returns matching []*Record or an error.
Read ( key string , opts ... ReadOption ) ( [ ] * Record , error )
// Write() writes a record to the store, and returns an error if the record was not written.
Write ( r * Record , opts ... WriteOption ) error
// Delete removes the record with the corresponding key from the store.
Delete ( key string , opts ... DeleteOption ) error
// List returns any keys that match, or an empty list with no error if none matched.
List ( opts ... ListOption ) ( [ ] string , error )
2020-04-08 09:51:10 +01:00
// Close the store
Close ( ) error
2020-04-01 23:27:15 +01:00
// String returns the name of the implementation.
String ( ) string
2019-05-31 00:43:23 +01:00
}
2020-03-12 13:41:30 +00:00
// Record is an item stored or retrieved from a Store
2019-05-31 00:43:23 +01:00
type Record struct {
2020-06-03 09:45:08 +01:00
// The key to store the record
Key string ` json:"key" `
// The value within the record
Value [ ] byte ` json:"value" `
// Any associated metadata for indexing
Metadata map [ string ] interface { } ` json:"metadata" `
// Time to expire a record: TODO: change to timestamp
2020-04-09 19:38:43 +01:00
Expiry time . Duration ` json:"expiry,omitempty" `
2019-05-31 00:43:23 +01:00
}