From ccacdf212c5ca3d952ef92f4556f3415b0b0b907 Mon Sep 17 00:00:00 2001 From: Tamo Date: Mon, 20 Jan 2025 15:54:43 +0100 Subject: [PATCH] fix the field distribution when upgrading from the v1_12 --- Cargo.lock | 1 + crates/milli/Cargo.toml | 1 + crates/milli/src/progress.rs | 14 ++++++++---- crates/milli/src/update/upgrade/mod.rs | 27 ++++++++++++++-------- crates/milli/src/update/upgrade/v1_12.rs | 29 ++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 crates/milli/src/update/upgrade/v1_12.rs diff --git a/Cargo.lock b/Cargo.lock index 3200d8333..32ae9bc58 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3776,6 +3776,7 @@ dependencies = [ "candle-transformers", "charabia", "concat-arrays", + "convert_case 0.6.0", "crossbeam-channel", "csv", "deserr", diff --git a/crates/milli/Cargo.toml b/crates/milli/Cargo.toml index d22829045..5eb89ea53 100644 --- a/crates/milli/Cargo.toml +++ b/crates/milli/Cargo.toml @@ -20,6 +20,7 @@ bytemuck = { version = "1.21.0", features = ["extern_crate_alloc"] } byteorder = "1.5.0" charabia = { version = "0.9.2", default-features = false } concat-arrays = "0.1.2" +convert_case = "0.6.0" crossbeam-channel = "0.5.14" deserr = "0.6.3" either = { version = "1.13.0", features = ["serde"] } diff --git a/crates/milli/src/progress.rs b/crates/milli/src/progress.rs index 870277bad..3837e173a 100644 --- a/crates/milli/src/progress.rs +++ b/crates/milli/src/progress.rs @@ -89,19 +89,24 @@ impl Step for AtomicSubStep { } } +#[doc(hidden)] +pub use convert_case as _private_convert_case; +#[doc(hidden)] +pub use enum_iterator as _private_enum_iterator; + #[macro_export] macro_rules! make_enum_progress { ($visibility:vis enum $name:ident { $($variant:ident,)+ }) => { #[repr(u8)] - #[derive(Debug, Clone, Copy, PartialEq, Eq, Sequence)] + #[derive(Debug, Clone, Copy, PartialEq, Eq, $crate::progress::_private_enum_iterator::Sequence)] #[allow(clippy::enum_variant_names)] $visibility enum $name { $($variant),+ } - impl Step for $name { - fn name(&self) -> Cow<'static, str> { - use convert_case::Casing; + impl $crate::progress::Step for $name { + fn name(&self) -> std::borrow::Cow<'static, str> { + use $crate::progress::_private_convert_case::Casing; match self { $( @@ -115,6 +120,7 @@ macro_rules! make_enum_progress { } fn total(&self) -> u32 { + use $crate::progress::_private_enum_iterator::Sequence; Self::CARDINALITY as u32 } } diff --git a/crates/milli/src/update/upgrade/mod.rs b/crates/milli/src/update/upgrade/mod.rs index 6e533177a..4be55e942 100644 --- a/crates/milli/src/update/upgrade/mod.rs +++ b/crates/milli/src/update/upgrade/mod.rs @@ -1,4 +1,7 @@ +mod v1_12; + use heed::RwTxn; +use v1_12::{v1_12_3_to_v1_13, v1_12_to_v1_12_3}; use crate::progress::{Progress, VariableNameStep}; use crate::{Index, InternalError, Result}; @@ -6,12 +9,21 @@ use crate::{Index, InternalError, Result}; /// Return true if the cached stats of the index must be regenerated pub fn upgrade(wtxn: &mut RwTxn, index: &Index, progress: Progress) -> Result { let from = index.get_version(wtxn)?; - let upgrade_functions = - [(v1_12_to_v1_13 as fn(&Index, Progress) -> Result<()>, "Upgrading from v1.12 to v1.13")]; + let upgrade_functions = [ + ( + v1_12_to_v1_12_3 as fn(&mut RwTxn, &Index, Progress) -> Result, + "Upgrading from v1.12.(0/1/2) to v1.12.3", + ), + ( + v1_12_3_to_v1_13 as fn(&mut RwTxn, &Index, Progress) -> Result, + "Upgrading from v1.12.3+ to v1.13", + ), + ]; - let (start, regenerate_stats) = match from { + let start = match from { // If there was no version it means we're coming from the v1.12 - None | Some((1, 12, _)) => (0, false), + None | Some((1, 12, 0..=2)) => 0, + Some((1, 12, 3..)) => 1, // We must handle the current version in the match because in case of a failure some index may have been upgraded but not other. Some((1, 13, _)) => return Ok(false), Some((major, minor, patch)) => { @@ -22,18 +34,15 @@ pub fn upgrade(wtxn: &mut RwTxn, index: &Index, progress: Progress) -> Result::new( upgrade_msg.to_string(), i as u32, upgrade_path.len() as u32, )); - (upgrade_function)(index, progress.clone())?; + regenerate_stats |= (upgrade_function)(wtxn, index, progress.clone())?; } Ok(regenerate_stats) } - -fn v1_12_to_v1_13(_index: &Index, _progress: Progress) -> Result<()> { - Ok(()) -} diff --git a/crates/milli/src/update/upgrade/v1_12.rs b/crates/milli/src/update/upgrade/v1_12.rs new file mode 100644 index 000000000..4f76752d1 --- /dev/null +++ b/crates/milli/src/update/upgrade/v1_12.rs @@ -0,0 +1,29 @@ +use heed::RwTxn; + +use crate::{make_enum_progress, Result}; + +use crate::{progress::Progress, Index}; + +// The field distribution was not computed correctly in the v1.12 until the v1.12.3 +pub(super) fn v1_12_to_v1_12_3( + wtxn: &mut RwTxn, + index: &Index, + progress: Progress, +) -> Result { + make_enum_progress! { + enum FieldDistribution { + RebuildingFieldDistribution, + } + }; + progress.update_progress(FieldDistribution::RebuildingFieldDistribution); + crate::update::new::reindex::field_distribution(index, wtxn, &progress)?; + Ok(true) +} + +pub(super) fn v1_12_3_to_v1_13( + _wtxn: &mut RwTxn, + _index: &Index, + _progress: Progress, +) -> Result { + Ok(false) +}