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, TWCS_TIME_WINDOW};
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 /// Time window for twcs compaction.
39 pub twcs_compaction_time_window: Option<Duration>,
40}
41
42impl InsertOptions {
43 /// Converts the insert options to a list of key-value string hints.
44 pub fn to_hints(&self) -> Vec<(&'static str, String)> {
45 let mut hints = vec![
46 (TTL_KEY, format_duration(self.ttl).to_string()),
47 (APPEND_MODE_KEY, self.append_mode.to_string()),
48 ];
49
50 if let Some(time_window) = self.twcs_compaction_time_window {
51 hints.push((TWCS_TIME_WINDOW, format_duration(time_window).to_string()));
52 }
53
54 hints
55 }
56}
57
58/// [`Inserter`] allows different components to share a unified mechanism for inserting data.
59///
60/// An implementation may perform the insert locally (e.g., via a direct procedure call) or
61/// delegate/forward it to another node for processing (e.g., MetaSrv forwarding to an
62/// available Frontend).
63#[async_trait::async_trait]
64pub trait Inserter: Send + Sync {
65 async fn insert_rows(&self, context: &Context<'_>, requests: RowInsertRequests) -> Result<()>;
66
67 fn set_options(&mut self, options: &InsertOptions);
68}