Skip to content

Commit 082a324

Browse files
committed
Get batch arg, when looking for EndTransaction, just look at the last entry
1 parent fa11dae commit 082a324

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

roachpb/batch.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,20 @@ func (ba *BatchRequest) IsRange() bool {
6060
return (ba.flags() & isRange) != 0
6161
}
6262

63-
// GetArg returns the first request of the given type, if possible.
63+
// GetArg returns the request of the given type.
64+
// If the given type is EndTransaction, just look at the last entry.
65+
// or else, returns the first request which match the given type
6466
func (ba *BatchRequest) GetArg(method Method) (Request, bool) {
65-
// TODO(tschottdorf): when looking for EndTransaction, just look at the
66-
// last entry.
67+
// when looking for EndTransaction, just look at the last entry.
68+
if method == EndTransaction {
69+
if length := len(ba.Requests); length > 0 {
70+
if req := ba.Requests[length-1].GetInner(); req.Method() == EndTransaction {
71+
return req, true
72+
}
73+
}
74+
return nil, false
75+
}
76+
6777
for _, arg := range ba.Requests {
6878
if req := arg.GetInner(); req.Method() == method {
6979
return req, true

roachpb/batch_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2014 The Cockroach Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
// implied. See the License for the specific language governing
13+
// permissions and limitations under the License. See the AUTHORS file
14+
// for names of contributors.
15+
//
16+
// Author: Veteran Lu (23907238@qq.com)
17+
18+
package roachpb
19+
20+
import "testing"
21+
22+
// TestBatchRequestGetArg tests that the method for BatchRequest.GetArg
23+
func TestBatchRequestGetArg(t *testing.T) {
24+
testCases := []struct {
25+
bu []RequestUnion
26+
exp bool
27+
}{
28+
{[]RequestUnion{}, false},
29+
{[]RequestUnion{{Get: &GetRequest{}}}, false},
30+
{[]RequestUnion{{EndTransaction: &EndTransactionRequest{}}, {Get: &GetRequest{}}}, false},
31+
{[]RequestUnion{{EndTransaction: &EndTransactionRequest{}}}, true},
32+
{[]RequestUnion{{Get: &GetRequest{}}, {EndTransaction: &EndTransactionRequest{}}}, true},
33+
}
34+
35+
for i, c := range testCases {
36+
br := BatchRequest{Requests: c.bu}
37+
if _, r := br.GetArg(EndTransaction); r != c.exp {
38+
t.Errorf("%d: unexpected next bytes for %v: %v", i, c.bu, c.exp)
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)