package kvsrv
import"6.5840/labrpc"import"crypto/rand"import"math/big"type Clerk struct{
server *labrpc.ClientEnd
// You will have to modify this struct.}funcnrand()int64{
max := big.NewInt(int64(1)<<62)
bigx,_:= rand.Int(rand.Reader, max)
x := bigx.Int64()return x
}funcMakeClerk(server *labrpc.ClientEnd)*Clerk {
ck :=new(Clerk)
ck.server = server
// You'll have to add code here.return ck
}// fetch the current value for a key.// returns "" if the key does not exist.// keeps trying forever in the face of all other errors.//// you can send an RPC with code like this:// ok := ck.server.Call("KVServer.Get", &args, &reply)//// the types of args and reply (including whether they are pointers)// 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{// You will have to modify this function.
args := GetArgs{}
args.Key = key
reply := GetReply{}for!ck.server.Call("KVServer.Get",&args,&reply){}return reply.Value
}// shared by Put and Append.//// you can send an RPC with code like this:// ok := ck.server.Call("KVServer."+op, &args, &reply)//// the types of args and reply (including whether they are pointers)// must match the declared types of the RPC handler function's// 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.
args := PutAppendArgs{}
args.Key = key
args.Value = value
args.MessageId =nrand()
args.Report =false
reply := PutAppendReply{}for!ck.server.Call("KVServer."+ op,&args,&reply){}
args.Report =true
retval := reply.Value
for!ck.server.Call("KVServer."+ op,&args,&reply){}return retval
}func(ck *Clerk)Put(key string, value string){
ck.PutAppend(key, value,"Put")}// Append value to key's value and return that valuefunc(ck *Clerk)Append(key string, value string)string{return ck.PutAppend(key, value,"Append")}
common.go
package kvsrv
// Put or Appendtype PutAppendArgs struct{
Key string
Value string// You'll have to add definitions here.// Field names must start with capital letters,// otherwise RPC will break.
MessageId int64
Report bool}type PutAppendReply struct{
Value string}type GetArgs struct{
Key string// You'll have to add definitions here.}type GetReply struct{
Value string}