-
Notifications
You must be signed in to change notification settings - Fork 212
/
checkandput.go
71 lines (59 loc) · 1.98 KB
/
checkandput.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
// Copyright (C) 2016 The GoHBase Authors. All rights reserved.
// This file is part of GoHBase.
// Use of this source code is governed by the Apache License 2.0
// that can be found in the COPYING file.
package hrpc
import (
"fmt"
"github.com/tsuna/gohbase/filter"
"github.com/tsuna/gohbase/pb"
"google.golang.org/protobuf/proto"
)
// CheckAndPut performs a provided Put operation if the value specified
// by condition equals to the one set in the HBase.
type CheckAndPut struct {
*Mutate
family []byte
qualifier []byte
comparator *pb.Comparator
}
// NewCheckAndPut creates a new CheckAndPut request that will compare provided
// expectedValue with the on in HBase located at put's row and provided family:qualifier,
// and if they are equal, perform the provided put request on the row
func NewCheckAndPut(put *Mutate, family string,
qualifier string, expectedValue []byte) (*CheckAndPut, error) {
if put.mutationType != pb.MutationProto_PUT {
return nil, fmt.Errorf("'CheckAndPut' only takes 'Put' request")
}
// The condition that needs to match for the edit to be applied.
exp := filter.NewByteArrayComparable(expectedValue)
cmp, err := filter.NewBinaryComparator(exp).ConstructPBComparator()
if err != nil {
return nil, err
}
// CheckAndPut is not batchable as MultiResponse doesn't return Processed field
// for Mutate Action
put.setSkipBatch(true)
return &CheckAndPut{
Mutate: put,
family: []byte(family),
qualifier: []byte(qualifier),
comparator: cmp,
}, nil
}
// ToProto converts the RPC into a protobuf message
func (cp *CheckAndPut) ToProto() proto.Message {
mutateRequest, _, _ := cp.toProto(false, nil)
mutateRequest.Condition = &pb.Condition{
Row: cp.key,
Family: cp.family,
Qualifier: cp.qualifier,
CompareType: pb.CompareType_EQUAL.Enum(),
Comparator: cp.comparator,
}
return mutateRequest
}
func (cp *CheckAndPut) CellBlocksEnabled() bool {
// cellblocks are not supported for check and put request
return false
}