Skip to main content

operator/
batcher.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::sync::Arc;
16
17use arrow::record_batch::RecordBatch;
18use async_trait::async_trait;
19use session::context::QueryContextRef;
20use table::metadata::TableInfoRef;
21use tokio::sync::OwnedSemaphorePermit;
22
23use crate::error::Result;
24
25/// Accepts prepared table writes without coupling the inserter to a batcher implementation.
26/// Schema creation, alteration and default evaluation remain the caller's responsibility.
27#[async_trait]
28pub trait PendingRowsBatcher: Send + Sync {
29    /// Acquires one slot per original request, shared by all of its table submissions.
30    async fn acquire(&self) -> Result<Arc<OwnedSemaphorePermit>>;
31
32    /// Submits rows according to the acknowledgement policy, retaining the slot until
33    /// writing completes even when the response acknowledges queue admission only.
34    /// Cancelling the response wait does not retract an already enqueued submission.
35    async fn submit(
36        &self,
37        table_info: TableInfoRef,
38        batch: RecordBatch,
39        ctx: QueryContextRef,
40        permit: Arc<OwnedSemaphorePermit>,
41    ) -> Result<usize>;
42}