From 1d0dca01b00a59f230cd6208876e7e881a0324aa Mon Sep 17 00:00:00 2001 From: sjcsjc123 <1401189096@qq.com> Date: Tue, 4 Jul 2023 22:18:31 +0800 Subject: [PATCH 01/13] feat: init server and client --- cmd/client/client.go | 5 +++++ cmd/server/server.go | 5 +++++ 2 files changed, 10 insertions(+) create mode 100644 cmd/client/client.go create mode 100644 cmd/server/server.go diff --git a/cmd/client/client.go b/cmd/client/client.go new file mode 100644 index 00000000..e9d76f2f --- /dev/null +++ b/cmd/client/client.go @@ -0,0 +1,5 @@ +package client + +func startDbClient() { + +} diff --git a/cmd/server/server.go b/cmd/server/server.go new file mode 100644 index 00000000..63d81bb9 --- /dev/null +++ b/cmd/server/server.go @@ -0,0 +1,5 @@ +package server + +func startDbServer() { + +} From a7cd407544235376291cd35e16bd447bc6a701ab Mon Sep 17 00:00:00 2001 From: sjcsjc123 <1401189096@qq.com> Date: Tue, 4 Jul 2023 22:32:03 +0800 Subject: [PATCH 02/13] feat: add db grpc server start --- cmd/client/client.go | 1 + cmd/server/server.go | 1 + config/options.go | 1 + engine/db.go | 33 +++++++++++++++++++++++++++++++++ lib/const/errors.go | 1 + 5 files changed, 37 insertions(+) diff --git a/cmd/client/client.go b/cmd/client/client.go index e9d76f2f..d8b2e8f8 100644 --- a/cmd/client/client.go +++ b/cmd/client/client.go @@ -1,5 +1,6 @@ package client +// startDbClient starts a db client. func startDbClient() { } diff --git a/cmd/server/server.go b/cmd/server/server.go index 63d81bb9..a2d1e434 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -1,5 +1,6 @@ package server +// startDbServer starts a db server. func startDbServer() { } diff --git a/config/options.go b/config/options.go index 833167b8..042b8c05 100644 --- a/config/options.go +++ b/config/options.go @@ -8,6 +8,7 @@ type Options struct { SyncWrite bool // Whether to persist data on every write IndexType IndexerType FIOType FIOType + Addr string // Addr DB Server Listen } // IteratorOptions is the configuration for index iteration. diff --git a/engine/db.go b/engine/db.go index 2faabafd..443cf6f6 100644 --- a/engine/db.go +++ b/engine/db.go @@ -9,13 +9,19 @@ import ( "github.com/ByteStorage/FlyDB/engine/index" "github.com/ByteStorage/FlyDB/lib/const" "go.uber.org/zap" + "google.golang.org/grpc" + "google.golang.org/grpc/health" + "google.golang.org/grpc/health/grpc_health_v1" "io" + "net" "os" + "os/signal" "path/filepath" "sort" "strconv" "strings" "sync" + "syscall" ) // DB represents a FlyDB database instance, @@ -53,6 +59,7 @@ type DB struct { index index.Indexer // Memory index transSeqNo uint64 // Transaction sequence number, globally increasing isMerging bool // Whether are merging + addr string // The address of the current node } // NewDB open a new db instance @@ -98,6 +105,9 @@ func NewDB(options config.Options) (*DB, error) { return nil, err } + // start grpc server + db.startGrpcServer() + return db, nil } @@ -108,6 +118,9 @@ func checkOptions(options config.Options) error { if options.DataFileSize <= 0 { return _const.ErrOptionDataFileSizeNotPositive } + if options.Addr == "" { + return _const.ErrOptionAddrIsEmpty + } return nil } @@ -542,3 +555,23 @@ func (db *DB) loadIndexFromDataFiles() error { return nil } + +func (db *DB) startGrpcServer() { + listener, err := net.Listen("tcp", db.options.Addr) + if err != nil { + panic(err) + } + server := grpc.NewServer() + grpc_health_v1.RegisterHealthServer(server, health.NewServer()) + go func() { + err := server.Serve(listener) + if err != nil { + panic(err) + } + }() + // graceful shutdown + sig := make(chan os.Signal) + signal.Notify(sig, syscall.SIGINT, syscall.SIGKILL) + + <-sig +} diff --git a/lib/const/errors.go b/lib/const/errors.go index 1a54f7c9..6aa27d4e 100644 --- a/lib/const/errors.go +++ b/lib/const/errors.go @@ -13,4 +13,5 @@ var ( ErrOptionDirPathIsEmpty = errors.New("OptionDirPathError : database dir path is empty") ErrOptionDataFileSizeNotPositive = errors.New("OptionDataFileSizeError : database data file size must be greater than 0") + ErrOptionAddrIsEmpty = errors.New("OptionAddrError : database addr is empty") ) From 0fe4c55658d15b0f5b24533ab911799f7e1eca5d Mon Sep 17 00:00:00 2001 From: sjcsjc123 <1401189096@qq.com> Date: Tue, 4 Jul 2023 22:50:18 +0800 Subject: [PATCH 03/13] feat: add db proto buf --- go.mod | 2 +- go.sum | 2 + lib/proto/db.pb.go | 608 ++++++++++++++++++++++++++++++++++++++++ lib/proto/db.proto | 44 +++ lib/proto/db_grpc.pb.go | 213 ++++++++++++++ 5 files changed, 868 insertions(+), 1 deletion(-) create mode 100644 lib/proto/db.pb.go create mode 100644 lib/proto/db.proto create mode 100644 lib/proto/db_grpc.pb.go diff --git a/go.mod b/go.mod index 04201960..f17b7f28 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( go.etcd.io/bbolt v1.3.7 go.uber.org/zap v1.24.0 google.golang.org/grpc v1.55.0 - google.golang.org/protobuf v1.30.0 + google.golang.org/protobuf v1.31.0 ) require ( diff --git a/go.sum b/go.sum index e7c6033b..004073ba 100644 --- a/go.sum +++ b/go.sum @@ -236,6 +236,8 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0 google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/AlecAivazis/survey.v1 v1.8.5/go.mod h1:iBNOmqKz/NUbZx3bA+4hAGLRC7fSK7tgtVDT4tB22XA= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/lib/proto/db.pb.go b/lib/proto/db.pb.go new file mode 100644 index 00000000..d460f329 --- /dev/null +++ b/lib/proto/db.pb.go @@ -0,0 +1,608 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.31.0 +// protoc v3.12.4 +// source: db.proto + +package proto + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type GetRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` +} + +func (x *GetRequest) Reset() { + *x = GetRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_db_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetRequest) ProtoMessage() {} + +func (x *GetRequest) ProtoReflect() protoreflect.Message { + mi := &file_db_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use GetRequest.ProtoReflect.Descriptor instead. +func (*GetRequest) Descriptor() ([]byte, []int) { + return file_db_proto_rawDescGZIP(), []int{0} +} + +func (x *GetRequest) GetKey() string { + if x != nil { + return x.Key + } + return "" +} + +type GetResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Value string `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"` +} + +func (x *GetResponse) Reset() { + *x = GetResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_db_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *GetResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*GetResponse) ProtoMessage() {} + +func (x *GetResponse) ProtoReflect() protoreflect.Message { + mi := &file_db_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use GetResponse.ProtoReflect.Descriptor instead. +func (*GetResponse) Descriptor() ([]byte, []int) { + return file_db_proto_rawDescGZIP(), []int{1} +} + +func (x *GetResponse) GetValue() string { + if x != nil { + return x.Value + } + return "" +} + +type PutRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` + Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"` +} + +func (x *PutRequest) Reset() { + *x = PutRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_db_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *PutRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*PutRequest) ProtoMessage() {} + +func (x *PutRequest) ProtoReflect() protoreflect.Message { + mi := &file_db_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use PutRequest.ProtoReflect.Descriptor instead. +func (*PutRequest) Descriptor() ([]byte, []int) { + return file_db_proto_rawDescGZIP(), []int{2} +} + +func (x *PutRequest) GetKey() string { + if x != nil { + return x.Key + } + return "" +} + +func (x *PutRequest) GetValue() string { + if x != nil { + return x.Value + } + return "" +} + +type PutResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Ok bool `protobuf:"varint,1,opt,name=ok,proto3" json:"ok,omitempty"` +} + +func (x *PutResponse) Reset() { + *x = PutResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_db_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *PutResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*PutResponse) ProtoMessage() {} + +func (x *PutResponse) ProtoReflect() protoreflect.Message { + mi := &file_db_proto_msgTypes[3] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use PutResponse.ProtoReflect.Descriptor instead. +func (*PutResponse) Descriptor() ([]byte, []int) { + return file_db_proto_rawDescGZIP(), []int{3} +} + +func (x *PutResponse) GetOk() bool { + if x != nil { + return x.Ok + } + return false +} + +type DelRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` +} + +func (x *DelRequest) Reset() { + *x = DelRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_db_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *DelRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*DelRequest) ProtoMessage() {} + +func (x *DelRequest) ProtoReflect() protoreflect.Message { + mi := &file_db_proto_msgTypes[4] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use DelRequest.ProtoReflect.Descriptor instead. +func (*DelRequest) Descriptor() ([]byte, []int) { + return file_db_proto_rawDescGZIP(), []int{4} +} + +func (x *DelRequest) GetKey() string { + if x != nil { + return x.Key + } + return "" +} + +type DelResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Ok bool `protobuf:"varint,1,opt,name=ok,proto3" json:"ok,omitempty"` +} + +func (x *DelResponse) Reset() { + *x = DelResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_db_proto_msgTypes[5] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *DelResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*DelResponse) ProtoMessage() {} + +func (x *DelResponse) ProtoReflect() protoreflect.Message { + mi := &file_db_proto_msgTypes[5] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use DelResponse.ProtoReflect.Descriptor instead. +func (*DelResponse) Descriptor() ([]byte, []int) { + return file_db_proto_rawDescGZIP(), []int{5} +} + +func (x *DelResponse) GetOk() bool { + if x != nil { + return x.Ok + } + return false +} + +type KeysRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Pattern string `protobuf:"bytes,1,opt,name=pattern,proto3" json:"pattern,omitempty"` +} + +func (x *KeysRequest) Reset() { + *x = KeysRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_db_proto_msgTypes[6] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *KeysRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*KeysRequest) ProtoMessage() {} + +func (x *KeysRequest) ProtoReflect() protoreflect.Message { + mi := &file_db_proto_msgTypes[6] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use KeysRequest.ProtoReflect.Descriptor instead. +func (*KeysRequest) Descriptor() ([]byte, []int) { + return file_db_proto_rawDescGZIP(), []int{6} +} + +func (x *KeysRequest) GetPattern() string { + if x != nil { + return x.Pattern + } + return "" +} + +type KeysResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Keys []string `protobuf:"bytes,1,rep,name=keys,proto3" json:"keys,omitempty"` +} + +func (x *KeysResponse) Reset() { + *x = KeysResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_db_proto_msgTypes[7] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *KeysResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*KeysResponse) ProtoMessage() {} + +func (x *KeysResponse) ProtoReflect() protoreflect.Message { + mi := &file_db_proto_msgTypes[7] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use KeysResponse.ProtoReflect.Descriptor instead. +func (*KeysResponse) Descriptor() ([]byte, []int) { + return file_db_proto_rawDescGZIP(), []int{7} +} + +func (x *KeysResponse) GetKeys() []string { + if x != nil { + return x.Keys + } + return nil +} + +var File_db_proto protoreflect.FileDescriptor + +var file_db_proto_rawDesc = []byte{ + 0x0a, 0x08, 0x64, 0x62, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x07, 0x63, 0x6c, 0x75, 0x73, + 0x74, 0x65, 0x72, 0x22, 0x1e, 0x0a, 0x0a, 0x47, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, + 0x6b, 0x65, 0x79, 0x22, 0x23, 0x0a, 0x0b, 0x47, 0x65, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x34, 0x0a, 0x0a, 0x50, 0x75, 0x74, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, + 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x1d, + 0x0a, 0x0b, 0x50, 0x75, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x0e, 0x0a, + 0x02, 0x6f, 0x6b, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x02, 0x6f, 0x6b, 0x22, 0x1e, 0x0a, + 0x0a, 0x44, 0x65, 0x6c, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x6b, + 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x1d, 0x0a, + 0x0b, 0x44, 0x65, 0x6c, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x0e, 0x0a, 0x02, + 0x6f, 0x6b, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x02, 0x6f, 0x6b, 0x22, 0x27, 0x0a, 0x0b, + 0x4b, 0x65, 0x79, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x70, + 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x70, 0x61, + 0x74, 0x74, 0x65, 0x72, 0x6e, 0x22, 0x22, 0x0a, 0x0c, 0x4b, 0x65, 0x79, 0x73, 0x52, 0x65, 0x73, + 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6b, 0x65, 0x79, 0x73, 0x18, 0x01, 0x20, + 0x03, 0x28, 0x09, 0x52, 0x04, 0x6b, 0x65, 0x79, 0x73, 0x32, 0xe1, 0x01, 0x0a, 0x0c, 0x46, 0x6c, + 0x79, 0x44, 0x42, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x32, 0x0a, 0x03, 0x47, 0x65, + 0x74, 0x12, 0x13, 0x2e, 0x63, 0x6c, 0x75, 0x73, 0x74, 0x65, 0x72, 0x2e, 0x47, 0x65, 0x74, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x63, 0x6c, 0x75, 0x73, 0x74, 0x65, 0x72, + 0x2e, 0x47, 0x65, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x32, + 0x0a, 0x03, 0x50, 0x75, 0x74, 0x12, 0x13, 0x2e, 0x63, 0x6c, 0x75, 0x73, 0x74, 0x65, 0x72, 0x2e, + 0x50, 0x75, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x63, 0x6c, 0x75, + 0x73, 0x74, 0x65, 0x72, 0x2e, 0x50, 0x75, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, + 0x22, 0x00, 0x12, 0x32, 0x0a, 0x03, 0x44, 0x65, 0x6c, 0x12, 0x13, 0x2e, 0x63, 0x6c, 0x75, 0x73, + 0x74, 0x65, 0x72, 0x2e, 0x44, 0x65, 0x6c, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, + 0x2e, 0x63, 0x6c, 0x75, 0x73, 0x74, 0x65, 0x72, 0x2e, 0x44, 0x65, 0x6c, 0x52, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x35, 0x0a, 0x04, 0x4b, 0x65, 0x79, 0x73, 0x12, 0x14, + 0x2e, 0x63, 0x6c, 0x75, 0x73, 0x74, 0x65, 0x72, 0x2e, 0x4b, 0x65, 0x79, 0x73, 0x52, 0x65, 0x71, + 0x75, 0x65, 0x73, 0x74, 0x1a, 0x15, 0x2e, 0x63, 0x6c, 0x75, 0x73, 0x74, 0x65, 0x72, 0x2e, 0x4b, + 0x65, 0x79, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x11, 0x5a, + 0x0f, 0x66, 0x6c, 0x79, 0x64, 0x62, 0x2f, 0x6c, 0x69, 0x62, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_db_proto_rawDescOnce sync.Once + file_db_proto_rawDescData = file_db_proto_rawDesc +) + +func file_db_proto_rawDescGZIP() []byte { + file_db_proto_rawDescOnce.Do(func() { + file_db_proto_rawDescData = protoimpl.X.CompressGZIP(file_db_proto_rawDescData) + }) + return file_db_proto_rawDescData +} + +var file_db_proto_msgTypes = make([]protoimpl.MessageInfo, 8) +var file_db_proto_goTypes = []interface{}{ + (*GetRequest)(nil), // 0: cluster.GetRequest + (*GetResponse)(nil), // 1: cluster.GetResponse + (*PutRequest)(nil), // 2: cluster.PutRequest + (*PutResponse)(nil), // 3: cluster.PutResponse + (*DelRequest)(nil), // 4: cluster.DelRequest + (*DelResponse)(nil), // 5: cluster.DelResponse + (*KeysRequest)(nil), // 6: cluster.KeysRequest + (*KeysResponse)(nil), // 7: cluster.KeysResponse +} +var file_db_proto_depIdxs = []int32{ + 0, // 0: cluster.FlyDBService.Get:input_type -> cluster.GetRequest + 2, // 1: cluster.FlyDBService.Put:input_type -> cluster.PutRequest + 4, // 2: cluster.FlyDBService.Del:input_type -> cluster.DelRequest + 6, // 3: cluster.FlyDBService.Keys:input_type -> cluster.KeysRequest + 1, // 4: cluster.FlyDBService.Get:output_type -> cluster.GetResponse + 3, // 5: cluster.FlyDBService.Put:output_type -> cluster.PutResponse + 5, // 6: cluster.FlyDBService.Del:output_type -> cluster.DelResponse + 7, // 7: cluster.FlyDBService.Keys:output_type -> cluster.KeysResponse + 4, // [4:8] is the sub-list for method output_type + 0, // [0:4] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_db_proto_init() } +func file_db_proto_init() { + if File_db_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_db_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_db_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*GetResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_db_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*PutRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_db_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*PutResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_db_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*DelRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_db_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*DelResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_db_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*KeysRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_db_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*KeysResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_db_proto_rawDesc, + NumEnums: 0, + NumMessages: 8, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_db_proto_goTypes, + DependencyIndexes: file_db_proto_depIdxs, + MessageInfos: file_db_proto_msgTypes, + }.Build() + File_db_proto = out.File + file_db_proto_rawDesc = nil + file_db_proto_goTypes = nil + file_db_proto_depIdxs = nil +} diff --git a/lib/proto/db.proto b/lib/proto/db.proto new file mode 100644 index 00000000..3865f075 --- /dev/null +++ b/lib/proto/db.proto @@ -0,0 +1,44 @@ +syntax = "proto3"; + +package cluster; +option go_package = "flydb/lib/proto"; + +service FlyDBService { + rpc Get(GetRequest) returns (GetResponse) {} + rpc Put(PutRequest) returns (PutResponse) {} + rpc Del(DelRequest) returns (DelResponse) {} + rpc Keys(KeysRequest) returns (KeysResponse) {} +} + +message GetRequest { + string key = 1; +} + +message GetResponse { + string value = 1; +} + +message PutRequest { + string key = 1; + string value = 2; +} + +message PutResponse { + bool ok = 1; +} + +message DelRequest { + string key = 1; +} + +message DelResponse { + bool ok = 1; +} + +message KeysRequest { + string pattern = 1; +} + +message KeysResponse { + repeated string keys = 1; +} \ No newline at end of file diff --git a/lib/proto/db_grpc.pb.go b/lib/proto/db_grpc.pb.go new file mode 100644 index 00000000..41f4de2e --- /dev/null +++ b/lib/proto/db_grpc.pb.go @@ -0,0 +1,213 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.2.0 +// - protoc v3.12.4 +// source: db.proto + +package proto + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.32.0 or later. +const _ = grpc.SupportPackageIsVersion7 + +// FlyDBServiceClient is the client API for FlyDBService service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type FlyDBServiceClient interface { + Get(ctx context.Context, in *GetRequest, opts ...grpc.CallOption) (*GetResponse, error) + Put(ctx context.Context, in *PutRequest, opts ...grpc.CallOption) (*PutResponse, error) + Del(ctx context.Context, in *DelRequest, opts ...grpc.CallOption) (*DelResponse, error) + Keys(ctx context.Context, in *KeysRequest, opts ...grpc.CallOption) (*KeysResponse, error) +} + +type flyDBServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewFlyDBServiceClient(cc grpc.ClientConnInterface) FlyDBServiceClient { + return &flyDBServiceClient{cc} +} + +func (c *flyDBServiceClient) Get(ctx context.Context, in *GetRequest, opts ...grpc.CallOption) (*GetResponse, error) { + out := new(GetResponse) + err := c.cc.Invoke(ctx, "/cluster.FlyDBService/Get", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *flyDBServiceClient) Put(ctx context.Context, in *PutRequest, opts ...grpc.CallOption) (*PutResponse, error) { + out := new(PutResponse) + err := c.cc.Invoke(ctx, "/cluster.FlyDBService/Put", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *flyDBServiceClient) Del(ctx context.Context, in *DelRequest, opts ...grpc.CallOption) (*DelResponse, error) { + out := new(DelResponse) + err := c.cc.Invoke(ctx, "/cluster.FlyDBService/Del", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *flyDBServiceClient) Keys(ctx context.Context, in *KeysRequest, opts ...grpc.CallOption) (*KeysResponse, error) { + out := new(KeysResponse) + err := c.cc.Invoke(ctx, "/cluster.FlyDBService/Keys", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// FlyDBServiceServer is the server API for FlyDBService service. +// All implementations must embed UnimplementedFlyDBServiceServer +// for forward compatibility +type FlyDBServiceServer interface { + Get(context.Context, *GetRequest) (*GetResponse, error) + Put(context.Context, *PutRequest) (*PutResponse, error) + Del(context.Context, *DelRequest) (*DelResponse, error) + Keys(context.Context, *KeysRequest) (*KeysResponse, error) + mustEmbedUnimplementedFlyDBServiceServer() +} + +// UnimplementedFlyDBServiceServer must be embedded to have forward compatible implementations. +type UnimplementedFlyDBServiceServer struct { +} + +func (UnimplementedFlyDBServiceServer) Get(context.Context, *GetRequest) (*GetResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method Get not implemented") +} +func (UnimplementedFlyDBServiceServer) Put(context.Context, *PutRequest) (*PutResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method Put not implemented") +} +func (UnimplementedFlyDBServiceServer) Del(context.Context, *DelRequest) (*DelResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method Del not implemented") +} +func (UnimplementedFlyDBServiceServer) Keys(context.Context, *KeysRequest) (*KeysResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method Keys not implemented") +} +func (UnimplementedFlyDBServiceServer) mustEmbedUnimplementedFlyDBServiceServer() {} + +// UnsafeFlyDBServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to FlyDBServiceServer will +// result in compilation errors. +type UnsafeFlyDBServiceServer interface { + mustEmbedUnimplementedFlyDBServiceServer() +} + +func RegisterFlyDBServiceServer(s grpc.ServiceRegistrar, srv FlyDBServiceServer) { + s.RegisterService(&FlyDBService_ServiceDesc, srv) +} + +func _FlyDBService_Get_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(GetRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(FlyDBServiceServer).Get(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/cluster.FlyDBService/Get", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(FlyDBServiceServer).Get(ctx, req.(*GetRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _FlyDBService_Put_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(PutRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(FlyDBServiceServer).Put(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/cluster.FlyDBService/Put", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(FlyDBServiceServer).Put(ctx, req.(*PutRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _FlyDBService_Del_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(DelRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(FlyDBServiceServer).Del(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/cluster.FlyDBService/Del", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(FlyDBServiceServer).Del(ctx, req.(*DelRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _FlyDBService_Keys_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(KeysRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(FlyDBServiceServer).Keys(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/cluster.FlyDBService/Keys", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(FlyDBServiceServer).Keys(ctx, req.(*KeysRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// FlyDBService_ServiceDesc is the grpc.ServiceDesc for FlyDBService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var FlyDBService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "cluster.FlyDBService", + HandlerType: (*FlyDBServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Get", + Handler: _FlyDBService_Get_Handler, + }, + { + MethodName: "Put", + Handler: _FlyDBService_Put_Handler, + }, + { + MethodName: "Del", + Handler: _FlyDBService_Del_Handler, + }, + { + MethodName: "Keys", + Handler: _FlyDBService_Keys_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "db.proto", +} From c68863d1add9365efc69afbe5f2a3a760d5d64d0 Mon Sep 17 00:00:00 2001 From: sjcsjc123 <1401189096@qq.com> Date: Tue, 4 Jul 2023 23:19:16 +0800 Subject: [PATCH 04/13] feat: add grpc method put,get,del,keys(#148) --- engine/grpc/db.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 engine/grpc/db.go diff --git a/engine/grpc/db.go b/engine/grpc/db.go new file mode 100644 index 00000000..e4d8d188 --- /dev/null +++ b/engine/grpc/db.go @@ -0,0 +1,45 @@ +package grpc + +import ( + "context" + "github.com/ByteStorage/FlyDB/engine" + "github.com/ByteStorage/FlyDB/lib/proto" +) + +type Service struct { + proto.FlyDBServiceServer + db *engine.DB +} + +func (s *Service) Put(ctx context.Context, req *proto.PutRequest) (*proto.PutResponse, error) { + err := s.db.Put([]byte(req.Key), []byte(req.Value)) + if err != nil { + return &proto.PutResponse{}, err + } + return &proto.PutResponse{Ok: true}, nil +} + +func (s *Service) Get(ctx context.Context, req *proto.GetRequest) (*proto.GetResponse, error) { + value, err := s.db.Get([]byte(req.Key)) + if err != nil { + return &proto.GetResponse{}, err + } + return &proto.GetResponse{Value: string(value)}, nil +} + +func (s *Service) Del(ctx context.Context, req *proto.DelRequest) (*proto.DelResponse, error) { + err := s.db.Delete([]byte(req.Key)) + if err != nil { + return &proto.DelResponse{}, err + } + return &proto.DelResponse{Ok: true}, nil +} + +func (s *Service) Keys(ctx context.Context, req *proto.KeysRequest) (*proto.KeysResponse, error) { + list := s.db.GetListKeys() + keys := make([]string, len(list)) + for i, bytes := range list { + keys[i] = string(bytes) + } + return &proto.KeysResponse{Keys: keys}, nil +} From ce85b5bb1a65b03faf141ebd5e63128d1b50d089 Mon Sep 17 00:00:00 2001 From: sjcsjc123 <1401189096@qq.com> Date: Tue, 4 Jul 2023 23:28:03 +0800 Subject: [PATCH 05/13] feat: add comment(#149) --- engine/grpc/{ => service}/db.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) rename engine/grpc/{ => service}/db.go (86%) diff --git a/engine/grpc/db.go b/engine/grpc/service/db.go similarity index 86% rename from engine/grpc/db.go rename to engine/grpc/service/db.go index e4d8d188..9cfc3b2a 100644 --- a/engine/grpc/db.go +++ b/engine/grpc/service/db.go @@ -1,4 +1,4 @@ -package grpc +package service import ( "context" @@ -6,11 +6,13 @@ import ( "github.com/ByteStorage/FlyDB/lib/proto" ) +// Service is a grpc service for db type Service struct { proto.FlyDBServiceServer db *engine.DB } +// Put is a grpc service for put func (s *Service) Put(ctx context.Context, req *proto.PutRequest) (*proto.PutResponse, error) { err := s.db.Put([]byte(req.Key), []byte(req.Value)) if err != nil { @@ -19,6 +21,7 @@ func (s *Service) Put(ctx context.Context, req *proto.PutRequest) (*proto.PutRes return &proto.PutResponse{Ok: true}, nil } +// Get is a grpc service for get func (s *Service) Get(ctx context.Context, req *proto.GetRequest) (*proto.GetResponse, error) { value, err := s.db.Get([]byte(req.Key)) if err != nil { @@ -27,6 +30,7 @@ func (s *Service) Get(ctx context.Context, req *proto.GetRequest) (*proto.GetRes return &proto.GetResponse{Value: string(value)}, nil } +// Del is a grpc service for del func (s *Service) Del(ctx context.Context, req *proto.DelRequest) (*proto.DelResponse, error) { err := s.db.Delete([]byte(req.Key)) if err != nil { @@ -35,6 +39,7 @@ func (s *Service) Del(ctx context.Context, req *proto.DelRequest) (*proto.DelRes return &proto.DelResponse{Ok: true}, nil } +// Keys is a grpc service for keys func (s *Service) Keys(ctx context.Context, req *proto.KeysRequest) (*proto.KeysResponse, error) { list := s.db.GetListKeys() keys := make([]string, len(list)) From b75c251d5f37ff1ddf665e5cbb56a6ae617caa18 Mon Sep 17 00:00:00 2001 From: sjcsjc123 <1401189096@qq.com> Date: Tue, 4 Jul 2023 23:37:28 +0800 Subject: [PATCH 06/13] feat: fix bug with test run fail(#149) --- config/options.go | 5 +++++ go.sum | 2 -- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/config/options.go b/config/options.go index 042b8c05..e9090409 100644 --- a/config/options.go +++ b/config/options.go @@ -46,12 +46,17 @@ const ( ART ) +const ( + DefaultAddr = "127.0.0.1:8999" +) + var DefaultOptions = Options{ DirPath: os.TempDir(), DataFileSize: 256 * 1024 * 1024, // 256MB SyncWrite: false, IndexType: ART, FIOType: MmapIOType, + Addr: DefaultAddr, } var DefaultIteratorOptions = IteratorOptions{ diff --git a/go.sum b/go.sum index 004073ba..4466a395 100644 --- a/go.sum +++ b/go.sum @@ -234,8 +234,6 @@ google.golang.org/grpc v1.55.0 h1:3Oj82/tFSCeUrRTg/5E/7d/W5A1tj6Ky1ABAuZuv5ag= google.golang.org/grpc v1.55.0/go.mod h1:iYEXKGkEBhg1PjZQvoYEVPTDkHo1/bjTnfwTeGONTY8= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= -google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/AlecAivazis/survey.v1 v1.8.5/go.mod h1:iBNOmqKz/NUbZx3bA+4hAGLRC7fSK7tgtVDT4tB22XA= From 0fd084e45a2fae85b3cf721e47571709105db4db Mon Sep 17 00:00:00 2001 From: sjcsjc123 <1401189096@qq.com> Date: Tue, 4 Jul 2023 23:41:50 +0800 Subject: [PATCH 07/13] feat: add client to dial db grpc service(#149) --- engine/grpc/client/client.go | 86 ++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 engine/grpc/client/client.go diff --git a/engine/grpc/client/client.go b/engine/grpc/client/client.go new file mode 100644 index 00000000..8e4a3bb4 --- /dev/null +++ b/engine/grpc/client/client.go @@ -0,0 +1,86 @@ +package client + +import ( + "context" + "errors" + "github.com/ByteStorage/FlyDB/lib/proto" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" +) + +// Client is a grpc client +type Client struct { + Addr string // db server address +} + +// newGrpcClient returns a grpc client +func newGrpcClient(addr string) (proto.FlyDBServiceClient, error) { + conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + return nil, err + } + client := proto.NewFlyDBServiceClient(conn) + return client, nil +} + +// Put puts a key-value pair into the db by client api +func (c *Client) Put(key []byte, value []byte) error { + client, err := newGrpcClient(c.Addr) + if err != nil { + return err + } + put, err := client.Put(context.Background(), &proto.PutRequest{Key: string(key), Value: string(value)}) + if err != nil { + return err + } + if !put.Ok { + return errors.New("put failed") + } + return nil +} + +// Get gets a value by key from the db by client api +func (c *Client) Get(key []byte) ([]byte, error) { + client, err := newGrpcClient(c.Addr) + if err != nil { + return nil, err + } + get, err := client.Get(context.Background(), &proto.GetRequest{Key: string(key)}) + if err != nil { + return nil, err + } + return []byte(get.Value), nil +} + +// Del deletes a key-value pair from the db by client api +func (c *Client) Del(key []byte) error { + client, err := newGrpcClient(c.Addr) + if err != nil { + return err + } + del, err := client.Del(context.Background(), &proto.DelRequest{Key: string(key)}) + if err != nil { + return err + } + if !del.Ok { + return errors.New("del failed") + } + return nil +} + +// Keys gets all keys from the db by client api +func (c *Client) Keys() ([][]byte, error) { + client, err := newGrpcClient(c.Addr) + if err != nil { + return nil, err + } + keys, err := client.Keys(context.Background(), &proto.KeysRequest{}) + if err != nil { + return nil, err + } + result := make([][]byte, len(keys.Keys)) + for i, key := range keys.Keys { + result[i] = []byte(key) + } + return result, nil +} From e663e3ace69e8813c8bd2333f95e74bcdc909c0b Mon Sep 17 00:00:00 2001 From: sjcsjc123 <1401189096@qq.com> Date: Tue, 4 Jul 2023 23:52:08 +0800 Subject: [PATCH 08/13] bug: port repeat use when run test(#149) --- engine/db.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/engine/db.go b/engine/db.go index 443cf6f6..34262910 100644 --- a/engine/db.go +++ b/engine/db.go @@ -4,6 +4,7 @@ package engine import ( + "fmt" "github.com/ByteStorage/FlyDB/config" data2 "github.com/ByteStorage/FlyDB/engine/data" "github.com/ByteStorage/FlyDB/engine/index" @@ -566,7 +567,7 @@ func (db *DB) startGrpcServer() { go func() { err := server.Serve(listener) if err != nil { - panic(err) + _ = fmt.Errorf("db server start error: %v", err) } }() // graceful shutdown From dba1e6b5392af556dc50f01bd74f3b66ef4e8e5b Mon Sep 17 00:00:00 2001 From: sjcsjc123 <1401189096@qq.com> Date: Wed, 5 Jul 2023 00:07:50 +0800 Subject: [PATCH 09/13] bug: port repeat use when run test(#149) --- cmd/client/app.go | 39 +++++++++++++++++++++++++++++++++++++++ engine/db.go | 3 ++- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 cmd/client/app.go diff --git a/cmd/client/app.go b/cmd/client/app.go new file mode 100644 index 00000000..7371227b --- /dev/null +++ b/cmd/client/app.go @@ -0,0 +1,39 @@ +package client + +import "github.com/desertbit/grumble" + +func Register(app *grumble.App) { + app.AddCommand(&grumble.Command{ + Name: "put", + Help: "put data", + Run: putData, + Args: func(a *grumble.Args) { + a.String("key", "key", grumble.Default("")) + a.String("value", "value", grumble.Default("")) + }, + }) + + app.AddCommand(&grumble.Command{ + Name: "get", + Help: "get data", + Run: getData, + Args: func(a *grumble.Args) { + a.String("key", "key", grumble.Default("")) + }, + }) + + app.AddCommand(&grumble.Command{ + Name: "delete", + Help: "delete key", + Run: deleteKey, + Args: func(a *grumble.Args) { + a.String("key", "key", grumble.Default("")) + }, + }) + + app.AddCommand(&grumble.Command{ + Name: "keys", + Help: "list keys", + Run: getKeys, + }) +} diff --git a/engine/db.go b/engine/db.go index 34262910..d1fe6105 100644 --- a/engine/db.go +++ b/engine/db.go @@ -560,7 +560,8 @@ func (db *DB) loadIndexFromDataFiles() error { func (db *DB) startGrpcServer() { listener, err := net.Listen("tcp", db.options.Addr) if err != nil { - panic(err) + _ = fmt.Errorf("tcp listen error: %v", err) + return } server := grpc.NewServer() grpc_health_v1.RegisterHealthServer(server, health.NewServer()) From c5d03ce8e6f000ed779a8bc2a6a8f55a95166a30 Mon Sep 17 00:00:00 2001 From: sjcsjc123 <1401189096@qq.com> Date: Wed, 5 Jul 2023 00:34:11 +0800 Subject: [PATCH 10/13] fix: test run fail when grpc server is not closed(#149) --- engine/db.go | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/engine/db.go b/engine/db.go index d1fe6105..1f0904cf 100644 --- a/engine/db.go +++ b/engine/db.go @@ -4,7 +4,6 @@ package engine import ( - "fmt" "github.com/ByteStorage/FlyDB/config" data2 "github.com/ByteStorage/FlyDB/engine/data" "github.com/ByteStorage/FlyDB/engine/index" @@ -16,13 +15,11 @@ import ( "io" "net" "os" - "os/signal" "path/filepath" "sort" "strconv" "strings" "sync" - "syscall" ) // DB represents a FlyDB database instance, @@ -60,7 +57,7 @@ type DB struct { index index.Indexer // Memory index transSeqNo uint64 // Transaction sequence number, globally increasing isMerging bool // Whether are merging - addr string // The address of the current node + server *grpc.Server // gRPC listener } // NewDB open a new db instance @@ -144,6 +141,8 @@ func (db *DB) Close() error { return err } } + // close grpc server + db.server.Stop() return nil } @@ -560,20 +559,17 @@ func (db *DB) loadIndexFromDataFiles() error { func (db *DB) startGrpcServer() { listener, err := net.Listen("tcp", db.options.Addr) if err != nil { - _ = fmt.Errorf("tcp listen error: %v", err) + panic("tcp listen error: " + err.Error()) return } server := grpc.NewServer() grpc_health_v1.RegisterHealthServer(server, health.NewServer()) + db.server = server go func() { err := server.Serve(listener) if err != nil { - _ = fmt.Errorf("db server start error: %v", err) + panic("db server start error: " + err.Error()) } }() - // graceful shutdown - sig := make(chan os.Signal) - signal.Notify(sig, syscall.SIGINT, syscall.SIGKILL) - <-sig } From cc4f2ccbdb99367fcb8aebd1421d279a30ba80d3 Mon Sep 17 00:00:00 2001 From: sjcsjc123 <1401189096@qq.com> Date: Wed, 5 Jul 2023 00:34:41 +0800 Subject: [PATCH 11/13] feat: refactor cli(#149) --- cmd/app.go | 44 -------------------- cmd/cli/flydb.go | 10 ----- cmd/client/app.go | 75 ++++++++++++++++++++-------------- cmd/client/cli/flydb-client.go | 11 +++++ cmd/client/client.go | 6 --- cmd/{ => client}/data.go | 35 +++++++--------- cmd/{ => client}/root.go | 25 +----------- cmd/server.go | 33 --------------- cmd/server/cli/flydb-server.go | 27 ++++++++++++ cmd/server/server.go | 23 ++++++++++- 10 files changed, 120 insertions(+), 169 deletions(-) delete mode 100644 cmd/app.go delete mode 100644 cmd/cli/flydb.go create mode 100644 cmd/client/cli/flydb-client.go delete mode 100644 cmd/client/client.go rename cmd/{ => client}/data.go (73%) rename cmd/{ => client}/root.go (66%) delete mode 100644 cmd/server.go create mode 100644 cmd/server/cli/flydb-server.go diff --git a/cmd/app.go b/cmd/app.go deleted file mode 100644 index 006f3444..00000000 --- a/cmd/app.go +++ /dev/null @@ -1,44 +0,0 @@ -package cmd - -import ( - "fmt" - "github.com/desertbit/grumble" - "github.com/fatih/color" - "os" - "path" - "strings" -) - -// App FlyDB command app -var App = grumble.New(&grumble.Config{ - Name: "FlyDB Cli", - Description: "A command of FlyDB", - HistoryFile: path.Join(os.TempDir(), ".FlyDB_Cli.history"), - HistoryLimit: 10000, - ErrorColor: color.New(color.FgRed, color.Bold, color.Faint), - HelpHeadlineColor: color.New(color.FgGreen), - HelpHeadlineUnderline: false, - HelpSubCommands: true, - Prompt: "flydb $> ", - PromptColor: color.New(color.FgBlue, color.Bold), - Flags: func(f *grumble.Flags) {}, -}) - -func init() { - App.OnInit(func(a *grumble.App, fm grumble.FlagMap) error { - return nil - }) - App.SetPrintASCIILogo(func(a *grumble.App) { - fmt.Println(strings.Join([]string{` - ______ __ ____ ____ - / ____/ / / __ __ / __ \ / __ ) - / / / / / / / / / / / / / /_/ / - / /_ / / / / / / / / / / / __ | - / __/ / / / /_/ / / / / / / / / / - / / / / \__, / / /_/ / / /_/ / - /_/ /_/ ,__/ / /_____/ /_____/ - /____/ -`}, "\r\n")) - }) - register(App) -} diff --git a/cmd/cli/flydb.go b/cmd/cli/flydb.go deleted file mode 100644 index 0e1552a4..00000000 --- a/cmd/cli/flydb.go +++ /dev/null @@ -1,10 +0,0 @@ -package main - -import ( - "github.com/ByteStorage/FlyDB/cmd" - "github.com/desertbit/grumble" -) - -func main() { - grumble.Main(cmd.App) -} diff --git a/cmd/client/app.go b/cmd/client/app.go index 7371227b..8cbaeb9b 100644 --- a/cmd/client/app.go +++ b/cmd/client/app.go @@ -1,39 +1,52 @@ package client -import "github.com/desertbit/grumble" +import ( + "errors" + "fmt" + "github.com/desertbit/grumble" + "github.com/fatih/color" + "os" + "path" + "strings" +) -func Register(app *grumble.App) { - app.AddCommand(&grumble.Command{ - Name: "put", - Help: "put data", - Run: putData, - Args: func(a *grumble.Args) { - a.String("key", "key", grumble.Default("")) - a.String("value", "value", grumble.Default("")) - }, - }) +var addr string - app.AddCommand(&grumble.Command{ - Name: "get", - Help: "get data", - Run: getData, - Args: func(a *grumble.Args) { - a.String("key", "key", grumble.Default("")) - }, - }) +// App FlyDB command app +var App = grumble.New(&grumble.Config{ + Name: "FlyDB Cli", + Description: "A command of FlyDB", + HistoryFile: path.Join(os.TempDir(), ".FlyDB_Cli.history"), + HistoryLimit: 10000, + ErrorColor: color.New(color.FgRed, color.Bold, color.Faint), + HelpHeadlineColor: color.New(color.FgGreen), + HelpHeadlineUnderline: false, + HelpSubCommands: true, + Prompt: "flydb $> ", + PromptColor: color.New(color.FgBlue, color.Bold), + Flags: func(f *grumble.Flags) {}, +}) - app.AddCommand(&grumble.Command{ - Name: "delete", - Help: "delete key", - Run: deleteKey, - Args: func(a *grumble.Args) { - a.String("key", "key", grumble.Default("")) - }, +func init() { + App.OnInit(func(a *grumble.App, fm grumble.FlagMap) error { + if len(os.Args) != 1 { + fmt.Println("usage: flydb-cli [addr]") + return errors.New("usage: flydb-cli [addr]") + } + addr = os.Args[1] + return nil }) - - app.AddCommand(&grumble.Command{ - Name: "keys", - Help: "list keys", - Run: getKeys, + App.SetPrintASCIILogo(func(a *grumble.App) { + fmt.Println(strings.Join([]string{` + ______ __ ____ ____ + / ____/ / / __ __ / __ \ / __ ) + / / / / / / / / / / / / / /_/ / + / /_ / / / / / / / / / / / __ | + / __/ / / / /_/ / / / / / / / / / + / / / / \__, / / /_/ / / /_/ / + /_/ /_/ ,__/ / /_____/ /_____/ + /____/ +`}, "\r\n")) }) + register(App) } diff --git a/cmd/client/cli/flydb-client.go b/cmd/client/cli/flydb-client.go new file mode 100644 index 00000000..3b604b12 --- /dev/null +++ b/cmd/client/cli/flydb-client.go @@ -0,0 +1,11 @@ +package main + +import ( + "github.com/ByteStorage/FlyDB/cmd/client" + "github.com/desertbit/grumble" +) + +func main() { + // start client CLI + grumble.Main(client.App) +} diff --git a/cmd/client/client.go b/cmd/client/client.go deleted file mode 100644 index d8b2e8f8..00000000 --- a/cmd/client/client.go +++ /dev/null @@ -1,6 +0,0 @@ -package client - -// startDbClient starts a db client. -func startDbClient() { - -} diff --git a/cmd/data.go b/cmd/client/data.go similarity index 73% rename from cmd/data.go rename to cmd/client/data.go index 009b5751..b748d2b7 100644 --- a/cmd/data.go +++ b/cmd/client/data.go @@ -1,10 +1,17 @@ -package cmd +package client import ( "fmt" + "github.com/ByteStorage/FlyDB/engine/grpc/client" "github.com/desertbit/grumble" ) +func newClient() *client.Client { + return &client.Client{ + Addr: addr, + } +} + func putData(c *grumble.Context) error { key := c.Args.String("key") value := c.Args.String("value") @@ -12,11 +19,7 @@ func putData(c *grumble.Context) error { fmt.Println("key or value is empty") return nil } - if db == nil { - fmt.Println("start server first") - return nil - } - err := db.Put([]byte(key), []byte(value)) + err := newClient().Put([]byte(key), []byte(value)) if err != nil { fmt.Println("put data error: ", err) return err @@ -31,11 +34,7 @@ func getData(c *grumble.Context) error { fmt.Println("key is empty") return nil } - if db == nil { - fmt.Println("start server first") - return nil - } - value, err := db.Get([]byte(key)) + value, err := newClient().Get([]byte(key)) if err != nil { fmt.Println("get data error: ", err) return err @@ -50,11 +49,7 @@ func deleteKey(c *grumble.Context) error { fmt.Println("key is empty") return nil } - if db == nil { - fmt.Println("start server first") - return nil - } - err := db.Delete([]byte(key)) + err := newClient().Del([]byte(key)) if err != nil { fmt.Println("delete key error: ", err) return err @@ -64,11 +59,11 @@ func deleteKey(c *grumble.Context) error { } func getKeys(c *grumble.Context) error { - if db == nil { - fmt.Println("start server first") - return nil + list, err := newClient().Keys() + if err != nil { + fmt.Println("get keys error: ", err) + return err } - list := db.GetListKeys() fmt.Println("Total keys: ", len(list)) for i, bytes := range list { fmt.Printf(string(bytes[:]) + "\t") diff --git a/cmd/root.go b/cmd/client/root.go similarity index 66% rename from cmd/root.go rename to cmd/client/root.go index 5fa366f0..d3e44d8c 100644 --- a/cmd/root.go +++ b/cmd/client/root.go @@ -1,28 +1,8 @@ -package cmd +package client -import ( - "github.com/desertbit/grumble" -) +import "github.com/desertbit/grumble" func register(app *grumble.App) { - app.AddCommand(&grumble.Command{ - Name: "start", - Help: "start server", - Run: startServer, - }) - - app.AddCommand(&grumble.Command{ - Name: "stop", - Help: "stop server", - Run: stopServer, - }) - - app.AddCommand(&grumble.Command{ - Name: "clean", - Help: "clean server", - Run: cleanServer, - }) - app.AddCommand(&grumble.Command{ Name: "put", Help: "put data", @@ -56,5 +36,4 @@ func register(app *grumble.App) { Help: "list keys", Run: getKeys, }) - } diff --git a/cmd/server.go b/cmd/server.go deleted file mode 100644 index e740c91e..00000000 --- a/cmd/server.go +++ /dev/null @@ -1,33 +0,0 @@ -package cmd - -import ( - "fmt" - "github.com/ByteStorage/FlyDB/config" - "github.com/ByteStorage/FlyDB/engine" - "github.com/ByteStorage/FlyDB/flydb" - "github.com/desertbit/grumble" -) - -var db *engine.DB - -func startServer(c *grumble.Context) error { - if len(c.Args) == 0 { - options := config.DefaultOptions - flyDb, err := flydb.NewFlyDB(options) - if err != nil { - fmt.Println("flydb start error: ", err) - return err - } - db = flyDb - fmt.Println("flydb start success") - } - return nil -} - -func stopServer(c *grumble.Context) error { - panic("implement me") -} - -func cleanServer(c *grumble.Context) error { - panic("implement me") -} diff --git a/cmd/server/cli/flydb-server.go b/cmd/server/cli/flydb-server.go new file mode 100644 index 00000000..ecb9adcb --- /dev/null +++ b/cmd/server/cli/flydb-server.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + "github.com/ByteStorage/FlyDB/cmd/server" + "os" +) + +func main() { + args := os.Args + if len(args) >= 2 { + fmt.Println("Usage: flydb-server [start|clean|stop]") + return + } + if len(args) == 1 { + //start server + server.StartServer() + } + switch args[1] { + case "start": + server.StartServer() + case "stop": + server.StopServer() + case "clean": + server.CleanServer() + } +} diff --git a/cmd/server/server.go b/cmd/server/server.go index a2d1e434..ca4ed328 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -1,6 +1,25 @@ package server -// startDbServer starts a db server. -func startDbServer() { +import ( + "fmt" + "github.com/ByteStorage/FlyDB/config" + "github.com/ByteStorage/FlyDB/flydb" +) +func StartServer() { + options := config.DefaultOptions + _, err := flydb.NewFlyDB(options) + if err != nil { + fmt.Println("flydb start error: ", err) + return + } + fmt.Println("flydb start success") +} + +func StopServer() { + panic("implement me") +} + +func CleanServer() { + panic("implement me") } From e575b1fdc5cad4ac5a4464b650245961290e7c6d Mon Sep 17 00:00:00 2001 From: sjcsjc123 <1401189096@qq.com> Date: Thu, 6 Jul 2023 14:47:25 +0800 Subject: [PATCH 12/13] feat:refactor cli importance(#149) --- cmd/client/app.go | 8 -- cmd/client/cli/flydb-client.go | 8 ++ cmd/client/data.go | 4 +- cmd/server/cli/flydb-server.go | 1 + cmd/server/server.go | 6 +- config/options.go | 4 - engine/db.go | 59 +------- engine/grpc/client/client.go | 14 +- engine/grpc/client/client_test.go | 33 +++++ engine/grpc/service/db.go | 110 ++++++++++++--- lib/proto/{ => dbs}/db.pb.go | 225 +++++++++++++++--------------- lib/proto/{ => dbs}/db.proto | 4 +- lib/proto/{ => dbs}/db_grpc.pb.go | 24 ++-- lib/proto/master_grpc.pb.go | 8 +- lib/proto/slave_grpc.pb.go | 8 +- 15 files changed, 285 insertions(+), 231 deletions(-) create mode 100644 engine/grpc/client/client_test.go rename lib/proto/{ => dbs}/db.pb.go (59%) rename lib/proto/{ => dbs}/db.proto (91%) rename lib/proto/{ => dbs}/db_grpc.pb.go (92%) diff --git a/cmd/client/app.go b/cmd/client/app.go index 8cbaeb9b..ccf7b99e 100644 --- a/cmd/client/app.go +++ b/cmd/client/app.go @@ -1,7 +1,6 @@ package client import ( - "errors" "fmt" "github.com/desertbit/grumble" "github.com/fatih/color" @@ -10,8 +9,6 @@ import ( "strings" ) -var addr string - // App FlyDB command app var App = grumble.New(&grumble.Config{ Name: "FlyDB Cli", @@ -29,11 +26,6 @@ var App = grumble.New(&grumble.Config{ func init() { App.OnInit(func(a *grumble.App, fm grumble.FlagMap) error { - if len(os.Args) != 1 { - fmt.Println("usage: flydb-cli [addr]") - return errors.New("usage: flydb-cli [addr]") - } - addr = os.Args[1] return nil }) App.SetPrintASCIILogo(func(a *grumble.App) { diff --git a/cmd/client/cli/flydb-client.go b/cmd/client/cli/flydb-client.go index 3b604b12..0bef82cd 100644 --- a/cmd/client/cli/flydb-client.go +++ b/cmd/client/cli/flydb-client.go @@ -1,11 +1,19 @@ package main import ( + "fmt" "github.com/ByteStorage/FlyDB/cmd/client" "github.com/desertbit/grumble" + "os" ) func main() { + if len(os.Args) != 2 { + fmt.Println("usage: flydb-cli [addr]") + return + } + client.Addr = os.Args[1] + os.Args = os.Args[:1] // start client CLI grumble.Main(client.App) } diff --git a/cmd/client/data.go b/cmd/client/data.go index b748d2b7..d1840b99 100644 --- a/cmd/client/data.go +++ b/cmd/client/data.go @@ -6,9 +6,11 @@ import ( "github.com/desertbit/grumble" ) +var Addr string + func newClient() *client.Client { return &client.Client{ - Addr: addr, + Addr: Addr, } } diff --git a/cmd/server/cli/flydb-server.go b/cmd/server/cli/flydb-server.go index ecb9adcb..3ce41b22 100644 --- a/cmd/server/cli/flydb-server.go +++ b/cmd/server/cli/flydb-server.go @@ -15,6 +15,7 @@ func main() { if len(args) == 1 { //start server server.StartServer() + return } switch args[1] { case "start": diff --git a/cmd/server/server.go b/cmd/server/server.go index ca4ed328..4193cbae 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -3,17 +3,19 @@ package server import ( "fmt" "github.com/ByteStorage/FlyDB/config" + "github.com/ByteStorage/FlyDB/engine/grpc/service" "github.com/ByteStorage/FlyDB/flydb" ) func StartServer() { options := config.DefaultOptions - _, err := flydb.NewFlyDB(options) + db, err := flydb.NewFlyDB(options) if err != nil { fmt.Println("flydb start error: ", err) return } - fmt.Println("flydb start success") + s := service.NewService(config.DefaultAddr, db) + s.StartServer() } func StopServer() { diff --git a/config/options.go b/config/options.go index 005b8399..4ff80e2e 100644 --- a/config/options.go +++ b/config/options.go @@ -8,8 +8,6 @@ type Options struct { SyncWrite bool // Whether to persist data on every write IndexType IndexerType FIOType FIOType - Addr string // Addr DB Server Listen - IsCli bool // Is Cli } // IteratorOptions is the configuration for index iteration. @@ -57,8 +55,6 @@ var DefaultOptions = Options{ SyncWrite: false, IndexType: ART, FIOType: MmapIOType, - Addr: DefaultAddr, - IsCli: false, } var DefaultIteratorOptions = IteratorOptions{ diff --git a/engine/db.go b/engine/db.go index b46f7f9c..32441fce 100644 --- a/engine/db.go +++ b/engine/db.go @@ -8,21 +8,15 @@ import ( data2 "github.com/ByteStorage/FlyDB/engine/data" "github.com/ByteStorage/FlyDB/engine/index" "github.com/ByteStorage/FlyDB/lib/const" + s "github.com/ByteStorage/FlyDB/lib/proto/dbs" "go.uber.org/zap" - "google.golang.org/grpc" - "google.golang.org/grpc/credentials/insecure" - "google.golang.org/grpc/health" - "google.golang.org/grpc/health/grpc_health_v1" "io" - "net" "os" - "os/signal" "path/filepath" "sort" "strconv" "strings" "sync" - "syscall" ) // DB represents a FlyDB database instance, @@ -52,6 +46,8 @@ import ( // FlyDB provides a powerful and efficient storage solution for applications // that prioritize speed and responsiveness. type DB struct { + // gRPC dbs + s.FlyDBServiceServer options config.Options lock *sync.RWMutex fileIds []int // File id, which can only be used when the index is loaded @@ -60,7 +56,6 @@ type DB struct { index index.Indexer // Memory index transSeqNo uint64 // Transaction sequence number, globally increasing isMerging bool // Whether are merging - server *grpc.Server // gRPC listener } // NewDB open a new db instance @@ -115,9 +110,6 @@ func checkOptions(options config.Options) error { if options.DataFileSize <= 0 { return _const.ErrOptionDataFileSizeNotPositive } - if options.Addr == "" { - return _const.ErrOptionAddrIsEmpty - } return nil } @@ -355,6 +347,7 @@ func (db *DB) getValueByPosition(logRecordPst *data2.LogRecordPst) ([]byte, erro return logRecord.Value, nil } +// Delete data according to the key func (db *DB) Delete(key []byte) error { zap.L().Info("delete", zap.ByteString("key", key)) @@ -553,53 +546,9 @@ func (db *DB) loadIndexFromDataFiles() error { return nil } -// StartGrpcServer starts the grpc server -func (db *DB) StartGrpcServer() { - listener, err := net.Listen("tcp", db.options.Addr) - if err != nil { - panic("tcp listen error: " + err.Error()) - return - } - server := grpc.NewServer() - grpc_health_v1.RegisterHealthServer(server, health.NewServer()) - db.server = server - go func() { - err := server.Serve(listener) - if err != nil { - panic("db server start error: " + err.Error()) - } - }() - //wait for server start - for { - conn, err := grpc.Dial(db.options.Addr, grpc.WithTransportCredentials(insecure.NewCredentials())) - if err != nil { - continue - } - err = conn.Close() - if err != nil { - continue - } - break - } - if db.options.IsCli { - // graceful shutdown - sig := make(chan os.Signal) - signal.Notify(sig, syscall.SIGINT, syscall.SIGKILL) - - <-sig - } -} - -func (db *DB) StopGrpcServer() { - if db.server != nil { - db.server.Stop() - } -} - // Clean the DB data directory after the test is complete func (db *DB) Clean() { if db != nil { - db.StopGrpcServer() _ = db.Close() err := os.RemoveAll(db.options.DirPath) if err != nil { diff --git a/engine/grpc/client/client.go b/engine/grpc/client/client.go index 8e4a3bb4..2f6103a8 100644 --- a/engine/grpc/client/client.go +++ b/engine/grpc/client/client.go @@ -3,7 +3,7 @@ package client import ( "context" "errors" - "github.com/ByteStorage/FlyDB/lib/proto" + "github.com/ByteStorage/FlyDB/lib/proto/dbs" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" ) @@ -14,12 +14,12 @@ type Client struct { } // newGrpcClient returns a grpc client -func newGrpcClient(addr string) (proto.FlyDBServiceClient, error) { +func newGrpcClient(addr string) (dbs.FlyDBServiceClient, error) { conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { return nil, err } - client := proto.NewFlyDBServiceClient(conn) + client := dbs.NewFlyDBServiceClient(conn) return client, nil } @@ -29,7 +29,7 @@ func (c *Client) Put(key []byte, value []byte) error { if err != nil { return err } - put, err := client.Put(context.Background(), &proto.PutRequest{Key: string(key), Value: string(value)}) + put, err := client.Put(context.Background(), &dbs.PutRequest{Key: string(key), Value: string(value)}) if err != nil { return err } @@ -45,7 +45,7 @@ func (c *Client) Get(key []byte) ([]byte, error) { if err != nil { return nil, err } - get, err := client.Get(context.Background(), &proto.GetRequest{Key: string(key)}) + get, err := client.Get(context.Background(), &dbs.GetRequest{Key: string(key)}) if err != nil { return nil, err } @@ -58,7 +58,7 @@ func (c *Client) Del(key []byte) error { if err != nil { return err } - del, err := client.Del(context.Background(), &proto.DelRequest{Key: string(key)}) + del, err := client.Del(context.Background(), &dbs.DelRequest{Key: string(key)}) if err != nil { return err } @@ -74,7 +74,7 @@ func (c *Client) Keys() ([][]byte, error) { if err != nil { return nil, err } - keys, err := client.Keys(context.Background(), &proto.KeysRequest{}) + keys, err := client.Keys(context.Background(), &dbs.KeysRequest{}) if err != nil { return nil, err } diff --git a/engine/grpc/client/client_test.go b/engine/grpc/client/client_test.go new file mode 100644 index 00000000..19cd2a40 --- /dev/null +++ b/engine/grpc/client/client_test.go @@ -0,0 +1,33 @@ +package client + +import ( + "github.com/ByteStorage/FlyDB/config" + "github.com/ByteStorage/FlyDB/engine" + "github.com/ByteStorage/FlyDB/engine/grpc/service" + "github.com/stretchr/testify/assert" + "os" + "testing" +) + +func TestClient_Put(t *testing.T) { + opts := config.DefaultOptions + dir, _ := os.MkdirTemp("", "flydb-put") + opts.DirPath = dir + opts.DataFileSize = 64 * 1024 * 1024 + db, err := engine.NewDB(opts) + defer db.Clean() + assert.Nil(t, err) + s := service.NewService(config.DefaultAddr, db) + go s.StartServer() + //wait for server start + for { + if s.IsGrpcServerRunning() { + break + } + } + client := &Client{ + Addr: config.DefaultAddr, + } + err = client.Put([]byte("test"), []byte("test")) + assert.Nil(t, err) +} diff --git a/engine/grpc/service/db.go b/engine/grpc/service/db.go index 9cfc3b2a..86660a40 100644 --- a/engine/grpc/service/db.go +++ b/engine/grpc/service/db.go @@ -2,49 +2,121 @@ package service import ( "context" + "fmt" "github.com/ByteStorage/FlyDB/engine" - "github.com/ByteStorage/FlyDB/lib/proto" + "github.com/ByteStorage/FlyDB/lib/proto/dbs" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" + "google.golang.org/grpc/health" + "google.golang.org/grpc/health/grpc_health_v1" + "net" + "os" + "os/signal" + "syscall" ) -// Service is a grpc service for db +// Service is a grpc Service for db type Service struct { - proto.FlyDBServiceServer - db *engine.DB + dbs.FlyDBServiceServer + Addr string // db server address + db *engine.DB + sig chan os.Signal } -// Put is a grpc service for put -func (s *Service) Put(ctx context.Context, req *proto.PutRequest) (*proto.PutResponse, error) { +// NewService returns a new grpc Service +func NewService(addr string, db *engine.DB) *Service { + return &Service{ + Addr: addr, + db: db, + sig: make(chan os.Signal), + } +} + +// IsGrpcServerRunning returns whether the grpc server is running +func (s *Service) IsGrpcServerRunning() bool { + conn, err := grpc.Dial(s.Addr, grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + return false + } + err = conn.Close() + if err != nil { + return false + } + return true +} + +// StartServer starts a grpc server +func (s *Service) StartServer() { + listener, err := net.Listen("tcp", s.Addr) + if err != nil { + panic("tcp listen error: " + err.Error()) + return + } + server := grpc.NewServer() + dbs.RegisterFlyDBServiceServer(server, s) + grpc_health_v1.RegisterHealthServer(server, health.NewServer()) + go func() { + err := server.Serve(listener) + if err != nil { + panic("db server start error: " + err.Error()) + } + }() + //wait for server start + for { + conn, err := grpc.Dial(s.Addr, grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + continue + } + err = conn.Close() + if err != nil { + continue + } + break + } + fmt.Println("flydb start success on ", s.Addr) + // graceful shutdown + signal.Notify(s.sig, syscall.SIGINT, syscall.SIGKILL) + + <-s.sig +} + +func (s *Service) StopServer() { + s.sig <- syscall.SIGINT +} + +// Put is a grpc s for put +func (s *Service) Put(ctx context.Context, req *dbs.PutRequest) (*dbs.PutResponse, error) { err := s.db.Put([]byte(req.Key), []byte(req.Value)) if err != nil { - return &proto.PutResponse{}, err + return &dbs.PutResponse{}, err } - return &proto.PutResponse{Ok: true}, nil + return &dbs.PutResponse{Ok: true}, nil } -// Get is a grpc service for get -func (s *Service) Get(ctx context.Context, req *proto.GetRequest) (*proto.GetResponse, error) { +// Get is a grpc s for get +func (s *Service) Get(ctx context.Context, req *dbs.GetRequest) (*dbs.GetResponse, error) { value, err := s.db.Get([]byte(req.Key)) if err != nil { - return &proto.GetResponse{}, err + return &dbs.GetResponse{}, err } - return &proto.GetResponse{Value: string(value)}, nil + return &dbs.GetResponse{Value: string(value)}, nil } -// Del is a grpc service for del -func (s *Service) Del(ctx context.Context, req *proto.DelRequest) (*proto.DelResponse, error) { +// Del is a grpc s for del +func (s *Service) Del(ctx context.Context, req *dbs.DelRequest) (*dbs.DelResponse, error) { err := s.db.Delete([]byte(req.Key)) if err != nil { - return &proto.DelResponse{}, err + return &dbs.DelResponse{}, err } - return &proto.DelResponse{Ok: true}, nil + return &dbs.DelResponse{Ok: true}, nil } -// Keys is a grpc service for keys -func (s *Service) Keys(ctx context.Context, req *proto.KeysRequest) (*proto.KeysResponse, error) { +// Keys is a grpc s for keys +func (s *Service) Keys(ctx context.Context, req *dbs.KeysRequest) (*dbs.KeysResponse, error) { list := s.db.GetListKeys() keys := make([]string, len(list)) for i, bytes := range list { keys[i] = string(bytes) } - return &proto.KeysResponse{Keys: keys}, nil + return &dbs.KeysResponse{Keys: keys}, nil } diff --git a/lib/proto/db.pb.go b/lib/proto/dbs/db.pb.go similarity index 59% rename from lib/proto/db.pb.go rename to lib/proto/dbs/db.pb.go index d460f329..5d745807 100644 --- a/lib/proto/db.pb.go +++ b/lib/proto/dbs/db.pb.go @@ -1,10 +1,10 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.31.0 +// protoc-gen-go v1.28.1 // protoc v3.12.4 -// source: db.proto +// source: lib/proto/dbs/db.proto -package proto +package dbs import ( protoreflect "google.golang.org/protobuf/reflect/protoreflect" @@ -31,7 +31,7 @@ type GetRequest struct { func (x *GetRequest) Reset() { *x = GetRequest{} if protoimpl.UnsafeEnabled { - mi := &file_db_proto_msgTypes[0] + mi := &file_lib_proto_dbs_db_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -44,7 +44,7 @@ func (x *GetRequest) String() string { func (*GetRequest) ProtoMessage() {} func (x *GetRequest) ProtoReflect() protoreflect.Message { - mi := &file_db_proto_msgTypes[0] + mi := &file_lib_proto_dbs_db_proto_msgTypes[0] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -57,7 +57,7 @@ func (x *GetRequest) ProtoReflect() protoreflect.Message { // Deprecated: Use GetRequest.ProtoReflect.Descriptor instead. func (*GetRequest) Descriptor() ([]byte, []int) { - return file_db_proto_rawDescGZIP(), []int{0} + return file_lib_proto_dbs_db_proto_rawDescGZIP(), []int{0} } func (x *GetRequest) GetKey() string { @@ -78,7 +78,7 @@ type GetResponse struct { func (x *GetResponse) Reset() { *x = GetResponse{} if protoimpl.UnsafeEnabled { - mi := &file_db_proto_msgTypes[1] + mi := &file_lib_proto_dbs_db_proto_msgTypes[1] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -91,7 +91,7 @@ func (x *GetResponse) String() string { func (*GetResponse) ProtoMessage() {} func (x *GetResponse) ProtoReflect() protoreflect.Message { - mi := &file_db_proto_msgTypes[1] + mi := &file_lib_proto_dbs_db_proto_msgTypes[1] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -104,7 +104,7 @@ func (x *GetResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use GetResponse.ProtoReflect.Descriptor instead. func (*GetResponse) Descriptor() ([]byte, []int) { - return file_db_proto_rawDescGZIP(), []int{1} + return file_lib_proto_dbs_db_proto_rawDescGZIP(), []int{1} } func (x *GetResponse) GetValue() string { @@ -126,7 +126,7 @@ type PutRequest struct { func (x *PutRequest) Reset() { *x = PutRequest{} if protoimpl.UnsafeEnabled { - mi := &file_db_proto_msgTypes[2] + mi := &file_lib_proto_dbs_db_proto_msgTypes[2] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -139,7 +139,7 @@ func (x *PutRequest) String() string { func (*PutRequest) ProtoMessage() {} func (x *PutRequest) ProtoReflect() protoreflect.Message { - mi := &file_db_proto_msgTypes[2] + mi := &file_lib_proto_dbs_db_proto_msgTypes[2] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -152,7 +152,7 @@ func (x *PutRequest) ProtoReflect() protoreflect.Message { // Deprecated: Use PutRequest.ProtoReflect.Descriptor instead. func (*PutRequest) Descriptor() ([]byte, []int) { - return file_db_proto_rawDescGZIP(), []int{2} + return file_lib_proto_dbs_db_proto_rawDescGZIP(), []int{2} } func (x *PutRequest) GetKey() string { @@ -180,7 +180,7 @@ type PutResponse struct { func (x *PutResponse) Reset() { *x = PutResponse{} if protoimpl.UnsafeEnabled { - mi := &file_db_proto_msgTypes[3] + mi := &file_lib_proto_dbs_db_proto_msgTypes[3] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -193,7 +193,7 @@ func (x *PutResponse) String() string { func (*PutResponse) ProtoMessage() {} func (x *PutResponse) ProtoReflect() protoreflect.Message { - mi := &file_db_proto_msgTypes[3] + mi := &file_lib_proto_dbs_db_proto_msgTypes[3] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -206,7 +206,7 @@ func (x *PutResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use PutResponse.ProtoReflect.Descriptor instead. func (*PutResponse) Descriptor() ([]byte, []int) { - return file_db_proto_rawDescGZIP(), []int{3} + return file_lib_proto_dbs_db_proto_rawDescGZIP(), []int{3} } func (x *PutResponse) GetOk() bool { @@ -227,7 +227,7 @@ type DelRequest struct { func (x *DelRequest) Reset() { *x = DelRequest{} if protoimpl.UnsafeEnabled { - mi := &file_db_proto_msgTypes[4] + mi := &file_lib_proto_dbs_db_proto_msgTypes[4] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -240,7 +240,7 @@ func (x *DelRequest) String() string { func (*DelRequest) ProtoMessage() {} func (x *DelRequest) ProtoReflect() protoreflect.Message { - mi := &file_db_proto_msgTypes[4] + mi := &file_lib_proto_dbs_db_proto_msgTypes[4] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -253,7 +253,7 @@ func (x *DelRequest) ProtoReflect() protoreflect.Message { // Deprecated: Use DelRequest.ProtoReflect.Descriptor instead. func (*DelRequest) Descriptor() ([]byte, []int) { - return file_db_proto_rawDescGZIP(), []int{4} + return file_lib_proto_dbs_db_proto_rawDescGZIP(), []int{4} } func (x *DelRequest) GetKey() string { @@ -274,7 +274,7 @@ type DelResponse struct { func (x *DelResponse) Reset() { *x = DelResponse{} if protoimpl.UnsafeEnabled { - mi := &file_db_proto_msgTypes[5] + mi := &file_lib_proto_dbs_db_proto_msgTypes[5] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -287,7 +287,7 @@ func (x *DelResponse) String() string { func (*DelResponse) ProtoMessage() {} func (x *DelResponse) ProtoReflect() protoreflect.Message { - mi := &file_db_proto_msgTypes[5] + mi := &file_lib_proto_dbs_db_proto_msgTypes[5] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -300,7 +300,7 @@ func (x *DelResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use DelResponse.ProtoReflect.Descriptor instead. func (*DelResponse) Descriptor() ([]byte, []int) { - return file_db_proto_rawDescGZIP(), []int{5} + return file_lib_proto_dbs_db_proto_rawDescGZIP(), []int{5} } func (x *DelResponse) GetOk() bool { @@ -321,7 +321,7 @@ type KeysRequest struct { func (x *KeysRequest) Reset() { *x = KeysRequest{} if protoimpl.UnsafeEnabled { - mi := &file_db_proto_msgTypes[6] + mi := &file_lib_proto_dbs_db_proto_msgTypes[6] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -334,7 +334,7 @@ func (x *KeysRequest) String() string { func (*KeysRequest) ProtoMessage() {} func (x *KeysRequest) ProtoReflect() protoreflect.Message { - mi := &file_db_proto_msgTypes[6] + mi := &file_lib_proto_dbs_db_proto_msgTypes[6] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -347,7 +347,7 @@ func (x *KeysRequest) ProtoReflect() protoreflect.Message { // Deprecated: Use KeysRequest.ProtoReflect.Descriptor instead. func (*KeysRequest) Descriptor() ([]byte, []int) { - return file_db_proto_rawDescGZIP(), []int{6} + return file_lib_proto_dbs_db_proto_rawDescGZIP(), []int{6} } func (x *KeysRequest) GetPattern() string { @@ -368,7 +368,7 @@ type KeysResponse struct { func (x *KeysResponse) Reset() { *x = KeysResponse{} if protoimpl.UnsafeEnabled { - mi := &file_db_proto_msgTypes[7] + mi := &file_lib_proto_dbs_db_proto_msgTypes[7] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -381,7 +381,7 @@ func (x *KeysResponse) String() string { func (*KeysResponse) ProtoMessage() {} func (x *KeysResponse) ProtoReflect() protoreflect.Message { - mi := &file_db_proto_msgTypes[7] + mi := &file_lib_proto_dbs_db_proto_msgTypes[7] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -394,7 +394,7 @@ func (x *KeysResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use KeysResponse.ProtoReflect.Descriptor instead. func (*KeysResponse) Descriptor() ([]byte, []int) { - return file_db_proto_rawDescGZIP(), []int{7} + return file_lib_proto_dbs_db_proto_rawDescGZIP(), []int{7} } func (x *KeysResponse) GetKeys() []string { @@ -404,79 +404,78 @@ func (x *KeysResponse) GetKeys() []string { return nil } -var File_db_proto protoreflect.FileDescriptor - -var file_db_proto_rawDesc = []byte{ - 0x0a, 0x08, 0x64, 0x62, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x07, 0x63, 0x6c, 0x75, 0x73, - 0x74, 0x65, 0x72, 0x22, 0x1e, 0x0a, 0x0a, 0x47, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, - 0x74, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, - 0x6b, 0x65, 0x79, 0x22, 0x23, 0x0a, 0x0b, 0x47, 0x65, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, - 0x73, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x34, 0x0a, 0x0a, 0x50, 0x75, 0x74, 0x52, - 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, - 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x1d, - 0x0a, 0x0b, 0x50, 0x75, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x0e, 0x0a, - 0x02, 0x6f, 0x6b, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x02, 0x6f, 0x6b, 0x22, 0x1e, 0x0a, - 0x0a, 0x44, 0x65, 0x6c, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x6b, - 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x1d, 0x0a, - 0x0b, 0x44, 0x65, 0x6c, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x0e, 0x0a, 0x02, - 0x6f, 0x6b, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x02, 0x6f, 0x6b, 0x22, 0x27, 0x0a, 0x0b, - 0x4b, 0x65, 0x79, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x70, - 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x70, 0x61, - 0x74, 0x74, 0x65, 0x72, 0x6e, 0x22, 0x22, 0x0a, 0x0c, 0x4b, 0x65, 0x79, 0x73, 0x52, 0x65, 0x73, - 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6b, 0x65, 0x79, 0x73, 0x18, 0x01, 0x20, - 0x03, 0x28, 0x09, 0x52, 0x04, 0x6b, 0x65, 0x79, 0x73, 0x32, 0xe1, 0x01, 0x0a, 0x0c, 0x46, 0x6c, - 0x79, 0x44, 0x42, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x32, 0x0a, 0x03, 0x47, 0x65, - 0x74, 0x12, 0x13, 0x2e, 0x63, 0x6c, 0x75, 0x73, 0x74, 0x65, 0x72, 0x2e, 0x47, 0x65, 0x74, 0x52, - 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x63, 0x6c, 0x75, 0x73, 0x74, 0x65, 0x72, - 0x2e, 0x47, 0x65, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x32, - 0x0a, 0x03, 0x50, 0x75, 0x74, 0x12, 0x13, 0x2e, 0x63, 0x6c, 0x75, 0x73, 0x74, 0x65, 0x72, 0x2e, - 0x50, 0x75, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x63, 0x6c, 0x75, - 0x73, 0x74, 0x65, 0x72, 0x2e, 0x50, 0x75, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, - 0x22, 0x00, 0x12, 0x32, 0x0a, 0x03, 0x44, 0x65, 0x6c, 0x12, 0x13, 0x2e, 0x63, 0x6c, 0x75, 0x73, - 0x74, 0x65, 0x72, 0x2e, 0x44, 0x65, 0x6c, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, - 0x2e, 0x63, 0x6c, 0x75, 0x73, 0x74, 0x65, 0x72, 0x2e, 0x44, 0x65, 0x6c, 0x52, 0x65, 0x73, 0x70, - 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x35, 0x0a, 0x04, 0x4b, 0x65, 0x79, 0x73, 0x12, 0x14, - 0x2e, 0x63, 0x6c, 0x75, 0x73, 0x74, 0x65, 0x72, 0x2e, 0x4b, 0x65, 0x79, 0x73, 0x52, 0x65, 0x71, - 0x75, 0x65, 0x73, 0x74, 0x1a, 0x15, 0x2e, 0x63, 0x6c, 0x75, 0x73, 0x74, 0x65, 0x72, 0x2e, 0x4b, - 0x65, 0x79, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x11, 0x5a, - 0x0f, 0x66, 0x6c, 0x79, 0x64, 0x62, 0x2f, 0x6c, 0x69, 0x62, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +var File_lib_proto_dbs_db_proto protoreflect.FileDescriptor + +var file_lib_proto_dbs_db_proto_rawDesc = []byte{ + 0x0a, 0x16, 0x6c, 0x69, 0x62, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x64, 0x62, 0x73, 0x2f, + 0x64, 0x62, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x03, 0x64, 0x62, 0x73, 0x22, 0x1e, 0x0a, + 0x0a, 0x47, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x6b, + 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x23, 0x0a, + 0x0b, 0x47, 0x65, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x14, 0x0a, 0x05, + 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, + 0x75, 0x65, 0x22, 0x34, 0x0a, 0x0a, 0x50, 0x75, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, + 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x1d, 0x0a, 0x0b, 0x50, 0x75, 0x74, 0x52, + 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x6f, 0x6b, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x08, 0x52, 0x02, 0x6f, 0x6b, 0x22, 0x1e, 0x0a, 0x0a, 0x44, 0x65, 0x6c, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x1d, 0x0a, 0x0b, 0x44, 0x65, 0x6c, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x6f, 0x6b, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x08, 0x52, 0x02, 0x6f, 0x6b, 0x22, 0x27, 0x0a, 0x0b, 0x4b, 0x65, 0x79, 0x73, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x70, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x22, + 0x22, 0x0a, 0x0c, 0x4b, 0x65, 0x79, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, + 0x12, 0x0a, 0x04, 0x6b, 0x65, 0x79, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x09, 0x52, 0x04, 0x6b, + 0x65, 0x79, 0x73, 0x32, 0xc1, 0x01, 0x0a, 0x0c, 0x46, 0x6c, 0x79, 0x44, 0x42, 0x53, 0x65, 0x72, + 0x76, 0x69, 0x63, 0x65, 0x12, 0x2a, 0x0a, 0x03, 0x47, 0x65, 0x74, 0x12, 0x0f, 0x2e, 0x64, 0x62, + 0x73, 0x2e, 0x47, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x10, 0x2e, 0x64, + 0x62, 0x73, 0x2e, 0x47, 0x65, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, + 0x12, 0x2a, 0x0a, 0x03, 0x50, 0x75, 0x74, 0x12, 0x0f, 0x2e, 0x64, 0x62, 0x73, 0x2e, 0x50, 0x75, + 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x10, 0x2e, 0x64, 0x62, 0x73, 0x2e, 0x50, + 0x75, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x2a, 0x0a, 0x03, + 0x44, 0x65, 0x6c, 0x12, 0x0f, 0x2e, 0x64, 0x62, 0x73, 0x2e, 0x44, 0x65, 0x6c, 0x52, 0x65, 0x71, + 0x75, 0x65, 0x73, 0x74, 0x1a, 0x10, 0x2e, 0x64, 0x62, 0x73, 0x2e, 0x44, 0x65, 0x6c, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x2d, 0x0a, 0x04, 0x4b, 0x65, 0x79, 0x73, + 0x12, 0x10, 0x2e, 0x64, 0x62, 0x73, 0x2e, 0x4b, 0x65, 0x79, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x1a, 0x11, 0x2e, 0x64, 0x62, 0x73, 0x2e, 0x4b, 0x65, 0x79, 0x73, 0x52, 0x65, 0x73, + 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x15, 0x5a, 0x13, 0x66, 0x6c, 0x79, 0x64, 0x62, + 0x2f, 0x6c, 0x69, 0x62, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x64, 0x62, 0x73, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( - file_db_proto_rawDescOnce sync.Once - file_db_proto_rawDescData = file_db_proto_rawDesc + file_lib_proto_dbs_db_proto_rawDescOnce sync.Once + file_lib_proto_dbs_db_proto_rawDescData = file_lib_proto_dbs_db_proto_rawDesc ) -func file_db_proto_rawDescGZIP() []byte { - file_db_proto_rawDescOnce.Do(func() { - file_db_proto_rawDescData = protoimpl.X.CompressGZIP(file_db_proto_rawDescData) +func file_lib_proto_dbs_db_proto_rawDescGZIP() []byte { + file_lib_proto_dbs_db_proto_rawDescOnce.Do(func() { + file_lib_proto_dbs_db_proto_rawDescData = protoimpl.X.CompressGZIP(file_lib_proto_dbs_db_proto_rawDescData) }) - return file_db_proto_rawDescData -} - -var file_db_proto_msgTypes = make([]protoimpl.MessageInfo, 8) -var file_db_proto_goTypes = []interface{}{ - (*GetRequest)(nil), // 0: cluster.GetRequest - (*GetResponse)(nil), // 1: cluster.GetResponse - (*PutRequest)(nil), // 2: cluster.PutRequest - (*PutResponse)(nil), // 3: cluster.PutResponse - (*DelRequest)(nil), // 4: cluster.DelRequest - (*DelResponse)(nil), // 5: cluster.DelResponse - (*KeysRequest)(nil), // 6: cluster.KeysRequest - (*KeysResponse)(nil), // 7: cluster.KeysResponse -} -var file_db_proto_depIdxs = []int32{ - 0, // 0: cluster.FlyDBService.Get:input_type -> cluster.GetRequest - 2, // 1: cluster.FlyDBService.Put:input_type -> cluster.PutRequest - 4, // 2: cluster.FlyDBService.Del:input_type -> cluster.DelRequest - 6, // 3: cluster.FlyDBService.Keys:input_type -> cluster.KeysRequest - 1, // 4: cluster.FlyDBService.Get:output_type -> cluster.GetResponse - 3, // 5: cluster.FlyDBService.Put:output_type -> cluster.PutResponse - 5, // 6: cluster.FlyDBService.Del:output_type -> cluster.DelResponse - 7, // 7: cluster.FlyDBService.Keys:output_type -> cluster.KeysResponse + return file_lib_proto_dbs_db_proto_rawDescData +} + +var file_lib_proto_dbs_db_proto_msgTypes = make([]protoimpl.MessageInfo, 8) +var file_lib_proto_dbs_db_proto_goTypes = []interface{}{ + (*GetRequest)(nil), // 0: dbs.GetRequest + (*GetResponse)(nil), // 1: dbs.GetResponse + (*PutRequest)(nil), // 2: dbs.PutRequest + (*PutResponse)(nil), // 3: dbs.PutResponse + (*DelRequest)(nil), // 4: dbs.DelRequest + (*DelResponse)(nil), // 5: dbs.DelResponse + (*KeysRequest)(nil), // 6: dbs.KeysRequest + (*KeysResponse)(nil), // 7: dbs.KeysResponse +} +var file_lib_proto_dbs_db_proto_depIdxs = []int32{ + 0, // 0: dbs.FlyDBService.Get:input_type -> dbs.GetRequest + 2, // 1: dbs.FlyDBService.Put:input_type -> dbs.PutRequest + 4, // 2: dbs.FlyDBService.Del:input_type -> dbs.DelRequest + 6, // 3: dbs.FlyDBService.Keys:input_type -> dbs.KeysRequest + 1, // 4: dbs.FlyDBService.Get:output_type -> dbs.GetResponse + 3, // 5: dbs.FlyDBService.Put:output_type -> dbs.PutResponse + 5, // 6: dbs.FlyDBService.Del:output_type -> dbs.DelResponse + 7, // 7: dbs.FlyDBService.Keys:output_type -> dbs.KeysResponse 4, // [4:8] is the sub-list for method output_type 0, // [0:4] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name @@ -484,13 +483,13 @@ var file_db_proto_depIdxs = []int32{ 0, // [0:0] is the sub-list for field type_name } -func init() { file_db_proto_init() } -func file_db_proto_init() { - if File_db_proto != nil { +func init() { file_lib_proto_dbs_db_proto_init() } +func file_lib_proto_dbs_db_proto_init() { + if File_lib_proto_dbs_db_proto != nil { return } if !protoimpl.UnsafeEnabled { - file_db_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + file_lib_proto_dbs_db_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*GetRequest); i { case 0: return &v.state @@ -502,7 +501,7 @@ func file_db_proto_init() { return nil } } - file_db_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + file_lib_proto_dbs_db_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*GetResponse); i { case 0: return &v.state @@ -514,7 +513,7 @@ func file_db_proto_init() { return nil } } - file_db_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + file_lib_proto_dbs_db_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*PutRequest); i { case 0: return &v.state @@ -526,7 +525,7 @@ func file_db_proto_init() { return nil } } - file_db_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + file_lib_proto_dbs_db_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*PutResponse); i { case 0: return &v.state @@ -538,7 +537,7 @@ func file_db_proto_init() { return nil } } - file_db_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { + file_lib_proto_dbs_db_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*DelRequest); i { case 0: return &v.state @@ -550,7 +549,7 @@ func file_db_proto_init() { return nil } } - file_db_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { + file_lib_proto_dbs_db_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*DelResponse); i { case 0: return &v.state @@ -562,7 +561,7 @@ func file_db_proto_init() { return nil } } - file_db_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} { + file_lib_proto_dbs_db_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*KeysRequest); i { case 0: return &v.state @@ -574,7 +573,7 @@ func file_db_proto_init() { return nil } } - file_db_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} { + file_lib_proto_dbs_db_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*KeysResponse); i { case 0: return &v.state @@ -591,18 +590,18 @@ func file_db_proto_init() { out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), - RawDescriptor: file_db_proto_rawDesc, + RawDescriptor: file_lib_proto_dbs_db_proto_rawDesc, NumEnums: 0, NumMessages: 8, NumExtensions: 0, NumServices: 1, }, - GoTypes: file_db_proto_goTypes, - DependencyIndexes: file_db_proto_depIdxs, - MessageInfos: file_db_proto_msgTypes, + GoTypes: file_lib_proto_dbs_db_proto_goTypes, + DependencyIndexes: file_lib_proto_dbs_db_proto_depIdxs, + MessageInfos: file_lib_proto_dbs_db_proto_msgTypes, }.Build() - File_db_proto = out.File - file_db_proto_rawDesc = nil - file_db_proto_goTypes = nil - file_db_proto_depIdxs = nil + File_lib_proto_dbs_db_proto = out.File + file_lib_proto_dbs_db_proto_rawDesc = nil + file_lib_proto_dbs_db_proto_goTypes = nil + file_lib_proto_dbs_db_proto_depIdxs = nil } diff --git a/lib/proto/db.proto b/lib/proto/dbs/db.proto similarity index 91% rename from lib/proto/db.proto rename to lib/proto/dbs/db.proto index 3865f075..ce77c241 100644 --- a/lib/proto/db.proto +++ b/lib/proto/dbs/db.proto @@ -1,7 +1,7 @@ syntax = "proto3"; -package cluster; -option go_package = "flydb/lib/proto"; +package dbs; +option go_package = "flydb/lib/proto/dbs"; service FlyDBService { rpc Get(GetRequest) returns (GetResponse) {} diff --git a/lib/proto/db_grpc.pb.go b/lib/proto/dbs/db_grpc.pb.go similarity index 92% rename from lib/proto/db_grpc.pb.go rename to lib/proto/dbs/db_grpc.pb.go index 41f4de2e..18d79ea0 100644 --- a/lib/proto/db_grpc.pb.go +++ b/lib/proto/dbs/db_grpc.pb.go @@ -2,9 +2,9 @@ // versions: // - protoc-gen-go-grpc v1.2.0 // - protoc v3.12.4 -// source: db.proto +// source: lib/proto/dbs/db.proto -package proto +package dbs import ( context "context" @@ -38,7 +38,7 @@ func NewFlyDBServiceClient(cc grpc.ClientConnInterface) FlyDBServiceClient { func (c *flyDBServiceClient) Get(ctx context.Context, in *GetRequest, opts ...grpc.CallOption) (*GetResponse, error) { out := new(GetResponse) - err := c.cc.Invoke(ctx, "/cluster.FlyDBService/Get", in, out, opts...) + err := c.cc.Invoke(ctx, "/dbs.FlyDBService/Get", in, out, opts...) if err != nil { return nil, err } @@ -47,7 +47,7 @@ func (c *flyDBServiceClient) Get(ctx context.Context, in *GetRequest, opts ...gr func (c *flyDBServiceClient) Put(ctx context.Context, in *PutRequest, opts ...grpc.CallOption) (*PutResponse, error) { out := new(PutResponse) - err := c.cc.Invoke(ctx, "/cluster.FlyDBService/Put", in, out, opts...) + err := c.cc.Invoke(ctx, "/dbs.FlyDBService/Put", in, out, opts...) if err != nil { return nil, err } @@ -56,7 +56,7 @@ func (c *flyDBServiceClient) Put(ctx context.Context, in *PutRequest, opts ...gr func (c *flyDBServiceClient) Del(ctx context.Context, in *DelRequest, opts ...grpc.CallOption) (*DelResponse, error) { out := new(DelResponse) - err := c.cc.Invoke(ctx, "/cluster.FlyDBService/Del", in, out, opts...) + err := c.cc.Invoke(ctx, "/dbs.FlyDBService/Del", in, out, opts...) if err != nil { return nil, err } @@ -65,7 +65,7 @@ func (c *flyDBServiceClient) Del(ctx context.Context, in *DelRequest, opts ...gr func (c *flyDBServiceClient) Keys(ctx context.Context, in *KeysRequest, opts ...grpc.CallOption) (*KeysResponse, error) { out := new(KeysResponse) - err := c.cc.Invoke(ctx, "/cluster.FlyDBService/Keys", in, out, opts...) + err := c.cc.Invoke(ctx, "/dbs.FlyDBService/Keys", in, out, opts...) if err != nil { return nil, err } @@ -122,7 +122,7 @@ func _FlyDBService_Get_Handler(srv interface{}, ctx context.Context, dec func(in } info := &grpc.UnaryServerInfo{ Server: srv, - FullMethod: "/cluster.FlyDBService/Get", + FullMethod: "/dbs.FlyDBService/Get", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { return srv.(FlyDBServiceServer).Get(ctx, req.(*GetRequest)) @@ -140,7 +140,7 @@ func _FlyDBService_Put_Handler(srv interface{}, ctx context.Context, dec func(in } info := &grpc.UnaryServerInfo{ Server: srv, - FullMethod: "/cluster.FlyDBService/Put", + FullMethod: "/dbs.FlyDBService/Put", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { return srv.(FlyDBServiceServer).Put(ctx, req.(*PutRequest)) @@ -158,7 +158,7 @@ func _FlyDBService_Del_Handler(srv interface{}, ctx context.Context, dec func(in } info := &grpc.UnaryServerInfo{ Server: srv, - FullMethod: "/cluster.FlyDBService/Del", + FullMethod: "/dbs.FlyDBService/Del", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { return srv.(FlyDBServiceServer).Del(ctx, req.(*DelRequest)) @@ -176,7 +176,7 @@ func _FlyDBService_Keys_Handler(srv interface{}, ctx context.Context, dec func(i } info := &grpc.UnaryServerInfo{ Server: srv, - FullMethod: "/cluster.FlyDBService/Keys", + FullMethod: "/dbs.FlyDBService/Keys", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { return srv.(FlyDBServiceServer).Keys(ctx, req.(*KeysRequest)) @@ -188,7 +188,7 @@ func _FlyDBService_Keys_Handler(srv interface{}, ctx context.Context, dec func(i // It's only intended for direct use with grpc.RegisterService, // and not to be introspected or modified (even as a copy) var FlyDBService_ServiceDesc = grpc.ServiceDesc{ - ServiceName: "cluster.FlyDBService", + ServiceName: "dbs.FlyDBService", HandlerType: (*FlyDBServiceServer)(nil), Methods: []grpc.MethodDesc{ { @@ -209,5 +209,5 @@ var FlyDBService_ServiceDesc = grpc.ServiceDesc{ }, }, Streams: []grpc.StreamDesc{}, - Metadata: "db.proto", + Metadata: "lib/proto/dbs/db.proto", } diff --git a/lib/proto/master_grpc.pb.go b/lib/proto/master_grpc.pb.go index 5a1ac2f5..8136fbdd 100644 --- a/lib/proto/master_grpc.pb.go +++ b/lib/proto/master_grpc.pb.go @@ -18,7 +18,7 @@ import ( // Requires gRPC-Go v1.32.0 or later. const _ = grpc.SupportPackageIsVersion7 -// MasterGrpcServiceClient is the client API for MasterGrpcService service. +// MasterGrpcServiceClient is the client API for MasterGrpcService dbs. // // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. type MasterGrpcServiceClient interface { @@ -152,7 +152,7 @@ func (c *masterGrpcServiceClient) ReceiveHeartbeat(ctx context.Context, in *Mast return out, nil } -// MasterGrpcServiceServer is the server API for MasterGrpcService service. +// MasterGrpcServiceServer is the server API for MasterGrpcService dbs. // All implementations must embed UnimplementedMasterGrpcServiceServer // for forward compatibility type MasterGrpcServiceServer interface { @@ -213,7 +213,7 @@ func (UnimplementedMasterGrpcServiceServer) ReceiveHeartbeat(context.Context, *M } func (UnimplementedMasterGrpcServiceServer) mustEmbedUnimplementedMasterGrpcServiceServer() {} -// UnsafeMasterGrpcServiceServer may be embedded to opt out of forward compatibility for this service. +// UnsafeMasterGrpcServiceServer may be embedded to opt out of forward compatibility for this dbs. // Use of this interface is not recommended, as added methods to MasterGrpcServiceServer will // result in compilation errors. type UnsafeMasterGrpcServiceServer interface { @@ -440,7 +440,7 @@ func _MasterGrpcService_ReceiveHeartbeat_Handler(srv interface{}, ctx context.Co return interceptor(ctx, in, info, handler) } -// MasterGrpcService_ServiceDesc is the grpc.ServiceDesc for MasterGrpcService service. +// MasterGrpcService_ServiceDesc is the grpc.ServiceDesc for MasterGrpcService dbs. // It's only intended for direct use with grpc.RegisterService, // and not to be introspected or modified (even as a copy) var MasterGrpcService_ServiceDesc = grpc.ServiceDesc{ diff --git a/lib/proto/slave_grpc.pb.go b/lib/proto/slave_grpc.pb.go index 080b3adb..f181fae3 100644 --- a/lib/proto/slave_grpc.pb.go +++ b/lib/proto/slave_grpc.pb.go @@ -18,7 +18,7 @@ import ( // Requires gRPC-Go v1.32.0 or later. const _ = grpc.SupportPackageIsVersion7 -// SlaveGrpcServiceClient is the client API for SlaveGrpcService service. +// SlaveGrpcServiceClient is the client API for SlaveGrpcService dbs. // // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. type SlaveGrpcServiceClient interface { @@ -112,7 +112,7 @@ func (c *slaveGrpcServiceClient) Heartbeat(ctx context.Context, in *SlaveHeartbe return out, nil } -// SlaveGrpcServiceServer is the server API for SlaveGrpcService service. +// SlaveGrpcServiceServer is the server API for SlaveGrpcService dbs. // All implementations must embed UnimplementedSlaveGrpcServiceServer // for forward compatibility type SlaveGrpcServiceServer interface { @@ -157,7 +157,7 @@ func (UnimplementedSlaveGrpcServiceServer) Heartbeat(context.Context, *SlaveHear } func (UnimplementedSlaveGrpcServiceServer) mustEmbedUnimplementedSlaveGrpcServiceServer() {} -// UnsafeSlaveGrpcServiceServer may be embedded to opt out of forward compatibility for this service. +// UnsafeSlaveGrpcServiceServer may be embedded to opt out of forward compatibility for this dbs. // Use of this interface is not recommended, as added methods to SlaveGrpcServiceServer will // result in compilation errors. type UnsafeSlaveGrpcServiceServer interface { @@ -312,7 +312,7 @@ func _SlaveGrpcService_Heartbeat_Handler(srv interface{}, ctx context.Context, d return interceptor(ctx, in, info, handler) } -// SlaveGrpcService_ServiceDesc is the grpc.ServiceDesc for SlaveGrpcService service. +// SlaveGrpcService_ServiceDesc is the grpc.ServiceDesc for SlaveGrpcService dbs. // It's only intended for direct use with grpc.RegisterService, // and not to be introspected or modified (even as a copy) var SlaveGrpcService_ServiceDesc = grpc.ServiceDesc{ From 97ee6add2333f6d6dbdb04ccf2eeb972b9a23217 Mon Sep 17 00:00:00 2001 From: sjcsjc123 <1401189096@qq.com> Date: Thu, 6 Jul 2023 15:16:22 +0800 Subject: [PATCH 13/13] feat:refactor cli importance(#149) --- config/tcpConfig.go | 2 +- engine/grpc/client/client.go | 10 +++---- engine/grpc/service/db.go | 6 ++-- lib/proto/dbs/db.pb.go | 54 ++++++++++++++++++------------------ lib/proto/dbs/db.proto | 14 +++++----- 5 files changed, 43 insertions(+), 43 deletions(-) diff --git a/config/tcpConfig.go b/config/tcpConfig.go index fc2ff6eb..c8c7a493 100644 --- a/config/tcpConfig.go +++ b/config/tcpConfig.go @@ -10,7 +10,7 @@ type TcpServerConfiguration struct { // Configuration is global tcp server config var Configuration *TcpServerConfiguration -// Init init global tcp server config +// Init global tcp server config func Init() *TcpServerConfiguration { Configuration = &TcpServerConfiguration{ Host: "127.0.0.1", diff --git a/engine/grpc/client/client.go b/engine/grpc/client/client.go index 2f6103a8..2e60ae66 100644 --- a/engine/grpc/client/client.go +++ b/engine/grpc/client/client.go @@ -29,7 +29,7 @@ func (c *Client) Put(key []byte, value []byte) error { if err != nil { return err } - put, err := client.Put(context.Background(), &dbs.PutRequest{Key: string(key), Value: string(value)}) + put, err := client.Put(context.Background(), &dbs.PutRequest{Key: key, Value: value}) if err != nil { return err } @@ -45,11 +45,11 @@ func (c *Client) Get(key []byte) ([]byte, error) { if err != nil { return nil, err } - get, err := client.Get(context.Background(), &dbs.GetRequest{Key: string(key)}) + get, err := client.Get(context.Background(), &dbs.GetRequest{Key: key}) if err != nil { return nil, err } - return []byte(get.Value), nil + return get.Value, nil } // Del deletes a key-value pair from the db by client api @@ -58,7 +58,7 @@ func (c *Client) Del(key []byte) error { if err != nil { return err } - del, err := client.Del(context.Background(), &dbs.DelRequest{Key: string(key)}) + del, err := client.Del(context.Background(), &dbs.DelRequest{Key: key}) if err != nil { return err } @@ -80,7 +80,7 @@ func (c *Client) Keys() ([][]byte, error) { } result := make([][]byte, len(keys.Keys)) for i, key := range keys.Keys { - result[i] = []byte(key) + result[i] = key } return result, nil } diff --git a/engine/grpc/service/db.go b/engine/grpc/service/db.go index 86660a40..b8f447e1 100644 --- a/engine/grpc/service/db.go +++ b/engine/grpc/service/db.go @@ -99,7 +99,7 @@ func (s *Service) Get(ctx context.Context, req *dbs.GetRequest) (*dbs.GetRespons if err != nil { return &dbs.GetResponse{}, err } - return &dbs.GetResponse{Value: string(value)}, nil + return &dbs.GetResponse{Value: value}, nil } // Del is a grpc s for del @@ -114,9 +114,9 @@ func (s *Service) Del(ctx context.Context, req *dbs.DelRequest) (*dbs.DelRespons // Keys is a grpc s for keys func (s *Service) Keys(ctx context.Context, req *dbs.KeysRequest) (*dbs.KeysResponse, error) { list := s.db.GetListKeys() - keys := make([]string, len(list)) + keys := make([][]byte, len(list)) for i, bytes := range list { - keys[i] = string(bytes) + keys[i] = bytes } return &dbs.KeysResponse{Keys: keys}, nil } diff --git a/lib/proto/dbs/db.pb.go b/lib/proto/dbs/db.pb.go index 5d745807..83424499 100644 --- a/lib/proto/dbs/db.pb.go +++ b/lib/proto/dbs/db.pb.go @@ -25,7 +25,7 @@ type GetRequest struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` + Key []byte `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` } func (x *GetRequest) Reset() { @@ -60,11 +60,11 @@ func (*GetRequest) Descriptor() ([]byte, []int) { return file_lib_proto_dbs_db_proto_rawDescGZIP(), []int{0} } -func (x *GetRequest) GetKey() string { +func (x *GetRequest) GetKey() []byte { if x != nil { return x.Key } - return "" + return nil } type GetResponse struct { @@ -72,7 +72,7 @@ type GetResponse struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Value string `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"` + Value []byte `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"` } func (x *GetResponse) Reset() { @@ -107,11 +107,11 @@ func (*GetResponse) Descriptor() ([]byte, []int) { return file_lib_proto_dbs_db_proto_rawDescGZIP(), []int{1} } -func (x *GetResponse) GetValue() string { +func (x *GetResponse) GetValue() []byte { if x != nil { return x.Value } - return "" + return nil } type PutRequest struct { @@ -119,8 +119,8 @@ type PutRequest struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` - Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"` + Key []byte `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` + Value []byte `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"` } func (x *PutRequest) Reset() { @@ -155,18 +155,18 @@ func (*PutRequest) Descriptor() ([]byte, []int) { return file_lib_proto_dbs_db_proto_rawDescGZIP(), []int{2} } -func (x *PutRequest) GetKey() string { +func (x *PutRequest) GetKey() []byte { if x != nil { return x.Key } - return "" + return nil } -func (x *PutRequest) GetValue() string { +func (x *PutRequest) GetValue() []byte { if x != nil { return x.Value } - return "" + return nil } type PutResponse struct { @@ -221,7 +221,7 @@ type DelRequest struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` + Key []byte `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` } func (x *DelRequest) Reset() { @@ -256,11 +256,11 @@ func (*DelRequest) Descriptor() ([]byte, []int) { return file_lib_proto_dbs_db_proto_rawDescGZIP(), []int{4} } -func (x *DelRequest) GetKey() string { +func (x *DelRequest) GetKey() []byte { if x != nil { return x.Key } - return "" + return nil } type DelResponse struct { @@ -315,7 +315,7 @@ type KeysRequest struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Pattern string `protobuf:"bytes,1,opt,name=pattern,proto3" json:"pattern,omitempty"` + Pattern []byte `protobuf:"bytes,1,opt,name=pattern,proto3" json:"pattern,omitempty"` } func (x *KeysRequest) Reset() { @@ -350,11 +350,11 @@ func (*KeysRequest) Descriptor() ([]byte, []int) { return file_lib_proto_dbs_db_proto_rawDescGZIP(), []int{6} } -func (x *KeysRequest) GetPattern() string { +func (x *KeysRequest) GetPattern() []byte { if x != nil { return x.Pattern } - return "" + return nil } type KeysResponse struct { @@ -362,7 +362,7 @@ type KeysResponse struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Keys []string `protobuf:"bytes,1,rep,name=keys,proto3" json:"keys,omitempty"` + Keys [][]byte `protobuf:"bytes,1,rep,name=keys,proto3" json:"keys,omitempty"` } func (x *KeysResponse) Reset() { @@ -397,7 +397,7 @@ func (*KeysResponse) Descriptor() ([]byte, []int) { return file_lib_proto_dbs_db_proto_rawDescGZIP(), []int{7} } -func (x *KeysResponse) GetKeys() []string { +func (x *KeysResponse) GetKeys() [][]byte { if x != nil { return x.Keys } @@ -410,23 +410,23 @@ var file_lib_proto_dbs_db_proto_rawDesc = []byte{ 0x0a, 0x16, 0x6c, 0x69, 0x62, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x64, 0x62, 0x73, 0x2f, 0x64, 0x62, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x03, 0x64, 0x62, 0x73, 0x22, 0x1e, 0x0a, 0x0a, 0x47, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x6b, - 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x23, 0x0a, + 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x23, 0x0a, 0x0b, 0x47, 0x65, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x14, 0x0a, 0x05, - 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, + 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x34, 0x0a, 0x0a, 0x50, 0x75, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, - 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, + 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x1d, 0x0a, 0x0b, 0x50, 0x75, 0x74, 0x52, + 0x0c, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x1d, 0x0a, 0x0b, 0x50, 0x75, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x6f, 0x6b, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x02, 0x6f, 0x6b, 0x22, 0x1e, 0x0a, 0x0a, 0x44, 0x65, 0x6c, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x1d, 0x0a, 0x0b, 0x44, 0x65, 0x6c, 0x52, 0x65, + 0x28, 0x0c, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x1d, 0x0a, 0x0b, 0x44, 0x65, 0x6c, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x6f, 0x6b, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x02, 0x6f, 0x6b, 0x22, 0x27, 0x0a, 0x0b, 0x4b, 0x65, 0x79, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x70, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x22, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x70, 0x61, 0x74, 0x74, 0x65, 0x72, 0x6e, 0x22, 0x22, 0x0a, 0x0c, 0x4b, 0x65, 0x79, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, - 0x12, 0x0a, 0x04, 0x6b, 0x65, 0x79, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x09, 0x52, 0x04, 0x6b, + 0x12, 0x0a, 0x04, 0x6b, 0x65, 0x79, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0c, 0x52, 0x04, 0x6b, 0x65, 0x79, 0x73, 0x32, 0xc1, 0x01, 0x0a, 0x0c, 0x46, 0x6c, 0x79, 0x44, 0x42, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x2a, 0x0a, 0x03, 0x47, 0x65, 0x74, 0x12, 0x0f, 0x2e, 0x64, 0x62, 0x73, 0x2e, 0x47, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x10, 0x2e, 0x64, diff --git a/lib/proto/dbs/db.proto b/lib/proto/dbs/db.proto index ce77c241..f641ea4d 100644 --- a/lib/proto/dbs/db.proto +++ b/lib/proto/dbs/db.proto @@ -11,16 +11,16 @@ service FlyDBService { } message GetRequest { - string key = 1; + bytes key = 1; } message GetResponse { - string value = 1; + bytes value = 1; } message PutRequest { - string key = 1; - string value = 2; + bytes key = 1; + bytes value = 2; } message PutResponse { @@ -28,7 +28,7 @@ message PutResponse { } message DelRequest { - string key = 1; + bytes key = 1; } message DelResponse { @@ -36,9 +36,9 @@ message DelResponse { } message KeysRequest { - string pattern = 1; + bytes pattern = 1; } message KeysResponse { - repeated string keys = 1; + repeated bytes keys = 1; } \ No newline at end of file