forked from IBM/sarama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_topics_request.go
202 lines (160 loc) · 3.83 KB
/
create_topics_request.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package sarama
type ReplicaAssignment struct {
PartitionID int32
Replicas []int32
}
type ConfigKV struct {
Key string
Value string
}
type CreateTopicRequest struct {
Topic string
NumPartitions int32
ReplicationFactor int16
ReplicaAssignments []ReplicaAssignment
Configs []ConfigKV
}
type CreateTopicsRequest struct {
CreateRequests []CreateTopicRequest
Timeout int32
}
func (cr *CreateTopicRequest) encode(pe packetEncoder) error {
if err := pe.putString(cr.Topic); err != nil {
return err
}
pe.putInt32(cr.NumPartitions)
pe.putInt16(cr.ReplicationFactor)
if err := pe.putArrayLength(len(cr.ReplicaAssignments)); err != nil {
return err
}
for i := range cr.ReplicaAssignments {
replicaAssignment := cr.ReplicaAssignments[i]
pe.putInt32(replicaAssignment.PartitionID)
if err := pe.putArrayLength(len(replicaAssignment.Replicas)); err != nil {
return err
}
for j := range replicaAssignment.Replicas {
pe.putInt32(replicaAssignment.Replicas[j])
}
}
if err := pe.putArrayLength(len(cr.Configs)); err != nil {
return err
}
for i := range cr.Configs {
if err := pe.putString(cr.Configs[i].Key); err != nil {
return err
}
if err := pe.putString(cr.Configs[i].Value); err != nil {
return err
}
}
return nil
}
func (cr *CreateTopicRequest) decode(pd packetDecoder, version int16) error {
topic, err := pd.getString()
if err != nil {
return err
}
cr.Topic = topic
numPartitions, err := pd.getInt32()
if err != nil {
return err
}
cr.NumPartitions = numPartitions
replicationFactor, err := pd.getInt16()
if err != nil {
return err
}
cr.ReplicationFactor = replicationFactor
partitionCount, err := pd.getArrayLength()
if err != nil {
return err
}
if partitionCount > 0 {
cr.ReplicaAssignments = make([]ReplicaAssignment, partitionCount)
for i := range cr.ReplicaAssignments {
replicaAssignment := ReplicaAssignment{}
partitionID, err := pd.getInt32()
if err != nil {
return err
}
replicaAssignment.PartitionID = partitionID
replicaCount, err := pd.getArrayLength()
if err != nil {
return err
}
replicaAssignment.Replicas = make([]int32, replicaCount)
for j := range replicaAssignment.Replicas {
replica, err := pd.getInt32()
if err != nil {
return err
}
replicaAssignment.Replicas[j] = replica
}
cr.ReplicaAssignments[i] = replicaAssignment
}
}
configCount, err := pd.getArrayLength()
if err != nil {
return err
}
if configCount > 0 {
cr.Configs = make([]ConfigKV, configCount)
for i := range cr.Configs {
key, err := pd.getString()
if err != nil {
return err
}
value, err := pd.getString()
if err != nil {
return err
}
cr.Configs[i] = ConfigKV{Key: key, Value: value}
}
}
return nil
}
func (ct *CreateTopicsRequest) encode(pe packetEncoder) error {
if err := pe.putArrayLength(len(ct.CreateRequests)); err != nil {
return err
}
for i := range ct.CreateRequests {
if err := ct.CreateRequests[i].encode(pe); err != nil {
return err
}
}
pe.putInt32(ct.Timeout)
return nil
}
func (ct *CreateTopicsRequest) decode(pd packetDecoder, version int16) error {
createTopicRequestCount, err := pd.getArrayLength()
if err != nil {
return err
}
if createTopicRequestCount == 0 {
return nil
}
ct.CreateRequests = make([]CreateTopicRequest, createTopicRequestCount)
for i := range ct.CreateRequests {
ct.CreateRequests[i] = CreateTopicRequest{}
err = ct.CreateRequests[i].decode(pd, version)
if err != nil {
return err
}
}
timeout, err := pd.getInt32()
if err != nil {
return err
}
ct.Timeout = timeout
return nil
}
func (ct *CreateTopicsRequest) key() int16 {
return 19
}
func (ct *CreateTopicsRequest) version() int16 {
return 0
}
func (ct *CreateTopicsRequest) requiredVersion() KafkaVersion {
return V0_10_0_0
}