Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions rust/segment/src/blockfile_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use chroma_types::F32_METADATA;
use chroma_types::FULL_TEXT_PLS;
use chroma_types::STRING_METADATA;
use chroma_types::U32_METADATA;
use chroma_types::{CollectionSchema, ValueType};
use chroma_types::{MaterializedLogOperation, MetadataValue, Segment, SegmentUuid};
use core::panic;
use roaring::RoaringBitmap;
Expand All @@ -37,6 +38,7 @@ pub struct MetadataSegmentWriter<'me> {
pub(crate) f32_metadata_index_writer: Option<MetadataIndexWriter<'me>>,
pub(crate) u32_metadata_index_writer: Option<MetadataIndexWriter<'me>>,
pub id: SegmentUuid,
pub schema: Option<HashMap<String, HashMap<ValueType, CollectionSchema>>>,
}

impl Debug for MetadataSegmentWriter<'_> {
Expand Down Expand Up @@ -102,6 +104,7 @@ impl<'me> MetadataSegmentWriter<'me> {
database_id: &DatabaseUuid,
segment: &Segment,
blockfile_provider: &BlockfileProvider,
schema: Option<HashMap<String, HashMap<ValueType, CollectionSchema>>>,
) -> Result<MetadataSegmentWriter<'me>, MetadataSegmentError> {
if segment.r#type != SegmentType::BlockfileMetadata {
return Err(MetadataSegmentError::InvalidSegmentType);
Expand Down Expand Up @@ -316,6 +319,7 @@ impl<'me> MetadataSegmentWriter<'me> {
f32_metadata_index_writer: Some(f32_metadata_index_writer),
u32_metadata_index_writer: Some(u32_metadata_index_writer),
id: segment.id,
schema,
})
}

