Skip to main content

lance_index/
frag_reuse.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright The Lance Authors
3
4//! `Index`-trait adapter for the fragment-reuse system index.
5//!
6//! The data structures and table-format logic live in
7//! [`lance_table::system_index::frag_reuse`]; this module re-exports them and
8//! provides newtype wrappers that implement the [`Index`] and [`RowIdRemapper`]
9//! traits.
10
11use std::any::Any;
12use std::sync::Arc;
13
14use arrow_array::RecordBatch;
15use async_trait::async_trait;
16use lance_core::Result;
17use lance_core::deepsize::DeepSizeOf;
18use lance_select::RowAddrTreeMap;
19use roaring::{RoaringBitmap, RoaringTreemap};
20use serde::Serialize;
21
22pub use lance_table::system_index::frag_reuse::*;
23
24use crate::scalar::RowIdRemapper;
25use crate::{Index, IndexType};
26
27/// Newtype wrapping [`FragReuseIndex`] so that `lance-index` can implement
28/// the `Index` and `RowIdRemapper` traits (orphan rules prevent implementing
29/// them directly in `lance-table`).
30pub struct FragReuseIndexHandle(pub Arc<FragReuseIndex>);
31
32impl std::fmt::Debug for FragReuseIndexHandle {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        f.debug_tuple("FragReuseIndexHandle")
35            .field(&self.0)
36            .finish()
37    }
38}
39
40impl DeepSizeOf for FragReuseIndexHandle {
41    fn deep_size_of_children(&self, context: &mut lance_core::deepsize::Context) -> usize {
42        self.0.deep_size_of_children(context)
43    }
44}
45
46#[derive(Serialize)]
47struct FragReuseStatistics {
48    num_versions: usize,
49}
50
51#[async_trait]
52impl Index for FragReuseIndexHandle {
53    fn as_any(&self) -> &dyn Any {
54        self
55    }
56
57    fn as_index(self: Arc<Self>) -> Arc<dyn Index> {
58        self
59    }
60
61    fn statistics(&self) -> Result<serde_json::Value> {
62        let stats = FragReuseStatistics {
63            num_versions: self.0.details.versions.len(),
64        };
65        serde_json::to_value(stats).map_err(|e| {
66            lance_core::Error::internal(format!(
67                "failed to serialize fragment reuse index statistics: {}",
68                e
69            ))
70        })
71    }
72
73    async fn prewarm(&self) -> Result<()> {
74        Ok(())
75    }
76
77    fn index_type(&self) -> IndexType {
78        IndexType::FragmentReuse
79    }
80
81    async fn calculate_included_frags(&self) -> Result<RoaringBitmap> {
82        unimplemented!()
83    }
84}
85
86impl RowIdRemapper for FragReuseIndexHandle {
87    fn remap_row_id(&self, row_id: u64) -> Option<u64> {
88        self.0.remap_row_id(row_id)
89    }
90
91    fn remap_row_addrs_tree_map(&self, row_addrs: &RowAddrTreeMap) -> RowAddrTreeMap {
92        self.0.remap_row_addrs_tree_map(row_addrs)
93    }
94
95    fn remap_row_ids_roaring_tree_map(&self, row_ids: &RoaringTreemap) -> RoaringTreemap {
96        self.0.remap_row_ids_roaring_tree_map(row_ids)
97    }
98
99    fn remap_row_ids_record_batch(
100        &self,
101        batch: RecordBatch,
102        row_id_idx: usize,
103    ) -> Result<RecordBatch> {
104        self.0.remap_row_ids_record_batch(batch, row_id_idx)
105    }
106}