2019-10-11 14:44:42 +01:00
|
|
|
// Package service implements the store service interface
|
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/client"
|
|
|
|
"github.com/micro/go-micro/v2/metadata"
|
|
|
|
"github.com/micro/go-micro/v2/store"
|
|
|
|
pb "github.com/micro/go-micro/v2/store/service/proto"
|
2019-10-11 14:44:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type serviceStore struct {
|
2019-12-16 14:38:51 +00:00
|
|
|
options store.Options
|
2019-10-11 14:44:42 +01:00
|
|
|
|
2019-12-16 12:13:18 +00:00
|
|
|
// Namespace to use
|
|
|
|
Namespace string
|
|
|
|
|
2019-10-11 14:44:42 +01:00
|
|
|
// Addresses of the nodes
|
|
|
|
Nodes []string
|
|
|
|
|
2019-12-16 12:13:18 +00:00
|
|
|
// Prefix to use
|
|
|
|
Prefix string
|
|
|
|
|
2019-10-11 14:44:42 +01:00
|
|
|
// store service client
|
|
|
|
Client pb.StoreService
|
|
|
|
}
|
|
|
|
|
2020-01-08 12:11:31 +00:00
|
|
|
func (s *serviceStore) Init(opts ...store.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&s.options)
|
|
|
|
}
|
|
|
|
s.Namespace = s.options.Namespace
|
|
|
|
s.Prefix = s.options.Prefix
|
|
|
|
s.Nodes = s.options.Nodes
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-16 12:13:18 +00:00
|
|
|
func (s *serviceStore) Context() context.Context {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
md := make(metadata.Metadata)
|
|
|
|
|
|
|
|
if len(s.Namespace) > 0 {
|
|
|
|
md["Micro-Namespace"] = s.Namespace
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.Prefix) > 0 {
|
|
|
|
md["Micro-Prefix"] = s.Prefix
|
|
|
|
}
|
|
|
|
|
|
|
|
return metadata.NewContext(ctx, md)
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:44:42 +01:00
|
|
|
// Sync all the known records
|
2019-10-23 22:05:39 +01:00
|
|
|
func (s *serviceStore) List() ([]*store.Record, error) {
|
2019-12-16 12:13:18 +00:00
|
|
|
stream, err := s.Client.List(s.Context(), &pb.ListRequest{}, client.WithAddress(s.Nodes...))
|
2019-10-11 14:44:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer stream.Close()
|
|
|
|
|
|
|
|
var records []*store.Record
|
|
|
|
|
|
|
|
for {
|
|
|
|
rsp, err := stream.Recv()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return records, err
|
|
|
|
}
|
2020-01-08 22:23:14 +00:00
|
|
|
|
2019-10-11 14:44:42 +01:00
|
|
|
for _, record := range rsp.Records {
|
|
|
|
records = append(records, &store.Record{
|
|
|
|
Key: record.Key,
|
|
|
|
Value: record.Value,
|
|
|
|
Expiry: time.Duration(record.Expiry) * time.Second,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return records, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read a record with key
|
2020-01-08 22:23:14 +00:00
|
|
|
func (s *serviceStore) Read(key string, opts ...store.ReadOption) ([]*store.Record, error) {
|
|
|
|
var options store.ReadOptions
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2019-12-16 12:13:18 +00:00
|
|
|
rsp, err := s.Client.Read(s.Context(), &pb.ReadRequest{
|
2020-01-08 22:23:14 +00:00
|
|
|
Key: key,
|
|
|
|
Options: &pb.ReadOptions{
|
|
|
|
Prefix: options.Prefix,
|
|
|
|
},
|
2019-10-11 14:44:42 +01:00
|
|
|
}, client.WithAddress(s.Nodes...))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-03 22:59:44 +03:00
|
|
|
|
|
|
|
records := make([]*store.Record, 0, len(rsp.Records))
|
2020-01-08 22:23:14 +00:00
|
|
|
|
2019-10-11 14:44:42 +01:00
|
|
|
for _, val := range rsp.Records {
|
|
|
|
records = append(records, &store.Record{
|
|
|
|
Key: val.Key,
|
|
|
|
Value: val.Value,
|
|
|
|
Expiry: time.Duration(val.Expiry) * time.Second,
|
|
|
|
})
|
|
|
|
}
|
2020-01-08 22:23:14 +00:00
|
|
|
|
2019-10-11 14:44:42 +01:00
|
|
|
return records, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write a record
|
2020-01-08 22:23:14 +00:00
|
|
|
func (s *serviceStore) Write(record *store.Record) error {
|
|
|
|
_, err := s.Client.Write(s.Context(), &pb.WriteRequest{
|
|
|
|
Record: &pb.Record{
|
2019-10-11 14:44:42 +01:00
|
|
|
Key: record.Key,
|
|
|
|
Value: record.Value,
|
|
|
|
Expiry: int64(record.Expiry.Seconds()),
|
2020-01-08 22:23:14 +00:00
|
|
|
},
|
2019-10-11 14:44:42 +01:00
|
|
|
}, client.WithAddress(s.Nodes...))
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete a record with key
|
2020-01-08 22:23:14 +00:00
|
|
|
func (s *serviceStore) Delete(key string) error {
|
2019-12-16 12:13:18 +00:00
|
|
|
_, err := s.Client.Delete(s.Context(), &pb.DeleteRequest{
|
2020-01-08 22:23:14 +00:00
|
|
|
Key: key,
|
2019-10-11 14:44:42 +01:00
|
|
|
}, client.WithAddress(s.Nodes...))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-10 19:13:55 +00:00
|
|
|
func (s *serviceStore) String() string {
|
|
|
|
return "service"
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:44:42 +01:00
|
|
|
// NewStore returns a new store service implementation
|
2019-12-16 14:38:51 +00:00
|
|
|
func NewStore(opts ...store.Option) store.Store {
|
|
|
|
var options store.Options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
2019-12-16 12:13:18 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:44:42 +01:00
|
|
|
service := &serviceStore{
|
2019-12-16 14:38:51 +00:00
|
|
|
options: options,
|
|
|
|
Namespace: options.Namespace,
|
|
|
|
Prefix: options.Prefix,
|
|
|
|
Nodes: options.Nodes,
|
2019-12-16 12:13:18 +00:00
|
|
|
Client: pb.NewStoreService("go.micro.store", client.DefaultClient),
|
2019-10-11 14:44:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return service
|
|
|
|
}
|