2019-10-11 14:44:42 +01:00
|
|
|
// Package service implements the store service interface
|
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-06-03 09:45:08 +01:00
|
|
|
"fmt"
|
2019-10-11 14:44:42 +01:00
|
|
|
"io"
|
2020-06-03 09:45:08 +01:00
|
|
|
"reflect"
|
2019-10-11 14:44:42 +01:00
|
|
|
"time"
|
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/client"
|
2020-03-16 10:30:56 +00:00
|
|
|
"github.com/micro/go-micro/v2/errors"
|
2020-01-30 14:39:00 +03:00
|
|
|
"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
|
|
|
|
2020-04-08 19:25:57 +01:00
|
|
|
// The database to use
|
|
|
|
Database string
|
|
|
|
|
|
|
|
// The table to use
|
|
|
|
Table string
|
2019-12-16 12:13:18 +00:00
|
|
|
|
2019-10-11 14:44:42 +01:00
|
|
|
// Addresses of the nodes
|
|
|
|
Nodes []string
|
|
|
|
|
|
|
|
// store service client
|
|
|
|
Client pb.StoreService
|
|
|
|
}
|
|
|
|
|
2020-04-08 09:51:10 +01:00
|
|
|
func (s *serviceStore) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-08 12:11:31 +00:00
|
|
|
func (s *serviceStore) Init(opts ...store.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&s.options)
|
|
|
|
}
|
2020-04-08 19:25:57 +01:00
|
|
|
s.Database = s.options.Database
|
|
|
|
s.Table = s.options.Table
|
2020-01-08 12:11:31 +00:00
|
|
|
s.Nodes = s.options.Nodes
|
|
|
|
|
2020-07-15 14:01:33 +01:00
|
|
|
if s.options.Client == nil {
|
|
|
|
s.options.Client = client.DefaultClient
|
|
|
|
}
|
|
|
|
s.Client = pb.NewStoreService("go.micro.store", s.options.Client)
|
|
|
|
|
2020-01-08 12:11:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-16 12:13:18 +00:00
|
|
|
func (s *serviceStore) Context() context.Context {
|
|
|
|
ctx := context.Background()
|
|
|
|
md := make(metadata.Metadata)
|
2020-05-01 00:25:17 +01:00
|
|
|
if len(s.Database) > 0 {
|
|
|
|
md["Micro-Database"] = s.Database
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.Table) > 0 {
|
|
|
|
md["Micro-Table"] = s.Table
|
|
|
|
}
|
2019-12-16 12:13:18 +00:00
|
|
|
return metadata.NewContext(ctx, md)
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:44:42 +01:00
|
|
|
// Sync all the known records
|
2020-03-12 13:41:30 +00:00
|
|
|
func (s *serviceStore) List(opts ...store.ListOption) ([]string, error) {
|
2020-05-01 15:22:44 +01:00
|
|
|
options := store.ListOptions{
|
|
|
|
Database: s.Database,
|
|
|
|
Table: s.Table,
|
|
|
|
}
|
|
|
|
|
2020-04-30 22:51:25 +01:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
listOpts := &pb.ListOptions{
|
|
|
|
Database: options.Database,
|
|
|
|
Table: options.Table,
|
|
|
|
Prefix: options.Prefix,
|
|
|
|
Suffix: options.Suffix,
|
|
|
|
Limit: uint64(options.Limit),
|
|
|
|
Offset: uint64(options.Offset),
|
|
|
|
}
|
|
|
|
|
|
|
|
stream, err := s.Client.List(s.Context(), &pb.ListRequest{Options: listOpts}, client.WithAddress(s.Nodes...))
|
2020-03-17 14:27:20 +03:00
|
|
|
if err != nil && errors.Equal(err, errors.NotFound("", "")) {
|
2020-03-16 10:30:56 +00:00
|
|
|
return nil, store.ErrNotFound
|
|
|
|
} else if err != nil {
|
2019-10-11 14:44:42 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer stream.Close()
|
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
var keys []string
|
2019-10-11 14:44:42 +01:00
|
|
|
|
|
|
|
for {
|
|
|
|
rsp, err := stream.Recv()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-03-12 13:41:30 +00:00
|
|
|
return keys, err
|
2019-10-11 14:44:42 +01:00
|
|
|
}
|
2020-01-08 22:23:14 +00:00
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
for _, key := range rsp.Keys {
|
|
|
|
keys = append(keys, key)
|
2019-10-11 14:44:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
return keys, nil
|
2019-10-11 14:44:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2020-05-01 15:22:44 +01:00
|
|
|
options := store.ReadOptions{
|
|
|
|
Database: s.Database,
|
|
|
|
Table: s.Table,
|
|
|
|
}
|
|
|
|
|
2020-01-08 22:23:14 +00:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2020-04-30 22:51:25 +01:00
|
|
|
readOpts := &pb.ReadOptions{
|
|
|
|
Database: options.Database,
|
|
|
|
Table: options.Table,
|
|
|
|
Prefix: options.Prefix,
|
|
|
|
Suffix: options.Suffix,
|
|
|
|
Limit: uint64(options.Limit),
|
|
|
|
Offset: uint64(options.Offset),
|
|
|
|
}
|
|
|
|
|
2019-12-16 12:13:18 +00:00
|
|
|
rsp, err := s.Client.Read(s.Context(), &pb.ReadRequest{
|
2020-04-30 22:51:25 +01:00
|
|
|
Key: key,
|
|
|
|
Options: readOpts,
|
2019-10-11 14:44:42 +01:00
|
|
|
}, client.WithAddress(s.Nodes...))
|
2020-03-17 14:27:20 +03:00
|
|
|
if err != nil && errors.Equal(err, errors.NotFound("", "")) {
|
2020-03-16 10:30:56 +00:00
|
|
|
return nil, store.ErrNotFound
|
|
|
|
} else if err != nil {
|
2019-10-11 14:44:42 +01:00
|
|
|
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 {
|
2020-06-03 09:45:08 +01:00
|
|
|
metadata := make(map[string]interface{})
|
|
|
|
|
|
|
|
for k, v := range val.Metadata {
|
|
|
|
switch v.Type {
|
|
|
|
// TODO: parse all types
|
|
|
|
default:
|
|
|
|
metadata[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:44:42 +01:00
|
|
|
records = append(records, &store.Record{
|
2020-06-03 09:45:08 +01:00
|
|
|
Key: val.Key,
|
|
|
|
Value: val.Value,
|
|
|
|
Expiry: time.Duration(val.Expiry) * time.Second,
|
|
|
|
Metadata: metadata,
|
2019-10-11 14:44:42 +01:00
|
|
|
})
|
|
|
|
}
|
2020-01-08 22:23:14 +00:00
|
|
|
|
2019-10-11 14:44:42 +01:00
|
|
|
return records, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write a record
|
2020-03-12 13:41:30 +00:00
|
|
|
func (s *serviceStore) Write(record *store.Record, opts ...store.WriteOption) error {
|
2020-05-01 15:22:44 +01:00
|
|
|
options := store.WriteOptions{
|
|
|
|
Database: s.Database,
|
|
|
|
Table: s.Table,
|
|
|
|
}
|
|
|
|
|
2020-04-30 22:51:25 +01:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
writeOpts := &pb.WriteOptions{
|
|
|
|
Database: options.Database,
|
|
|
|
Table: options.Table,
|
|
|
|
}
|
|
|
|
|
2020-06-03 09:45:08 +01:00
|
|
|
metadata := make(map[string]*pb.Field)
|
|
|
|
|
|
|
|
for k, v := range record.Metadata {
|
|
|
|
metadata[k] = &pb.Field{
|
|
|
|
Type: reflect.TypeOf(v).String(),
|
|
|
|
Value: fmt.Sprintf("%v", v),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-08 22:23:14 +00:00
|
|
|
_, err := s.Client.Write(s.Context(), &pb.WriteRequest{
|
|
|
|
Record: &pb.Record{
|
2020-06-03 09:45:08 +01:00
|
|
|
Key: record.Key,
|
|
|
|
Value: record.Value,
|
|
|
|
Expiry: int64(record.Expiry.Seconds()),
|
|
|
|
Metadata: metadata,
|
2020-01-08 22:23:14 +00:00
|
|
|
},
|
2020-04-30 22:51:25 +01:00
|
|
|
Options: writeOpts}, client.WithAddress(s.Nodes...))
|
2020-03-17 14:27:20 +03:00
|
|
|
if err != nil && errors.Equal(err, errors.NotFound("", "")) {
|
|
|
|
return store.ErrNotFound
|
|
|
|
}
|
2019-10-11 14:44:42 +01:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete a record with key
|
2020-03-12 13:41:30 +00:00
|
|
|
func (s *serviceStore) Delete(key string, opts ...store.DeleteOption) error {
|
2020-05-01 15:22:44 +01:00
|
|
|
options := store.DeleteOptions{
|
|
|
|
Database: s.Database,
|
|
|
|
Table: s.Table,
|
|
|
|
}
|
|
|
|
|
2020-04-30 22:51:25 +01:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteOpts := &pb.DeleteOptions{
|
|
|
|
Database: options.Database,
|
|
|
|
Table: options.Table,
|
|
|
|
}
|
|
|
|
|
2019-12-16 12:13:18 +00:00
|
|
|
_, err := s.Client.Delete(s.Context(), &pb.DeleteRequest{
|
2020-04-30 22:51:25 +01:00
|
|
|
Key: key,
|
|
|
|
Options: deleteOpts,
|
2019-10-11 14:44:42 +01:00
|
|
|
}, client.WithAddress(s.Nodes...))
|
2020-03-17 14:27:20 +03:00
|
|
|
if err != nil && errors.Equal(err, errors.NotFound("", "")) {
|
2020-03-16 10:30:56 +00:00
|
|
|
return store.ErrNotFound
|
|
|
|
}
|
2020-03-17 14:27:20 +03:00
|
|
|
|
2019-10-11 14:44:42 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-10 19:13:55 +00:00
|
|
|
func (s *serviceStore) String() string {
|
|
|
|
return "service"
|
|
|
|
}
|
|
|
|
|
2020-03-12 13:41:30 +00:00
|
|
|
func (s *serviceStore) Options() store.Options {
|
|
|
|
return s.options
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-11 17:57:39 +01:00
|
|
|
if options.Client == nil {
|
|
|
|
options.Client = client.DefaultClient
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:44:42 +01:00
|
|
|
service := &serviceStore{
|
2020-04-08 19:25:57 +01:00
|
|
|
options: options,
|
|
|
|
Database: options.Database,
|
|
|
|
Table: options.Table,
|
|
|
|
Nodes: options.Nodes,
|
2020-05-11 17:57:39 +01:00
|
|
|
Client: pb.NewStoreService("go.micro.store", options.Client),
|
2019-10-11 14:44:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return service
|
|
|
|
}
|