Skip to main content

vortex_layout/segments/
sink.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::sync::Arc;
5
6use async_trait::async_trait;
7use vortex_buffer::ByteBuffer;
8use vortex_error::VortexResult;
9
10use crate::segments::SegmentId;
11use crate::sequence::SequenceId;
12
13/// Shared writer-side segment sink.
14pub type SegmentSinkRef = Arc<dyn SegmentSink>;
15
16#[async_trait]
17/// Assigns segment ids and writes segment buffers during layout writing.
18///
19/// Segment sinks are responsible for preserving any ordering guarantees required by the storage
20/// backend. The [`SequenceId`] argument lets sinks serialize id assignment while still allowing
21/// upstream layout strategies to do work concurrently.
22pub trait SegmentSink: Send + Sync {
23    /// Write the given data into a segment, ordered based on the provided sequence identifier.
24    ///
25    /// Implementations of this trait should call [`SequenceId::collapse`] on the provided
26    /// `sequence_id` if they need to ensure that the segment IDs are monotonically increasing.
27    /// While they hold onto the returned `SequenceId`, they can be sure that no other subsequent
28    /// calls to [`SequenceId::collapse`] will complete.
29    ///
30    /// If they do not require ordered segment IDs, for example if segments are stored in
31    /// random-access key/values storage, then the sequence ID can be dropped and the segment
32    /// written immediately.
33    async fn write(
34        &self,
35        sequence_id: SequenceId,
36        buffers: Vec<ByteBuffer>,
37    ) -> VortexResult<SegmentId>;
38}