annotate rust/hg-core/src/revlog/inner_revlog.rs @ 52185:8d35941689af

rust-vfs: support checkambig This was missing from the Rust code, which means worse caching. See https://wiki.mercurial-scm.org/ExactCacheValidationPlan. Explanations on what ambiguity means inline.
author Raphaël Gomès <rgomes@octobus.net>
date Thu, 10 Oct 2024 15:54:45 +0200
parents e01e84e5e426
children a3fa37bdb7ec
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
52164
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1 //! A layer of lower-level revlog functionality to encapsulate most of the
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
2 //! IO work and expensive operations.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
3 use std::{
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
4 borrow::Cow,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
5 cell::RefCell,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
6 io::{ErrorKind, Seek, SeekFrom, Write},
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
7 ops::Deref,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
8 path::PathBuf,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
9 sync::{Arc, Mutex},
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
10 };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
11
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
12 use schnellru::{ByMemoryUsage, LruMap};
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
13 use sha1::{Digest, Sha1};
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
14
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
15 use crate::{
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
16 errors::{HgError, IoResultExt},
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
17 exit_codes,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
18 transaction::Transaction,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
19 vfs::Vfs,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
20 };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
21
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
22 use super::{
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
23 compression::{
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
24 uncompressed_zstd_data, CompressionConfig, Compressor, NoneCompressor,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
25 ZlibCompressor, ZstdCompressor, ZLIB_BYTE, ZSTD_BYTE,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
26 },
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
27 file_io::{DelayedBuffer, FileHandle, RandomAccessFile, WriteHandles},
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
28 index::{Index, IndexHeader, INDEX_ENTRY_SIZE},
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
29 node::{NODE_BYTES_LENGTH, NULL_NODE},
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
30 options::{RevlogDataConfig, RevlogDeltaConfig, RevlogFeatureConfig},
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
31 BaseRevision, Node, Revision, RevlogEntry, RevlogError, RevlogIndex,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
32 UncheckedRevision, NULL_REVISION, NULL_REVLOG_ENTRY_FLAGS,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
33 };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
34
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
35 /// Matches the `_InnerRevlog` class in the Python code, as an arbitrary
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
36 /// boundary to incrementally rewrite higher-level revlog functionality in
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
37 /// Rust.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
38 pub struct InnerRevlog {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
39 /// When index and data are not interleaved: bytes of the revlog index.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
40 /// When index and data are interleaved (inline revlog): bytes of the
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
41 /// revlog index and data.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
42 pub index: Index,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
43 /// The store vfs that is used to interact with the filesystem
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
44 vfs: Box<dyn Vfs>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
45 /// The index file path, relative to the vfs root
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
46 pub index_file: PathBuf,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
47 /// The data file path, relative to the vfs root (same as `index_file`
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
48 /// if inline)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
49 data_file: PathBuf,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
50 /// Data config that applies to this revlog
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
51 data_config: RevlogDataConfig,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
52 /// Delta config that applies to this revlog
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
53 delta_config: RevlogDeltaConfig,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
54 /// Feature config that applies to this revlog
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
55 feature_config: RevlogFeatureConfig,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
56 /// A view into this revlog's data file
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
57 segment_file: RandomAccessFile,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
58 /// A cache of uncompressed chunks that have previously been restored.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
59 /// Its eviction policy is defined in [`Self::new`].
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
60 uncompressed_chunk_cache: Option<UncompressedChunkCache>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
61 /// Used to keep track of the actual target during diverted writes
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
62 /// for the changelog
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
63 original_index_file: Option<PathBuf>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
64 /// Write handles to the index and data files
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
65 /// XXX why duplicate from `index` and `segment_file`?
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
66 writing_handles: Option<WriteHandles>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
67 /// See [`DelayedBuffer`].
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
68 delayed_buffer: Option<Arc<Mutex<DelayedBuffer>>>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
69 /// Whether this revlog is inline. XXX why duplicate from `index`?
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
70 pub inline: bool,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
71 /// A cache of the last revision, which is usually accessed multiple
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
72 /// times.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
73 pub last_revision_cache: Mutex<Option<SingleRevisionCache>>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
74 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
75
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
76 impl InnerRevlog {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
77 pub fn new(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
78 vfs: Box<dyn Vfs>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
79 index: Index,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
80 index_file: PathBuf,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
81 data_file: PathBuf,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
82 data_config: RevlogDataConfig,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
83 delta_config: RevlogDeltaConfig,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
84 feature_config: RevlogFeatureConfig,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
85 ) -> Self {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
86 assert!(index_file.is_relative());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
87 assert!(data_file.is_relative());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
88 let segment_file = RandomAccessFile::new(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
89 dyn_clone::clone_box(&*vfs),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
90 if index.is_inline() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
91 index_file.to_owned()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
92 } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
93 data_file.to_owned()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
94 },
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
95 );
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
96
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
97 let uncompressed_chunk_cache =
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
98 data_config.uncompressed_cache_factor.map(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
99 // Arbitrary initial value
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
100 // TODO check if using a hasher specific to integers is useful
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
101 |_factor| RefCell::new(LruMap::with_memory_budget(65536)),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
102 );
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
103
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
104 let inline = index.is_inline();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
105 Self {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
106 index,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
107 vfs,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
108 index_file,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
109 data_file,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
110 data_config,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
111 delta_config,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
112 feature_config,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
113 segment_file,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
114 uncompressed_chunk_cache,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
115 original_index_file: None,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
116 writing_handles: None,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
117 delayed_buffer: None,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
118 inline,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
119 last_revision_cache: Mutex::new(None),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
120 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
121 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
122
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
123 /// Return number of entries of the revlog index
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
124 pub fn len(&self) -> usize {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
125 self.index.len()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
126 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
127
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
128 /// Return `true` if this revlog has no entries
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
129 pub fn is_empty(&self) -> bool {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
130 self.len() == 0
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
131 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
132
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
133 /// Return whether this revlog is inline (mixed index and data)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
134 pub fn is_inline(&self) -> bool {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
135 self.inline
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
136 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
137
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
138 /// Clear all caches from this revlog
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
139 pub fn clear_cache(&mut self) {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
140 assert!(!self.is_delaying());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
141 if let Some(cache) = self.uncompressed_chunk_cache.as_ref() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
142 // We don't clear the allocation here because it's probably faster.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
143 // We could change our minds later if this ends up being a problem
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
144 // with regards to memory consumption.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
145 cache.borrow_mut().clear();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
146 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
147 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
148
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
149 /// Return an entry for the null revision
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
150 pub fn make_null_entry(&self) -> RevlogEntry {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
151 RevlogEntry {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
152 revlog: self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
153 rev: NULL_REVISION,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
154 uncompressed_len: 0,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
155 p1: NULL_REVISION,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
156 p2: NULL_REVISION,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
157 flags: NULL_REVLOG_ENTRY_FLAGS,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
158 hash: NULL_NODE,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
159 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
160 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
161
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
162 /// Return the [`RevlogEntry`] for a [`Revision`] that is known to exist
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
163 pub fn get_entry_for_checked_rev(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
164 &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
165 rev: Revision,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
166 ) -> Result<RevlogEntry, RevlogError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
167 if rev == NULL_REVISION {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
168 return Ok(self.make_null_entry());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
169 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
170 let index_entry = self
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
171 .index
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
172 .get_entry(rev)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
173 .ok_or_else(|| RevlogError::InvalidRevision(rev.to_string()))?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
174 let p1 =
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
175 self.index.check_revision(index_entry.p1()).ok_or_else(|| {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
176 RevlogError::corrupted(format!(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
177 "p1 for rev {} is invalid",
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
178 rev
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
179 ))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
180 })?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
181 let p2 =
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
182 self.index.check_revision(index_entry.p2()).ok_or_else(|| {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
183 RevlogError::corrupted(format!(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
184 "p2 for rev {} is invalid",
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
185 rev
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
186 ))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
187 })?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
188 let entry = RevlogEntry {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
189 revlog: self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
190 rev,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
191 uncompressed_len: index_entry.uncompressed_len(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
192 p1,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
193 p2,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
194 flags: index_entry.flags(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
195 hash: *index_entry.hash(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
196 };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
197 Ok(entry)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
198 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
199
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
200 /// Return the [`RevlogEntry`] for `rev`. If `rev` fails to check, this
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
201 /// returns a [`RevlogError`].
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
202 /// TODO normalize naming across the index and all revlogs
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
203 /// (changelog, etc.) so that `get_entry` is always on an unchecked rev and
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
204 /// `get_entry_for_checked_rev` is for checked rev
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
205 pub fn get_entry(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
206 &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
207 rev: UncheckedRevision,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
208 ) -> Result<RevlogEntry, RevlogError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
209 if rev == NULL_REVISION.into() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
210 return Ok(self.make_null_entry());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
211 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
212 let rev = self.index.check_revision(rev).ok_or_else(|| {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
213 RevlogError::corrupted(format!("rev {} is invalid", rev))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
214 })?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
215 self.get_entry_for_checked_rev(rev)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
216 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
217
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
218 /// Is the revlog currently delaying the visibility of written data?
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
219 ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
220 /// The delaying mechanism can be either in-memory or written on disk in a
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
221 /// side-file.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
222 pub fn is_delaying(&self) -> bool {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
223 self.delayed_buffer.is_some() || self.original_index_file.is_some()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
224 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
225
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
226 /// The offset of the data chunk for this revision
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
227 #[inline(always)]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
228 pub fn start(&self, rev: Revision) -> usize {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
229 self.index.start(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
230 rev,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
231 &self
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
232 .index
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
233 .get_entry(rev)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
234 .unwrap_or_else(|| self.index.make_null_entry()),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
235 )
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
236 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
237
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
238 /// The length of the data chunk for this revision
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
239 /// TODO rename this method and others to more explicit names than the
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
240 /// existing ones that were copied over from Python
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
241 #[inline(always)]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
242 pub fn length(&self, rev: Revision) -> usize {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
243 self.index
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
244 .get_entry(rev)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
245 .unwrap_or_else(|| self.index.make_null_entry())
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
246 .compressed_len() as usize
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
247 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
248
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
249 /// The end of the data chunk for this revision
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
250 #[inline(always)]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
251 pub fn end(&self, rev: Revision) -> usize {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
252 self.start(rev) + self.length(rev)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
253 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
254
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
255 /// Return the delta parent of the given revision
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
256 pub fn delta_parent(&self, rev: Revision) -> Revision {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
257 let base = self
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
258 .index
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
259 .get_entry(rev)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
260 .unwrap()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
261 .base_revision_or_base_of_delta_chain();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
262 if base.0 == rev.0 {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
263 NULL_REVISION
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
264 } else if self.delta_config.general_delta {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
265 Revision(base.0)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
266 } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
267 Revision(rev.0 - 1)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
268 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
269 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
270
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
271 /// Return whether `rev` points to a snapshot revision (i.e. does not have
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
272 /// a delta base).
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
273 pub fn is_snapshot(&self, rev: Revision) -> Result<bool, RevlogError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
274 if !self.delta_config.sparse_revlog {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
275 return Ok(self.delta_parent(rev) == NULL_REVISION);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
276 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
277 self.index.is_snapshot_unchecked(rev)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
278 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
279
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
280 /// Return the delta chain for `rev` according to this revlog's config.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
281 /// See [`Index::delta_chain`] for more information.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
282 pub fn delta_chain(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
283 &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
284 rev: Revision,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
285 stop_rev: Option<Revision>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
286 ) -> Result<(Vec<Revision>, bool), HgError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
287 self.index.delta_chain(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
288 rev,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
289 stop_rev,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
290 self.delta_config.general_delta.into(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
291 )
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
292 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
293
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
294 fn compressor(&self) -> Result<Box<dyn Compressor>, HgError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
295 // TODO cache the compressor?
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
296 Ok(match self.feature_config.compression_engine {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
297 CompressionConfig::Zlib { level } => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
298 Box::new(ZlibCompressor::new(level))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
299 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
300 CompressionConfig::Zstd { level, threads } => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
301 Box::new(ZstdCompressor::new(level, threads))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
302 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
303 CompressionConfig::None => Box::new(NoneCompressor),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
304 })
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
305 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
306
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
307 /// Generate a possibly-compressed representation of data.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
308 /// Returns `None` if the data was not compressed.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
309 pub fn compress<'data>(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
310 &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
311 data: &'data [u8],
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
312 ) -> Result<Option<Cow<'data, [u8]>>, RevlogError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
313 if data.is_empty() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
314 return Ok(Some(data.into()));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
315 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
316 let res = self.compressor()?.compress(data)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
317 if let Some(compressed) = res {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
318 // The revlog compressor added the header in the returned data.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
319 return Ok(Some(compressed.into()));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
320 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
321
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
322 if data[0] == b'\0' {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
323 return Ok(Some(data.into()));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
324 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
325 Ok(None)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
326 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
327
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
328 /// Decompress a revlog chunk.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
329 ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
330 /// The chunk is expected to begin with a header identifying the
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
331 /// format type so it can be routed to an appropriate decompressor.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
332 pub fn decompress<'a>(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
333 &'a self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
334 data: &'a [u8],
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
335 ) -> Result<Cow<[u8]>, RevlogError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
336 if data.is_empty() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
337 return Ok(data.into());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
338 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
339
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
340 // Revlogs are read much more frequently than they are written and many
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
341 // chunks only take microseconds to decompress, so performance is
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
342 // important here.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
343
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
344 let header = data[0];
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
345 match header {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
346 // Settings don't matter as they only affect compression
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
347 ZLIB_BYTE => Ok(ZlibCompressor::new(0).decompress(data)?.into()),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
348 // Settings don't matter as they only affect compression
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
349 ZSTD_BYTE => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
350 Ok(ZstdCompressor::new(0, 0).decompress(data)?.into())
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
351 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
352 b'\0' => Ok(data.into()),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
353 b'u' => Ok((&data[1..]).into()),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
354 other => Err(HgError::UnsupportedFeature(format!(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
355 "unknown compression header '{}'",
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
356 other
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
357 ))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
358 .into()),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
359 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
360 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
361
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
362 /// Obtain a segment of raw data corresponding to a range of revisions.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
363 ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
364 /// Requests for data may be satisfied by a cache.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
365 ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
366 /// Returns a 2-tuple of (offset, data) for the requested range of
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
367 /// revisions. Offset is the integer offset from the beginning of the
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
368 /// revlog and data is a slice of the raw byte data.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
369 ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
370 /// Callers will need to call `self.start(rev)` and `self.length(rev)`
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
371 /// to determine where each revision's data begins and ends.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
372 pub fn get_segment_for_revs(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
373 &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
374 start_rev: Revision,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
375 end_rev: Revision,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
376 ) -> Result<(usize, Vec<u8>), HgError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
377 let start = if start_rev == NULL_REVISION {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
378 0
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
379 } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
380 let start_entry = self
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
381 .index
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
382 .get_entry(start_rev)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
383 .expect("null revision segment");
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
384 self.index.start(start_rev, &start_entry)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
385 };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
386 let end_entry = self
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
387 .index
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
388 .get_entry(end_rev)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
389 .expect("null revision segment");
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
390 let end = self.index.start(end_rev, &end_entry) + self.length(end_rev);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
391
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
392 let length = end - start;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
393
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
394 // XXX should we use mmap instead of doing this for platforms that
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
395 // support madvise/populate?
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
396 Ok((start, self.segment_file.read_chunk(start, length)?))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
397 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
398
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
399 /// Return the uncompressed raw data for `rev`
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
400 pub fn chunk_for_rev(&self, rev: Revision) -> Result<Arc<[u8]>, HgError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
401 if let Some(cache) = self.uncompressed_chunk_cache.as_ref() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
402 if let Some(chunk) = cache.borrow_mut().get(&rev) {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
403 return Ok(chunk.clone());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
404 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
405 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
406 // TODO revlogv2 should check the compression mode
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
407 let data = self.get_segment_for_revs(rev, rev)?.1;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
408 let uncompressed = self.decompress(&data).map_err(|e| {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
409 HgError::abort(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
410 format!("revlog decompression error: {}", e),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
411 exit_codes::ABORT,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
412 None,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
413 )
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
414 })?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
415 let uncompressed: Arc<[u8]> = Arc::from(uncompressed.into_owned());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
416 if let Some(cache) = self.uncompressed_chunk_cache.as_ref() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
417 cache.borrow_mut().insert(rev, uncompressed.clone());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
418 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
419 Ok(uncompressed)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
420 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
421
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
422 /// Execute `func` within a read context for the data file, meaning that
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
423 /// the read handle will be taken and discarded after the operation.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
424 pub fn with_read<R>(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
425 &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
426 func: impl FnOnce() -> Result<R, RevlogError>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
427 ) -> Result<R, RevlogError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
428 self.enter_reading_context()?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
429 let res = func();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
430 self.exit_reading_context();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
431 res.map_err(Into::into)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
432 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
433
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
434 /// `pub` only for use in hg-cpython
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
435 #[doc(hidden)]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
436 pub fn enter_reading_context(&self) -> Result<(), HgError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
437 if self.is_empty() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
438 // Nothing to be read
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
439 return Ok(());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
440 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
441 if self.delayed_buffer.is_some() && self.is_inline() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
442 return Err(HgError::abort(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
443 "revlog with delayed write should not be inline",
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
444 exit_codes::ABORT,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
445 None,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
446 ));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
447 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
448 self.segment_file.get_read_handle()?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
449 Ok(())
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
450 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
451
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
452 /// `pub` only for use in hg-cpython
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
453 #[doc(hidden)]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
454 pub fn exit_reading_context(&self) {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
455 self.segment_file.exit_reading_context()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
456 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
457
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
458 /// Fill the buffer returned by `get_buffer` with the possibly un-validated
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
459 /// raw text for a revision. It can be already validated if it comes
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
460 /// from the cache.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
461 pub fn raw_text<G, T>(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
462 &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
463 rev: Revision,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
464 get_buffer: G,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
465 ) -> Result<(), RevlogError>
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
466 where
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
467 G: FnOnce(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
468 usize,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
469 &mut dyn FnMut(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
470 &mut dyn RevisionBuffer<Target = T>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
471 ) -> Result<(), RevlogError>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
472 ) -> Result<(), RevlogError>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
473 {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
474 let entry = &self.get_entry_for_checked_rev(rev)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
475 let raw_size = entry.uncompressed_len();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
476 let mut mutex_guard = self
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
477 .last_revision_cache
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
478 .lock()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
479 .expect("lock should not be held");
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
480 let cached_rev = if let Some((_node, rev, data)) = &*mutex_guard {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
481 Some((*rev, data.deref().as_ref()))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
482 } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
483 None
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
484 };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
485 if let Some(cache) = &self.uncompressed_chunk_cache {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
486 let cache = &mut cache.borrow_mut();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
487 if let Some(size) = raw_size {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
488 // Dynamically update the uncompressed_chunk_cache size to the
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
489 // largest revision we've seen in this revlog.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
490 // Do it *before* restoration in case the current revision
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
491 // is the largest.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
492 let factor = self
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
493 .data_config
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
494 .uncompressed_cache_factor
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
495 .expect("cache should not exist without factor");
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
496 let candidate_size = (size as f64 * factor) as usize;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
497 let limiter_mut = cache.limiter_mut();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
498 if candidate_size > limiter_mut.max_memory_usage() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
499 std::mem::swap(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
500 limiter_mut,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
501 &mut ByMemoryUsage::new(candidate_size),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
502 );
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
503 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
504 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
505 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
506 entry.rawdata(cached_rev, get_buffer)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
507 // drop cache to save memory, the caller is expected to update
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
508 // the revision cache after validating the text
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
509 mutex_guard.take();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
510 Ok(())
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
511 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
512
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
513 /// Only `pub` for `hg-cpython`.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
514 /// Obtain decompressed raw data for the specified revisions that are
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
515 /// assumed to be in ascending order.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
516 ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
517 /// Returns a list with decompressed data for each requested revision.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
518 #[doc(hidden)]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
519 pub fn chunks(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
520 &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
521 revs: Vec<Revision>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
522 target_size: Option<u64>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
523 ) -> Result<Vec<Arc<[u8]>>, RevlogError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
524 if revs.is_empty() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
525 return Ok(vec![]);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
526 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
527 let mut fetched_revs = vec![];
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
528 let mut chunks = Vec::with_capacity(revs.len());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
529
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
530 match self.uncompressed_chunk_cache.as_ref() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
531 Some(cache) => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
532 let mut cache = cache.borrow_mut();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
533 for rev in revs.iter() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
534 match cache.get(rev) {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
535 Some(hit) => chunks.push((*rev, hit.to_owned())),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
536 None => fetched_revs.push(*rev),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
537 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
538 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
539 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
540 None => fetched_revs = revs,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
541 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
542
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
543 let already_cached = chunks.len();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
544
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
545 let sliced_chunks = if fetched_revs.is_empty() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
546 vec![]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
547 } else if !self.data_config.with_sparse_read || self.is_inline() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
548 vec![fetched_revs]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
549 } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
550 self.slice_chunk(&fetched_revs, target_size)?
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
551 };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
552
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
553 self.with_read(|| {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
554 for revs_chunk in sliced_chunks {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
555 let first_rev = revs_chunk[0];
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
556 // Skip trailing revisions with empty diff
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
557 let last_rev_idx = revs_chunk
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
558 .iter()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
559 .rposition(|r| self.length(*r) != 0)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
560 .unwrap_or(revs_chunk.len() - 1);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
561
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
562 let last_rev = revs_chunk[last_rev_idx];
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
563
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
564 let (offset, data) =
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
565 self.get_segment_for_revs(first_rev, last_rev)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
566
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
567 let revs_chunk = &revs_chunk[..=last_rev_idx];
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
568
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
569 for rev in revs_chunk {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
570 let chunk_start = self.start(*rev);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
571 let chunk_length = self.length(*rev);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
572 // TODO revlogv2 should check the compression mode
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
573 let bytes = &data[chunk_start - offset..][..chunk_length];
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
574 let chunk = if !bytes.is_empty() && bytes[0] == ZSTD_BYTE {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
575 // If we're using `zstd`, we want to try a more
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
576 // specialized decompression
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
577 let entry = self.index.get_entry(*rev).unwrap();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
578 let is_delta = entry
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
579 .base_revision_or_base_of_delta_chain()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
580 != (*rev).into();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
581 let uncompressed = uncompressed_zstd_data(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
582 bytes,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
583 is_delta,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
584 entry.uncompressed_len(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
585 )?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
586 Cow::Owned(uncompressed)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
587 } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
588 // Otherwise just fallback to generic decompression.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
589 self.decompress(bytes)?
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
590 };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
591
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
592 chunks.push((*rev, chunk.into()));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
593 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
594 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
595 Ok(())
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
596 })?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
597
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
598 if let Some(cache) = self.uncompressed_chunk_cache.as_ref() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
599 let mut cache = cache.borrow_mut();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
600 for (rev, chunk) in chunks.iter().skip(already_cached) {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
601 cache.insert(*rev, chunk.clone());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
602 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
603 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
604 // Use stable sort here since it's *mostly* sorted
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
605 chunks.sort_by(|a, b| a.0.cmp(&b.0));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
606 Ok(chunks.into_iter().map(|(_r, chunk)| chunk).collect())
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
607 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
608
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
609 /// Slice revs to reduce the amount of unrelated data to be read from disk.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
610 ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
611 /// ``revs`` is sliced into groups that should be read in one time.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
612 /// Assume that revs are sorted.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
613 ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
614 /// The initial chunk is sliced until the overall density
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
615 /// (payload/chunks-span ratio) is above
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
616 /// `revlog.data_config.sr_density_threshold`.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
617 /// No gap smaller than `revlog.data_config.sr_min_gap_size` is skipped.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
618 ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
619 /// If `target_size` is set, no chunk larger than `target_size`
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
620 /// will be returned.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
621 /// For consistency with other slicing choices, this limit won't go lower
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
622 /// than `revlog.data_config.sr_min_gap_size`.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
623 ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
624 /// If individual revision chunks are larger than this limit, they will
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
625 /// still be raised individually.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
626 pub fn slice_chunk(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
627 &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
628 revs: &[Revision],
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
629 target_size: Option<u64>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
630 ) -> Result<Vec<Vec<Revision>>, RevlogError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
631 let target_size =
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
632 target_size.map(|size| size.max(self.data_config.sr_min_gap_size));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
633
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
634 let target_density = self.data_config.sr_density_threshold;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
635 let min_gap_size = self.data_config.sr_min_gap_size as usize;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
636 let to_density = self.index.slice_chunk_to_density(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
637 revs,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
638 target_density,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
639 min_gap_size,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
640 );
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
641
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
642 let mut sliced = vec![];
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
643
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
644 for chunk in to_density {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
645 sliced.extend(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
646 self.slice_chunk_to_size(&chunk, target_size)?
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
647 .into_iter()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
648 .map(ToOwned::to_owned),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
649 );
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
650 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
651
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
652 Ok(sliced)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
653 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
654
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
655 /// Slice revs to match the target size
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
656 ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
657 /// This is intended to be used on chunks that density slicing selected,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
658 /// but that are still too large compared to the read guarantee of revlogs.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
659 /// This might happen when the "minimal gap size" interrupted the slicing
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
660 /// or when chains are built in a way that create large blocks next to
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
661 /// each other.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
662 fn slice_chunk_to_size<'a>(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
663 &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
664 revs: &'a [Revision],
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
665 target_size: Option<u64>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
666 ) -> Result<Vec<&'a [Revision]>, RevlogError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
667 let mut start_data = self.start(revs[0]);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
668 let end_data = self.end(revs[revs.len() - 1]);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
669 let full_span = end_data - start_data;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
670
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
671 let nothing_to_do = target_size
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
672 .map(|size| full_span <= size as usize)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
673 .unwrap_or(true);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
674
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
675 if nothing_to_do {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
676 return Ok(vec![revs]);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
677 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
678 let target_size = target_size.expect("target_size is set") as usize;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
679
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
680 let mut start_rev_idx = 0;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
681 let mut end_rev_idx = 1;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
682 let mut chunks = vec![];
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
683
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
684 for (idx, rev) in revs.iter().enumerate().skip(1) {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
685 let span = self.end(*rev) - start_data;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
686 let is_snapshot = self.is_snapshot(*rev)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
687 if span <= target_size && is_snapshot {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
688 end_rev_idx = idx + 1;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
689 } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
690 let chunk =
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
691 self.trim_chunk(revs, start_rev_idx, Some(end_rev_idx));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
692 if !chunk.is_empty() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
693 chunks.push(chunk);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
694 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
695 start_rev_idx = idx;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
696 start_data = self.start(*rev);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
697 end_rev_idx = idx + 1;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
698 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
699 if !is_snapshot {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
700 break;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
701 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
702 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
703
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
704 // For the others, we use binary slicing to quickly converge towards
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
705 // valid chunks (otherwise, we might end up looking for the start/end
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
706 // of many revisions). This logic is not looking for the perfect
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
707 // slicing point, it quickly converges towards valid chunks.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
708 let number_of_items = revs.len();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
709
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
710 while (end_data - start_data) > target_size {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
711 end_rev_idx = number_of_items;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
712 if number_of_items - start_rev_idx <= 1 {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
713 // Protect against individual chunks larger than the limit
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
714 break;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
715 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
716 let mut local_end_data = self.end(revs[end_rev_idx - 1]);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
717 let mut span = local_end_data - start_data;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
718 while span > target_size {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
719 if end_rev_idx - start_rev_idx <= 1 {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
720 // Protect against individual chunks larger than the limit
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
721 break;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
722 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
723 end_rev_idx -= (end_rev_idx - start_rev_idx) / 2;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
724 local_end_data = self.end(revs[end_rev_idx - 1]);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
725 span = local_end_data - start_data;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
726 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
727 let chunk =
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
728 self.trim_chunk(revs, start_rev_idx, Some(end_rev_idx));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
729 if !chunk.is_empty() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
730 chunks.push(chunk);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
731 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
732 start_rev_idx = end_rev_idx;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
733 start_data = self.start(revs[start_rev_idx]);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
734 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
735
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
736 let chunk = self.trim_chunk(revs, start_rev_idx, None);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
737 if !chunk.is_empty() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
738 chunks.push(chunk);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
739 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
740
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
741 Ok(chunks)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
742 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
743
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
744 /// Returns `revs[startidx..endidx]` without empty trailing revs
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
745 fn trim_chunk<'a>(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
746 &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
747 revs: &'a [Revision],
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
748 start_rev_idx: usize,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
749 end_rev_idx: Option<usize>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
750 ) -> &'a [Revision] {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
751 let mut end_rev_idx = end_rev_idx.unwrap_or(revs.len());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
752
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
753 // If we have a non-empty delta candidate, there is nothing to trim
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
754 if revs[end_rev_idx - 1].0 < self.len() as BaseRevision {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
755 // Trim empty revs at the end, except the very first rev of a chain
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
756 while end_rev_idx > 1
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
757 && end_rev_idx > start_rev_idx
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
758 && self.length(revs[end_rev_idx - 1]) == 0
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
759 {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
760 end_rev_idx -= 1
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
761 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
762 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
763
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
764 &revs[start_rev_idx..end_rev_idx]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
765 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
766
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
767 /// Check the hash of some given data against the recorded hash.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
768 pub fn check_hash(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
769 &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
770 p1: Revision,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
771 p2: Revision,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
772 expected: &[u8],
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
773 data: &[u8],
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
774 ) -> bool {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
775 let e1 = self.index.get_entry(p1);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
776 let h1 = match e1 {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
777 Some(ref entry) => entry.hash(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
778 None => &NULL_NODE,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
779 };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
780 let e2 = self.index.get_entry(p2);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
781 let h2 = match e2 {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
782 Some(ref entry) => entry.hash(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
783 None => &NULL_NODE,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
784 };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
785
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
786 hash(data, h1.as_bytes(), h2.as_bytes()) == expected
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
787 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
788
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
789 /// Returns whether we are currently in a [`Self::with_write`] context
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
790 pub fn is_writing(&self) -> bool {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
791 self.writing_handles.is_some()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
792 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
793
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
794 /// Open the revlog files for writing
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
795 ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
796 /// Adding content to a revlog should be done within this context.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
797 /// TODO try using `BufRead` and `BufWrite` and see if performance improves
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
798 pub fn with_write<R>(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
799 &mut self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
800 transaction: &mut impl Transaction,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
801 data_end: Option<usize>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
802 func: impl FnOnce() -> R,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
803 ) -> Result<R, HgError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
804 if self.is_writing() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
805 return Ok(func());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
806 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
807 self.enter_writing_context(data_end, transaction)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
808 .map_err(|e| {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
809 self.exit_writing_context();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
810 e
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
811 })?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
812 let res = func();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
813 self.exit_writing_context();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
814 Ok(res)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
815 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
816
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
817 /// `pub` only for use in hg-cpython
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
818 #[doc(hidden)]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
819 pub fn exit_writing_context(&mut self) {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
820 self.writing_handles.take();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
821 self.segment_file.writing_handle.take();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
822 self.segment_file.reading_handle.take();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
823 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
824
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
825 /// `pub` only for use in hg-cpython
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
826 #[doc(hidden)]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
827 pub fn python_writing_handles(&self) -> Option<&WriteHandles> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
828 self.writing_handles.as_ref()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
829 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
830
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
831 /// `pub` only for use in hg-cpython
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
832 #[doc(hidden)]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
833 pub fn enter_writing_context(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
834 &mut self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
835 data_end: Option<usize>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
836 transaction: &mut impl Transaction,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
837 ) -> Result<(), HgError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
838 let data_size = if self.is_empty() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
839 0
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
840 } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
841 self.end(Revision((self.len() - 1) as BaseRevision))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
842 };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
843 let data_handle = if !self.is_inline() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
844 let data_handle = match self.vfs.open(&self.data_file) {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
845 Ok(mut f) => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
846 if let Some(end) = data_end {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
847 f.seek(SeekFrom::Start(end as u64))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
848 .when_reading_file(&self.data_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
849 } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
850 f.seek(SeekFrom::End(0))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
851 .when_reading_file(&self.data_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
852 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
853 f
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
854 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
855 Err(e) => match e {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
856 HgError::IoError { error, context } => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
857 if error.kind() != ErrorKind::NotFound {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
858 return Err(HgError::IoError { error, context });
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
859 }
52185
8d35941689af rust-vfs: support checkambig
Raphaël Gomès <rgomes@octobus.net>
parents: 52164
diff changeset
860 self.vfs.create(&self.data_file, true)?
52164
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
861 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
862 e => return Err(e),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
863 },
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
864 };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
865 transaction.add(&self.data_file, data_size);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
866 Some(FileHandle::from_file(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
867 data_handle,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
868 dyn_clone::clone_box(&*self.vfs),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
869 &self.data_file,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
870 ))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
871 } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
872 None
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
873 };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
874 let index_size = self.len() * INDEX_ENTRY_SIZE;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
875 let index_handle = self.index_write_handle()?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
876 if self.is_inline() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
877 transaction.add(&self.index_file, data_size);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
878 } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
879 transaction.add(&self.index_file, index_size);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
880 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
881 self.writing_handles = Some(WriteHandles {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
882 index_handle: index_handle.clone(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
883 data_handle: data_handle.clone(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
884 });
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
885 *self.segment_file.reading_handle.borrow_mut() = if self.is_inline() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
886 Some(index_handle)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
887 } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
888 data_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
889 };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
890 Ok(())
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
891 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
892
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
893 /// Get a write handle to the index, sought to the end of its data.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
894 fn index_write_handle(&self) -> Result<FileHandle, HgError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
895 let res = if self.delayed_buffer.is_none() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
896 if self.data_config.check_ambig {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
897 self.vfs.open_check_ambig(&self.index_file)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
898 } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
899 self.vfs.open(&self.index_file)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
900 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
901 } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
902 self.vfs.open(&self.index_file)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
903 };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
904 match res {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
905 Ok(mut handle) => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
906 handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
907 .seek(SeekFrom::End(0))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
908 .when_reading_file(&self.index_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
909 Ok(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
910 if let Some(delayed_buffer) = self.delayed_buffer.as_ref()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
911 {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
912 FileHandle::from_file_delayed(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
913 handle,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
914 dyn_clone::clone_box(&*self.vfs),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
915 &self.index_file,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
916 delayed_buffer.clone(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
917 )?
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
918 } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
919 FileHandle::from_file(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
920 handle,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
921 dyn_clone::clone_box(&*self.vfs),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
922 &self.index_file,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
923 )
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
924 },
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
925 )
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
926 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
927 Err(e) => match e {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
928 HgError::IoError { error, context } => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
929 if error.kind() != ErrorKind::NotFound {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
930 return Err(HgError::IoError { error, context });
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
931 };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
932 if let Some(delayed_buffer) = self.delayed_buffer.as_ref()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
933 {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
934 FileHandle::new_delayed(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
935 dyn_clone::clone_box(&*self.vfs),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
936 &self.index_file,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
937 true,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
938 delayed_buffer.clone(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
939 )
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
940 } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
941 FileHandle::new(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
942 dyn_clone::clone_box(&*self.vfs),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
943 &self.index_file,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
944 true,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
945 true,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
946 )
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
947 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
948 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
949 e => Err(e),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
950 },
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
951 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
952 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
953
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
954 /// Split the data of an inline revlog into an index and a data file
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
955 pub fn split_inline(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
956 &mut self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
957 header: IndexHeader,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
958 new_index_file_path: Option<PathBuf>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
959 ) -> Result<PathBuf, RevlogError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
960 assert!(self.delayed_buffer.is_none());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
961 let existing_handles = self.writing_handles.is_some();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
962 if let Some(handles) = &mut self.writing_handles {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
963 handles.index_handle.flush()?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
964 self.writing_handles.take();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
965 self.segment_file.writing_handle.take();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
966 }
52185
8d35941689af rust-vfs: support checkambig
Raphaël Gomès <rgomes@octobus.net>
parents: 52164
diff changeset
967 let mut new_data_file_handle =
8d35941689af rust-vfs: support checkambig
Raphaël Gomès <rgomes@octobus.net>
parents: 52164
diff changeset
968 self.vfs.create(&self.data_file, true)?;
52164
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
969 // Drop any potential data, possibly redundant with the VFS impl.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
970 new_data_file_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
971 .set_len(0)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
972 .when_writing_file(&self.data_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
973
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
974 self.with_read(|| -> Result<(), RevlogError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
975 for r in 0..self.index.len() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
976 let rev = Revision(r as BaseRevision);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
977 let rev_segment = self.get_segment_for_revs(rev, rev)?.1;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
978 new_data_file_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
979 .write_all(&rev_segment)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
980 .when_writing_file(&self.data_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
981 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
982 new_data_file_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
983 .flush()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
984 .when_writing_file(&self.data_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
985 Ok(())
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
986 })?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
987
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
988 if let Some(index_path) = new_index_file_path {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
989 self.index_file = index_path
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
990 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
991
52185
8d35941689af rust-vfs: support checkambig
Raphaël Gomès <rgomes@octobus.net>
parents: 52164
diff changeset
992 let mut new_index_handle = self.vfs.create(&self.index_file, true)?;
52164
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
993 let mut new_data = Vec::with_capacity(self.len() * INDEX_ENTRY_SIZE);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
994 for r in 0..self.len() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
995 let rev = Revision(r as BaseRevision);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
996 let entry = self.index.entry_binary(rev).unwrap_or_else(|| {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
997 panic!(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
998 "entry {} should exist in {}",
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
999 r,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1000 self.index_file.display()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1001 )
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1002 });
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1003 if r == 0 {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1004 new_data.extend(header.header_bytes);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1005 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1006 new_data.extend(entry);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1007 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1008 new_index_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1009 .write_all(&new_data)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1010 .when_writing_file(&self.index_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1011 // Replace the index with a new one because the buffer contains inline
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1012 // data
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1013 self.index = Index::new(Box::new(new_data), header)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1014 self.inline = false;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1015
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1016 self.segment_file = RandomAccessFile::new(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1017 dyn_clone::clone_box(&*self.vfs),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1018 self.data_file.to_owned(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1019 );
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1020 if existing_handles {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1021 // Switched from inline to conventional, reopen the index
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1022 let new_data_handle = Some(FileHandle::from_file(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1023 new_data_file_handle,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1024 dyn_clone::clone_box(&*self.vfs),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1025 &self.data_file,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1026 ));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1027 self.writing_handles = Some(WriteHandles {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1028 index_handle: self.index_write_handle()?,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1029 data_handle: new_data_handle.clone(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1030 });
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1031 *self.segment_file.writing_handle.borrow_mut() = new_data_handle;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1032 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1033
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1034 Ok(self.index_file.to_owned())
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1035 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1036
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1037 /// Write a new entry to this revlog.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1038 /// - `entry` is the index bytes
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1039 /// - `header_and_data` is the compression header and the revision data
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1040 /// - `offset` is the position in the data file to write to
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1041 /// - `index_end` is the overwritten position in the index in revlog-v2,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1042 /// since the format may allow a rewrite of garbage data at the end.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1043 /// - `data_end` is the overwritten position in the data-file in revlog-v2,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1044 /// since the format may allow a rewrite of garbage data at the end.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1045 ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1046 /// XXX Why do we have `data_end` *and* `offset`? Same question in Python
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1047 pub fn write_entry(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1048 &mut self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1049 mut transaction: impl Transaction,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1050 entry: &[u8],
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1051 header_and_data: (&[u8], &[u8]),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1052 mut offset: usize,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1053 index_end: Option<u64>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1054 data_end: Option<u64>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1055 ) -> Result<(u64, Option<u64>), HgError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1056 let current_revision = self.len() - 1;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1057 let canonical_index_file = self.canonical_index_file();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1058
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1059 let is_inline = self.is_inline();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1060 let handles = match &mut self.writing_handles {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1061 None => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1062 return Err(HgError::abort(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1063 "adding revision outside of the `with_write` context",
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1064 exit_codes::ABORT,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1065 None,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1066 ));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1067 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1068 Some(handles) => handles,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1069 };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1070 let index_handle = &mut handles.index_handle;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1071 let data_handle = &mut handles.data_handle;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1072 if let Some(end) = index_end {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1073 index_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1074 .seek(SeekFrom::Start(end))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1075 .when_reading_file(&self.index_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1076 } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1077 index_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1078 .seek(SeekFrom::End(0))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1079 .when_reading_file(&self.index_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1080 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1081 if let Some(data_handle) = data_handle {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1082 if let Some(end) = data_end {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1083 data_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1084 .seek(SeekFrom::Start(end))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1085 .when_reading_file(&self.data_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1086 } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1087 data_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1088 .seek(SeekFrom::End(0))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1089 .when_reading_file(&self.data_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1090 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1091 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1092 let (header, data) = header_and_data;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1093
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1094 if !is_inline {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1095 transaction.add(&self.data_file, offset);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1096 transaction
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1097 .add(&canonical_index_file, current_revision * entry.len());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1098 let data_handle = data_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1099 .as_mut()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1100 .expect("data handle should exist when not inline");
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1101 if !header.is_empty() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1102 data_handle.write_all(header)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1103 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1104 data_handle.write_all(data)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1105 match &mut self.delayed_buffer {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1106 Some(buf) => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1107 buf.lock()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1108 .expect("propagate the panic")
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1109 .buffer
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1110 .write_all(entry)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1111 .expect("write to delay buffer should succeed");
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1112 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1113 None => index_handle.write_all(entry)?,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1114 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1115 } else if self.delayed_buffer.is_some() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1116 return Err(HgError::abort(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1117 "invalid delayed write on inline revlog",
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1118 exit_codes::ABORT,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1119 None,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1120 ));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1121 } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1122 offset += current_revision * entry.len();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1123 transaction.add(&canonical_index_file, offset);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1124 index_handle.write_all(entry)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1125 index_handle.write_all(header)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1126 index_handle.write_all(data)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1127 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1128 let data_position = match data_handle {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1129 Some(h) => Some(h.position()?),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1130 None => None,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1131 };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1132 Ok((index_handle.position()?, data_position))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1133 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1134
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1135 /// Return the real target index file and not the temporary when diverting
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1136 pub fn canonical_index_file(&self) -> PathBuf {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1137 self.original_index_file
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1138 .as_ref()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1139 .map(ToOwned::to_owned)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1140 .unwrap_or_else(|| self.index_file.to_owned())
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1141 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1142
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1143 /// Return the path to the diverted index
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1144 fn diverted_index(&self) -> PathBuf {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1145 self.index_file.with_extension("i.a")
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1146 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1147
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1148 /// True if we're in a [`Self::with_write`] or [`Self::with_read`] context
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1149 pub fn is_open(&self) -> bool {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1150 self.segment_file.is_open()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1151 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1152
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1153 /// Set this revlog to delay its writes to a buffer
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1154 pub fn delay(&mut self) -> Result<Option<PathBuf>, HgError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1155 assert!(!self.is_open());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1156 if self.is_inline() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1157 return Err(HgError::abort(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1158 "revlog with delayed write should not be inline",
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1159 exit_codes::ABORT,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1160 None,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1161 ));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1162 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1163 if self.delayed_buffer.is_some() || self.original_index_file.is_some()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1164 {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1165 // Delay or divert already happening
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1166 return Ok(None);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1167 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1168 if self.is_empty() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1169 self.original_index_file = Some(self.index_file.to_owned());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1170 self.index_file = self.diverted_index();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1171 if self.vfs.exists(&self.index_file) {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1172 self.vfs.unlink(&self.index_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1173 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1174 Ok(Some(self.index_file.to_owned()))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1175 } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1176 self.delayed_buffer =
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1177 Some(Arc::new(Mutex::new(DelayedBuffer::default())));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1178 Ok(None)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1179 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1180 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1181
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1182 /// Write the pending data (in memory) if any to the diverted index file
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1183 /// (on disk temporary file)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1184 pub fn write_pending(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1185 &mut self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1186 ) -> Result<(Option<PathBuf>, bool), HgError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1187 assert!(!self.is_open());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1188 if self.is_inline() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1189 return Err(HgError::abort(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1190 "revlog with delayed write should not be inline",
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1191 exit_codes::ABORT,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1192 None,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1193 ));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1194 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1195 if self.original_index_file.is_some() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1196 return Ok((None, true));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1197 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1198 let mut any_pending = false;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1199 let pending_index_file = self.diverted_index();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1200 if self.vfs.exists(&pending_index_file) {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1201 self.vfs.unlink(&pending_index_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1202 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1203 self.vfs.copy(&self.index_file, &pending_index_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1204 if let Some(delayed_buffer) = self.delayed_buffer.take() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1205 let mut index_file_handle = self.vfs.open(&pending_index_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1206 index_file_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1207 .seek(SeekFrom::End(0))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1208 .when_writing_file(&pending_index_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1209 let delayed_data =
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1210 &delayed_buffer.lock().expect("propagate the panic").buffer;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1211 index_file_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1212 .write_all(delayed_data)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1213 .when_writing_file(&pending_index_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1214 any_pending = true;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1215 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1216 self.original_index_file = Some(self.index_file.to_owned());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1217 self.index_file = pending_index_file;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1218 Ok((Some(self.index_file.to_owned()), any_pending))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1219 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1220
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1221 /// Overwrite the canonical file with the diverted file, or write out the
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1222 /// delayed buffer.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1223 /// Returns an error if the revlog is neither diverted nor delayed.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1224 pub fn finalize_pending(&mut self) -> Result<PathBuf, HgError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1225 assert!(!self.is_open());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1226 if self.is_inline() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1227 return Err(HgError::abort(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1228 "revlog with delayed write should not be inline",
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1229 exit_codes::ABORT,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1230 None,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1231 ));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1232 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1233 match (
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1234 self.delayed_buffer.as_ref(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1235 self.original_index_file.as_ref(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1236 ) {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1237 (None, None) => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1238 return Err(HgError::abort(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1239 "neither delay nor divert found on this revlog",
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1240 exit_codes::ABORT,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1241 None,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1242 ));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1243 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1244 (Some(delay), None) => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1245 let mut index_file_handle = self.vfs.open(&self.index_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1246 index_file_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1247 .seek(SeekFrom::End(0))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1248 .when_writing_file(&self.index_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1249 index_file_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1250 .write_all(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1251 &delay.lock().expect("propagate the panic").buffer,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1252 )
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1253 .when_writing_file(&self.index_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1254 self.delayed_buffer = None;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1255 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1256 (None, Some(divert)) => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1257 if self.vfs.exists(&self.index_file) {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1258 self.vfs.rename(&self.index_file, divert, true)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1259 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1260 divert.clone_into(&mut self.index_file);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1261 self.original_index_file = None;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1262 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1263 (Some(_), Some(_)) => unreachable!(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1264 "{} is in an inconsistent state of both delay and divert",
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1265 self.canonical_index_file().display(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1266 ),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1267 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1268 Ok(self.canonical_index_file())
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1269 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1270
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1271 /// `pub` only for `hg-cpython`. This is made a different method than
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1272 /// [`Revlog::index`] in case there is a different invariant that pops up
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1273 /// later.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1274 #[doc(hidden)]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1275 pub fn shared_index(&self) -> &Index {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1276 &self.index
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1277 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1278 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1279
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1280 /// The use of a [`Refcell`] assumes that a given revlog will only
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1281 /// be accessed (read or write) by a single thread.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1282 type UncompressedChunkCache =
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1283 RefCell<LruMap<Revision, Arc<[u8]>, ByMemoryUsage>>;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1284
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1285 /// The node, revision and data for the last revision we've seen. Speeds up
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1286 /// a lot of sequential operations of the revlog.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1287 ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1288 /// The data is not just bytes since it can come from Python and we want to
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1289 /// avoid copies if possible.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1290 type SingleRevisionCache =
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1291 (Node, Revision, Box<dyn Deref<Target = [u8]> + Send>);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1292
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1293 /// A way of progressively filling a buffer with revision data, then return
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1294 /// that buffer. Used to abstract away Python-allocated code to reduce copying
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1295 /// for performance reasons.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1296 pub trait RevisionBuffer {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1297 /// The owned buffer type to return
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1298 type Target;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1299 /// Copies the slice into the buffer
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1300 fn extend_from_slice(&mut self, slice: &[u8]);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1301 /// Returns the now finished owned buffer
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1302 fn finish(self) -> Self::Target;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1303 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1304
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1305 /// A simple vec-based buffer. This is uselessly complicated for the pure Rust
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1306 /// case, but it's the price to pay for Python compatibility.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1307 #[derive(Debug)]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1308 pub(super) struct CoreRevisionBuffer {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1309 buf: Vec<u8>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1310 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1311
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1312 impl CoreRevisionBuffer {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1313 pub fn new() -> Self {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1314 Self { buf: vec![] }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1315 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1316
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1317 #[inline]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1318 pub fn resize(&mut self, size: usize) {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1319 self.buf.reserve_exact(size - self.buf.capacity());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1320 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1321 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1322
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1323 impl RevisionBuffer for CoreRevisionBuffer {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1324 type Target = Vec<u8>;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1325
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1326 #[inline]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1327 fn extend_from_slice(&mut self, slice: &[u8]) {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1328 self.buf.extend_from_slice(slice);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1329 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1330
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1331 #[inline]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1332 fn finish(self) -> Self::Target {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1333 self.buf
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1334 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1335 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1336
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1337 /// Calculate the hash of a revision given its data and its parents.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1338 pub fn hash(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1339 data: &[u8],
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1340 p1_hash: &[u8],
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1341 p2_hash: &[u8],
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1342 ) -> [u8; NODE_BYTES_LENGTH] {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1343 let mut hasher = Sha1::new();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1344 let (a, b) = (p1_hash, p2_hash);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1345 if a > b {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1346 hasher.update(b);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1347 hasher.update(a);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1348 } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1349 hasher.update(a);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1350 hasher.update(b);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1351 }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1352 hasher.update(data);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1353 *hasher.finalize().as_ref()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
1354 }