datanode/store/
fs.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 std::{fs, path};
16
17use common_telemetry::info;
18use object_store::services::Fs;
19use object_store::util::join_dir;
20use object_store::ObjectStore;
21use snafu::prelude::*;
22
23use crate::config::FileConfig;
24use crate::error::{self, Result};
25use crate::store;
26
27/// A helper function to create a file system object store.
28pub async fn new_fs_object_store(
29    data_home: &str,
30    _file_config: &FileConfig,
31) -> Result<ObjectStore> {
32    fs::create_dir_all(path::Path::new(&data_home))
33        .context(error::CreateDirSnafu { dir: data_home })?;
34    info!("The file storage home is: {}", data_home);
35
36    let atomic_write_dir = join_dir(data_home, ".tmp/");
37    store::clean_temp_dir(&atomic_write_dir)?;
38
39    let builder = Fs::default()
40        .root(data_home)
41        .atomic_write_dir(&atomic_write_dir);
42
43    let object_store = ObjectStore::new(builder)
44        .context(error::InitBackendSnafu)?
45        .finish();
46
47    Ok(object_store)
48}