mirror of
https://github.com/go-micro/go-micro.git
synced 2025-09-16 08:36:30 +02:00
remove service implementations (#2094)
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -1,253 +0,0 @@
|
||||
// Code generated by protoc-gen-micro. DO NOT EDIT.
|
||||
// source: github.com/micro/go-micro/store/service/proto/store.proto
|
||||
|
||||
package go_micro_store
|
||||
|
||||
import (
|
||||
fmt "fmt"
|
||||
proto "github.com/golang/protobuf/proto"
|
||||
math "math"
|
||||
)
|
||||
|
||||
import (
|
||||
context "context"
|
||||
api "github.com/micro/go-micro/v2/api"
|
||||
client "github.com/micro/go-micro/v2/client"
|
||||
server "github.com/micro/go-micro/v2/server"
|
||||
)
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ api.Endpoint
|
||||
var _ context.Context
|
||||
var _ client.Option
|
||||
var _ server.Option
|
||||
|
||||
// Api Endpoints for Store service
|
||||
|
||||
func NewStoreEndpoints() []*api.Endpoint {
|
||||
return []*api.Endpoint{}
|
||||
}
|
||||
|
||||
// Client API for Store service
|
||||
|
||||
type StoreService interface {
|
||||
Read(ctx context.Context, in *ReadRequest, opts ...client.CallOption) (*ReadResponse, error)
|
||||
Write(ctx context.Context, in *WriteRequest, opts ...client.CallOption) (*WriteResponse, error)
|
||||
Delete(ctx context.Context, in *DeleteRequest, opts ...client.CallOption) (*DeleteResponse, error)
|
||||
List(ctx context.Context, in *ListRequest, opts ...client.CallOption) (Store_ListService, error)
|
||||
Databases(ctx context.Context, in *DatabasesRequest, opts ...client.CallOption) (*DatabasesResponse, error)
|
||||
Tables(ctx context.Context, in *TablesRequest, opts ...client.CallOption) (*TablesResponse, error)
|
||||
}
|
||||
|
||||
type storeService struct {
|
||||
c client.Client
|
||||
name string
|
||||
}
|
||||
|
||||
func NewStoreService(name string, c client.Client) StoreService {
|
||||
return &storeService{
|
||||
c: c,
|
||||
name: name,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *storeService) Read(ctx context.Context, in *ReadRequest, opts ...client.CallOption) (*ReadResponse, error) {
|
||||
req := c.c.NewRequest(c.name, "Store.Read", in)
|
||||
out := new(ReadResponse)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *storeService) Write(ctx context.Context, in *WriteRequest, opts ...client.CallOption) (*WriteResponse, error) {
|
||||
req := c.c.NewRequest(c.name, "Store.Write", in)
|
||||
out := new(WriteResponse)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *storeService) Delete(ctx context.Context, in *DeleteRequest, opts ...client.CallOption) (*DeleteResponse, error) {
|
||||
req := c.c.NewRequest(c.name, "Store.Delete", in)
|
||||
out := new(DeleteResponse)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *storeService) List(ctx context.Context, in *ListRequest, opts ...client.CallOption) (Store_ListService, error) {
|
||||
req := c.c.NewRequest(c.name, "Store.List", &ListRequest{})
|
||||
stream, err := c.c.Stream(ctx, req, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := stream.Send(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &storeServiceList{stream}, nil
|
||||
}
|
||||
|
||||
type Store_ListService interface {
|
||||
Context() context.Context
|
||||
SendMsg(interface{}) error
|
||||
RecvMsg(interface{}) error
|
||||
Close() error
|
||||
Recv() (*ListResponse, error)
|
||||
}
|
||||
|
||||
type storeServiceList struct {
|
||||
stream client.Stream
|
||||
}
|
||||
|
||||
func (x *storeServiceList) Close() error {
|
||||
return x.stream.Close()
|
||||
}
|
||||
|
||||
func (x *storeServiceList) Context() context.Context {
|
||||
return x.stream.Context()
|
||||
}
|
||||
|
||||
func (x *storeServiceList) SendMsg(m interface{}) error {
|
||||
return x.stream.Send(m)
|
||||
}
|
||||
|
||||
func (x *storeServiceList) RecvMsg(m interface{}) error {
|
||||
return x.stream.Recv(m)
|
||||
}
|
||||
|
||||
func (x *storeServiceList) Recv() (*ListResponse, error) {
|
||||
m := new(ListResponse)
|
||||
err := x.stream.Recv(m)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (c *storeService) Databases(ctx context.Context, in *DatabasesRequest, opts ...client.CallOption) (*DatabasesResponse, error) {
|
||||
req := c.c.NewRequest(c.name, "Store.Databases", in)
|
||||
out := new(DatabasesResponse)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *storeService) Tables(ctx context.Context, in *TablesRequest, opts ...client.CallOption) (*TablesResponse, error) {
|
||||
req := c.c.NewRequest(c.name, "Store.Tables", in)
|
||||
out := new(TablesResponse)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Server API for Store service
|
||||
|
||||
type StoreHandler interface {
|
||||
Read(context.Context, *ReadRequest, *ReadResponse) error
|
||||
Write(context.Context, *WriteRequest, *WriteResponse) error
|
||||
Delete(context.Context, *DeleteRequest, *DeleteResponse) error
|
||||
List(context.Context, *ListRequest, Store_ListStream) error
|
||||
Databases(context.Context, *DatabasesRequest, *DatabasesResponse) error
|
||||
Tables(context.Context, *TablesRequest, *TablesResponse) error
|
||||
}
|
||||
|
||||
func RegisterStoreHandler(s server.Server, hdlr StoreHandler, opts ...server.HandlerOption) error {
|
||||
type store interface {
|
||||
Read(ctx context.Context, in *ReadRequest, out *ReadResponse) error
|
||||
Write(ctx context.Context, in *WriteRequest, out *WriteResponse) error
|
||||
Delete(ctx context.Context, in *DeleteRequest, out *DeleteResponse) error
|
||||
List(ctx context.Context, stream server.Stream) error
|
||||
Databases(ctx context.Context, in *DatabasesRequest, out *DatabasesResponse) error
|
||||
Tables(ctx context.Context, in *TablesRequest, out *TablesResponse) error
|
||||
}
|
||||
type Store struct {
|
||||
store
|
||||
}
|
||||
h := &storeHandler{hdlr}
|
||||
return s.Handle(s.NewHandler(&Store{h}, opts...))
|
||||
}
|
||||
|
||||
type storeHandler struct {
|
||||
StoreHandler
|
||||
}
|
||||
|
||||
func (h *storeHandler) Read(ctx context.Context, in *ReadRequest, out *ReadResponse) error {
|
||||
return h.StoreHandler.Read(ctx, in, out)
|
||||
}
|
||||
|
||||
func (h *storeHandler) Write(ctx context.Context, in *WriteRequest, out *WriteResponse) error {
|
||||
return h.StoreHandler.Write(ctx, in, out)
|
||||
}
|
||||
|
||||
func (h *storeHandler) Delete(ctx context.Context, in *DeleteRequest, out *DeleteResponse) error {
|
||||
return h.StoreHandler.Delete(ctx, in, out)
|
||||
}
|
||||
|
||||
func (h *storeHandler) List(ctx context.Context, stream server.Stream) error {
|
||||
m := new(ListRequest)
|
||||
if err := stream.Recv(m); err != nil {
|
||||
return err
|
||||
}
|
||||
return h.StoreHandler.List(ctx, m, &storeListStream{stream})
|
||||
}
|
||||
|
||||
type Store_ListStream interface {
|
||||
Context() context.Context
|
||||
SendMsg(interface{}) error
|
||||
RecvMsg(interface{}) error
|
||||
Close() error
|
||||
Send(*ListResponse) error
|
||||
}
|
||||
|
||||
type storeListStream struct {
|
||||
stream server.Stream
|
||||
}
|
||||
|
||||
func (x *storeListStream) Close() error {
|
||||
return x.stream.Close()
|
||||
}
|
||||
|
||||
func (x *storeListStream) Context() context.Context {
|
||||
return x.stream.Context()
|
||||
}
|
||||
|
||||
func (x *storeListStream) SendMsg(m interface{}) error {
|
||||
return x.stream.Send(m)
|
||||
}
|
||||
|
||||
func (x *storeListStream) RecvMsg(m interface{}) error {
|
||||
return x.stream.Recv(m)
|
||||
}
|
||||
|
||||
func (x *storeListStream) Send(m *ListResponse) error {
|
||||
return x.stream.Send(m)
|
||||
}
|
||||
|
||||
func (h *storeHandler) Databases(ctx context.Context, in *DatabasesRequest, out *DatabasesResponse) error {
|
||||
return h.StoreHandler.Databases(ctx, in, out)
|
||||
}
|
||||
|
||||
func (h *storeHandler) Tables(ctx context.Context, in *TablesRequest, out *TablesResponse) error {
|
||||
return h.StoreHandler.Tables(ctx, in, out)
|
||||
}
|
@@ -1,109 +0,0 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package go.micro.store;
|
||||
|
||||
service Store {
|
||||
rpc Read(ReadRequest) returns (ReadResponse) {};
|
||||
rpc Write(WriteRequest) returns (WriteResponse) {};
|
||||
rpc Delete(DeleteRequest) returns (DeleteResponse) {};
|
||||
rpc List(ListRequest) returns (stream ListResponse) {};
|
||||
rpc Databases(DatabasesRequest) returns (DatabasesResponse) {};
|
||||
rpc Tables(TablesRequest) returns (TablesResponse) {};
|
||||
}
|
||||
|
||||
message Field {
|
||||
// type of value e.g string, int, int64, bool, float64
|
||||
string type = 1;
|
||||
// the actual value
|
||||
string value = 2;
|
||||
}
|
||||
|
||||
message Record {
|
||||
// key of the record
|
||||
string key = 1;
|
||||
// value in the record
|
||||
bytes value = 2;
|
||||
// time.Duration (signed int64 nanoseconds)
|
||||
int64 expiry = 3;
|
||||
// the associated metadata
|
||||
map<string,Field> metadata = 4;
|
||||
}
|
||||
|
||||
message ReadOptions {
|
||||
string database = 1;
|
||||
string table = 2;
|
||||
bool prefix = 3;
|
||||
bool suffix = 4;
|
||||
uint64 limit = 5;
|
||||
uint64 offset = 6;
|
||||
}
|
||||
|
||||
message ReadRequest {
|
||||
string key = 1;
|
||||
ReadOptions options = 2;
|
||||
}
|
||||
|
||||
message ReadResponse {
|
||||
repeated Record records = 1;
|
||||
}
|
||||
|
||||
message WriteOptions {
|
||||
string database = 1;
|
||||
string table = 2;
|
||||
// time.Time
|
||||
int64 expiry = 3;
|
||||
// time.Duration
|
||||
int64 ttl = 4;
|
||||
}
|
||||
|
||||
message WriteRequest {
|
||||
Record record = 1;
|
||||
WriteOptions options = 2;
|
||||
}
|
||||
|
||||
message WriteResponse {}
|
||||
|
||||
message DeleteOptions {
|
||||
string database = 1;
|
||||
string table = 2;
|
||||
}
|
||||
|
||||
message DeleteRequest {
|
||||
string key = 1;
|
||||
DeleteOptions options = 2;
|
||||
}
|
||||
|
||||
message DeleteResponse {}
|
||||
|
||||
message ListOptions {
|
||||
string database = 1;
|
||||
string table = 2;
|
||||
string prefix = 3;
|
||||
string suffix = 4;
|
||||
uint64 limit = 5;
|
||||
uint64 offset = 6;
|
||||
}
|
||||
|
||||
|
||||
message ListRequest {
|
||||
ListOptions options = 1;
|
||||
}
|
||||
|
||||
message ListResponse {
|
||||
reserved 1; //repeated Record records = 1;
|
||||
repeated string keys = 2;
|
||||
}
|
||||
|
||||
message DatabasesRequest {}
|
||||
|
||||
message DatabasesResponse {
|
||||
repeated string databases = 1;
|
||||
}
|
||||
|
||||
message TablesRequest {
|
||||
string database = 1;
|
||||
}
|
||||
|
||||
message TablesResponse {
|
||||
repeated string tables = 1;
|
||||
}
|
@@ -1,258 +0,0 @@
|
||||
// Package service implements the store service interface
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
"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"
|
||||
)
|
||||
|
||||
type serviceStore struct {
|
||||
options store.Options
|
||||
|
||||
// The database to use
|
||||
Database string
|
||||
|
||||
// The table to use
|
||||
Table string
|
||||
|
||||
// Addresses of the nodes
|
||||
Nodes []string
|
||||
|
||||
// store service client
|
||||
Client pb.StoreService
|
||||
}
|
||||
|
||||
func (s *serviceStore) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *serviceStore) Init(opts ...store.Option) error {
|
||||
for _, o := range opts {
|
||||
o(&s.options)
|
||||
}
|
||||
s.Database = s.options.Database
|
||||
s.Table = 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.Database) > 0 {
|
||||
md["Micro-Database"] = s.Database
|
||||
}
|
||||
|
||||
if len(s.Table) > 0 {
|
||||
md["Micro-Table"] = s.Table
|
||||
}
|
||||
return metadata.NewContext(ctx, md)
|
||||
}
|
||||
|
||||
// Sync all the known records
|
||||
func (s *serviceStore) List(opts ...store.ListOption) ([]string, error) {
|
||||
options := store.ListOptions{
|
||||
Database: s.Database,
|
||||
Table: s.Table,
|
||||
}
|
||||
|
||||
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...))
|
||||
if err != nil && errors.Equal(err, errors.NotFound("", "")) {
|
||||
return nil, store.ErrNotFound
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer stream.Close()
|
||||
|
||||
var keys []string
|
||||
|
||||
for {
|
||||
rsp, err := stream.Recv()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return keys, err
|
||||
}
|
||||
|
||||
for _, key := range rsp.Keys {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
}
|
||||
|
||||
return keys, nil
|
||||
}
|
||||
|
||||
// Read a record with key
|
||||
func (s *serviceStore) Read(key string, opts ...store.ReadOption) ([]*store.Record, error) {
|
||||
options := store.ReadOptions{
|
||||
Database: s.Database,
|
||||
Table: s.Table,
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
readOpts := &pb.ReadOptions{
|
||||
Database: options.Database,
|
||||
Table: options.Table,
|
||||
Prefix: options.Prefix,
|
||||
Suffix: options.Suffix,
|
||||
Limit: uint64(options.Limit),
|
||||
Offset: uint64(options.Offset),
|
||||
}
|
||||
|
||||
rsp, err := s.Client.Read(s.Context(), &pb.ReadRequest{
|
||||
Key: key,
|
||||
Options: readOpts,
|
||||
}, client.WithAddress(s.Nodes...))
|
||||
if err != nil && errors.Equal(err, errors.NotFound("", "")) {
|
||||
return nil, store.ErrNotFound
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
records := make([]*store.Record, 0, len(rsp.Records))
|
||||
|
||||
for _, val := range rsp.Records {
|
||||
metadata := make(map[string]interface{})
|
||||
|
||||
for k, v := range val.Metadata {
|
||||
switch v.Type {
|
||||
// TODO: parse all types
|
||||
default:
|
||||
metadata[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
records = append(records, &store.Record{
|
||||
Key: val.Key,
|
||||
Value: val.Value,
|
||||
Expiry: time.Duration(val.Expiry) * time.Second,
|
||||
Metadata: metadata,
|
||||
})
|
||||
}
|
||||
|
||||
return records, nil
|
||||
}
|
||||
|
||||
// Write a record
|
||||
func (s *serviceStore) Write(record *store.Record, opts ...store.WriteOption) error {
|
||||
options := store.WriteOptions{
|
||||
Database: s.Database,
|
||||
Table: s.Table,
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
writeOpts := &pb.WriteOptions{
|
||||
Database: options.Database,
|
||||
Table: options.Table,
|
||||
}
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
_, err := s.Client.Write(s.Context(), &pb.WriteRequest{
|
||||
Record: &pb.Record{
|
||||
Key: record.Key,
|
||||
Value: record.Value,
|
||||
Expiry: int64(record.Expiry.Seconds()),
|
||||
Metadata: metadata,
|
||||
},
|
||||
Options: writeOpts}, client.WithAddress(s.Nodes...))
|
||||
if err != nil && errors.Equal(err, errors.NotFound("", "")) {
|
||||
return store.ErrNotFound
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete a record with key
|
||||
func (s *serviceStore) Delete(key string, opts ...store.DeleteOption) error {
|
||||
options := store.DeleteOptions{
|
||||
Database: s.Database,
|
||||
Table: s.Table,
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
deleteOpts := &pb.DeleteOptions{
|
||||
Database: options.Database,
|
||||
Table: options.Table,
|
||||
}
|
||||
|
||||
_, err := s.Client.Delete(s.Context(), &pb.DeleteRequest{
|
||||
Key: key,
|
||||
Options: deleteOpts,
|
||||
}, client.WithAddress(s.Nodes...))
|
||||
if err != nil && errors.Equal(err, errors.NotFound("", "")) {
|
||||
return store.ErrNotFound
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *serviceStore) String() string {
|
||||
return "service"
|
||||
}
|
||||
|
||||
func (s *serviceStore) Options() store.Options {
|
||||
return s.options
|
||||
}
|
||||
|
||||
// NewStore returns a new store service implementation
|
||||
func NewStore(opts ...store.Option) store.Store {
|
||||
var options store.Options
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
if options.Client == nil {
|
||||
options.Client = client.DefaultClient
|
||||
}
|
||||
|
||||
service := &serviceStore{
|
||||
options: options,
|
||||
Database: options.Database,
|
||||
Table: options.Table,
|
||||
Nodes: options.Nodes,
|
||||
Client: pb.NewStoreService("go.micro.store", options.Client),
|
||||
}
|
||||
|
||||
return service
|
||||
}
|
Reference in New Issue
Block a user