mito2/sst/
location.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
15use object_store::util;
16use store_api::metric_engine_consts::{DATA_REGION_SUBDIR, METADATA_REGION_SUBDIR};
17use store_api::path_utils::region_name;
18use store_api::region_request::PathType;
19use store_api::storage::RegionId;
20
21use crate::sst::file::RegionFileId;
22
23/// Generate region dir from table_dir, region_id and path_type
24pub fn region_dir_from_table_dir(
25    table_dir: &str,
26    region_id: RegionId,
27    path_type: PathType,
28) -> String {
29    let region_name = region_name(region_id.table_id(), region_id.region_sequence());
30    let base_region_dir = util::join_dir(table_dir, &region_name);
31
32    match path_type {
33        PathType::Bare => base_region_dir,
34        PathType::Data => util::join_dir(&base_region_dir, DATA_REGION_SUBDIR),
35        PathType::Metadata => util::join_dir(&base_region_dir, METADATA_REGION_SUBDIR),
36    }
37}
38
39pub fn sst_file_path(table_dir: &str, region_file_id: RegionFileId, path_type: PathType) -> String {
40    let region_dir = region_dir_from_table_dir(table_dir, region_file_id.region_id(), path_type);
41    util::join_path(
42        &region_dir,
43        &format!("{}.parquet", region_file_id.file_id()),
44    )
45}
46
47pub fn index_file_path(
48    table_dir: &str,
49    region_file_id: RegionFileId,
50    path_type: PathType,
51) -> String {
52    let region_dir = region_dir_from_table_dir(table_dir, region_file_id.region_id(), path_type);
53    let index_dir = util::join_dir(&region_dir, "index");
54    util::join_path(&index_dir, &format!("{}.puffin", region_file_id.file_id()))
55}
56
57#[cfg(test)]
58mod tests {
59    use store_api::storage::RegionId;
60
61    use super::*;
62    use crate::sst::file::FileId;
63
64    #[test]
65    fn test_sst_file_path() {
66        let file_id = FileId::random();
67        let region_file_id = RegionFileId::new(RegionId::new(1, 2), file_id);
68        assert_eq!(
69            sst_file_path("table_dir", region_file_id, PathType::Bare),
70            format!("table_dir/1_0000000002/{}.parquet", file_id)
71        );
72        assert_eq!(
73            sst_file_path("table_dir", region_file_id, PathType::Data),
74            format!("table_dir/1_0000000002/data/{}.parquet", file_id)
75        );
76        assert_eq!(
77            sst_file_path("table_dir", region_file_id, PathType::Metadata),
78            format!("table_dir/1_0000000002/metadata/{}.parquet", file_id)
79        );
80    }
81
82    #[test]
83    fn test_index_file_path() {
84        let file_id = FileId::random();
85        let region_file_id = RegionFileId::new(RegionId::new(1, 2), file_id);
86        assert_eq!(
87            index_file_path("table_dir", region_file_id, PathType::Bare),
88            format!("table_dir/1_0000000002/index/{}.puffin", file_id)
89        );
90        assert_eq!(
91            index_file_path("table_dir", region_file_id, PathType::Data),
92            format!("table_dir/1_0000000002/data/index/{}.puffin", file_id)
93        );
94        assert_eq!(
95            index_file_path("table_dir", region_file_id, PathType::Metadata),
96            format!("table_dir/1_0000000002/metadata/index/{}.puffin", file_id)
97        );
98    }
99}