-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.go
189 lines (151 loc) · 4.94 KB
/
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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package multiraftbase
import (
"fmt"
"github.com/gogo/protobuf/proto"
)
type Method int
//go:generate stringer -type=Method
const (
// Get fetches the value for a key from the KV map, respecting a
// possibly historical timestamp. If the timestamp is 0, returns
// the most recent value.
Get Method = iota
// Put sets the value for a key at the specified timestamp. If the
// timestamp is 0, the value is set with the current time as timestamp.
Put
// TruncateLog discards a prefix of the raft log.
TruncateLog
// HasKey check that the key exist
HasKey
// Delete removes the value for the specified key.
Delete
// change raft conf
ChangeConf
)
// Request is an interface for RPC requests.
type Request interface {
proto.Message
// Method returns the request method.
Method() Method
// ShallowCopy returns a shallow copy of the receiver.
ShallowCopy() Request
flags() int
}
// Method implements the Request interface.
func (*GetRequest) Method() Method { return Get }
// ShallowCopy implements the Request interface.
func (gr *GetRequest) ShallowCopy() Request {
shallowCopy := *gr
return &shallowCopy
}
func (*GetRequest) flags() int { return 0 }
// Method implements the Request interface.
func (*PutRequest) Method() Method { return Put }
// ShallowCopy implements the Request interface.
func (pr *PutRequest) ShallowCopy() Request {
shallowCopy := *pr
return &shallowCopy
}
func (*PutRequest) flags() int { return 0 }
// Method implements the Request interface.
func (*DeleteRequest) Method() Method { return Delete }
// ShallowCopy implements the Request interface.
func (gr *DeleteRequest) ShallowCopy() Request {
shallowCopy := *gr
return &shallowCopy
}
func (*DeleteRequest) flags() int { return 0 }
// Method implements the Request interface.
func (*TruncateLogRequest) Method() Method { return TruncateLog }
// ShallowCopy implements the Request interface.
func (tr *TruncateLogRequest) ShallowCopy() Request {
shallowCopy := *tr
return &shallowCopy
}
func (*TruncateLogRequest) flags() int { return 0 }
// Method implements the Request interface.
func (*ChangeConfRequest) Method() Method { return ChangeConf }
// ShallowCopy implements the Request interface.
func (tr *ChangeConfRequest) ShallowCopy() Request {
shallowCopy := *tr
return &shallowCopy
}
func (*ChangeConfRequest) flags() int { return 0 }
func (*HasKeyRequest) flags() int { return 0 }
// Method implements the Request interface.
func (*HasKeyRequest) Method() Method { return HasKey }
// ShallowCopy implements the Request interface.
func (tr *HasKeyRequest) ShallowCopy() Request {
shallowCopy := *tr
return &shallowCopy
}
// NewGet returns a Request initialized to get the value at key.
func NewGet(key Key) Request {
return &GetRequest{
Key: key,
}
}
// NewPut returns a Request initialized to put the value at key.
func NewPut(key Key, value Value) Request {
return &PutRequest{
Key: key,
Value: value,
Size_: uint64(len(value)),
}
}
// GetInner returns the Request contained in the union.
func (ru RequestUnion) GetInner() Request {
return ru.GetValue().(Request)
}
// GetInner returns the Response contained in the union.
func (ru ResponseUnion) GetInner() Response {
return ru.GetValue().(Response)
}
// MustSetInner sets the Request contained in the union. It panics if the
// request is not recognized by the union type. The RequestUnion is reset
// before being repopulated.
func (ru *ResponseUnion) MustSetInner(args Response) {
ru.Reset()
if !ru.SetValue(args) {
panic(fmt.Sprintf("%T excludes %T", ru, args))
}
}
// MustSetInner sets the Request contained in the union. It panics if the
// request is not recognized by the union type. The RequestUnion is reset
// before being repopulated.
func (ru *RequestUnion) MustSetInner(args Request) {
ru.Reset()
if !ru.SetValue(args) {
panic(fmt.Sprintf("%T excludes %T", ru, args))
}
}
// Response is an interface for RPC result.
type Response interface {
proto.Message
// Method returns the request method.
Method() Method
// Header returns the response header.
Header() ResponseHeader
// SetHeader sets the response header.
SetHeader(ResponseHeader)
}
// Method implements the Request interface.
func (*GetResponse) Method() Method { return Get }
// Method implements the Request interface.
func (*PutResponse) Method() Method { return Put }
// Method implements the Request interface.
func (*HasKeyResponse) Method() Method { return HasKey }
// Method implements the Request interface.
func (*DeleteResponse) Method() Method { return Delete }
// Method implements the Request interface.
func (*TruncateLogResponse) Method() Method { return TruncateLog }
// Method implements the Request interface.
func (*ChangeConfResponse) Method() Method { return ChangeConf }
// Header implements the Response interface for ResponseHeader.
func (rh ResponseHeader) Header() ResponseHeader {
return rh
}
// SetHeader implements the Response interface.
func (rh *ResponseHeader) SetHeader(other ResponseHeader) {
*rh = other
}