Skip to main content

mito2/sst/range_index/
deleter.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Direct deletion of per-SST range index files.
16
17use object_store::{ErrorKind, ObjectStore};
18use snafu::ResultExt;
19use store_api::storage::{FileId, RegionId};
20
21use crate::error::{OpenDalSnafu, Result};
22use crate::metrics::SERIES_INDEX_FILE_OPERATION_TOTAL;
23
24/// Deletes range indexes belonging to one region, independently of SST garbage collection.
25#[derive(Debug, Clone)]
26pub struct RangeIndexDeleter {
27    store: ObjectStore,
28    region_id: RegionId,
29}
30
31impl RangeIndexDeleter {
32    /// Creates a deleter using the owning region ID, including for imported SSTs.
33    pub fn new(store: ObjectStore, region_id: RegionId) -> Self {
34        Self { store, region_id }
35    }
36
37    /// Deletes the range index directly from the index store.
38    pub async fn delete(&self, file_id: FileId) -> Result<()> {
39        let path = range_index_path(self.region_id, file_id);
40        let result = match self.store.delete(&path).await {
41            Ok(()) => Ok(()),
42            Err(error) if error.kind() == ErrorKind::NotFound => Ok(()),
43            Err(error) => Err(error).context(OpenDalSnafu),
44        };
45        SERIES_INDEX_FILE_OPERATION_TOTAL
46            .with_label_values(&[
47                "range",
48                "delete",
49                if result.is_ok() { "success" } else { "failure" },
50            ])
51            .inc();
52        result
53    }
54}
55
56pub(crate) fn range_index_path(region_id: RegionId, file_id: FileId) -> String {
57    format!("{}/range/{file_id}.parquet", region_id.as_u64())
58}