change use of store namespace/prefix in sql store
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/micro/go-micro/client"
|
||||
"github.com/micro/go-micro/config/options"
|
||||
"github.com/micro/go-micro/metadata"
|
||||
"github.com/micro/go-micro/store"
|
||||
pb "github.com/micro/go-micro/store/service/proto"
|
||||
)
|
||||
@@ -15,16 +16,38 @@ import (
|
||||
type serviceStore struct {
|
||||
options.Options
|
||||
|
||||
// Namespace to use
|
||||
Namespace string
|
||||
|
||||
// Addresses of the nodes
|
||||
Nodes []string
|
||||
|
||||
// Prefix to use
|
||||
Prefix string
|
||||
|
||||
// store service client
|
||||
Client pb.StoreService
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// Sync all the known records
|
||||
func (s *serviceStore) List() ([]*store.Record, error) {
|
||||
stream, err := s.Client.List(context.Background(), &pb.ListRequest{}, client.WithAddress(s.Nodes...))
|
||||
stream, err := s.Client.List(s.Context(), &pb.ListRequest{}, client.WithAddress(s.Nodes...))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -54,7 +77,7 @@ func (s *serviceStore) List() ([]*store.Record, error) {
|
||||
|
||||
// Read a record with key
|
||||
func (s *serviceStore) Read(keys ...string) ([]*store.Record, error) {
|
||||
rsp, err := s.Client.Read(context.Background(), &pb.ReadRequest{
|
||||
rsp, err := s.Client.Read(s.Context(), &pb.ReadRequest{
|
||||
Keys: keys,
|
||||
}, client.WithAddress(s.Nodes...))
|
||||
if err != nil {
|
||||
@@ -84,7 +107,7 @@ func (s *serviceStore) Write(recs ...*store.Record) error {
|
||||
})
|
||||
}
|
||||
|
||||
_, err := s.Client.Write(context.Background(), &pb.WriteRequest{
|
||||
_, err := s.Client.Write(s.Context(), &pb.WriteRequest{
|
||||
Records: records,
|
||||
}, client.WithAddress(s.Nodes...))
|
||||
|
||||
@@ -93,7 +116,7 @@ func (s *serviceStore) Write(recs ...*store.Record) error {
|
||||
|
||||
// Delete a record with key
|
||||
func (s *serviceStore) Delete(keys ...string) error {
|
||||
_, err := s.Client.Delete(context.Background(), &pb.DeleteRequest{
|
||||
_, err := s.Client.Delete(s.Context(), &pb.DeleteRequest{
|
||||
Keys: keys,
|
||||
}, client.WithAddress(s.Nodes...))
|
||||
return err
|
||||
@@ -104,16 +127,30 @@ func NewStore(opts ...options.Option) store.Store {
|
||||
options := options.NewOptions(opts...)
|
||||
|
||||
var nodes []string
|
||||
var namespace string
|
||||
var prefix string
|
||||
|
||||
n, ok := options.Values().Get("store.nodes")
|
||||
if ok {
|
||||
nodes = n.([]string)
|
||||
}
|
||||
|
||||
ns, ok := options.Values().Get("store.namespace")
|
||||
if ok {
|
||||
namespace = ns.(string)
|
||||
}
|
||||
|
||||
prx, ok := options.Values().Get("store.prefix")
|
||||
if ok {
|
||||
prefix = prx.(string)
|
||||
}
|
||||
|
||||
service := &serviceStore{
|
||||
Options: options,
|
||||
Nodes: nodes,
|
||||
Client: pb.NewStoreService("go.micro.store", client.DefaultClient),
|
||||
Options: options,
|
||||
Namespace: namespace,
|
||||
Prefix: prefix,
|
||||
Nodes: nodes,
|
||||
Client: pb.NewStoreService("go.micro.store", client.DefaultClient),
|
||||
}
|
||||
|
||||
return service
|
||||
|
Reference in New Issue
Block a user