Debian Codegraph @

rust/hg-core/src/revlog/options.rs

raw
1 //! Helpers for the revlog config and opening options
2
3 use std::collections::HashSet;
4
5 use super::compression::CompressionConfig;
6 use super::index::FLAG_DELTA_INFO;
7 use super::index::FLAG_FILELOG_META;
8 use super::index::FLAG_GENERALDELTA;
9 use super::index::FLAG_INLINE_DATA;
10 use super::RevlogType;
11 use crate::config::Config;
12 use crate::config::ResourceProfileValue;
13 use crate::errors::HgError;
14 use crate::requirements::CHANGELOGV2_REQUIREMENT;
15 use crate::requirements::DELTA_INFO_REQUIREMENT;
16 use crate::requirements::GENERALDELTA_REQUIREMENT;
17 use crate::requirements::NARROW_REQUIREMENT;
18 use crate::requirements::NODEMAP_REQUIREMENT;
19 use crate::requirements::REVLOGV1_REQUIREMENT;
20 use crate::requirements::REVLOGV2_REQUIREMENT;
21 use crate::requirements::SPARSEREVLOG_REQUIREMENT;
22
23 const DEFAULT_CHUNK_CACHE_SIZE: u64 = 65536;
24 const DEFAULT_SPARSE_READ_DENSITY_THRESHOLD: f64 = 0.50;
25 const DEFAULT_SPARSE_READ_MIN_GAP_SIZE: u64 = 262144;
26
27 /// The known revlog versions and their options
28 #[derive(Debug, Copy, Clone, PartialEq)]
29 pub enum RevlogVersionOptions {
30 V0,
31 V1 {
32 general_delta: bool,
33 hasmeta_flag: bool,
34 delta_info: bool,
35 inline: bool,
36 },
37 V2,
38 ChangelogV2 {
39 compute_rank: bool,
40 },
41 }
42
43 /// Options to govern how a revlog should be opened, usually from the
44 /// repository configuration or requirements.
45 #[derive(Debug, Copy, Clone)]
46 pub struct RevlogOpenOptions {
47 /// The revlog version, along with any option specific to this version
48 pub version: RevlogVersionOptions,
49 /// Whether the revlog uses a persistent nodemap.
50 pub use_nodemap: bool,
51 pub delta_config: RevlogDeltaConfig,
52 pub data_config: RevlogDataConfig,
53 pub feature_config: RevlogFeatureConfig,
54 }
55
56 #[cfg(test)]
57 impl Default for RevlogOpenOptions {
58 fn default() -> Self {
59 Self {
60 version: RevlogVersionOptions::V1 {
61 general_delta: true,
62 inline: false,
63 hasmeta_flag: false,
64 delta_info: false,
65 },
66 use_nodemap: true,
67 data_config: Default::default(),
68 delta_config: Default::default(),
69 feature_config: Default::default(),
70 }
71 }
72 }
73
74 impl RevlogOpenOptions {
75 pub fn new(
76 inline: bool,
77 data_config: RevlogDataConfig,
78 delta_config: RevlogDeltaConfig,
79 feature_config: RevlogFeatureConfig,
80 ) -> Self {
81 Self {
82 version: RevlogVersionOptions::V1 {
83 general_delta: data_config.general_delta,
84 delta_info: delta_config.delta_info,
85 hasmeta_flag: feature_config.hasmeta_flag,
86 inline,
87 },
88 use_nodemap: false,
89 data_config,
90 delta_config,
91 feature_config,
92 }
93 }
94
95 pub fn index_header(&self) -> super::index::IndexHeader {
96 super::index::IndexHeader {
97 header_bytes: match self.version {
98 RevlogVersionOptions::V0 => [0, 0, 0, 0],
99 RevlogVersionOptions::V1 {
100 general_delta,
101 hasmeta_flag,
102 delta_info,
103 inline,
104 } => [
105 0,
106 ((if general_delta { FLAG_GENERALDELTA } else { 0 })
107 | (if hasmeta_flag { FLAG_FILELOG_META } else { 0 })
108 | (if delta_info { FLAG_DELTA_INFO } else { 0 })
109 | (if inline { FLAG_INLINE_DATA } else { 0 }))
110 as u8,
111 0,
112 1,
113 ],
114 RevlogVersionOptions::V2 => 0xDEADu32.to_be_bytes(),
115 RevlogVersionOptions::ChangelogV2 { compute_rank: _ } => {
116 0xD34Du32.to_be_bytes()
117 }
118 },
119 }
120 }
121 }
122
123 /// Technically only Linux 2.5.46+ has `MAP_POPULATE` and only `2.6.23` on
124 /// private mappings, but if you're using such ancient Linux, you have other
125 /// problems.
126 #[cfg(target_os = "linux")]
127 const fn can_populate_mmap() -> bool {
128 true
129 }
130
131 /// There is a of populating mmaps for Windows, but it would need testing.
132 #[cfg(not(target_os = "linux"))]
133 const fn can_populate_mmap() -> bool {
134 false
135 }
136
137 #[derive(Debug, Clone, Copy, PartialEq)]
138 /// Holds configuration values about how the revlog data is read
139 pub struct RevlogDataConfig {
140 /// Should we try to open the "pending" version of the revlog
141 pub try_pending: bool,
142 /// Should we try to open the "split" version of the revlog
143 pub try_split: bool,
144 /// When True, `indexfile` should be opened with `checkambig=True` at
145 /// writing time, to avoid file stat ambiguity
146 pub check_ambig: bool,
147 /// If true, use mmap instead of reading to deal with large indexes
148 pub mmap_large_index: bool,
149 /// How much data is considered large
150 pub mmap_index_threshold: Option<u64>,
151 /// How much data to read and cache into the raw revlog data cache
152 pub chunk_cache_size: u64,
153 /// The size of the uncompressed cache compared to the largest revision
154 /// seen
155 pub uncompressed_cache_factor: Option<usize>,
156 /// The number of chunks cached
157 pub uncompressed_cache_count: Option<u64>,
158 /// Allow sparse reading of the revlog data
159 pub with_sparse_read: bool,
160 /// Minimal density of a sparse read chunk
161 pub sr_density_threshold: f64,
162 /// Minimal size of the data we skip when performing sparse reads
163 pub sr_min_gap_size: u64,
164 /// Whether deltas are encoded against arbitrary bases
165 pub general_delta: bool,
166 /// Whether extra information about delta are stored in the index
167 pub delta_info: bool,
168 }
169
170 impl RevlogDataConfig {
171 pub fn new(
172 config: &Config,
173 requirements: &HashSet<String>,
174 ) -> Result<Self, HgError> {
175 let mut data_config = Self::default();
176 if let Some(chunk_cache_size) =
177 config.get_byte_size(b"format", b"chunkcachesize")?
178 {
179 data_config.chunk_cache_size = chunk_cache_size;
180 }
181
182 let memory_profile = config.get_resource_profile(Some("memory"));
183 if memory_profile.value >= ResourceProfileValue::Medium {
184 data_config.uncompressed_cache_count = Some(10_000);
185 data_config.uncompressed_cache_factor = Some(4);
186 if memory_profile.value >= ResourceProfileValue::High {
187 data_config.uncompressed_cache_factor = Some(10)
188 }
189 }
190
191 // Use mmap if requested, or by default if we can fully populate it
192 let mmap_index = config
193 .get_option_no_default(b"storage", b"revlog.mmap.index")?
194 .unwrap_or(can_populate_mmap());
195 if mmap_index {
196 if let Some(mmap_index_threshold) = config.get_byte_size(
197 b"storage",
198 b"revlog.mmap.index:size-threshold",
199 )? {
200 // Only mmap if above the requested size threshold
201 data_config.mmap_index_threshold = Some(mmap_index_threshold);
202 }
203 }
204
205 let with_sparse_read =
206 config.get_bool(b"experimental", b"sparse-read")?;
207 if let Some(sr_density_threshold) =
208 config.get_f64(b"experimental", b"sparse-read.density-threshold")?
209 {
210 data_config.sr_density_threshold = sr_density_threshold;
211 }
212 data_config.with_sparse_read = with_sparse_read;
213 if let Some(sr_min_gap_size) = config
214 .get_byte_size(b"experimental", b"sparse-read.min-gap-size")?
215 {
216 data_config.sr_min_gap_size = sr_min_gap_size;
217 }
218
219 data_config.with_sparse_read =
220 requirements.contains(SPARSEREVLOG_REQUIREMENT);
221
222 Ok(data_config)
223 }
224 }
225
226 impl Default for RevlogDataConfig {
227 fn default() -> Self {
228 Self {
229 chunk_cache_size: DEFAULT_CHUNK_CACHE_SIZE,
230 sr_density_threshold: DEFAULT_SPARSE_READ_DENSITY_THRESHOLD,
231 sr_min_gap_size: DEFAULT_SPARSE_READ_MIN_GAP_SIZE,
232 try_pending: Default::default(),
233 try_split: Default::default(),
234 check_ambig: Default::default(),
235 mmap_large_index: Default::default(),
236 mmap_index_threshold: Default::default(),
237 uncompressed_cache_factor: Default::default(),
238 uncompressed_cache_count: Default::default(),
239 with_sparse_read: Default::default(),
240 general_delta: Default::default(),
241 delta_info: Default::default(),
242 }
243 }
244 }
245
246 #[derive(Debug, Clone, Copy, PartialEq)]
247 /// Holds configuration values about how new deltas are computed.
248 ///
249 /// Some attributes are duplicated from [`RevlogDataConfig`] to help having
250 /// each object self contained.
251 pub struct RevlogDeltaConfig {
252 /// Whether deltas can be encoded against arbitrary bases
253 pub general_delta: bool,
254 /// Allow sparse writing of the revlog data
255 pub sparse_revlog: bool,
256 /// Store additionnal information about delta in the index
257 /// (and adjust delta computation to leverage it)
258 pub delta_info: bool,
259 /// Actually persist quality information when available an supported by the
260 /// revlog.
261 pub store_quality: bool,
262 /// Maximum length of a delta chain
263 pub max_chain_len: Option<u64>,
264 /// Maximum distance between a delta chain's start and end
265 pub max_deltachain_span: Option<u64>,
266 /// If `upper_bound_comp` is not None, this is the expected maximal
267 /// gain from compression for the data content
268 pub upper_bound_comp: Option<f64>,
269 /// Should we try a delta against both parents
270 pub delta_both_parents: bool,
271 /// Test delta base candidate groups by chunks of this maximal size
272 pub candidate_group_chunk_size: u64,
273 /// Should we display debug information about delta computation
274 pub debug_delta: bool,
275 /// Trust incoming deltas by default
276 pub lazy_delta: bool,
277 /// Trust the base of incoming deltas by default
278 pub lazy_delta_base: bool,
279 /// Trust the compression of incoming deltas (when avaiable and compatible)
280 pub lazy_compression: bool,
281 /// A theoretical maximum compression ratio for file content
282 /// Used to estimate delta size before compression. value <= 0 disable such
283 /// estimate.
284 pub file_max_comp_ratio: u64,
285 }
286
287 impl RevlogDeltaConfig {
288 pub fn new(
289 config: &Config,
290 requirements: &HashSet<String>,
291 revlog_type: RevlogType,
292 ) -> Result<Self, HgError> {
293 let mut delta_config = Self {
294 delta_both_parents: config
295 .get_option_no_default(
296 b"storage",
297 b"revlog.optimize-delta-parent-choice",
298 )?
299 .unwrap_or(true),
300 candidate_group_chunk_size: config
301 .get_u64(
302 b"storage",
303 b"revlog.delta-parent-search.candidate-group-chunk-size",
304 )?
305 .unwrap_or_default(),
306 ..Default::default()
307 };
308
309 delta_config.debug_delta =
310 config.get_bool(b"debug", b"revlog.debug-delta")?;
311
312 delta_config.general_delta =
313 requirements.contains(GENERALDELTA_REQUIREMENT);
314
315 let lazy_delta =
316 config.get_bool(b"storage", b"revlog.reuse-external-delta")?;
317
318 if revlog_type == RevlogType::Manifestlog {
319 // upper bound of what we expect from compression
320 // (real life value seems to be 3)
321 delta_config.upper_bound_comp = Some(3.0)
322 }
323
324 let mut lazy_delta_base = false;
325 if lazy_delta {
326 lazy_delta_base = match config.get_option_no_default(
327 b"storage",
328 b"revlog.reuse-external-delta-parent",
329 )? {
330 Some(base) => base,
331 None => config.get_bool(b"format", b"generaldelta")?,
332 };
333 }
334 delta_config.lazy_delta = lazy_delta;
335 delta_config.lazy_delta_base = lazy_delta_base;
336
337 delta_config.lazy_compression = config
338 .get_bool(b"storage", b"revlog.reuse-external-delta-compression")?;
339
340 delta_config.max_deltachain_span =
341 match config.get_i64(b"experimental", b"maxdeltachainspan")? {
342 Some(span) => {
343 if span < 0 {
344 None
345 } else {
346 Some(span as u64)
347 }
348 }
349 None => None,
350 };
351
352 // we should not create delta that rely on sparse logic if the revlog
353 // does not suport general delta.
354 delta_config.sparse_revlog = delta_config.general_delta
355 && requirements.contains(SPARSEREVLOG_REQUIREMENT);
356
357 // we should not rely on richer delta information if the revlog does
358 // not suport general delta.
359 delta_config.delta_info = delta_config.general_delta
360 && requirements.contains(DELTA_INFO_REQUIREMENT);
361
362 delta_config.store_quality =
363 config.get_bool(b"storage", b"revlog.record-delta-quality")?;
364
365 delta_config.max_chain_len =
366 config.get_byte_size_no_default(b"format", b"maxchainlen")?;
367
368 delta_config.file_max_comp_ratio = match config
369 .get_i64(b"storage", b"filelog.expected-max-compression-ratio")?
370 {
371 Some(ratio) => {
372 if ratio < 0 {
373 0
374 } else {
375 ratio as u64
376 }
377 }
378 None => 10,
379 };
380
381 Ok(delta_config)
382 }
383 }
384
385 impl Default for RevlogDeltaConfig {
386 fn default() -> Self {
387 Self {
388 delta_both_parents: true,
389 lazy_delta: true,
390 lazy_compression: true,
391 general_delta: Default::default(),
392 sparse_revlog: Default::default(),
393 delta_info: Default::default(),
394 store_quality: true,
395 max_chain_len: Default::default(),
396 max_deltachain_span: Default::default(),
397 upper_bound_comp: Default::default(),
398 candidate_group_chunk_size: Default::default(),
399 debug_delta: Default::default(),
400 lazy_delta_base: Default::default(),
401 file_max_comp_ratio: 10,
402 }
403 }
404 }
405
406 #[derive(Debug, Default, Clone, Copy, PartialEq)]
407 /// Holds configuration values about the available revlog features
408 pub struct RevlogFeatureConfig {
409 /// The compression engine and its options
410 pub compression_engine: CompressionConfig,
411 /// Can we use censor on this revlog
412 pub censorable: bool,
413 /// Should censored filelog revisions be ignored and return an empty string
414 pub ignore_filelog_censored_revisions: bool,
415 /// Does this revlog use the "side data" feature
416 pub has_side_data: bool,
417 /// Might remove this configuration once the rank computation has no
418 /// impact
419 pub compute_rank: bool,
420 /// Parent order is supposed to be semantically irrelevant, so we
421 /// normally re-sort parents to ensure that the first parent is non-null,
422 /// if there is a non-null parent at all.
423 /// filelog abuses the parent order as a flag to mark some instances of
424 /// meta-encoded files, so allow it to disable this behavior.
425 pub canonical_parent_order: bool,
426 /// Can ellipsis commit be used
427 pub enable_ellipsis: bool,
428 /// use a flag to signal that a filerevision constains metadata
429 pub hasmeta_flag: bool,
430 }
431
432 impl RevlogFeatureConfig {
433 pub fn new(
434 config: &Config,
435 requirements: &HashSet<String>,
436 revlog_type: RevlogType,
437 ) -> Result<Self, HgError> {
438 let ignore_filelog_censored_revisions = revlog_type
439 == RevlogType::Filelog
440 && config.get_str(b"censor", b"policy")? == Some("ignore");
441 Ok(Self {
442 compression_engine: CompressionConfig::new(config, requirements)?,
443 enable_ellipsis: requirements.contains(NARROW_REQUIREMENT),
444 hasmeta_flag: requirements.contains(DELTA_INFO_REQUIREMENT),
445 ignore_filelog_censored_revisions,
446 ..Default::default()
447 })
448 }
449 }
450
451 /// Return the default options for a revlog of `revlog_type` according to the
452 /// current config and requirements.
453 pub fn default_revlog_options(
454 config: &Config,
455 requirements: &HashSet<String>,
456 revlog_type: RevlogType,
457 ) -> Result<RevlogOpenOptions, HgError> {
458 let is_changelog = revlog_type == RevlogType::Changelog;
459 let version =
460 if is_changelog && requirements.contains(CHANGELOGV2_REQUIREMENT) {
461 let compute_rank = config
462 .get_bool(b"experimental", b"changelog-v2.compute-rank")?;
463 RevlogVersionOptions::ChangelogV2 { compute_rank }
464 } else if requirements.contains(REVLOGV2_REQUIREMENT) {
465 RevlogVersionOptions::V2
466 } else if requirements.contains(REVLOGV1_REQUIREMENT) {
467 RevlogVersionOptions::V1 {
468 general_delta: requirements.contains(GENERALDELTA_REQUIREMENT),
469 hasmeta_flag: requirements.contains(DELTA_INFO_REQUIREMENT),
470 delta_info: requirements.contains(DELTA_INFO_REQUIREMENT),
471 inline: !is_changelog,
472 }
473 } else {
474 RevlogVersionOptions::V0
475 };
476 Ok(RevlogOpenOptions {
477 version,
478 // We don't need to dance around the slow path like in the Python
479 // implementation since we know we have access to the fast code.
480 use_nodemap: requirements.contains(NODEMAP_REQUIREMENT),
481 delta_config: RevlogDeltaConfig::new(
482 config,
483 requirements,
484 revlog_type,
485 )?,
486 data_config: RevlogDataConfig::new(config, requirements)?,
487 feature_config: RevlogFeatureConfig::new(
488 config,
489 requirements,
490 revlog_type,
491 )?,
492 })
493 }