index/external_provider.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 async_trait::async_trait;
16use futures::{AsyncRead, AsyncWrite};
17
18use crate::error::Error;
19
20pub type Writer = Box<dyn AsyncWrite + Unpin + Send>;
21pub type Reader = Box<dyn AsyncRead + Unpin + Send>;
22
23/// Trait for managing intermediate files to control memory usage for a particular index.
24#[mockall::automock]
25#[async_trait]
26pub trait ExternalTempFileProvider: Send + Sync {
27 /// Creates and opens a new intermediate file associated with a specific `file_group` for writing.
28 /// The implementation should ensure that the file does not already exist.
29 ///
30 /// - `file_group`: a unique identifier for the group of files
31 /// - `file_id`: a unique identifier for the new file
32 async fn create(&self, file_group: &str, file_id: &str) -> Result<Writer, Error>;
33
34 /// Retrieves all intermediate files and their associated file identifiers for a specific `file_group`.
35 ///
36 /// `file_group` is a unique identifier for the group of files.
37 async fn read_all(&self, file_group: &str) -> Result<Vec<(String, Reader)>, Error>;
38}