micro/store/service/service.go

176 lines
3.6 KiB
Go
Raw Normal View History

2019-10-11 16:44:42 +03:00
// Package service implements the store service interface
package service
import (
"context"
"io"
"time"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/errors"
"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 16:44:42 +03:00
)
type serviceStore struct {
2019-12-16 17:38:51 +03:00
options store.Options
2019-10-11 16:44:42 +03:00
// Namespace to use
Namespace string
2019-10-11 16:44:42 +03:00
// Addresses of the nodes
Nodes []string
// Prefix to use
Prefix string
// Suffix to use
Suffix string
2019-10-11 16:44:42 +03:00
// store service client
Client pb.StoreService
}
func (s *serviceStore) Init(opts ...store.Option) error {
for _, o := range opts {
o(&s.options)
}
s.Namespace = s.options.Database
s.Prefix = s.options.Table
s.Nodes = s.options.Nodes
return nil
}
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 16:44:42 +03:00
// Sync all the known records
func (s *serviceStore) List(opts ...store.ListOption) ([]string, error) {
stream, err := s.Client.List(s.Context(), &pb.ListRequest{}, client.WithAddress(s.Nodes...))
if err != nil && errors.Equal(err, errors.NotFound("", "")) {
return nil, store.ErrNotFound
} else if err != nil {
2019-10-11 16:44:42 +03:00
return nil, err
}
defer stream.Close()
var keys []string
2019-10-11 16:44:42 +03:00
for {
rsp, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
return keys, err
2019-10-11 16:44:42 +03:00
}
for _, key := range rsp.Keys {
keys = append(keys, key)
2019-10-11 16:44:42 +03:00
}
}
return keys, nil
2019-10-11 16:44:42 +03:00
}
// Read a record with key
func (s *serviceStore) Read(key string, opts ...store.ReadOption) ([]*store.Record, error) {
var options store.ReadOptions
for _, o := range opts {
o(&options)
}
rsp, err := s.Client.Read(s.Context(), &pb.ReadRequest{
Key: key,
Options: &pb.ReadOptions{
Prefix: options.Prefix,
},
2019-10-11 16:44:42 +03:00
}, client.WithAddress(s.Nodes...))
if err != nil && errors.Equal(err, errors.NotFound("", "")) {
return nil, store.ErrNotFound
} else if err != nil {
2019-10-11 16:44:42 +03:00
return nil, err
}
records := make([]*store.Record, 0, len(rsp.Records))
2019-10-11 16:44:42 +03:00
for _, val := range rsp.Records {
records = append(records, &store.Record{
Key: val.Key,
Value: val.Value,
Expiry: time.Duration(val.Expiry) * time.Second,
})
}
2019-10-11 16:44:42 +03:00
return records, nil
}
// Write a record
func (s *serviceStore) Write(record *store.Record, opts ...store.WriteOption) error {
_, err := s.Client.Write(s.Context(), &pb.WriteRequest{
Record: &pb.Record{
2019-10-11 16:44:42 +03:00
Key: record.Key,
Value: record.Value,
Expiry: int64(record.Expiry.Seconds()),
},
2019-10-11 16:44:42 +03:00
}, client.WithAddress(s.Nodes...))
if err != nil && errors.Equal(err, errors.NotFound("", "")) {
return store.ErrNotFound
}
2019-10-11 16:44:42 +03:00
return err
}
// Delete a record with key
func (s *serviceStore) Delete(key string, opts ...store.DeleteOption) error {
_, err := s.Client.Delete(s.Context(), &pb.DeleteRequest{
Key: key,
2019-10-11 16:44:42 +03:00
}, client.WithAddress(s.Nodes...))
if err != nil && errors.Equal(err, errors.NotFound("", "")) {
return store.ErrNotFound
}
2019-10-11 16:44:42 +03:00
return err
}
func (s *serviceStore) String() string {
return "service"
}
func (s *serviceStore) Options() store.Options {
return s.options
}
2019-10-11 16:44:42 +03:00
// NewStore returns a new store service implementation
2019-12-16 17:38:51 +03:00
func NewStore(opts ...store.Option) store.Store {
var options store.Options
for _, o := range opts {
o(&options)
}
2019-10-11 16:44:42 +03:00
service := &serviceStore{
2019-12-16 17:38:51 +03:00
options: options,
Namespace: options.Database,
Prefix: options.Table,
2019-12-16 17:38:51 +03:00
Nodes: options.Nodes,
Client: pb.NewStoreService("go.micro.store", client.DefaultClient),
2019-10-11 16:44:42 +03:00
}
return service
}