forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 2
/
call.go
168 lines (154 loc) · 4.14 KB
/
call.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
// Copyright 2014 The Cockroach 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. See the AUTHORS file
// for names of contributors.
//
// Author: Spencer Kimball (spencer.kimball@gmail.com)
package proto
import (
"fmt"
gogoproto "github.com/gogo/protobuf/proto"
)
// A Call is a pending database API call.
type Call struct {
Args Request // The argument to the command
Reply Response // The reply from the command
Post func() error // Function to be called after successful completion
}
// Method returns the method of the database command for the call.
func (c *Call) Method() Method {
return c.Args.Method()
}
// GetCall returns a Call object initialized to get the value at key.
func GetCall(key Key) Call {
return Call{
Args: &GetRequest{
RequestHeader: RequestHeader{
Key: key,
},
},
Reply: &GetResponse{},
}
}
// GetProtoCall returns a Call object initialized to get the value at key and
// then to decode it as a protobuf message.
func GetProtoCall(key Key, msg gogoproto.Message) Call {
c := GetCall(key)
c.Post = func() error {
reply := c.Reply.(*GetResponse)
if reply.Value == nil {
return fmt.Errorf("%s: no value present", key)
}
return gogoproto.Unmarshal(reply.Value.Bytes, msg)
}
return c
}
// IncrementCall returns a Call object initialized to increment the value at
// key by increment.
func IncrementCall(key Key, increment int64) Call {
return Call{
Args: &IncrementRequest{
RequestHeader: RequestHeader{
Key: key,
},
Increment: increment,
},
Reply: &IncrementResponse{},
}
}
// PutCall returns a Call object initialized to put the value at key.
func PutCall(key Key, value Value) Call {
value.InitChecksum(key)
return Call{
Args: &PutRequest{
RequestHeader: RequestHeader{
Key: key,
},
Value: value,
},
Reply: &PutResponse{},
}
}
// ConditionalPutCall returns a Call object initialized to put value as a byte
// slice at key if the existing value at key equals expValueBytes.
func ConditionalPutCall(key Key, valueBytes, expValueBytes []byte) Call {
value := Value{Bytes: valueBytes}
value.InitChecksum(key)
var expValue *Value
if expValueBytes != nil {
expValue = &Value{Bytes: expValueBytes}
expValue.InitChecksum(key)
}
return Call{
Args: &ConditionalPutRequest{
RequestHeader: RequestHeader{
Key: key,
},
Value: value,
ExpValue: expValue,
},
Reply: &ConditionalPutResponse{},
}
}
// DeleteCall returns a Call object initialized to delete the value at key.
func DeleteCall(key Key) Call {
return Call{
Args: &DeleteRequest{
RequestHeader: RequestHeader{
Key: key,
},
},
Reply: &DeleteResponse{},
}
}
// DeleteRangeCall returns a Call object initialized to delete the values in
// the given key range (excluding the endpoint).
func DeleteRangeCall(startKey, endKey Key) Call {
return Call{
Args: &DeleteRangeRequest{
RequestHeader: RequestHeader{
Key: startKey,
EndKey: endKey,
},
},
Reply: &DeleteRangeResponse{},
}
}
// ScanCall returns a Call object initialized to scan from start to end keys
// with max results.
func ScanCall(key, endKey Key, maxResults int64) Call {
return Call{
Args: &ScanRequest{
RequestHeader: RequestHeader{
Key: key,
EndKey: endKey,
},
MaxResults: maxResults,
},
Reply: &ScanResponse{},
}
}
// ReverseScanCall returns a Call object initialized to reverse scan from end to
// start keys with max results.
func ReverseScanCall(key, endKey Key, maxResults int64) Call {
return Call{
Args: &ReverseScanRequest{
RequestHeader: RequestHeader{
Key: key,
EndKey: endKey,
},
MaxResults: maxResults,
},
Reply: &ReverseScanResponse{},
}
}