forked from etcd-io/etcd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kv.go
143 lines (119 loc) · 3.96 KB
/
kv.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2016 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package grpcproxy
import (
"github.com/coreos/etcd/clientv3"
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
"github.com/coreos/etcd/proxy/grpcproxy/cache"
"golang.org/x/net/context"
)
type kvProxy struct {
c *clientv3.Client
cache cache.Cache
}
func NewKvProxy(c *clientv3.Client) *kvProxy {
return &kvProxy{
c: c,
cache: cache.NewCache(cache.DefaultMaxEntries),
}
}
func (p *kvProxy) Range(ctx context.Context, r *pb.RangeRequest) (*pb.RangeResponse, error) {
// if request set Serializable, serve it from local cache first
if r.Serializable {
if resp, err := p.cache.Get(r); err == nil || err == cache.ErrCompacted {
return resp, err
}
}
resp, err := p.c.Do(ctx, RangeRequestToOp(r))
if err != nil {
p.cache.Add(r, (*pb.RangeResponse)(resp.Get()))
}
return (*pb.RangeResponse)(resp.Get()), err
}
func (p *kvProxy) Put(ctx context.Context, r *pb.PutRequest) (*pb.PutResponse, error) {
resp, err := p.c.Do(ctx, PutRequestToOp(r))
return (*pb.PutResponse)(resp.Put()), err
}
func (p *kvProxy) DeleteRange(ctx context.Context, r *pb.DeleteRangeRequest) (*pb.DeleteRangeResponse, error) {
resp, err := p.c.Do(ctx, DelRequestToOp(r))
return (*pb.DeleteRangeResponse)(resp.Del()), err
}
func (p *kvProxy) Txn(ctx context.Context, r *pb.TxnRequest) (*pb.TxnResponse, error) {
txn := p.c.Txn(ctx)
cmps := make([]clientv3.Cmp, len(r.Compare))
thenops := make([]clientv3.Op, len(r.Success))
elseops := make([]clientv3.Op, len(r.Failure))
for i := range r.Compare {
cmps[i] = (clientv3.Cmp)(*r.Compare[i])
}
for i := range r.Success {
thenops[i] = requestOpToOp(r.Success[i])
}
for i := range r.Failure {
elseops[i] = requestOpToOp(r.Failure[i])
}
resp, err := txn.If(cmps...).Then(thenops...).Else(elseops...).Commit()
return (*pb.TxnResponse)(resp), err
}
func (p *kvProxy) Close() error {
return p.c.Close()
}
func requestOpToOp(union *pb.RequestOp) clientv3.Op {
switch tv := union.Request.(type) {
case *pb.RequestOp_RequestRange:
if tv.RequestRange != nil {
return RangeRequestToOp(tv.RequestRange)
}
case *pb.RequestOp_RequestPut:
if tv.RequestPut != nil {
return PutRequestToOp(tv.RequestPut)
}
case *pb.RequestOp_RequestDeleteRange:
if tv.RequestDeleteRange != nil {
return DelRequestToOp(tv.RequestDeleteRange)
}
}
panic("unknown request")
}
func RangeRequestToOp(r *pb.RangeRequest) clientv3.Op {
opts := []clientv3.OpOption{}
if len(r.RangeEnd) != 0 {
opts = append(opts, clientv3.WithRange(string(r.RangeEnd)))
}
opts = append(opts, clientv3.WithRev(r.Revision))
opts = append(opts, clientv3.WithLimit(r.Limit))
opts = append(opts, clientv3.WithSort(
clientv3.SortTarget(r.SortTarget),
clientv3.SortOrder(r.SortOrder)),
)
if r.Serializable {
opts = append(opts, clientv3.WithSerializable())
}
return clientv3.OpGet(string(r.Key), opts...)
}
func PutRequestToOp(r *pb.PutRequest) clientv3.Op {
opts := []clientv3.OpOption{}
opts = append(opts, clientv3.WithLease(clientv3.LeaseID(r.Lease)))
return clientv3.OpPut(string(r.Key), string(r.Value), opts...)
}
func DelRequestToOp(r *pb.DeleteRangeRequest) clientv3.Op {
opts := []clientv3.OpOption{}
if len(r.RangeEnd) != 0 {
opts = append(opts, clientv3.WithRange(string(r.RangeEnd)))
}
return clientv3.OpDelete(string(r.Key), opts...)
}
func (p *kvProxy) Compact(ctx context.Context, r *pb.CompactionRequest) (*pb.CompactionResponse, error) {
panic("unimplemented")
}