This commit is contained in:
2025-01-01 18:25:05 +07:00
parent db55e49c10
commit 63706b6685
2 changed files with 51 additions and 14 deletions

View File

@@ -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) {

View File

@@ -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
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
}