forked from talent-plan/tinykv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathraw_api.go
102 lines (90 loc) · 2.68 KB
/
raw_api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package server
import (
"context"
"github.com/pingcap-incubator/tinykv/kv/storage"
"github.com/pingcap-incubator/tinykv/proto/pkg/kvrpcpb"
)
// The functions below are Server's Raw API. (implements TinyKvServer).
// Some helper methods can be found in sever.go in the current directory
// 上层允许err时返回nil结构体
// RawGet return the corresponding Get response based on RawGetRequest's CF and Key fields
func (server *Server) RawGet(_ context.Context, req *kvrpcpb.RawGetRequest) (*kvrpcpb.RawGetResponse, error) {
// Your Code Here (1).
reader, err := server.storage.Reader(req.Context)
if err != nil {
return nil, err
}
val, err := reader.GetCF(req.Cf, req.Key)
if err != nil {
return nil, err
}
resp := &kvrpcpb.RawGetResponse{
Value: val,
NotFound: false,
}
if val == nil {
resp.NotFound = true
}
return resp, nil
}
// RawPut puts the target data into storage and returns the corresponding response
func (server *Server) RawPut(_ context.Context, req *kvrpcpb.RawPutRequest) (*kvrpcpb.RawPutResponse, error) {
// Your Code Here (1).
// Hint: Consider using Storage.Modify to store data to be modified
put := storage.Put{
Key: req.Key,
Value: req.Value,
Cf: req.Cf,
}
batch := storage.Modify{Data: put}
err := server.storage.Write(req.Context, []storage.Modify{batch})
if err != nil {
return nil, err
}
return &kvrpcpb.RawPutResponse{}, nil
}
// RawDelete delete the target data from storage and returns the corresponding response
func (server *Server) RawDelete(_ context.Context, req *kvrpcpb.RawDeleteRequest) (*kvrpcpb.RawDeleteResponse, error) {
// Your Code Here (1).
// Hint: Consider using Storage.Modify to store data to be deleted
del := storage.Delete{
Key: req.Key,
Cf: req.Cf,
}
batch := storage.Modify{Data: del}
err := server.storage.Write(req.Context, []storage.Modify{batch})
if err != nil {
return nil, err
}
return &kvrpcpb.RawDeleteResponse{}, nil
}
// RawScan scan the data starting from the start key up to limit. and return the corresponding result
func (server *Server) RawScan(_ context.Context, req *kvrpcpb.RawScanRequest) (*kvrpcpb.RawScanResponse, error) {
// Your Code Here (1).
// Hint: Consider using reader.IterCF
reader, err := server.storage.Reader(req.Context)
if err != nil {
return nil, err
}
var Kvs []*kvrpcpb.KvPair
limit := req.Limit
iter := reader.IterCF(req.Cf)
defer iter.Close()
for iter.Seek(req.StartKey); iter.Valid(); iter.Next() {
item := iter.Item()
key := item.Key()
val, _ := item.Value()
Kvs = append(Kvs, &kvrpcpb.KvPair{
Key: key,
Value: val,
})
limit--
if limit == 0 {
break
}
}
resp := &kvrpcpb.RawScanResponse{
Kvs: Kvs,
}
return resp, nil
}