Compare commits

..

2 Commits

Author SHA1 Message Date
dffbcad936 cp before pse 2025-01-06 18:51:37 +07:00
63706b6685 cp1 2025-01-01 18:25:05 +07:00
3 changed files with 64 additions and 14 deletions

View File

@@ -1,9 +1,13 @@
package kvsrv package kvsrv
import "6.5840/labrpc" import (
import "crypto/rand" "crypto/rand"
import "math/big" "log"
"math/big"
"6.5840/labrpc"
"github.com/google/uuid"
)
type Clerk struct { type Clerk struct {
server *labrpc.ClientEnd server *labrpc.ClientEnd
@@ -35,9 +39,21 @@ func MakeClerk(server *labrpc.ClientEnd) *Clerk {
// must match the declared types of the RPC handler function's // must match the declared types of the RPC handler function's
// arguments. and reply must be passed as a pointer. // arguments. and reply must be passed as a pointer.
func (ck *Clerk) Get(key string) string { func (ck *Clerk) Get(key string) string {
id := uuid.New()
args := GetArgs{
Key: key,
Uuid: id,
}
reply := GetReply{}
ok := false
for !ok {
ok = ck.server.Call("KVServer.Get", &args, &reply)
if !ok {
log.Println("RPC call failed: GET op")
}
}
// You will have to modify this function. // You will have to modify this function.
return "" return reply.Value
} }
// shared by Put and Append. // shared by Put and Append.
@@ -50,7 +66,22 @@ func (ck *Clerk) Get(key string) string {
// arguments. and reply must be passed as a pointer. // arguments. and reply must be passed as a pointer.
func (ck *Clerk) PutAppend(key string, value string, op string) string { func (ck *Clerk) PutAppend(key string, value string, op string) string {
// You will have to modify this function. // You will have to modify this function.
return "" id := uuid.New()
args := PutAppendArgs{
Key: key,
Value: value,
Uuid: id,
}
reply := PutAppendReply{}
ok := false
for !ok {
ok := ck.server.Call("KVServer."+op, &args, &reply)
if !ok {
log.Printf("RPC call failed: %s op\n", op)
}
}
return reply.Value
} }
func (ck *Clerk) Put(key string, value string) { func (ck *Clerk) Put(key string, value string) {

View File

@@ -7,6 +7,7 @@ type PutAppendArgs struct {
// You'll have to add definitions here. // You'll have to add definitions here.
// Field names must start with capital letters, // Field names must start with capital letters,
// otherwise RPC will break. // otherwise RPC will break.
Uuid string
} }
type PutAppendReply struct { type PutAppendReply struct {
@@ -16,6 +17,7 @@ type PutAppendReply struct {
type GetArgs struct { type GetArgs struct {
Key string Key string
// You'll have to add definitions here. // You'll have to add definitions here.
Uuid string
} }
type GetReply struct { type GetReply struct {

View File

@@ -5,7 +5,7 @@ import (
"sync" "sync"
) )
const Debug = false const Debug = true
func DPrintf(format string, a ...interface{}) (n int, err error) { func DPrintf(format string, a ...interface{}) (n int, err error) {
if Debug { if Debug {
@@ -14,30 +14,47 @@ func DPrintf(format string, a ...interface{}) (n int, err error) {
return return
} }
type KVServer struct { type KVServer struct {
mu sync.Mutex mu sync.Mutex
KVStore map[string]string
OpLog map[string]bool
// Your definitions here. // Your definitions here.
} }
func (kv *KVServer) Get(args *GetArgs, reply *GetReply) { func (kv *KVServer) Get(args *GetArgs, reply *GetReply) {
// Your code here. // Your code here.
kv.mu.Lock()
defer kv.mu.Unlock()
reply.Value = kv.KVStore[args.Key]
} }
func (kv *KVServer) Put(args *PutAppendArgs, reply *PutAppendReply) { func (kv *KVServer) Put(args *PutAppendArgs, reply *PutAppendReply) {
// Your code here. // 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) { func (kv *KVServer) Append(args *PutAppendArgs, reply *PutAppendReply) {
// Your code here. // 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 { func StartKVServer() *KVServer {
kv := new(KVServer) kv := new(KVServer)
kv.KVStore = map[string]string{}
// You may need initialization code here.
return kv return kv
} }