forked from IBM/sarama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
produce_response.go
112 lines (94 loc) · 2.12 KB
/
produce_response.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
package sarama
type ProduceResponseBlock struct {
Err KError
Offset int64
}
func (pr *ProduceResponseBlock) decode(pd packetDecoder) (err error) {
tmp, err := pd.getInt16()
if err != nil {
return err
}
pr.Err = KError(tmp)
pr.Offset, err = pd.getInt64()
if err != nil {
return err
}
return nil
}
type ProduceResponse struct {
Blocks map[string]map[int32]*ProduceResponseBlock
}
func (pr *ProduceResponse) decode(pd packetDecoder) (err error) {
numTopics, err := pd.getArrayLength()
if err != nil {
return err
}
pr.Blocks = make(map[string]map[int32]*ProduceResponseBlock, numTopics)
for i := 0; i < numTopics; i++ {
name, err := pd.getString()
if err != nil {
return err
}
numBlocks, err := pd.getArrayLength()
if err != nil {
return err
}
pr.Blocks[name] = make(map[int32]*ProduceResponseBlock, numBlocks)
for j := 0; j < numBlocks; j++ {
id, err := pd.getInt32()
if err != nil {
return err
}
block := new(ProduceResponseBlock)
err = block.decode(pd)
if err != nil {
return err
}
pr.Blocks[name][id] = block
}
}
return nil
}
func (pr *ProduceResponse) encode(pe packetEncoder) error {
err := pe.putArrayLength(len(pr.Blocks))
if err != nil {
return err
}
for topic, partitions := range pr.Blocks {
err = pe.putString(topic)
if err != nil {
return err
}
err = pe.putArrayLength(len(partitions))
if err != nil {
return err
}
for id, prb := range partitions {
pe.putInt32(id)
pe.putInt16(int16(prb.Err))
pe.putInt64(prb.Offset)
}
}
return nil
}
func (pr *ProduceResponse) GetBlock(topic string, partition int32) *ProduceResponseBlock {
if pr.Blocks == nil {
return nil
}
if pr.Blocks[topic] == nil {
return nil
}
return pr.Blocks[topic][partition]
}
// Testing API
func (pr *ProduceResponse) AddTopicPartition(topic string, partition int32, err KError) {
if pr.Blocks == nil {
pr.Blocks = make(map[string]map[int32]*ProduceResponseBlock)
}
byTopic, ok := pr.Blocks[topic]
if !ok {
byTopic = make(map[int32]*ProduceResponseBlock)
pr.Blocks[topic] = byTopic
}
byTopic[partition] = &ProduceResponseBlock{Err: err}
}