Skip to main content

lance_index/
mem_wal.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright The Lance Authors
3
4//! `Index`-trait adapter for the MemWAL system index.
5//!
6//! The data structures and table-format logic live in
7//! [`lance_table::system_index::mem_wal`]; this module re-exports them and
8//! provides a newtype wrapper that implements the [`Index`] trait.
9
10use std::any::Any;
11use std::sync::Arc;
12
13use async_trait::async_trait;
14use lance_core::Result;
15use lance_core::deepsize::DeepSizeOf;
16use roaring::RoaringBitmap;
17use serde::Serialize;
18
19pub use lance_table::system_index::mem_wal::*;
20
21use crate::{Index, IndexType};
22
23/// Newtype wrapping [`MemWalIndex`] so that `lance-index` can implement
24/// the `Index` trait (orphan rules prevent implementing it directly in
25/// `lance-table`).
26pub struct MemWalIndexHandle(pub Arc<MemWalIndex>);
27
28impl DeepSizeOf for MemWalIndexHandle {
29    fn deep_size_of_children(&self, context: &mut lance_core::deepsize::Context) -> usize {
30        self.0.deep_size_of_children(context)
31    }
32}
33
34#[derive(Serialize)]
35struct MemWalStatistics {
36    num_shards: u32,
37    num_compacted_sstables: usize,
38    num_shard_specs: usize,
39    num_maintained_indexes: usize,
40    num_index_catchup_entries: usize,
41}
42
43#[async_trait]
44impl Index for MemWalIndexHandle {
45    fn as_any(&self) -> &dyn Any {
46        self
47    }
48
49    fn as_index(self: Arc<Self>) -> Arc<dyn Index> {
50        self
51    }
52
53    fn statistics(&self) -> Result<serde_json::Value> {
54        let stats = MemWalStatistics {
55            num_shards: self.0.details.num_shards,
56            num_compacted_sstables: self.0.details.compacted_sstables.len(),
57            num_shard_specs: self.0.details.sharding_specs.len(),
58            num_maintained_indexes: self.0.details.maintained_indexes.len(),
59            num_index_catchup_entries: self.0.details.index_catchup.len(),
60        };
61        serde_json::to_value(stats).map_err(|e| {
62            lance_core::Error::internal(format!(
63                "failed to serialize MemWAL index statistics: {}",
64                e
65            ))
66        })
67    }
68
69    async fn prewarm(&self) -> Result<()> {
70        Ok(())
71    }
72
73    fn index_type(&self) -> IndexType {
74        IndexType::MemWal
75    }
76
77    async fn calculate_included_frags(&self) -> Result<RoaringBitmap> {
78        Ok(RoaringBitmap::new())
79    }
80}