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