forked from tsuna/gohbase
/
get.go
161 lines (143 loc) · 4.04 KB
/
get.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
// Copyright (C) 2015 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 (
"context"
"github.com/baiweiguo/gohbase/pb"
"google.golang.org/protobuf/proto"
)
// Get represents a Get HBase call.
type Get struct {
base
baseQuery
// Don't return any KeyValue, just say whether the row key exists in the
// table or not.
existsOnly bool
skipbatch bool
}
// baseGet returns a Get struct with default values set.
func baseGet(ctx context.Context, table []byte, key []byte,
options ...func(Call) error) (*Get, error) {
g := &Get{
base: base{
key: key,
table: table,
ctx: ctx,
resultch: make(chan RPCResult, 1),
},
baseQuery: newBaseQuery(),
}
err := applyOptions(g, options...)
if err != nil {
return nil, err
}
return g, nil
}
// NewGet creates a new Get request for the given table and row key.
func NewGet(ctx context.Context, table, key []byte,
options ...func(Call) error) (*Get, error) {
return baseGet(ctx, table, key, options...)
}
// NewGetStr creates a new Get request for the given table and row key.
func NewGetStr(ctx context.Context, table, key string,
options ...func(Call) error) (*Get, error) {
return NewGet(ctx, []byte(table), []byte(key), options...)
}
// Name returns the name of this RPC call.
func (g *Get) Name() string {
return "Get"
}
// Description returns the description of this RPC call.
func (g *Get) Description() string {
return g.Name()
}
// SkipBatch returns true if the Get request shouldn't be batched,
// but should be sent to Region Server right away.
func (g *Get) SkipBatch() bool {
return g.skipbatch
}
func (g *Get) setSkipBatch(v bool) {
g.skipbatch = v
}
// ExistsOnly makes this Get request not return any KeyValue, merely whether
// or not the given row key exists in the table.
func (g *Get) ExistsOnly() {
g.existsOnly = true
}
// ToProto converts this RPC into a protobuf message.
func (g *Get) ToProto() proto.Message {
get := &pb.GetRequest{
Region: g.regionSpecifier(),
Get: &pb.Get{
Row: g.key,
Column: familiesToColumn(g.families),
TimeRange: &pb.TimeRange{},
},
}
/* added support for limit number of cells per row */
if g.storeLimit != DefaultMaxResultsPerColumnFamily {
get.Get.StoreLimit = &g.storeLimit
}
if g.storeOffset != 0 {
get.Get.StoreOffset = &g.storeOffset
}
if g.maxVersions != DefaultMaxVersions {
get.Get.MaxVersions = &g.maxVersions
}
if g.fromTimestamp != MinTimestamp {
get.Get.TimeRange.From = &g.fromTimestamp
}
if g.toTimestamp != MaxTimestamp {
get.Get.TimeRange.To = &g.toTimestamp
}
if g.existsOnly {
get.Get.ExistenceOnly = proto.Bool(true)
}
if g.cacheBlocks != DefaultCacheBlocks {
get.Get.CacheBlocks = &g.cacheBlocks
}
if g.consistency != DefaultConsistency {
get.Get.Consistency = g.consistency.toProto()
}
get.Get.Filter = g.filter
return get
}
// NewResponse creates an empty protobuf message to read the response of this
// RPC.
func (g *Get) NewResponse() proto.Message {
return &pb.GetResponse{}
}
// DeserializeCellBlocks deserializes get result from cell blocks
func (g *Get) DeserializeCellBlocks(m proto.Message, b []byte) (uint32, error) {
resp := m.(*pb.GetResponse)
if resp.Result == nil {
// TODO: is this possible?
return 0, nil
}
cells, read, err := deserializeCellBlocks(b, uint32(resp.Result.GetAssociatedCellCount()))
if err != nil {
return 0, err
}
resp.Result.Cell = append(resp.Result.Cell, cells...)
return read, nil
}
// familiesToColumn takes a map from strings to lists of strings, and converts
// them into protobuf Columns
func familiesToColumn(families map[string][]string) []*pb.Column {
cols := make([]*pb.Column, len(families))
counter := 0
for family, qualifiers := range families {
bytequals := make([][]byte, len(qualifiers))
for i, qual := range qualifiers {
bytequals[i] = []byte(qual)
}
cols[counter] = &pb.Column{
Family: []byte(family),
Qualifier: bytequals,
}
counter++
}
return cols
}