diff --git a/.gitignore b/.gitignore index 49ce3c1..27a500c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ -/vendor \ No newline at end of file +/vendor +__pycache__/ +/dion-env + diff --git a/go.mod b/go.mod index 7a5773d..5970e64 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,8 @@ go 1.20 require ( github.com/gin-gonic/gin v1.9.1 github.com/golang-jwt/jwt v3.2.2+incompatible + google.golang.org/grpc v1.59.0 + google.golang.org/protobuf v1.31.0 ) require ( @@ -16,6 +18,7 @@ require ( github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.14.0 // indirect github.com/goccy/go-json v0.10.2 // indirect + github.com/golang/protobuf v1.5.3 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.4 // indirect github.com/leodido/go-urn v1.2.4 // indirect @@ -26,10 +29,10 @@ require ( github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.11 // indirect golang.org/x/arch v0.3.0 // indirect - golang.org/x/crypto v0.9.0 // indirect - golang.org/x/net v0.10.0 // indirect - golang.org/x/sys v0.8.0 // indirect - golang.org/x/text v0.9.0 // indirect - google.golang.org/protobuf v1.30.0 // indirect + golang.org/x/crypto v0.12.0 // indirect + golang.org/x/net v0.14.0 // indirect + golang.org/x/sys v0.11.0 // indirect + golang.org/x/text v0.12.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 0a7a091..7ea8c21 100644 --- a/go.sum +++ b/go.sum @@ -25,8 +25,10 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= @@ -64,21 +66,25 @@ github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZ golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= -golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g= -golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0= -golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= -golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk= +golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw= +golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14= +golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI= golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= -golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= -golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= +golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc= +golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M= +google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk= +google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -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.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +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/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index b89df6e..3244398 100644 --- a/main.go +++ b/main.go @@ -31,4 +31,26 @@ func main() { fmt.Print(err.Error()) } fmt.Print(val) + } + +// import ( +// "sixletters/simple-db/pkg/server" +// "google.golang.org/grpc" +// "log" +// "net" +// ) +// func main() { +// lis, err := net.Listen("tcp", ":8080") +// if err != nil { +// log.Fatalf("Failed to listen: %v", err) +// } + +// grpcServer := grpc.NewServer() +// server.RegisterKeyValueServiceServer(grpcServer, server.NewKeyValueServer()) + +// log.Println("Server is listening on :8080") +// if err := grpcServer.Serve(lis); err != nil { +// log.Fatalf("Failed to serve: %v", err) +// } +// } diff --git a/pkg/protos/client-without-internode.proto b/pkg/protos/client-without-internode.proto new file mode 100644 index 0000000..8039365 --- /dev/null +++ b/pkg/protos/client-without-internode.proto @@ -0,0 +1,25 @@ +syntax = "proto3"; + +package clientwithoutinternode; + +message GetRequest { + string key = 1; +} + +message GetResponse { + string value = 1; +} + +message PutRequest { + string key = 1; + string value = 2; +} + +message PutResponse { + bool success = 1; +} + +service KeyValueService { + rpc Get(GetRequest) returns (GetResponse); + rpc Put(PutRequest) returns (PutResponse); +} diff --git a/pkg/server/grpcServer.go b/pkg/server/grpcServer.go new file mode 100644 index 0000000..aa34b9e --- /dev/null +++ b/pkg/server/grpcServer.go @@ -0,0 +1,45 @@ +package server + +import ( + "context" + "fmt" + "sixletters/simple-db/pkg/storage" + "sixletters/simple-db/pkg/tree" + "sixletters/simple-db/pkg/stubs" +) + +type grpcServer struct{ + storageEngine storage.StorageEngine +} + +func NewGrpcServer() *grpcServer { + newStorageConfig := storage.NewConfigWithOpts( + storage.WithTreeType(tree.BTree), + storage.WithFilePath("testFile.txt"), + ) + storageEngine, err := storage.NewSingletonEngine(newStorageConfig) + if err != nil { + panic(err) + } + return &grpcServer{ + storageEngine: storageEngine, + } +} + +func (s *grpcServer) Get(ctx context.Context, req *stubs.GetRequest) (*stubs.GetResponse, error) { + res, err := s.storageEngine.Get(context.Background(), req.Key) + if err != nil { + fmt.Print(err.Error()) + return nil, err + } + return &stubs.GetResponse{Value: res}, nil +} + +func (s *grpcServer) Put(ctx context.Context, req *stubs.PutRequest) (*stubs.PutResponse, error) { + err := s.storageEngine.Put(context.Background(), req.Key, req.Value) + if err != nil { + fmt.Print(err.Error()) + return &stubs.PutResponse{Success: false}, err + } + return &stubs.PutResponse{Success: true}, nil +} diff --git a/pkg/storage/simpledb.go b/pkg/storage/simpledb.go new file mode 100644 index 0000000..950ea79 --- /dev/null +++ b/pkg/storage/simpledb.go @@ -0,0 +1,23 @@ +package storage + +import "context" + +type StorageEngine interface { + Put(ctx context.Context, key string, value string) error + Get(ctx context.Context, key string) (string, error) +} + +type storageEngine struct { +} + +func NewStorageEngine() StorageEngine { + return &storageEngine{} +} + +func (s *storageEngine) Put(ctx context.Context, key string, value string) error { + return nil +} + +func (s *storageEngine) Get(ctx context.Context, key string) (string, error) { + return "", nil +} diff --git a/pkg/stubs/client-without-internode.pb.go b/pkg/stubs/client-without-internode.pb.go new file mode 100644 index 0000000..7344d2e --- /dev/null +++ b/pkg/stubs/client-without-internode.pb.go @@ -0,0 +1,347 @@ +package stubs + +import ( + reflect "reflect" + sync "sync" + + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" +) + +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_client_without_internode_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_client_without_internode_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_client_without_internode_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_client_without_internode_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_client_without_internode_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_client_without_internode_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_client_without_internode_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_client_without_internode_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_client_without_internode_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 + + Success bool `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty"` +} + +func (x *PutResponse) Reset() { + *x = PutResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_client_without_internode_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_client_without_internode_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_client_without_internode_proto_rawDescGZIP(), []int{3} +} + +func (x *PutResponse) GetSuccess() bool { + if x != nil { + return x.Success + } + return false +} + +var File_client_without_internode_proto protoreflect.FileDescriptor + +var file_client_without_internode_proto_rawDesc = []byte{ + 0x0a, 0x1e, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x2d, 0x77, 0x69, 0x74, 0x68, 0x6f, 0x75, 0x74, + 0x2d, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x6f, 0x64, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x12, 0x08, 0x6b, 0x65, 0x79, 0x76, 0x61, 0x6c, 0x75, 0x65, 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, 0x27, 0x0a, 0x0b, 0x50, 0x75, 0x74, 0x52, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x32, 0x79, + 0x0a, 0x0f, 0x4b, 0x65, 0x79, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, + 0x65, 0x12, 0x32, 0x0a, 0x03, 0x47, 0x65, 0x74, 0x12, 0x14, 0x2e, 0x6b, 0x65, 0x79, 0x76, 0x61, + 0x6c, 0x75, 0x65, 0x2e, 0x47, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x15, + 0x2e, 0x6b, 0x65, 0x79, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x2e, 0x47, 0x65, 0x74, 0x52, 0x65, 0x73, + 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x32, 0x0a, 0x03, 0x50, 0x75, 0x74, 0x12, 0x14, 0x2e, 0x6b, + 0x65, 0x79, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x2e, 0x50, 0x75, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x1a, 0x15, 0x2e, 0x6b, 0x65, 0x79, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x2e, 0x50, 0x75, + 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x20, 0x5a, 0x1e, 0x67, 0x69, 0x74, + 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x73, 0x69, 0x78, 0x6c, 0x65, 0x74, 0x74, 0x65, + 0x72, 0x73, 0x2f, 0x73, 0x69, 0x6d, 0x70, 0x6c, 0x65, 0x44, 0x42, 0x62, 0x06, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x33, +} + +var ( + file_client_without_internode_proto_rawDescOnce sync.Once + file_client_without_internode_proto_rawDescData = file_client_without_internode_proto_rawDesc +) + +func file_client_without_internode_proto_rawDescGZIP() []byte { + file_client_without_internode_proto_rawDescOnce.Do(func() { + file_client_without_internode_proto_rawDescData = protoimpl.X.CompressGZIP(file_client_without_internode_proto_rawDescData) + }) + return file_client_without_internode_proto_rawDescData +} + +var file_client_without_internode_proto_msgTypes = make([]protoimpl.MessageInfo, 4) +var file_client_without_internode_proto_goTypes = []interface{}{ + (*GetRequest)(nil), // 0: keyvalue.GetRequest + (*GetResponse)(nil), // 1: keyvalue.GetResponse + (*PutRequest)(nil), // 2: keyvalue.PutRequest + (*PutResponse)(nil), // 3: keyvalue.PutResponse +} +var file_client_without_internode_proto_depIdxs = []int32{ + 0, // 0: keyvalue.KeyValueService.Get:input_type -> keyvalue.GetRequest + 2, // 1: keyvalue.KeyValueService.Put:input_type -> keyvalue.PutRequest + 1, // 2: keyvalue.KeyValueService.Get:output_type -> keyvalue.GetResponse + 3, // 3: keyvalue.KeyValueService.Put:output_type -> keyvalue.PutResponse + 2, // [2:4] is the sub-list for method output_type + 0, // [0:2] 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_client_without_internode_proto_init() } +func file_client_without_internode_proto_init() { + if File_client_without_internode_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_client_without_internode_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_client_without_internode_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_client_without_internode_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_client_without_internode_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 + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_client_without_internode_proto_rawDesc, + NumEnums: 0, + NumMessages: 4, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_client_without_internode_proto_goTypes, + DependencyIndexes: file_client_without_internode_proto_depIdxs, + MessageInfos: file_client_without_internode_proto_msgTypes, + }.Build() + File_client_without_internode_proto = out.File + file_client_without_internode_proto_rawDesc = nil + file_client_without_internode_proto_goTypes = nil + file_client_without_internode_proto_depIdxs = nil +} diff --git a/pkg/stubs/client-without-internode_grpc.pb.go b/pkg/stubs/client-without-internode_grpc.pb.go new file mode 100644 index 0000000..3ad5ce8 --- /dev/null +++ b/pkg/stubs/client-without-internode_grpc.pb.go @@ -0,0 +1,136 @@ +package stubs + +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 + +// KeyValueServiceClient is the client API for KeyValueService 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 KeyValueServiceClient interface { + Get(ctx context.Context, in *GetRequest, opts ...grpc.CallOption) (*GetResponse, error) + Put(ctx context.Context, in *PutRequest, opts ...grpc.CallOption) (*PutResponse, error) +} + +type keyValueServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewKeyValueServiceClient(cc grpc.ClientConnInterface) KeyValueServiceClient { + return &keyValueServiceClient{cc} +} + +func (c *keyValueServiceClient) Get(ctx context.Context, in *GetRequest, opts ...grpc.CallOption) (*GetResponse, error) { + out := new(GetResponse) + err := c.cc.Invoke(ctx, "/keyvalue.KeyValueService/Get", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *keyValueServiceClient) Put(ctx context.Context, in *PutRequest, opts ...grpc.CallOption) (*PutResponse, error) { + out := new(PutResponse) + err := c.cc.Invoke(ctx, "/keyvalue.KeyValueService/Put", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// KeyValueServiceServer is the server API for KeyValueService service. +// All implementations must embed UnimplementedKeyValueServiceServer +// for forward compatibility +type KeyValueServiceServer interface { + Get(context.Context, *GetRequest) (*GetResponse, error) + Put(context.Context, *PutRequest) (*PutResponse, error) + mustEmbedUnimplementedKeyValueServiceServer() +} + +// UnimplementedKeyValueServiceServer must be embedded to have forward compatible implementations. +type UnimplementedKeyValueServiceServer struct { +} + +func (UnimplementedKeyValueServiceServer) Get(context.Context, *GetRequest) (*GetResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method Get not implemented") +} +func (UnimplementedKeyValueServiceServer) Put(context.Context, *PutRequest) (*PutResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method Put not implemented") +} +func (UnimplementedKeyValueServiceServer) mustEmbedUnimplementedKeyValueServiceServer() {} + +// UnsafeKeyValueServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to KeyValueServiceServer will +// result in compilation errors. +type UnsafeKeyValueServiceServer interface { + mustEmbedUnimplementedKeyValueServiceServer() +} + +func RegisterKeyValueServiceServer(s grpc.ServiceRegistrar, srv KeyValueServiceServer) { + s.RegisterService(&KeyValueService_ServiceDesc, srv) +} + +func _KeyValueService_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.(KeyValueServiceServer).Get(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/keyvalue.KeyValueService/Get", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(KeyValueServiceServer).Get(ctx, req.(*GetRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _KeyValueService_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.(KeyValueServiceServer).Put(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/keyvalue.KeyValueService/Put", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(KeyValueServiceServer).Put(ctx, req.(*PutRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// KeyValueService_ServiceDesc is the grpc.ServiceDesc for KeyValueService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var KeyValueService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "keyvalue.KeyValueService", + HandlerType: (*KeyValueServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Get", + Handler: _KeyValueService_Get_Handler, + }, + { + MethodName: "Put", + Handler: _KeyValueService_Put_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "client-without-internode.proto", +} diff --git a/pkg/stubs/client_without_internode_pb2.py b/pkg/stubs/client_without_internode_pb2.py new file mode 100644 index 0000000..b35a85f --- /dev/null +++ b/pkg/stubs/client_without_internode_pb2.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: client-without-internode.proto +"""Generated protocol buffer code.""" +from google.protobuf import descriptor as _descriptor +from google.protobuf import descriptor_pool as _descriptor_pool +from google.protobuf import symbol_database as _symbol_database +from google.protobuf.internal import builder as _builder +# @@protoc_insertion_point(imports) + +_sym_db = _symbol_database.Default() + + + + +DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x1e\x63lient-without-internode.proto\x12\x08keyvalue\"\x19\n\nGetRequest\x12\x0b\n\x03key\x18\x01 \x01(\t\"\x1c\n\x0bGetResponse\x12\r\n\x05value\x18\x01 \x01(\t\"(\n\nPutRequest\x12\x0b\n\x03key\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t\"\x1e\n\x0bPutResponse\x12\x0f\n\x07success\x18\x01 \x01(\x08\x32y\n\x0fKeyValueService\x12\x32\n\x03Get\x12\x14.keyvalue.GetRequest\x1a\x15.keyvalue.GetResponse\x12\x32\n\x03Put\x12\x14.keyvalue.PutRequest\x1a\x15.keyvalue.PutResponseb\x06proto3') + +_globals = globals() +_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) +_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'client_without_internode_pb2', _globals) +if _descriptor._USE_C_DESCRIPTORS == False: + DESCRIPTOR._options = None + _globals['_GETREQUEST']._serialized_start=44 + _globals['_GETREQUEST']._serialized_end=69 + _globals['_GETRESPONSE']._serialized_start=71 + _globals['_GETRESPONSE']._serialized_end=99 + _globals['_PUTREQUEST']._serialized_start=101 + _globals['_PUTREQUEST']._serialized_end=141 + _globals['_PUTRESPONSE']._serialized_start=143 + _globals['_PUTRESPONSE']._serialized_end=173 + _globals['_KEYVALUESERVICE']._serialized_start=175 + _globals['_KEYVALUESERVICE']._serialized_end=296 +# @@protoc_insertion_point(module_scope) diff --git a/pkg/stubs/client_without_internode_pb2_grpc.py b/pkg/stubs/client_without_internode_pb2_grpc.py new file mode 100644 index 0000000..59187b5 --- /dev/null +++ b/pkg/stubs/client_without_internode_pb2_grpc.py @@ -0,0 +1,99 @@ +# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT! +"""Client and server classes corresponding to protobuf-defined services.""" +import grpc + +import client_without_internode_pb2 as client__without__internode__pb2 + + +class KeyValueServiceStub(object): + """Missing associated documentation comment in .proto file.""" + + def __init__(self, channel): + """Constructor. + + Args: + channel: A grpc.Channel. + """ + self.Get = channel.unary_unary( + '/keyvalue.KeyValueService/Get', + request_serializer=client__without__internode__pb2.GetRequest.SerializeToString, + response_deserializer=client__without__internode__pb2.GetResponse.FromString, + ) + self.Put = channel.unary_unary( + '/keyvalue.KeyValueService/Put', + request_serializer=client__without__internode__pb2.PutRequest.SerializeToString, + response_deserializer=client__without__internode__pb2.PutResponse.FromString, + ) + + +class KeyValueServiceServicer(object): + """Missing associated documentation comment in .proto file.""" + + def Get(self, request, context): + """Missing associated documentation comment in .proto file.""" + context.set_code(grpc.StatusCode.UNIMPLEMENTED) + context.set_details('Method not implemented!') + raise NotImplementedError('Method not implemented!') + + def Put(self, request, context): + """Missing associated documentation comment in .proto file.""" + context.set_code(grpc.StatusCode.UNIMPLEMENTED) + context.set_details('Method not implemented!') + raise NotImplementedError('Method not implemented!') + + +def add_KeyValueServiceServicer_to_server(servicer, server): + rpc_method_handlers = { + 'Get': grpc.unary_unary_rpc_method_handler( + servicer.Get, + request_deserializer=client__without__internode__pb2.GetRequest.FromString, + response_serializer=client__without__internode__pb2.GetResponse.SerializeToString, + ), + 'Put': grpc.unary_unary_rpc_method_handler( + servicer.Put, + request_deserializer=client__without__internode__pb2.PutRequest.FromString, + response_serializer=client__without__internode__pb2.PutResponse.SerializeToString, + ), + } + generic_handler = grpc.method_handlers_generic_handler( + 'keyvalue.KeyValueService', rpc_method_handlers) + server.add_generic_rpc_handlers((generic_handler,)) + + + # This class is part of an EXPERIMENTAL API. +class KeyValueService(object): + """Missing associated documentation comment in .proto file.""" + + @staticmethod + def Get(request, + target, + options=(), + channel_credentials=None, + call_credentials=None, + insecure=False, + compression=None, + wait_for_ready=None, + timeout=None, + metadata=None): + return grpc.experimental.unary_unary(request, target, '/keyvalue.KeyValueService/Get', + client__without__internode__pb2.GetRequest.SerializeToString, + client__without__internode__pb2.GetResponse.FromString, + options, channel_credentials, + insecure, call_credentials, compression, wait_for_ready, timeout, metadata) + + @staticmethod + def Put(request, + target, + options=(), + channel_credentials=None, + call_credentials=None, + insecure=False, + compression=None, + wait_for_ready=None, + timeout=None, + metadata=None): + return grpc.experimental.unary_unary(request, target, '/keyvalue.KeyValueService/Put', + client__without__internode__pb2.PutRequest.SerializeToString, + client__without__internode__pb2.PutResponse.FromString, + options, channel_credentials, + insecure, call_credentials, compression, wait_for_ready, timeout, metadata) diff --git a/py-command-line-client/KVStoreController.py b/py-command-line-client/KVStoreController.py new file mode 100644 index 0000000..7e45877 --- /dev/null +++ b/py-command-line-client/KVStoreController.py @@ -0,0 +1,17 @@ +import KVStoreService +from enum.command import Command + +class KVStoreController(): + def __init__(self, server_address): + self.service = KVStoreService(server_address) + + self.commands = { + Command.COMMAND_GET.value: self.service.get_value, + Command.COMMAND_PUT.value: self.service.insert_value, + Command.COMMAND_UPDATE.value: self.service.update_value, + Command.COMMAND_DELETE.value: self.service.delete_value, + } + + def execute_command(self, command, data): + service_function = self.commands[command] + return service_function(data) \ No newline at end of file diff --git a/py-command-line-client/KVStoreService.py b/py-command-line-client/KVStoreService.py new file mode 100644 index 0000000..f884827 --- /dev/null +++ b/py-command-line-client/KVStoreService.py @@ -0,0 +1,29 @@ +import SimpleDB + +class KVStoreService(): + def __init__(self, server_address): + self.KVStore = SimpleDB(server_address) + + def get_value(self, data): + if data["key"] not in self.KVStore: + raise ValueError("No such Key Value pair.") + return self.KVStore.get_value(data["key"]) + + def insert_value(self, data): + if data["key"] in self.KVStore: + raise ValueError("Key Value pair already exist") + return self.KVStore.set_key_value(data["key"], data["val"]) + + + # To implement update and delete methods + # def update_value(self, data): + # if data["key"] not in self.KVStore: + # raise ValueError("Key Value pair does not exist") + # self.KVStore[data["key"]] = data["val"] + # return self.KVStore + + # def delete_value(self, data): + # if data["key"] not in self.KVStore: + # raise ValueError("Key Value pair does not exist") + # self.KVStore.pop(data["key"]) + # return self.KVStore \ No newline at end of file diff --git a/py-command-line-client/SimpleDB.py b/py-command-line-client/SimpleDB.py new file mode 100644 index 0000000..b34a874 --- /dev/null +++ b/py-command-line-client/SimpleDB.py @@ -0,0 +1,19 @@ +import grpc +from ..pkg.stubs import client_without_internode_pb2 +from ..pkg.stubs import client_without_internode_pb2_grpc + +# Simple Db SDK +class SimpleDB: + def __init__(self, server_address): + self.channel = grpc.insecure_channel(server_address) + self.stub = client_without_internode_pb2_grpc.KeyValueServiceStub(self.channel) + + def set_key_value(self, key, value): + request = client_without_internode_pb2.PutRequest(key=key, value=value) + response = self.stub.Put(request) + return response + + def get_value(self, key): + request = client_without_internode_pb2.GetRequest(key=key) + response = self.stub.Get(request) + return response.value \ No newline at end of file diff --git a/py-command-line-client/client.py b/py-command-line-client/client.py index 96c27b7..7454e45 100644 --- a/py-command-line-client/client.py +++ b/py-command-line-client/client.py @@ -1,11 +1,11 @@ from enum import Enum -class Command(Enum): - COMMAND_GET = "get" - COMMAND_PUT = "put" - COMMAND_UPDATE = "update" - COMMAND_DELETE = "delete" +from enum.command import Command +import KVStoreController + class Client(): + def __init__(self, server_address): + self.KVStoreController = KVStoreController(server_address) def get_data_from_input_array(self, input_array): if input_array[1] == Command.COMMAND_GET.value: @@ -46,51 +46,12 @@ def run(self): "key": key, "val": val } - resp = KVStoreController.execute_command(command, data) + resp = self.KVStoreController.execute_command(command, data) print(resp) except ValueError as e: print(f"Invalid Input: {e}") -class KVStoreService(): - def __init__(self): - self.KVStore = {} - - def get_value(self, data): - if data["key"] not in self.KVStore: - raise ValueError("No such Key Value pair.") - return self.KVStore[data["key"]] - - def insert_value(self, data): - if data["key"] in self.KVStore: - raise ValueError("Key Value pair already exist") - self.KVStore[data["key"]] = data["val"] - return self.KVStore - - def update_value(self, data): - if data["key"] not in self.KVStore: - raise ValueError("Key Value pair does not exist") - self.KVStore[data["key"]] = data["val"] - return self.KVStore - - def delete_value(self, data): - if data["key"] not in self.KVStore: - raise ValueError("Key Value pair does not exist") - self.KVStore.pop(data["key"]) - return self.KVStore - -class KVStoreController(): - service = KVStoreService() - - commands = { - Command.COMMAND_GET.value: service.get_value, - Command.COMMAND_PUT.value: service.insert_value, - Command.COMMAND_UPDATE.value: service.update_value, - Command.COMMAND_DELETE.value: service.delete_value, - } - def execute_command(command, data): - service_function = KVStoreController.commands[command] - return service_function(data) @@ -99,9 +60,9 @@ def execute_command(command, data): if __name__ == "__main__": - client = Client() + client = Client('localhost:8080') try: print("####### Client has started #######") client.run() except (Exception, KeyboardInterrupt): - print("####### Client has stopped #######") + print("####### Client has stopped #######") \ No newline at end of file diff --git a/py-command-line-client/enum/command.py b/py-command-line-client/enum/command.py new file mode 100644 index 0000000..3635f9c --- /dev/null +++ b/py-command-line-client/enum/command.py @@ -0,0 +1,5 @@ +class Command(Enum): + COMMAND_GET = "get" + COMMAND_PUT = "put" + COMMAND_UPDATE = "update" + COMMAND_DELETE = "delete" \ No newline at end of file diff --git a/py-sdk-client/__init__.py b/py-sdk-client/__init__.py deleted file mode 100644 index bc1e889..0000000 --- a/py-sdk-client/__init__.py +++ /dev/null @@ -1,15 +0,0 @@ -class Client(): - def __init__(self, host='127.0.0.1', port=4002): - if isinstance(host, list) and not isinstance(host, str): - self.host_list = host - else: - self.host_list = [[host, port]] - - - def put(key, val): - # TODO - return - - def get(key, val): - # TODO - return \ No newline at end of file