2020-03-12 16:41:30 +03:00
// Package store is an interface for distributed data storage.
2020-07-14 15:13:32 +03:00
// The design document is located at https://github.com/micro/development/blob/master/design/framework/store.md
2019-06-12 09:46:20 +03:00
package store
2019-05-31 02:43:23 +03:00
import (
2020-09-17 15:15:42 +03:00
"context"
2019-05-31 02:43:23 +03:00
"errors"
"time"
)
var (
2020-03-12 16:41:30 +03:00
// ErrNotFound is returned when a key doesn't exist
2020-11-03 01:08:23 +03:00
ErrNotFound = errors . New ( "not found" )
// DefaultStore is the global default store
DefaultStore Store = NewStore ( )
2019-05-31 02:43:23 +03:00
)
2019-06-12 09:46:20 +03:00
// Store is a data storage interface
type Store interface {
2020-03-12 16:41:30 +03: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-10-16 09:38:57 +03:00
Init ( opts ... Option ) error
2020-11-03 01:08:23 +03:00
// Connect is used when store needs to be connected
2020-10-16 09:38:57 +03:00
Connect ( ctx context . Context ) error
2020-03-12 16:41:30 +03: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.
2020-09-17 15:15:42 +03:00
Read ( ctx context . Context , key string , opts ... ReadOption ) ( [ ] * Record , error )
2020-03-12 16:41:30 +03:00
// Write() writes a record to the store, and returns an error if the record was not written.
2020-09-17 15:15:42 +03:00
Write ( ctx context . Context , r * Record , opts ... WriteOption ) error
2020-03-12 16:41:30 +03:00
// Delete removes the record with the corresponding key from the store.
2020-09-17 15:15:42 +03:00
Delete ( ctx context . Context , key string , opts ... DeleteOption ) error
2020-03-12 16:41:30 +03:00
// List returns any keys that match, or an empty list with no error if none matched.
2020-09-17 15:15:42 +03:00
List ( ctx context . Context , opts ... ListOption ) ( [ ] string , error )
2020-10-16 09:38:57 +03:00
// Disconnect the store
Disconnect ( ctx context . Context ) error
2020-04-02 01:27:15 +03:00
// String returns the name of the implementation.
String ( ) string
2019-05-31 02:43:23 +03:00
}
2020-03-12 16:41:30 +03:00
// Record is an item stored or retrieved from a Store
2019-05-31 02:43:23 +03:00
type Record struct {
2020-06-03 11:45:08 +03: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 21:38:43 +03:00
Expiry time . Duration ` json:"expiry,omitempty" `
2019-05-31 02:43:23 +03:00
}