2 Commits
main ... lab2

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
import "6.5840/labrpc"
import "crypto/rand"
import "math/big"
import (
"crypto/rand"
"log"
"math/big"
"6.5840/labrpc"
"github.com/google/uuid"
)
type Clerk struct {
server *labrpc.ClientEnd
@@ -35,9 +39,21 @@ 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 {
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.
return ""
return reply.Value
}
// 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.
func (ck *Clerk) PutAppend(key string, value string, op string) string {
// 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) {

View File

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

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,47 @@ func DPrintf(format string, a ...interface{}) (n int, err error) {
return
}
type KVServer struct {
mu sync.Mutex
KVStore map[string]string
OpLog map[string]bool
// 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
}