lance_table/format/
index.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright The Lance Authors
3
4//! Metadata for index
5
6use deepsize::DeepSizeOf;
7use roaring::RoaringBitmap;
8use snafu::location;
9use uuid::Uuid;
10
11use super::pb;
12use lance_core::{Error, Result};
13
14/// Index metadata
15#[derive(Debug, Clone)]
16pub struct Index {
17    /// Unique ID across all dataset versions.
18    pub uuid: Uuid,
19
20    /// Fields to build the index.
21    pub fields: Vec<i32>,
22
23    /// Human readable index name
24    pub name: String,
25
26    /// The latest version of the dataset this index covers
27    pub dataset_version: u64,
28
29    /// The fragment ids this index covers.
30    ///
31    /// If this is None, then this is unknown.
32    pub fragment_bitmap: Option<RoaringBitmap>,
33
34    /// Metadata specific to the index type
35    ///
36    /// This is an Option because older versions of Lance may not have this defined.  However, it should always
37    /// be present in newer versions.
38    pub index_details: Option<prost_types::Any>,
39}
40
41impl DeepSizeOf for Index {
42    fn deep_size_of_children(&self, context: &mut deepsize::Context) -> usize {
43        self.uuid.as_bytes().deep_size_of_children(context)
44            + self.fields.deep_size_of_children(context)
45            + self.name.deep_size_of_children(context)
46            + self.dataset_version.deep_size_of_children(context)
47            + self
48                .fragment_bitmap
49                .as_ref()
50                .map(|fragment_bitmap| fragment_bitmap.serialized_size())
51                .unwrap_or(0)
52    }
53}
54
55impl TryFrom<pb::IndexMetadata> for Index {
56    type Error = Error;
57
58    fn try_from(proto: pb::IndexMetadata) -> Result<Self> {
59        let fragment_bitmap = if proto.fragment_bitmap.is_empty() {
60            None
61        } else {
62            Some(RoaringBitmap::deserialize_from(
63                &mut proto.fragment_bitmap.as_slice(),
64            )?)
65        };
66
67        Ok(Self {
68            uuid: proto.uuid.as_ref().map(Uuid::try_from).ok_or_else(|| {
69                Error::io(
70                    "uuid field does not exist in Index metadata".to_string(),
71                    location!(),
72                )
73            })??,
74            name: proto.name,
75            fields: proto.fields,
76            dataset_version: proto.dataset_version,
77            fragment_bitmap,
78            index_details: proto.index_details,
79        })
80    }
81}
82
83impl From<&Index> for pb::IndexMetadata {
84    fn from(idx: &Index) -> Self {
85        let mut fragment_bitmap = Vec::new();
86        if let Some(bitmap) = &idx.fragment_bitmap {
87            if let Err(e) = bitmap.serialize_into(&mut fragment_bitmap) {
88                // In theory, this should never error. But if we do, just
89                // recover gracefully.
90                log::error!("Failed to serialize fragment bitmap: {}", e);
91                fragment_bitmap.clear();
92            }
93        }
94
95        Self {
96            uuid: Some((&idx.uuid).into()),
97            name: idx.name.clone(),
98            fields: idx.fields.clone(),
99            dataset_version: idx.dataset_version,
100            fragment_bitmap,
101            index_details: idx.index_details.clone(),
102        }
103    }
104}