introduce a trait to upgrade the indexes

This commit is contained in:
Tamo
2025-01-23 10:50:16 +01:00
committed by Louis Dureuil
parent fd5649091d
commit c27c923439
4 changed files with 104 additions and 46 deletions

View File

@ -1,32 +1,39 @@
mod v1_12;
use heed::RwTxn;
use v1_12::{v1_12_3_to_v1_13, v1_12_to_v1_12_3};
use v1_12::{V1_12_3_To_Current, V1_12_To_V1_12_3};
use crate::progress::{Progress, VariableNameStep};
use crate::{Index, InternalError, Result};
trait UpgradeIndex {
/// Returns true if the index scheduler must regenerate its cached stats
fn upgrade(
&self,
wtxn: &mut RwTxn,
index: &Index,
original: (u32, u32, u32),
progress: Progress,
) -> Result<bool>;
fn target_version(&self) -> (u32, u32, u32);
}
/// Return true if the cached stats of the index must be regenerated
pub fn upgrade(wtxn: &mut RwTxn, index: &Index, progress: Progress) -> Result<bool> {
let from = index.get_version(wtxn)?;
let upgrade_functions = [
(
v1_12_to_v1_12_3 as fn(&mut RwTxn, &Index, Progress) -> Result<bool>,
"Upgrading from v1.12.(0/1/2) to v1.12.3",
),
(
v1_12_3_to_v1_13 as fn(&mut RwTxn, &Index, Progress) -> Result<bool>,
"Upgrading from v1.12.3+ to v1.13",
),
];
pub fn upgrade(
wtxn: &mut RwTxn,
index: &Index,
db_version: (u32, u32, u32),
progress: Progress,
) -> Result<bool> {
let from = index.get_version(wtxn)?.unwrap_or(db_version);
let upgrade_functions: &[&dyn UpgradeIndex] = &[&V1_12_To_V1_12_3 {}, &V1_12_3_To_Current()];
let start = match from {
// If there was no version it means we're coming from the v1.12
None | Some((1, 12, 0..=2)) => 0,
Some((1, 12, 3..)) => 1,
(1, 12, 0..=2) => 0,
(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)) => {
(1, 13, _) => return Ok(false),
(major, minor, patch) => {
return Err(InternalError::CannotUpgradeToVersion(major, minor, patch).into())
}
};
@ -34,14 +41,26 @@ pub fn upgrade(wtxn: &mut RwTxn, index: &Index, progress: Progress) -> Result<bo
enum UpgradeVersion {}
let upgrade_path = &upgrade_functions[start..];
let mut current_version = from;
let mut regenerate_stats = false;
for (i, (upgrade_function, upgrade_msg)) in upgrade_path.iter().enumerate() {
for (i, upgrade) in upgrade_path.iter().enumerate() {
let target = upgrade.target_version();
progress.update_progress(VariableNameStep::<UpgradeVersion>::new(
upgrade_msg.to_string(),
format!(
"Upgrading from v{}.{}.{} to v{}.{}.{}",
current_version.0,
current_version.1,
current_version.2,
target.0,
target.1,
target.2
),
i as u32,
upgrade_path.len() as u32,
));
regenerate_stats |= (upgrade_function)(wtxn, index, progress.clone())?;
regenerate_stats |= upgrade.upgrade(wtxn, index, from, progress.clone())?;
current_version = target;
}
Ok(regenerate_stats)