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 pathclient_ops.go
150 lines (115 loc) · 3.28 KB
/
client_ops.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
package hbase
import (
"github.com/lazyshot/go-hbase/proto"
"fmt"
)
func (c *Client) Get(table string, get *Get) (*ResultRow, error) {
ch := c.action([]byte(table), get.key, get, true, 0)
response := <-ch
switch r := response.(type) {
case *proto.GetResponse:
return newResultRow(r.GetResult()), nil
}
return nil, fmt.Errorf("No valid response seen [response: %#v]", response)
}
func (c *Client) AsyncGets(table string, results chan *ResultRow, gets []*Get) {
actions := make([]multiaction, len(gets))
for i, v := range gets {
actions[i] = multiaction{
row: v.key,
action: v,
}
}
ch := c.multiaction([]byte(table), actions, true, 0)
for r := range ch {
switch rs := r.(type) {
case *proto.MultiResponse:
log.Debug("Received region action result [n=%d]", len(rs.GetRegionActionResult()))
for _, v := range rs.GetRegionActionResult() {
log.Debug("Received result or exceptions [n=%d]", len(v.GetResultOrException()))
for _, v2 := range v.GetResultOrException() {
if res := v2.GetResult(); res != nil {
results <- newResultRow(res)
}
}
}
}
}
log.Debug("Finished receiving region action results")
close(results)
}
func (c *Client) Gets(table string, gets []*Get) ([]*ResultRow, error) {
results := make(chan *ResultRow, 100)
go c.AsyncGets(table, results, gets)
tbr := make([]*ResultRow, 0)
for r := range results {
tbr = append(tbr, r)
}
return tbr, nil
}
func (c *Client) Put(table string, put *Put) (bool, error) {
ch := c.action([]byte(table), put.key, put, true, 0)
response := <-ch
switch r := response.(type) {
case *proto.MutateResponse:
return r.GetProcessed(), nil
}
return false, fmt.Errorf("No valid response seen [response: %#v]", response)
}
func (c *Client) Puts(table string, puts []*Put) (bool, error) {
actions := make([]multiaction, len(puts))
for i, v := range puts {
actions[i] = multiaction{
row: v.key,
action: v,
}
}
ch := c.multiaction([]byte(table), actions, true, 0)
for _ = range ch {
}
return true, nil
}
func (c *Client) Delete(table string, del *Delete) (bool, error) {
ch := c.action([]byte(table), del.key, del, true, 0)
response := <-ch
switch r := response.(type) {
case *proto.MutateResponse:
return r.GetProcessed(), nil
}
return false, fmt.Errorf("No valid response seen [response: %#v]", response)
}
func (c *Client) Deletes(table string, dels []*Delete) (bool, error) {
actions := make([]multiaction, len(dels))
for i, v := range dels {
actions[i] = multiaction{
row: v.key,
action: v,
}
}
ch := c.multiaction([]byte(table), actions, true, 0)
for _ = range ch {
}
return true, nil
}
func (c *Client) Scan(table string) *Scan {
return newScan([]byte(table), c)
}
func (c *Client) GetTables() []TableInfo {
res := c.adminAction(&proto.GetTableDescriptorsRequest{})
response := <-res
switch r := response.(type) {
case *proto.GetTableDescriptorsResponse:
tables := make([]TableInfo, len(r.GetTableSchema()))
for i, table := range r.GetTableSchema() {
tables[i] = TableInfo{
TableName: string(table.GetTableName().GetQualifier()),
Families: make([]string, len(table.GetColumnFamilies())),
}
for j, cf := range table.GetColumnFamilies() {
tables[i].Families[j] = string(cf.GetName())
}
}
return tables
}
return nil
}