client/inserter.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::time::Duration;
16
17use api::v1::RowInsertRequests;
18use humantime::format_duration;
19use store_api::mito_engine_options::{APPEND_MODE_KEY, TTL_KEY};
20
21use crate::error::Result;
22
23/// Context holds the catalog and schema information.
24pub struct Context<'a> {
25 /// The catalog name.
26 pub catalog: &'a str,
27 /// The schema name.
28 pub schema: &'a str,
29}
30
31/// Options for insert operations.
32#[derive(Debug, Clone, Copy)]
33pub struct InsertOptions {
34 /// Time-to-live for the inserted data.
35 pub ttl: Duration,
36 /// Whether to use append mode for the insert.
37 pub append_mode: bool,
38}
39
40impl InsertOptions {
41 /// Converts the insert options to a list of key-value string hints.
42 pub fn to_hints(&self) -> Vec<(&'static str, String)> {
43 vec![
44 (TTL_KEY, format_duration(self.ttl).to_string()),
45 (APPEND_MODE_KEY, self.append_mode.to_string()),
46 ]
47 }
48}
49
50/// [`Inserter`] allows different components to share a unified mechanism for inserting data.
51///
52/// An implementation may perform the insert locally (e.g., via a direct procedure call) or
53/// delegate/forward it to another node for processing (e.g., MetaSrv forwarding to an
54/// available Frontend).
55#[async_trait::async_trait]
56pub trait Inserter: Send + Sync {
57 async fn insert_rows(&self, context: &Context<'_>, requests: RowInsertRequests) -> Result<()>;
58
59 fn set_options(&mut self, options: &InsertOptions);
60}