Skip to main content

lance_index/
progress.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright The Lance Authors
3
4use async_trait::async_trait;
5use lance_core::Result;
6use std::sync::Arc;
7
8/// Progress callback for index building.
9///
10/// Called at stage boundaries during index construction. Stages are sequential:
11/// `stage_complete` is always called before the next `stage_start`, so only one
12/// stage is active at a time. Stage names are index-type-specific (e.g.
13/// "train_ivf", "shuffle", "build_partitions" for vector indices; "load_data",
14/// "build_pages" for scalar indices).
15///
16/// Methods take `&self` to allow concurrent calls from within a single stage.
17/// Implementations must be thread-safe.
18#[async_trait]
19pub trait IndexBuildProgress: std::fmt::Debug + Sync + Send {
20    /// A named stage has started.
21    ///
22    /// `total` is the number of work units if known, and `unit` describes
23    /// what is being counted (e.g. "partitions", "batches", "rows").
24    async fn stage_start(&self, stage: &str, total: Option<u64>, unit: &str) -> Result<()>;
25
26    /// Progress within the current stage.
27    async fn stage_progress(&self, stage: &str, completed: u64) -> Result<()>;
28
29    /// A named stage has completed.
30    async fn stage_complete(&self, stage: &str) -> Result<()>;
31}
32
33#[derive(Debug, Clone, Default)]
34pub struct NoopIndexBuildProgress;
35
36#[async_trait]
37impl IndexBuildProgress for NoopIndexBuildProgress {
38    async fn stage_start(&self, _: &str, _: Option<u64>, _: &str) -> Result<()> {
39        Ok(())
40    }
41    async fn stage_progress(&self, _: &str, _: u64) -> Result<()> {
42        Ok(())
43    }
44    async fn stage_complete(&self, _: &str) -> Result<()> {
45        Ok(())
46    }
47}
48
49/// Helper to create a default noop progress instance.
50pub fn noop_progress() -> Arc<dyn IndexBuildProgress> {
51    Arc::new(NoopIndexBuildProgress)
52}