kafka_client_engine/admin/partitions/
model.rs1use kafka_client_core::{
4 CreatePartitionsPlan, CreatePartitionsPlanError,
5 CreatePartitionsSpecification as CoreSpecification,
6};
7
8#[derive(Clone, Debug, Eq, PartialEq)]
10pub struct PartitionIncrease {
11 topic: String,
12 total_count: i32,
13 replica_assignments: Option<Vec<Vec<i32>>>,
14}
15
16impl PartitionIncrease {
17 pub fn new(topic: impl Into<String>, total_count: i32) -> Self {
19 Self {
20 topic: topic.into(),
21 total_count,
22 replica_assignments: None,
23 }
24 }
25
26 pub fn with_replica_assignments(
28 topic: impl Into<String>,
29 total_count: i32,
30 replica_assignments: Vec<Vec<i32>>,
31 ) -> Self {
32 Self {
33 topic: topic.into(),
34 total_count,
35 replica_assignments: Some(replica_assignments),
36 }
37 }
38}
39
40#[derive(Clone, Debug, Eq, PartialEq)]
42pub struct CreatePartitionsRequest {
43 topics: Vec<PartitionIncrease>,
44 validate_only: bool,
45}
46
47impl CreatePartitionsRequest {
48 pub const fn new(topics: Vec<PartitionIncrease>) -> Self {
50 Self {
51 topics,
52 validate_only: false,
53 }
54 }
55
56 #[must_use]
58 pub const fn with_validate_only(mut self, validate_only: bool) -> Self {
59 self.validate_only = validate_only;
60 self
61 }
62
63 pub(crate) fn into_plan(self) -> Result<CreatePartitionsPlan, CreatePartitionsPlanError> {
64 CreatePartitionsPlan::new(
65 self.topics
66 .into_iter()
67 .map(|topic| match topic.replica_assignments {
68 Some(assignments) => CoreSpecification::with_replica_assignments(
69 topic.topic,
70 topic.total_count,
71 assignments,
72 ),
73 None => CoreSpecification::new(topic.topic, topic.total_count),
74 })
75 .collect(),
76 self.validate_only,
77 )
78 }
79
80 pub(crate) fn canonicalize(mut self) -> Self {
81 for topic in &mut self.topics {
82 topic.topic = canonical_string(std::mem::take(&mut topic.topic));
83 if let Some(assignments) = &mut topic.replica_assignments {
84 for broker_ids in assignments.iter_mut() {
85 *broker_ids = canonical_vec(std::mem::take(broker_ids));
86 }
87 *assignments = canonical_vec(std::mem::take(assignments));
88 }
89 }
90 self.topics = canonical_vec(self.topics);
91 self
92 }
93
94 pub(crate) fn retained_charge(&self) -> Option<usize> {
95 let mut text_bytes = 0usize;
96 let mut assignment_count = 0usize;
97 let mut broker_id_count = 0usize;
98 for topic in &self.topics {
99 text_bytes = text_bytes.checked_add(topic.topic.len())?;
100 if let Some(assignments) = &topic.replica_assignments {
101 assignment_count = assignment_count.checked_add(assignments.len())?;
102 for broker_ids in assignments {
103 broker_id_count = broker_id_count.checked_add(broker_ids.len())?;
104 }
105 }
106 }
107 crate::admin::retention::request_with_assignments_charge(
108 self.topics.len(),
109 0,
110 assignment_count,
111 broker_id_count,
112 text_bytes,
113 )
114 }
115
116 #[cfg(test)]
117 pub(super) fn storage_is_canonical(&self) -> bool {
118 self.topics.capacity() == self.topics.len()
119 && self.topics.iter().all(|topic| {
120 topic.topic.capacity() == topic.topic.len()
121 && topic
122 .replica_assignments
123 .as_ref()
124 .is_none_or(|assignments| {
125 assignments.capacity() == assignments.len()
126 && assignments
127 .iter()
128 .all(|broker_ids| broker_ids.capacity() == broker_ids.len())
129 })
130 })
131 }
132}
133
134fn canonical_string(value: String) -> String {
135 value.into_boxed_str().into_string()
136}
137
138fn canonical_vec<T>(value: Vec<T>) -> Vec<T> {
139 value.into_boxed_slice().into_vec()
140}