forked from micro/micro
/
store.go
executable file
·117 lines (103 loc) · 3.5 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Original source: github.com/micro/go-micro/v3/store/store.go
// Package store is an interface for distributed data storage.
// The design document is located at https://github.com/micro/development/blob/master/design/framework/store.md
package store
import (
"encoding/json"
"errors"
"time"
)
// Order
type Order string
const (
OrderAsc = Order("asc")
OrderDesc = Order("desc")
)
var (
// DefaultStore implementation
DefaultStore Store
// DefaultBlobStore implementation
DefaultBlobStore BlobStore
// ErrNotFound is returned when a key doesn't exist
ErrNotFound = errors.New("not found")
)
// Store is a data storage interface
type Store interface {
// 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.
Init(...Option) error
// 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)
// Close the store
Close() error
// String returns the name of the implementation.
String() string
}
// Record is an item stored or retrieved from a Store
type Record struct {
// 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
Expiry time.Duration `json:"expiry,omitempty"`
}
// NewRecord returns a record from key, val
func NewRecord(key string, val interface{}) *Record {
b, _ := json.Marshal(val)
return &Record{
Key: key,
Value: b,
}
}
// Encode will marshal any type into the byte Value field
func (r *Record) Encode(v interface{}) error {
b, err := json.Marshal(v)
if err != nil {
return err
}
r.Value = b
return nil
}
// Decode is a convenience helper for decoding records
func (r *Record) Decode(v interface{}) error {
return json.Unmarshal(r.Value, v)
}
// Read records
func Read(key string, opts ...ReadOption) ([]*Record, error) {
// execute the query
return DefaultStore.Read(key, opts...)
}
// Write a record to the store
func Write(r *Record) error {
return DefaultStore.Write(r)
}
// Delete removes the record with the corresponding key from the store.
func Delete(key string) error {
return DefaultStore.Delete(key)
}
// List returns any keys that match, or an empty list with no error if none matched.
func List(opts ...ListOption) ([]string, error) {
return DefaultStore.List(opts...)
}