diff --git a/src/kvsrv/client.go b/src/kvsrv/client.go index 9e11c27..673eedb 100644 --- a/src/kvsrv/client.go +++ b/src/kvsrv/client.go @@ -1,9 +1,12 @@ package kvsrv -import "6.5840/labrpc" -import "crypto/rand" -import "math/big" +import ( + "crypto/rand" + "log" + "math/big" + "6.5840/labrpc" +) type Clerk struct { server *labrpc.ClientEnd @@ -35,9 +38,16 @@ func MakeClerk(server *labrpc.ClientEnd) *Clerk { // must match the declared types of the RPC handler function's // arguments. and reply must be passed as a pointer. func (ck *Clerk) Get(key string) string { - + args := GetArgs{ + Key: key, + } + reply := GetReply{} + ok := false + for !ok { + ok = ck.server.Call("KVServer.Get", &args, &reply) + } // You will have to modify this function. - return "" + return reply.Value } // shared by Put and Append. @@ -50,7 +60,18 @@ func (ck *Clerk) Get(key string) string { // arguments. and reply must be passed as a pointer. func (ck *Clerk) PutAppend(key string, value string, op string) string { // You will have to modify this function. - return "" + args := PutAppendArgs{ + Key: key, + Value: value, + } + reply := PutAppendReply{} + ok := ck.server.Call("KVServer."+op, &args, &reply) + + if !ok { + log.Fatalf("RPC call failed: %s op", op) + } + + return reply.Value } func (ck *Clerk) Put(key string, value string) { diff --git a/src/kvsrv/server.go b/src/kvsrv/server.go index 6d0841e..c726e83 100644 --- a/src/kvsrv/server.go +++ b/src/kvsrv/server.go @@ -5,7 +5,7 @@ import ( "sync" ) -const Debug = false +const Debug = true func DPrintf(format string, a ...interface{}) (n int, err error) { if Debug { @@ -14,30 +14,46 @@ func DPrintf(format string, a ...interface{}) (n int, err error) { return } - type KVServer struct { - mu sync.Mutex - + mu sync.Mutex + KVStore map[string]string // Your definitions here. } - func (kv *KVServer) Get(args *GetArgs, reply *GetReply) { // Your code here. + kv.mu.Lock() + defer kv.mu.Unlock() + + reply.Value = kv.KVStore[args.Key] } func (kv *KVServer) Put(args *PutAppendArgs, reply *PutAppendReply) { // Your code here. + kv.mu.Lock() + defer kv.mu.Unlock() + + kv.KVStore[args.Key] = args.Value + reply.Value = args.Value } func (kv *KVServer) Append(args *PutAppendArgs, reply *PutAppendReply) { // Your code here. + kv.mu.Lock() + defer kv.mu.Unlock() + + val, exists := kv.KVStore[args.Key] + if !exists { + kv.KVStore[args.Key] = args.Value + reply.Value = "" + } else { + kv.KVStore[args.Key] = val + args.Value + reply.Value = val + } } func StartKVServer() *KVServer { kv := new(KVServer) - - // You may need initialization code here. - + kv.KVStore = map[string]string{} return kv }