Skip to main content

iggy_cli/commands/binary_segments/
delete_segments.rs

1/* Licensed to the Apache Software Foundation (ASF) under one
2 * or more contributor license agreements.  See the NOTICE file
3 * distributed with this work for additional information
4 * regarding copyright ownership.  The ASF licenses this file
5 * to you under the Apache License, Version 2.0 (the
6 * "License"); you may not use this file except in compliance
7 * with the License.  You may obtain a copy of the License at
8 *
9 *   http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied.  See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18use crate::commands::cli_command::{CliCommand, PRINT_TARGET};
19use anyhow::Context;
20use async_trait::async_trait;
21use iggy_common::Client;
22use iggy_common::Identifier;
23use iggy_common::delete_segments::DeleteSegments;
24use tracing::{Level, event};
25
26pub struct DeleteSegmentsCmd {
27    delete_segments: DeleteSegments,
28}
29
30impl DeleteSegmentsCmd {
31    pub fn new(
32        stream_id: Identifier,
33        topic_id: Identifier,
34        partition_id: u32,
35        segments_count: u32,
36    ) -> Self {
37        Self {
38            delete_segments: DeleteSegments {
39                stream_id,
40                topic_id,
41                partition_id,
42                segments_count,
43            },
44        }
45    }
46}
47
48#[async_trait]
49impl CliCommand for DeleteSegmentsCmd {
50    fn explain(&self) -> String {
51        let mut segments = String::from("segment");
52        if self.delete_segments.segments_count > 1 {
53            segments.push('s');
54        };
55
56        format!(
57            "delete {} {segments} for topic with ID: {}, stream with ID: {} and partition with ID: {}",
58            self.delete_segments.segments_count,
59            self.delete_segments.topic_id,
60            self.delete_segments.stream_id,
61            self.delete_segments.partition_id
62        )
63    }
64
65    async fn execute_cmd(&mut self, client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
66        let mut segments = String::from("segment");
67        if self.delete_segments.segments_count > 1 {
68            segments.push('s');
69        };
70
71        client
72            .delete_segments(
73                &self.delete_segments.stream_id,
74                &self.delete_segments.topic_id,
75                self.delete_segments.partition_id,
76                self.delete_segments.segments_count,
77            )
78            .await
79            .with_context(|| {
80                format!(
81                    "Problem deleting {} {segments} for topic with ID: {}, stream with ID: {} and partition with ID: {}",
82                    self.delete_segments.segments_count,
83                    self.delete_segments.topic_id,
84                    self.delete_segments.stream_id,
85                    self.delete_segments.partition_id
86                )
87            })?;
88
89        event!(target: PRINT_TARGET, Level::INFO,
90            "Deleted {} {segments} for topic with ID: {}, stream with ID: {} and partition with ID: {}",
91            self.delete_segments.segments_count,
92            self.delete_segments.topic_id,
93            self.delete_segments.stream_id,
94            self.delete_segments.partition_id
95        );
96
97        Ok(())
98    }
99}