forked from Lazyshot/go-hbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
call.go
73 lines (62 loc) · 1.63 KB
/
call.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
package hbase
import (
pb "github.com/golang/protobuf/proto"
"github.com/lazyshot/go-hbase/proto"
"reflect"
)
type call struct {
id uint32
methodName string
request pb.Message
responseBuffer pb.Message
responseCh chan pb.Message
}
func newCall(request pb.Message) *call {
var responseBuffer pb.Message
var methodName string
switch request.(type) {
case *proto.GetRequest:
responseBuffer = &proto.GetResponse{}
methodName = "Get"
case *proto.MutateRequest:
responseBuffer = &proto.MutateResponse{}
methodName = "Mutate"
case *proto.MultiRequest:
responseBuffer = &proto.MultiResponse{}
methodName = "Multi"
case *proto.ScanRequest:
responseBuffer = &proto.ScanResponse{}
methodName = "Scan"
case *proto.GetTableDescriptorsRequest:
responseBuffer = &proto.GetTableDescriptorsResponse{}
methodName = "GetTableDescriptors"
}
return &call{
methodName: methodName,
request: request,
responseBuffer: responseBuffer,
responseCh: make(chan pb.Message, 1),
}
}
func (c *call) setid(id uint32) {
c.id = id
}
func (c *call) complete(err error, response []byte) {
log.Debug("Response received [callId=%d] [methodName=%s] [err=%#v] [response_n=%d]", c.id, c.methodName, err, len(response))
defer close(c.responseCh)
if err != nil {
c.responseCh <- &exception{
msg: err.Error(),
}
return
}
err2 := pb.Unmarshal(response, c.responseBuffer)
if err2 != nil {
c.responseCh <- &exception{
msg: err2.Error(),
}
return
}
log.Debug("Response unmarshaled [callId=%d] [type=%s]", c.id, reflect.TypeOf(c.responseBuffer).String())
c.responseCh <- c.responseBuffer
}