lance_table/format/
index.rs1use chrono::{DateTime, Utc};
7use deepsize::DeepSizeOf;
8use roaring::RoaringBitmap;
9use snafu::location;
10use uuid::Uuid;
11
12use super::pb;
13use lance_core::{Error, Result};
14#[derive(Debug, Clone, PartialEq)]
16pub struct Index {
17 pub uuid: Uuid,
19
20 pub fields: Vec<i32>,
22
23 pub name: String,
25
26 pub dataset_version: u64,
28
29 pub fragment_bitmap: Option<RoaringBitmap>,
33
34 pub index_details: Option<prost_types::Any>,
39
40 pub index_version: i32,
42
43 pub created_at: Option<DateTime<Utc>>,
48}
49
50impl DeepSizeOf for Index {
51 fn deep_size_of_children(&self, context: &mut deepsize::Context) -> usize {
52 self.uuid.as_bytes().deep_size_of_children(context)
53 + self.fields.deep_size_of_children(context)
54 + self.name.deep_size_of_children(context)
55 + self.dataset_version.deep_size_of_children(context)
56 + self
57 .fragment_bitmap
58 .as_ref()
59 .map(|fragment_bitmap| fragment_bitmap.serialized_size())
60 .unwrap_or(0)
61 }
62}
63
64impl TryFrom<pb::IndexMetadata> for Index {
65 type Error = Error;
66
67 fn try_from(proto: pb::IndexMetadata) -> Result<Self> {
68 let fragment_bitmap = if proto.fragment_bitmap.is_empty() {
69 None
70 } else {
71 Some(RoaringBitmap::deserialize_from(
72 &mut proto.fragment_bitmap.as_slice(),
73 )?)
74 };
75
76 Ok(Self {
77 uuid: proto.uuid.as_ref().map(Uuid::try_from).ok_or_else(|| {
78 Error::io(
79 "uuid field does not exist in Index metadata".to_string(),
80 location!(),
81 )
82 })??,
83 name: proto.name,
84 fields: proto.fields,
85 dataset_version: proto.dataset_version,
86 fragment_bitmap,
87 index_details: proto.index_details,
88 index_version: proto.index_version.unwrap_or_default(),
89 created_at: proto.created_at.map(|ts| {
90 DateTime::from_timestamp_millis(ts as i64)
91 .expect("Invalid timestamp in index metadata")
92 }),
93 })
94 }
95}
96
97impl From<&Index> for pb::IndexMetadata {
98 fn from(idx: &Index) -> Self {
99 let mut fragment_bitmap = Vec::new();
100 if let Some(bitmap) = &idx.fragment_bitmap {
101 if let Err(e) = bitmap.serialize_into(&mut fragment_bitmap) {
102 log::error!("Failed to serialize fragment bitmap: {}", e);
105 fragment_bitmap.clear();
106 }
107 }
108
109 Self {
110 uuid: Some((&idx.uuid).into()),
111 name: idx.name.clone(),
112 fields: idx.fields.clone(),
113 dataset_version: idx.dataset_version,
114 fragment_bitmap,
115 index_details: idx.index_details.clone(),
116 index_version: Some(idx.index_version),
117 created_at: idx.created_at.map(|dt| dt.timestamp_millis() as u64),
118 }
119 }
120}