Expand All @@ -325,6 +329,37 @@ impl<'me> MetadataSegmentWriter<'me> {
key: &MetadataValue,
offset_id: u32,
) -> Result<(), MetadataIndexError> {
let mut should_index = true;
if let Some(schema) = &self.schema {
let schema_key = prefix;
if let Some(value_type_map) = schema.get(schema_key) {
match key {
MetadataValue::Str(_) => {
if let Some(value_type_map) = value_type_map.get(&ValueType::String) {
should_index = value_type_map.metadata_index;
}
}
MetadataValue::Int(_) => {
if let Some(value_type_map) = value_type_map.get(&ValueType::Int) {
should_index = value_type_map.metadata_index;
}
}
MetadataValue::Float(_) => {
if let Some(value_type_map) = value_type_map.get(&ValueType::Float) {
should_index = value_type_map.metadata_index;
}
}
MetadataValue::Bool(_) => {
if let Some(value_type_map) = value_type_map.get(&ValueType::Boolean) {
should_index = value_type_map.metadata_index;
}
}
}
}
}
if !should_index {
return Ok(());
}
match key {
MetadataValue::Str(v) => {
match &self.string_metadata_index_writer {
Expand Down Expand Up @@ -391,6 +426,37 @@ impl<'me> MetadataSegmentWriter<'me> {
key: &MetadataValue,
offset_id: u32,
) -> Result<(), MetadataIndexError> {
let mut should_index = true;
if let Some(schema) = &self.schema {
let schema_key = prefix;
if let Some(value_type_map) = schema.get(schema_key) {
match key {
MetadataValue::Str(_) => {
if let Some(value_type_map) = value_type_map.get(&ValueType::String) {
should_index = value_type_map.metadata_index;
}
}
MetadataValue::Int(_) => {
if let Some(value_type_map) = value_type_map.get(&ValueType::Int) {
should_index = value_type_map.metadata_index;
}
}
MetadataValue::Float(_) => {
if let Some(value_type_map) = value_type_map.get(&ValueType::Float) {
should_index = value_type_map.metadata_index;
}
}
MetadataValue::Bool(_) => {
if let Some(value_type_map) = value_type_map.get(&ValueType::Boolean) {
should_index = value_type_map.metadata_index;
}
}
}
}
}
if !should_index {
return Ok(());
}
match key {
MetadataValue::Str(v) => {
match &self.string_metadata_index_writer {
Expand Down Expand Up @@ -1063,6 +1129,7 @@ mod test {
&database_id,
&metadata_segment,
&blockfile_provider,
None,
)
.await
.expect("Error creating segment writer");
Expand Down Expand Up @@ -1202,6 +1269,7 @@ mod test {
&database_id,
&metadata_segment,
&blockfile_provider,
None,
)
.await
.expect("Error creating segment writer");
Expand Down Expand Up @@ -1291,6 +1359,7 @@ mod test {
&database_id,
&metadata_segment,
&blockfile_provider,
None,
)
.await
.expect("Error creating segment writer");
Expand Down Expand Up @@ -1388,6 +1457,7 @@ mod test {
&database_id,
&metadata_segment,
&blockfile_provider,
None,
)
.await
.expect("Error creating segment writer");
Expand Down Expand Up @@ -1530,6 +1600,7 @@ mod test {
&database_id,
&metadata_segment,
&blockfile_provider,
None,
)
.await
.expect("Error creating segment writer");
Expand Down Expand Up @@ -1658,6 +1729,7 @@ mod test {
&database_id,
&metadata_segment,
&blockfile_provider,
None,
)
.await
.expect("Error creating segment writer");
Expand Down Expand Up @@ -1773,6 +1845,7 @@ mod test {
&database_id,
&metadata_segment,
&blockfile_provider,
None,
)
.await
.expect("Error creating segment writer");
Expand Down Expand Up @@ -1897,6 +1970,7 @@ mod test {
&database_id,
&metadata_segment,
&blockfile_provider,
None,
)
.await
.expect("Error creating segment writer");
Expand Down Expand Up @@ -2001,6 +2075,7 @@ mod test {
&database_id,
&metadata_segment,
&blockfile_provider,
None,
)
.await
.expect("Error creating segment writer");
Expand Down Expand Up @@ -2124,6 +2199,7 @@ mod test {
&database_id,
&metadata_segment,
&blockfile_provider,
None,
)
.await
.expect("Error creating segment writer");
Expand Down
1 change: 1 addition & 0 deletions rust/segment/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl TestDistributedSegment {
&self.collection.database_id,
&self.metadata_segment,
&self.blockfile_provider,
self.collection.config.schema.clone(),
)
.await
.expect("Should be able to initialize metadata writer.");
Expand Down
22 changes: 22 additions & 0 deletions rust/types/src/collection_configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,28 @@ pub struct UpdateCollectionConfiguration {
pub schema: Option<HashMap<String, HashMap<ValueType, CollectionSchema>>>,
}

pub fn diff_metadata_index_enable(
old_schema: &Option<HashMap<String, HashMap<ValueType, CollectionSchema>>>,
update_schema: &HashMap<String, HashMap<ValueType, CollectionSchema>>,
) -> Vec<(String, ValueType)> {
let mut backfill_needed = Vec::new();
for (update_key, update_value) in update_schema {
for (update_value_type, update_collection_schema) in update_value {
let old_metadata_index = old_schema
.as_ref()
.and_then(|s| s.get(update_key))
.and_then(|vt_map| vt_map.get(update_value_type))
.map(|cs| cs.metadata_index)
.unwrap_or(true); // default to true if not present
let new_metadata_index = update_collection_schema.metadata_index;
if !old_metadata_index && new_metadata_index {
backfill_needed.push((update_key.clone(), update_value_type.clone()));
}
}
}
backfill_needed
}

#[cfg(test)]
mod tests {
use crate::hnsw_configuration::HnswConfiguration;
Expand Down
1 change: 1 addition & 0 deletions rust/worker/src/execution/orchestration/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ impl Handler<TaskResult<GetCollectionAndSegmentsOutput, GetCollectionAndSegments
&collection.database_id,
&metadata_segment,
&self.blockfile_provider,
collection.config.schema.clone(),
)
.await,
ctx,
Expand Down
Loading