| 1 |
// revlog/config.rs |
| 2 |
// |
| 3 |
// Copyright 2020-2024 Raphaël Gomès <[email protected]> |
| 4 |
// 2024 Georges Racinet <[email protected]> |
| 5 |
// |
| 6 |
// This software may be used and distributed according to the terms of the |
| 7 |
// GNU General Public License version 2 or any later version. |
| 8 |
use std::sync::OnceLock; |
| 9 |
|
| 10 |
use hg::revlog::compression::CompressionConfig; |
| 11 |
use hg::revlog::options::RevlogDataConfig; |
| 12 |
use hg::revlog::options::RevlogDeltaConfig; |
| 13 |
use hg::revlog::options::RevlogFeatureConfig; |
| 14 |
use hg::revlog::RevlogType; |
| 15 |
use pyo3::conversion::FromPyObject; |
| 16 |
use pyo3::exceptions::PyValueError; |
| 17 |
use pyo3::intern; |
| 18 |
use pyo3::prelude::*; |
| 19 |
use pyo3::types::PyBytes; |
| 20 |
use pyo3::types::PyDict; |
| 21 |
use pyo3::types::PyDictMethods; |
| 22 |
|
| 23 |
/// Helper trait for configuration dicts |
| 24 |
/// |
| 25 |
/// In Mercurial, it is customary for such dicts to have bytes keys. |
| 26 |
trait ConfigPyDict<'a, 'py: 'a, D: FromPyObject<'py, 'py>> { |
| 27 |
fn extract_item(&'a self, key: &[u8]) -> PyResult<Option<D>>; |
| 28 |
} |
| 29 |
|
| 30 |
impl<'a, 'py, D> ConfigPyDict<'a, 'py, D> for Bound<'py, PyDict> |
| 31 |
where |
| 32 |
'py: 'a, |
| 33 |
D: FromPyObjectOwned<'py>, |
| 34 |
{ |
| 35 |
fn extract_item(&'a self, key: &[u8]) -> PyResult<Option<D>> { |
| 36 |
let py_item = self.get_item(PyBytes::new(self.py(), key))?; |
| 37 |
match py_item { |
| 38 |
Some(value) => { |
| 39 |
if value.is_none() { |
| 40 |
Ok(None) |
| 41 |
} else { |
| 42 |
Ok(Some(value.extract().map_err(Into::into)?)) |
| 43 |
} |
| 44 |
} |
| 45 |
None => Ok(None), |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/// Extraction helper for PyObject attributes. |
| 51 |
/// |
| 52 |
/// `$obj` is a `Bound('_, PyAny)` and `$attr` is a static String slice. |
| 53 |
/// This is both syntactic sugar and more efficient than using `getattr()` |
| 54 |
/// manually, as this uses [`intern!`] for efficiency. |
| 55 |
/// |
| 56 |
/// See the many examples in this module. |
| 57 |
/// |
| 58 |
/// This does not work to return references (e.g. bytes). Quoting the |
| 59 |
/// compiler: "returns a value referencing data owned by the current function" |
| 60 |
macro_rules! extract_attr { |
| 61 |
($obj: expr, $attr: expr) => { |
| 62 |
$obj.getattr(intern!($obj.py(), $attr)) |
| 63 |
.and_then(|a| a.extract().map_err(Into::into)) |
| 64 |
}; |
| 65 |
} |
| 66 |
|
| 67 |
// There are no static generics in Rust (because their implementation is |
| 68 |
// hard, I'm guessing it's due to different compilation stages, etc.). |
| 69 |
// So manually generate all three caches and use them in |
| 70 |
// `with_filelog_cache`. |
| 71 |
static DELTA_CONFIG_CACHE: OnceLock<(Py<PyAny>, RevlogDeltaConfig)> = |
| 72 |
OnceLock::new(); |
| 73 |
static DATA_CONFIG_CACHE: OnceLock<(Py<PyAny>, RevlogDataConfig)> = |
| 74 |
OnceLock::new(); |
| 75 |
static FEATURE_CONFIG_CACHE: OnceLock<(Py<PyAny>, RevlogFeatureConfig)> = |
| 76 |
OnceLock::new(); |
| 77 |
|
| 78 |
/// TODO don't do this and build a `Config` in Rust, expose it to Python and |
| 79 |
/// cast it (after refactoring Python to re-use the same config objects?). |
| 80 |
/// |
| 81 |
/// Cache the first conversion from Python of filelog config. Other |
| 82 |
/// revlog types are not cached. |
| 83 |
/// |
| 84 |
/// All filelogs in a given repository *most likely* have the |
| 85 |
/// exact same config, hence it makes a difference to look it up |
| 86 |
/// from Python code only once, especially given that it can be in a |
| 87 |
/// loop. |
| 88 |
fn with_filelog_config_cache<T: Copy>( |
| 89 |
py_config: &Bound<'_, PyAny>, |
| 90 |
revlog_type: RevlogType, |
| 91 |
cache: &OnceLock<(Py<PyAny>, T)>, |
| 92 |
callback: impl Fn() -> PyResult<T>, |
| 93 |
) -> PyResult<T> { |
| 94 |
let mut was_cached = false; |
| 95 |
if revlog_type == RevlogType::Filelog { |
| 96 |
if let Some((cached_py_config, rust_config)) = cache.get() { |
| 97 |
was_cached = true; |
| 98 |
// it's not impossible that some extensions do some magic with |
| 99 |
// configs, or that this code will be used for |
| 100 |
// longer-running processes, ot that we open multiple |
| 101 |
// repositories in the process. So compare the source `PyObject` in |
| 102 |
// case the source changed, at the cost of some |
| 103 |
// overhead. |
| 104 |
if py_config.eq(cached_py_config)? { |
| 105 |
return Ok(*rust_config); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
let config = callback()?; |
| 110 |
// Do not call the lock unnecessarily if it's already been set. |
| 111 |
if !was_cached && revlog_type == RevlogType::Filelog { |
| 112 |
cache.set((py_config.clone().unbind(), config)).ok(); |
| 113 |
} |
| 114 |
Ok(config) |
| 115 |
} |
| 116 |
|
| 117 |
pub fn extract_delta_config( |
| 118 |
conf: &Bound<'_, PyAny>, |
| 119 |
revlog_type: RevlogType, |
| 120 |
) -> PyResult<RevlogDeltaConfig> { |
| 121 |
with_filelog_config_cache(conf, revlog_type, &DELTA_CONFIG_CACHE, || { |
| 122 |
let max_deltachain_span: i64 = |
| 123 |
extract_attr!(conf, "max_deltachain_span")?; |
| 124 |
let revlog_delta_config = RevlogDeltaConfig { |
| 125 |
general_delta: extract_attr!(conf, "general_delta")?, |
| 126 |
sparse_revlog: extract_attr!(conf, "sparse_revlog")?, |
| 127 |
delta_info: extract_attr!(conf, "delta_info")?, |
| 128 |
store_quality: extract_attr!(conf, "store_quality")?, |
| 129 |
max_chain_len: extract_attr!(conf, "max_chain_len")?, |
| 130 |
max_deltachain_span: if max_deltachain_span < 0 { |
| 131 |
None |
| 132 |
} else { |
| 133 |
Some(max_deltachain_span as u64) |
| 134 |
}, |
| 135 |
upper_bound_comp: extract_attr!(conf, "upper_bound_comp")?, |
| 136 |
delta_both_parents: extract_attr!(conf, "delta_both_parents")?, |
| 137 |
candidate_group_chunk_size: extract_attr!( |
| 138 |
conf, |
| 139 |
"candidate_group_chunk_size" |
| 140 |
)?, |
| 141 |
debug_delta: extract_attr!(conf, "debug_delta")?, |
| 142 |
lazy_delta: extract_attr!(conf, "lazy_delta")?, |
| 143 |
lazy_delta_base: extract_attr!(conf, "lazy_delta_base")?, |
| 144 |
lazy_compression: extract_attr!(conf, "lazy_compression")?, |
| 145 |
file_max_comp_ratio: extract_attr!(conf, "file_max_comp_ratio")?, |
| 146 |
}; |
| 147 |
Ok(revlog_delta_config) |
| 148 |
}) |
| 149 |
} |
| 150 |
|
| 151 |
pub fn extract_data_config( |
| 152 |
conf: &Bound<'_, PyAny>, |
| 153 |
revlog_type: RevlogType, |
| 154 |
) -> PyResult<RevlogDataConfig> { |
| 155 |
with_filelog_config_cache(conf, revlog_type, &DATA_CONFIG_CACHE, || { |
| 156 |
Ok(RevlogDataConfig { |
| 157 |
try_pending: extract_attr!(conf, "try_pending")?, |
| 158 |
try_split: extract_attr!(conf, "try_split")?, |
| 159 |
check_ambig: extract_attr!(conf, "check_ambig")?, |
| 160 |
mmap_large_index: extract_attr!(conf, "mmap_large_index")?, |
| 161 |
mmap_index_threshold: extract_attr!(conf, "mmap_index_threshold")?, |
| 162 |
chunk_cache_size: extract_attr!(conf, "chunk_cache_size")?, |
| 163 |
uncompressed_cache_factor: extract_attr!( |
| 164 |
conf, |
| 165 |
"uncompressed_cache_factor" |
| 166 |
)?, |
| 167 |
uncompressed_cache_count: extract_attr!( |
| 168 |
conf, |
| 169 |
"uncompressed_cache_count" |
| 170 |
)?, |
| 171 |
with_sparse_read: extract_attr!(conf, "with_sparse_read")?, |
| 172 |
sr_density_threshold: extract_attr!(conf, "sr_density_threshold")?, |
| 173 |
sr_min_gap_size: extract_attr!(conf, "sr_min_gap_size")?, |
| 174 |
general_delta: extract_attr!(conf, "generaldelta")?, |
| 175 |
delta_info: extract_attr!(conf, "delta_info")?, |
| 176 |
}) |
| 177 |
}) |
| 178 |
} |
| 179 |
|
| 180 |
fn extract_compression_config( |
| 181 |
conf: &Bound<'_, PyAny>, |
| 182 |
) -> PyResult<CompressionConfig> { |
| 183 |
let compression_options: Bound<'_, PyDict> = |
| 184 |
extract_attr!(conf, "compression_engine_options")?; |
| 185 |
|
| 186 |
let name_bound = conf.getattr("compression_engine")?; |
| 187 |
let name_bytes: &[u8] = name_bound.extract()?; |
| 188 |
|
| 189 |
let compression_engine = match name_bytes { |
| 190 |
b"zlib" => { |
| 191 |
let level = compression_options.extract_item(b"zlib.level")?; |
| 192 |
let mut engine = CompressionConfig::default(); |
| 193 |
if let Some(level) = level { |
| 194 |
engine |
| 195 |
.set_level(level) |
| 196 |
.expect("invalid compression level from Python"); |
| 197 |
} |
| 198 |
engine |
| 199 |
} |
| 200 |
b"zstd" => { |
| 201 |
let zstd_level = compression_options.extract_item(b"zstd.level")?; |
| 202 |
let level = if let Some(level) = zstd_level { |
| 203 |
Some(level) |
| 204 |
} else { |
| 205 |
compression_options.extract_item(b"level")? |
| 206 |
}; |
| 207 |
|
| 208 |
CompressionConfig::zstd(level) |
| 209 |
.expect("invalid compression level from Python") |
| 210 |
} |
| 211 |
b"none" => CompressionConfig::None, |
| 212 |
unknown => { |
| 213 |
return Err(PyValueError::new_err(format!( |
| 214 |
"invalid compression engine {}", |
| 215 |
String::from_utf8_lossy(unknown) |
| 216 |
))); |
| 217 |
} |
| 218 |
}; |
| 219 |
Ok(compression_engine) |
| 220 |
} |
| 221 |
|
| 222 |
pub fn extract_feature_config( |
| 223 |
conf: &Bound<'_, PyAny>, |
| 224 |
revlog_type: RevlogType, |
| 225 |
) -> PyResult<RevlogFeatureConfig> { |
| 226 |
with_filelog_config_cache(conf, revlog_type, &FEATURE_CONFIG_CACHE, || { |
| 227 |
Ok(RevlogFeatureConfig { |
| 228 |
compression_engine: extract_compression_config(conf)?, |
| 229 |
censorable: extract_attr!(conf, "censorable")?, |
| 230 |
has_side_data: extract_attr!(conf, "has_side_data")?, |
| 231 |
compute_rank: extract_attr!(conf, "compute_rank")?, |
| 232 |
canonical_parent_order: extract_attr!( |
| 233 |
conf, |
| 234 |
"canonical_parent_order" |
| 235 |
)?, |
| 236 |
enable_ellipsis: extract_attr!(conf, "enable_ellipsis")?, |
| 237 |
hasmeta_flag: extract_attr!(conf, "hasmeta_flag")?, |
| 238 |
ignore_filelog_censored_revisions: extract_attr!( |
| 239 |
conf, |
| 240 |
"ignore_filelog_censored_revisions" |
| 241 |
)?, |
| 242 |
}) |
| 243 |
}) |
| 244 |
} |