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 and distributed index finalization.
9///
10/// Called at stage boundaries during index construction. For a single logical stream, stages are
11/// sequential: `stage_complete` is always called before the next `stage_start`, so only one stage
12/// is active at a time. Callers that orchestrate independent sub-builds in parallel may prefix
13/// stage names (for example `segment_plan[0]/merge_partitions`) to represent separate logical
14/// streams. Stage names are index-type-specific (e.g. "train_ivf", "shuffle", "merge_partitions"
15/// for vector indices; "load_data", "build_pages" for scalar indices; merge/finalization stages
16/// for distributed index construction).
17///
18/// Methods take `&self` to allow concurrent calls from within a single stage. Implementations
19/// must be thread-safe.
20#[async_trait]
21pub trait IndexBuildProgress: std::fmt::Debug + Sync + Send {
22 /// A named stage has started.
23 ///
24 /// `total` is the number of work units if known, and `unit` describes
25 /// what is being counted (e.g. "partitions", "batches", "rows").
26 async fn stage_start(&self, stage: &str, total: Option<u64>, unit: &str) -> Result<()>;
27
28 /// Progress within the current stage.
29 async fn stage_progress(&self, stage: &str, completed: u64) -> Result<()>;
30
31 /// A named stage has completed.
32 async fn stage_complete(&self, stage: &str) -> Result<()>;
33}
34
35#[derive(Debug, Clone, Default)]
36pub struct NoopIndexBuildProgress;
37
38#[async_trait]
39impl IndexBuildProgress for NoopIndexBuildProgress {
40 async fn stage_start(&self, _: &str, _: Option<u64>, _: &str) -> Result<()> {
41 Ok(())
42 }
43 async fn stage_progress(&self, _: &str, _: u64) -> Result<()> {
44 Ok(())
45 }
46 async fn stage_complete(&self, _: &str) -> Result<()> {
47 Ok(())
48 }
49}
50
51/// Helper to create a default noop progress instance.
52pub fn noop_progress() -> Arc<dyn IndexBuildProgress> {
53 Arc::new(NoopIndexBuildProgress)
54}