This repository has been archived by the owner on Sep 17, 2021. It is now read-only.
forked from c4pt0r/go-hbase
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathput.go
92 lines (76 loc) · 1.79 KB
/
put.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
package hbase
import (
"bytes"
"math"
pb "github.com/golang/protobuf/proto"
"github.com/pingcap/go-hbase/proto"
)
type Put struct {
Row []byte
Families [][]byte
Qualifiers [][][]byte
Values [][][]byte
Timestamp uint64
}
func NewPut(row []byte) *Put {
return &Put{
Row: row,
Families: make([][]byte, 0),
Qualifiers: make([][][]byte, 0),
Values: make([][][]byte, 0),
}
}
func (p *Put) GetRow() []byte {
return p.Row
}
func (p *Put) AddValue(family, qual, value []byte) *Put {
pos := p.posOfFamily(family)
if pos == -1 {
p.Families = append(p.Families, family)
p.Qualifiers = append(p.Qualifiers, make([][]byte, 0))
p.Values = append(p.Values, make([][]byte, 0))
pos = p.posOfFamily(family)
}
p.Qualifiers[pos] = append(p.Qualifiers[pos], qual)
p.Values[pos] = append(p.Values[pos], value)
return p
}
func (p *Put) AddStringValue(family, column, value string) *Put {
return p.AddValue([]byte(family), []byte(column), []byte(value))
}
func (p *Put) AddTimestamp(ts uint64) *Put {
if ts == 0 {
p.Timestamp = math.MaxInt64
} else {
p.Timestamp = ts
}
return p
}
func (p *Put) posOfFamily(family []byte) int {
for p, v := range p.Families {
if bytes.Equal(family, v) {
return p
}
}
return -1
}
func (p *Put) ToProto() pb.Message {
put := &proto.MutationProto{
Row: p.Row,
MutateType: proto.MutationProto_PUT.Enum(),
}
for i, family := range p.Families {
cv := &proto.MutationProto_ColumnValue{
Family: family,
}
for j := range p.Qualifiers[i] {
cv.QualifierValue = append(cv.QualifierValue, &proto.MutationProto_ColumnValue_QualifierValue{
Qualifier: p.Qualifiers[i][j],
Value: p.Values[i][j],
Timestamp: pb.Uint64(p.Timestamp),
})
}
put.ColumnValue = append(put.ColumnValue, cv)
}
return put
}