This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathput.go
94 lines (77 loc) · 2.22 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
93
94
package hbase
import (
pb "github.com/golang/protobuf/proto"
"github.com/lazyshot/go-hbase/proto"
"bytes"
)
type Put struct {
key []byte
families [][]byte
qualifiers [][][]byte
values [][][]byte
timestamp [][]int64
}
func CreateNewPut(key []byte) *Put {
return &Put{
key: key,
families: make([][]byte, 0),
qualifiers: make([][][]byte, 0),
values: make([][][]byte, 0),
timestamp: make([][]int64, 0),
}
}
func (this *Put) AddValue(family, column, value []byte) {
this.AddValueTS(family, column, value, 0)
}
// AddValueTS use user specified timestamp
func (this *Put) AddValueTS(family, column, value []byte, ts int64) {
pos := this.posOfFamily(family)
if pos == -1 {
this.families = append(this.families, family)
this.qualifiers = append(this.qualifiers, make([][]byte, 0))
this.values = append(this.values, make([][]byte, 0))
this.timestamp = append(this.timestamp, make([]int64, 0))
pos = this.posOfFamily(family)
}
this.qualifiers[pos] = append(this.qualifiers[pos], column)
this.values[pos] = append(this.values[pos], value)
this.timestamp[pos] = append(this.timestamp[pos], ts)
}
func (this *Put) AddStringValue(family, column, value string) {
this.AddValueTS([]byte(family), []byte(column), []byte(value), 0)
}
// AddStringValueTS use user specified timestamp
func (this *Put) AddStringValueTS(family, column, value string, ts int64) {
this.AddValueTS([]byte(family), []byte(column), []byte(value), ts)
}
func (this *Put) posOfFamily(family []byte) int {
for p, v := range this.families {
if bytes.Equal(family, v) {
return p
}
}
return -1
}
func (this *Put) toProto() pb.Message {
p := &proto.MutationProto{
Row: this.key,
MutateType: proto.MutationProto_PUT.Enum(),
}
for i, family := range this.families {
cv := &proto.MutationProto_ColumnValue{
Family: family,
}
for j, _ := range this.qualifiers[i] {
qv := &proto.MutationProto_ColumnValue_QualifierValue{
Qualifier: this.qualifiers[i][j],
Value: this.values[i][j],
}
if this.timestamp[i][j] > 0 {
qv.Timestamp = pb.Uint64(uint64(this.timestamp[i][j]))
}
cv.QualifierValue = append(cv.QualifierValue, qv)
}
p.ColumnValue = append(p.ColumnValue, cv)
}
return p
}