rust/hg-core/src/revlog/inner_revlog.rs
author Raphaël Gomès <rgomes@octobus.net>
Tue, 29 Oct 2024 11:35:01 +0100
changeset 52295 f90796d33aa0
parent 52294 a3fa37bdb7ec
child 52298 645d247d4c75
permissions -rw-r--r--
rust: fix clippy lints These were highlighted by the latest version of clippy (1.82.0).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
52294
a3fa37bdb7ec rust: normalize `_for_unchecked_rev` naming among revlogs and the index
Raphaël Gomès <rgomes@octobus.net>
parents: 52185
diff changeset
   163
    pub fn get_entry(
52164
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`].
52294
a3fa37bdb7ec rust: normalize `_for_unchecked_rev` naming among revlogs and the index
Raphaël Gomès <rgomes@octobus.net>
parents: 52185
diff changeset
   202
    pub fn get_entry_for_unchecked_rev(
52164
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   203
        &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   204
        rev: UncheckedRevision,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   205
    ) -> Result<RevlogEntry, RevlogError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   206
        if rev == NULL_REVISION.into() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   207
            return Ok(self.make_null_entry());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   208
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   209
        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
   210
            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
   211
        })?;
52294
a3fa37bdb7ec rust: normalize `_for_unchecked_rev` naming among revlogs and the index
Raphaël Gomès <rgomes@octobus.net>
parents: 52185
diff changeset
   212
        self.get_entry(rev)
52164
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   213
    }
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
    /// 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
   216
    ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   217
    /// 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
   218
    /// side-file.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   219
    pub fn is_delaying(&self) -> bool {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   220
        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
   221
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   222
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   223
    /// 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
   224
    #[inline(always)]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   225
    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
   226
        self.index.start(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   227
            rev,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   228
            &self
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   229
                .index
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   230
                .get_entry(rev)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   231
                .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
   232
        )
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   233
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   234
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   235
    /// 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
   236
    /// 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
   237
    /// 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
   238
    #[inline(always)]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   239
    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
   240
        self.index
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   241
            .get_entry(rev)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   242
            .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
   243
            .compressed_len() as usize
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   244
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   245
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   246
    /// 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
   247
    #[inline(always)]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   248
    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
   249
        self.start(rev) + self.length(rev)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   250
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   251
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   252
    /// 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
   253
    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
   254
        let base = self
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   255
            .index
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   256
            .get_entry(rev)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   257
            .unwrap()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   258
            .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
   259
        if base.0 == rev.0 {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   260
            NULL_REVISION
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   261
        } 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
   262
            Revision(base.0)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   263
        } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   264
            Revision(rev.0 - 1)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   265
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   266
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   267
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   268
    /// 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
   269
    /// a delta base).
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   270
    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
   271
        if !self.delta_config.sparse_revlog {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   272
            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
   273
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   274
        self.index.is_snapshot_unchecked(rev)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   275
    }
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
    /// 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
   278
    /// 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
   279
    pub fn delta_chain(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   280
        &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   281
        rev: Revision,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   282
        stop_rev: Option<Revision>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   283
    ) -> Result<(Vec<Revision>, bool), HgError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   284
        self.index.delta_chain(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   285
            rev,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   286
            stop_rev,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   287
            self.delta_config.general_delta.into(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   288
        )
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   289
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   290
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   291
    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
   292
        // TODO cache the compressor?
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   293
        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
   294
            CompressionConfig::Zlib { level } => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   295
                Box::new(ZlibCompressor::new(level))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   296
            }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   297
            CompressionConfig::Zstd { level, threads } => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   298
                Box::new(ZstdCompressor::new(level, threads))
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::None => Box::new(NoneCompressor),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   301
        })
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
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   304
    /// 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
   305
    /// 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
   306
    pub fn compress<'data>(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   307
        &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   308
        data: &'data [u8],
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   309
    ) -> Result<Option<Cow<'data, [u8]>>, RevlogError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   310
        if data.is_empty() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   311
            return Ok(Some(data.into()));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   312
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   313
        let res = self.compressor()?.compress(data)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   314
        if let Some(compressed) = res {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   315
            // 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
   316
            return Ok(Some(compressed.into()));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   317
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   318
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   319
        if data[0] == b'\0' {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   320
            return Ok(Some(data.into()));
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
        Ok(None)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   323
    }
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
    /// Decompress a revlog chunk.
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
    /// 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
   328
    /// 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
   329
    pub fn decompress<'a>(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   330
        &'a self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   331
        data: &'a [u8],
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   332
    ) -> Result<Cow<[u8]>, RevlogError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   333
        if data.is_empty() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   334
            return Ok(data.into());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   335
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   336
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   337
        // 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
   338
        // 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
   339
        // important here.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   340
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   341
        let header = data[0];
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   342
        match header {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   343
            // 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
   344
            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
   345
            // 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
   346
            ZSTD_BYTE => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   347
                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
   348
            }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   349
            b'\0' => Ok(data.into()),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   350
            b'u' => Ok((&data[1..]).into()),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   351
            other => Err(HgError::UnsupportedFeature(format!(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   352
                "unknown compression header '{}'",
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   353
                other
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   354
            ))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   355
            .into()),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   356
        }
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
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   359
    /// 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
   360
    ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   361
    /// 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
   362
    ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   363
    /// 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
   364
    /// 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
   365
    /// 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
   366
    ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   367
    /// 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
   368
    /// 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
   369
    pub fn get_segment_for_revs(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   370
        &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   371
        start_rev: Revision,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   372
        end_rev: Revision,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   373
    ) -> Result<(usize, Vec<u8>), HgError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   374
        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
   375
            0
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   376
        } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   377
            let start_entry = self
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   378
                .index
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   379
                .get_entry(start_rev)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   380
                .expect("null revision segment");
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   381
            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
   382
        };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   383
        let end_entry = self
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   384
            .index
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   385
            .get_entry(end_rev)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   386
            .expect("null revision segment");
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   387
        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
   388
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   389
        let length = end - start;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   390
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   391
        // 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
   392
        // support madvise/populate?
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   393
        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
   394
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   395
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   396
    /// 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
   397
    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
   398
        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
   399
            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
   400
                return Ok(chunk.clone());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   401
            }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   402
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   403
        // 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
   404
        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
   405
        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
   406
            HgError::abort(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   407
                format!("revlog decompression error: {}", e),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   408
                exit_codes::ABORT,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   409
                None,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   410
            )
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   411
        })?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   412
        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
   413
        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
   414
            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
   415
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   416
        Ok(uncompressed)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   417
    }
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
    /// 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
   420
    /// 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
   421
    pub fn with_read<R>(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   422
        &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   423
        func: impl FnOnce() -> Result<R, RevlogError>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   424
    ) -> Result<R, RevlogError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   425
        self.enter_reading_context()?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   426
        let res = func();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   427
        self.exit_reading_context();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   428
        res.map_err(Into::into)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   429
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   430
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   431
    /// `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
   432
    #[doc(hidden)]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   433
    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
   434
        if self.is_empty() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   435
            // Nothing to be read
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   436
            return Ok(());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   437
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   438
        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
   439
            return Err(HgError::abort(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   440
                "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
   441
                exit_codes::ABORT,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   442
                None,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   443
            ));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   444
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   445
        self.segment_file.get_read_handle()?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   446
        Ok(())
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
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   449
    /// `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
   450
    #[doc(hidden)]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   451
    pub fn exit_reading_context(&self) {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   452
        self.segment_file.exit_reading_context()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   453
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   454
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   455
    /// 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
   456
    /// 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
   457
    /// from the cache.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   458
    pub fn raw_text<G, T>(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   459
        &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   460
        rev: Revision,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   461
        get_buffer: G,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   462
    ) -> Result<(), RevlogError>
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   463
    where
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   464
        G: FnOnce(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   465
            usize,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   466
            &mut dyn FnMut(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   467
                &mut dyn RevisionBuffer<Target = T>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   468
            ) -> Result<(), RevlogError>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   469
        ) -> Result<(), RevlogError>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   470
    {
52294
a3fa37bdb7ec rust: normalize `_for_unchecked_rev` naming among revlogs and the index
Raphaël Gomès <rgomes@octobus.net>
parents: 52185
diff changeset
   471
        let entry = &self.get_entry(rev)?;
52164
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   472
        let raw_size = entry.uncompressed_len();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   473
        let mut mutex_guard = self
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   474
            .last_revision_cache
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   475
            .lock()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   476
            .expect("lock should not be held");
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   477
        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
   478
            Some((*rev, data.deref().as_ref()))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   479
        } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   480
            None
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   481
        };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   482
        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
   483
            let cache = &mut cache.borrow_mut();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   484
            if let Some(size) = raw_size {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   485
                // 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
   486
                // 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
   487
                // 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
   488
                // is the largest.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   489
                let factor = self
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   490
                    .data_config
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   491
                    .uncompressed_cache_factor
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   492
                    .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
   493
                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
   494
                let limiter_mut = cache.limiter_mut();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   495
                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
   496
                    std::mem::swap(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   497
                        limiter_mut,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   498
                        &mut ByMemoryUsage::new(candidate_size),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   499
                    );
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   500
                }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   501
            }
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
        entry.rawdata(cached_rev, get_buffer)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   504
        // 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
   505
        // 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
   506
        mutex_guard.take();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   507
        Ok(())
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   508
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   509
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   510
    /// Only `pub` for `hg-cpython`.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   511
    /// 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
   512
    /// assumed to be in ascending order.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   513
    ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   514
    /// 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
   515
    #[doc(hidden)]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   516
    pub fn chunks(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   517
        &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   518
        revs: Vec<Revision>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   519
        target_size: Option<u64>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   520
    ) -> Result<Vec<Arc<[u8]>>, RevlogError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   521
        if revs.is_empty() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   522
            return Ok(vec![]);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   523
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   524
        let mut fetched_revs = vec![];
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   525
        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
   526
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   527
        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
   528
            Some(cache) => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   529
                let mut cache = cache.borrow_mut();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   530
                for rev in revs.iter() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   531
                    match cache.get(rev) {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   532
                        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
   533
                        None => fetched_revs.push(*rev),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   534
                    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   535
                }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   536
            }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   537
            None => fetched_revs = revs,
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
        let already_cached = chunks.len();
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
        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
   543
            vec![]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   544
        } 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
   545
            vec![fetched_revs]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   546
        } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   547
            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
   548
        };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   549
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   550
        self.with_read(|| {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   551
            for revs_chunk in sliced_chunks {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   552
                let first_rev = revs_chunk[0];
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   553
                // Skip trailing revisions with empty diff
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   554
                let last_rev_idx = revs_chunk
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   555
                    .iter()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   556
                    .rposition(|r| self.length(*r) != 0)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   557
                    .unwrap_or(revs_chunk.len() - 1);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   558
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   559
                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
   560
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   561
                let (offset, data) =
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   562
                    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
   563
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   564
                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
   565
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   566
                for rev in revs_chunk {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   567
                    let chunk_start = self.start(*rev);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   568
                    let chunk_length = self.length(*rev);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   569
                    // 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
   570
                    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
   571
                    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
   572
                        // 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
   573
                        // specialized decompression
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   574
                        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
   575
                        let is_delta = entry
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   576
                            .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
   577
                            != (*rev).into();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   578
                        let uncompressed = uncompressed_zstd_data(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   579
                            bytes,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   580
                            is_delta,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   581
                            entry.uncompressed_len(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   582
                        )?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   583
                        Cow::Owned(uncompressed)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   584
                    } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   585
                        // Otherwise just fallback to generic decompression.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   586
                        self.decompress(bytes)?
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   587
                    };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   588
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   589
                    chunks.push((*rev, chunk.into()));
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
            Ok(())
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
        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
   596
            let mut cache = cache.borrow_mut();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   597
            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
   598
                cache.insert(*rev, chunk.clone());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   599
            }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   600
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   601
        // 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
   602
        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
   603
        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
   604
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   605
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   606
    /// 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
   607
    ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   608
    /// ``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
   609
    /// Assume that revs are sorted.
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
    /// 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
   612
    /// (payload/chunks-span ratio) is above
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   613
    /// `revlog.data_config.sr_density_threshold`.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   614
    /// 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
   615
    ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   616
    /// 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
   617
    /// will be returned.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   618
    /// 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
   619
    /// 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
   620
    ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   621
    /// 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
   622
    /// still be raised individually.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   623
    pub fn slice_chunk(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   624
        &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   625
        revs: &[Revision],
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   626
        target_size: Option<u64>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   627
    ) -> Result<Vec<Vec<Revision>>, RevlogError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   628
        let target_size =
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   629
            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
   630
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   631
        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
   632
        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
   633
        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
   634
            revs,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   635
            target_density,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   636
            min_gap_size,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   637
        );
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   638
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   639
        let mut sliced = vec![];
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
        for chunk in to_density {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   642
            sliced.extend(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   643
                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
   644
                    .into_iter()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   645
                    .map(ToOwned::to_owned),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   646
            );
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   647
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   648
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   649
        Ok(sliced)
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
    /// 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
   653
    ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   654
    /// 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
   655
    /// 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
   656
    /// 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
   657
    /// 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
   658
    /// each other.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   659
    fn slice_chunk_to_size<'a>(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   660
        &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   661
        revs: &'a [Revision],
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   662
        target_size: Option<u64>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   663
    ) -> Result<Vec<&'a [Revision]>, RevlogError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   664
        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
   665
        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
   666
        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
   667
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   668
        let nothing_to_do = target_size
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   669
            .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
   670
            .unwrap_or(true);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   671
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   672
        if nothing_to_do {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   673
            return Ok(vec![revs]);
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
        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
   676
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   677
        let mut start_rev_idx = 0;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   678
        let mut end_rev_idx = 1;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   679
        let mut chunks = vec![];
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   680
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   681
        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
   682
            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
   683
            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
   684
            if span <= target_size && is_snapshot {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   685
                end_rev_idx = idx + 1;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   686
            } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   687
                let chunk =
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   688
                    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
   689
                if !chunk.is_empty() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   690
                    chunks.push(chunk);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   691
                }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   692
                start_rev_idx = idx;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   693
                start_data = self.start(*rev);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   694
                end_rev_idx = idx + 1;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   695
            }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   696
            if !is_snapshot {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   697
                break;
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
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   700
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   701
        // 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
   702
        // 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
   703
        // 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
   704
        // 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
   705
        let number_of_items = revs.len();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   706
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   707
        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
   708
            end_rev_idx = number_of_items;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   709
            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
   710
                // 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
   711
                break;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   712
            }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   713
            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
   714
            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
   715
            while span > target_size {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   716
                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
   717
                    // 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
   718
                    break;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   719
                }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   720
                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
   721
                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
   722
                span = local_end_data - start_data;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   723
            }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   724
            let chunk =
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   725
                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
   726
            if !chunk.is_empty() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   727
                chunks.push(chunk);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   728
            }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   729
            start_rev_idx = end_rev_idx;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   730
            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
   731
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   732
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   733
        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
   734
        if !chunk.is_empty() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   735
            chunks.push(chunk);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   736
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   737
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   738
        Ok(chunks)
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
    /// 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
   742
    fn trim_chunk<'a>(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   743
        &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   744
        revs: &'a [Revision],
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   745
        start_rev_idx: usize,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   746
        end_rev_idx: Option<usize>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   747
    ) -> &'a [Revision] {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   748
        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
   749
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   750
        // 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
   751
        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
   752
            // 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
   753
            while end_rev_idx > 1
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   754
                && end_rev_idx > start_rev_idx
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   755
                && 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
   756
            {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   757
                end_rev_idx -= 1
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   758
            }
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
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   761
        &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
   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
    /// 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
   765
    pub fn check_hash(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   766
        &self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   767
        p1: Revision,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   768
        p2: Revision,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   769
        expected: &[u8],
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   770
        data: &[u8],
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   771
    ) -> bool {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   772
        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
   773
        let h1 = match e1 {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   774
            Some(ref entry) => entry.hash(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   775
            None => &NULL_NODE,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   776
        };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   777
        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
   778
        let h2 = match e2 {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   779
            Some(ref entry) => entry.hash(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   780
            None => &NULL_NODE,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   781
        };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   782
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   783
        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
   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
    /// 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
   787
    pub fn is_writing(&self) -> bool {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   788
        self.writing_handles.is_some()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   789
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   790
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   791
    /// Open the revlog files for writing
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
    /// 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
   794
    /// 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
   795
    pub fn with_write<R>(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   796
        &mut self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   797
        transaction: &mut impl Transaction,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   798
        data_end: Option<usize>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   799
        func: impl FnOnce() -> R,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   800
    ) -> Result<R, HgError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   801
        if self.is_writing() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   802
            return Ok(func());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   803
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   804
        self.enter_writing_context(data_end, transaction)
52295
f90796d33aa0 rust: fix clippy lints
Raphaël Gomès <rgomes@octobus.net>
parents: 52294
diff changeset
   805
            .inspect_err(|_| {
52164
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   806
                self.exit_writing_context();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   807
            })?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   808
        let res = func();
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
        Ok(res)
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
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   813
    /// `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
   814
    #[doc(hidden)]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   815
    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
   816
        self.writing_handles.take();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   817
        self.segment_file.writing_handle.take();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   818
        self.segment_file.reading_handle.take();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   819
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   820
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   821
    /// `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
   822
    #[doc(hidden)]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   823
    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
   824
        self.writing_handles.as_ref()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   825
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   826
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   827
    /// `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
   828
    #[doc(hidden)]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   829
    pub fn enter_writing_context(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   830
        &mut self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   831
        data_end: Option<usize>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   832
        transaction: &mut impl Transaction,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   833
    ) -> Result<(), HgError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   834
        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
   835
            0
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   836
        } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   837
            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
   838
        };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   839
        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
   840
            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
   841
                Ok(mut f) => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   842
                    if let Some(end) = data_end {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   843
                        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
   844
                            .when_reading_file(&self.data_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   845
                    } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   846
                        f.seek(SeekFrom::End(0))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   847
                            .when_reading_file(&self.data_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   848
                    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   849
                    f
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   850
                }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   851
                Err(e) => match e {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   852
                    HgError::IoError { error, context } => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   853
                        if error.kind() != ErrorKind::NotFound {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   854
                            return Err(HgError::IoError { error, context });
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   855
                        }
52185
8d35941689af rust-vfs: support checkambig
Raphaël Gomès <rgomes@octobus.net>
parents: 52164
diff changeset
   856
                        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
   857
                    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   858
                    e => return Err(e),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   859
                },
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   860
            };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   861
            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
   862
            Some(FileHandle::from_file(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   863
                data_handle,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   864
                dyn_clone::clone_box(&*self.vfs),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   865
                &self.data_file,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   866
            ))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   867
        } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   868
            None
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   869
        };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   870
        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
   871
        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
   872
        if self.is_inline() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   873
            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
   874
        } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   875
            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
   876
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   877
        self.writing_handles = Some(WriteHandles {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   878
            index_handle: index_handle.clone(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   879
            data_handle: data_handle.clone(),
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.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
   882
            Some(index_handle)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   883
        } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   884
            data_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   885
        };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   886
        Ok(())
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   887
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   888
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   889
    /// 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
   890
    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
   891
        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
   892
            if self.data_config.check_ambig {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   893
                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
   894
            } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   895
                self.vfs.open(&self.index_file)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   896
            }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   897
        } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   898
            self.vfs.open(&self.index_file)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   899
        };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   900
        match res {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   901
            Ok(mut handle) => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   902
                handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   903
                    .seek(SeekFrom::End(0))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   904
                    .when_reading_file(&self.index_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   905
                Ok(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   906
                    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
   907
                    {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   908
                        FileHandle::from_file_delayed(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   909
                            handle,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   910
                            dyn_clone::clone_box(&*self.vfs),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   911
                            &self.index_file,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   912
                            delayed_buffer.clone(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   913
                        )?
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   914
                    } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   915
                        FileHandle::from_file(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   916
                            handle,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   917
                            dyn_clone::clone_box(&*self.vfs),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   918
                            &self.index_file,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   919
                        )
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   920
                    },
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   921
                )
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   922
            }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   923
            Err(e) => match e {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   924
                HgError::IoError { error, context } => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   925
                    if error.kind() != ErrorKind::NotFound {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   926
                        return Err(HgError::IoError { error, context });
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   927
                    };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   928
                    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
   929
                    {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   930
                        FileHandle::new_delayed(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   931
                            dyn_clone::clone_box(&*self.vfs),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   932
                            &self.index_file,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   933
                            true,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   934
                            delayed_buffer.clone(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   935
                        )
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   936
                    } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   937
                        FileHandle::new(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   938
                            dyn_clone::clone_box(&*self.vfs),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   939
                            &self.index_file,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   940
                            true,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   941
                            true,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   942
                        )
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   943
                    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   944
                }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   945
                e => Err(e),
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
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   950
    /// 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
   951
    pub fn split_inline(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   952
        &mut self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   953
        header: IndexHeader,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   954
        new_index_file_path: Option<PathBuf>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   955
    ) -> Result<PathBuf, RevlogError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   956
        assert!(self.delayed_buffer.is_none());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   957
        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
   958
        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
   959
            handles.index_handle.flush()?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   960
            self.writing_handles.take();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   961
            self.segment_file.writing_handle.take();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   962
        }
52185
8d35941689af rust-vfs: support checkambig
Raphaël Gomès <rgomes@octobus.net>
parents: 52164
diff changeset
   963
        let mut new_data_file_handle =
8d35941689af rust-vfs: support checkambig
Raphaël Gomès <rgomes@octobus.net>
parents: 52164
diff changeset
   964
            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
   965
        // 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
   966
        new_data_file_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   967
            .set_len(0)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   968
            .when_writing_file(&self.data_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   969
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   970
        self.with_read(|| -> Result<(), RevlogError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   971
            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
   972
                let rev = Revision(r as BaseRevision);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   973
                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
   974
                new_data_file_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   975
                    .write_all(&rev_segment)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   976
                    .when_writing_file(&self.data_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   977
            }
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
                .flush()
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
            Ok(())
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   982
        })?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   983
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   984
        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
   985
            self.index_file = index_path
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
52185
8d35941689af rust-vfs: support checkambig
Raphaël Gomès <rgomes@octobus.net>
parents: 52164
diff changeset
   988
        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
   989
        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
   990
        for r in 0..self.len() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   991
            let rev = Revision(r as BaseRevision);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   992
            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
   993
                panic!(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   994
                    "entry {} should exist in {}",
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   995
                    r,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   996
                    self.index_file.display()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   997
                )
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   998
            });
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   999
            if r == 0 {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1000
                new_data.extend(header.header_bytes);
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
            new_data.extend(entry);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1003
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1004
        new_index_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1005
            .write_all(&new_data)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1006
            .when_writing_file(&self.index_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1007
        // 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
  1008
        // data
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1009
        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
  1010
        self.inline = false;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1011
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1012
        self.segment_file = RandomAccessFile::new(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1013
            dyn_clone::clone_box(&*self.vfs),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1014
            self.data_file.to_owned(),
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
        if existing_handles {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1017
            // 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
  1018
            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
  1019
                new_data_file_handle,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1020
                dyn_clone::clone_box(&*self.vfs),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1021
                &self.data_file,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1022
            ));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1023
            self.writing_handles = Some(WriteHandles {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1024
                index_handle: self.index_write_handle()?,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1025
                data_handle: new_data_handle.clone(),
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.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
  1028
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1029
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1030
        Ok(self.index_file.to_owned())
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1031
    }
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
    /// 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
  1034
    /// - `entry` is the index bytes
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1035
    /// - `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
  1036
    /// - `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
  1037
    /// - `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
  1038
    ///   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
  1039
    /// - `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
  1040
    ///   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
  1041
    ///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1042
    /// 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
  1043
    pub fn write_entry(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1044
        &mut self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1045
        mut transaction: impl Transaction,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1046
        entry: &[u8],
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1047
        header_and_data: (&[u8], &[u8]),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1048
        mut offset: usize,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1049
        index_end: Option<u64>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1050
        data_end: Option<u64>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1051
    ) -> Result<(u64, Option<u64>), HgError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1052
        let current_revision = self.len() - 1;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1053
        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
  1054
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1055
        let is_inline = self.is_inline();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1056
        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
  1057
            None => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1058
                return Err(HgError::abort(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1059
                    "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
  1060
                    exit_codes::ABORT,
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
                ));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1063
            }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1064
            Some(handles) => handles,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1065
        };
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1066
        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
  1067
        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
  1068
        if let Some(end) = index_end {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1069
            index_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1070
                .seek(SeekFrom::Start(end))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1071
                .when_reading_file(&self.index_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1072
        } else {
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::End(0))
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
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1077
        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
  1078
            if let Some(end) = data_end {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1079
                data_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1080
                    .seek(SeekFrom::Start(end))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1081
                    .when_reading_file(&self.data_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1082
            } else {
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::End(0))
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
            }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1087
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1088
        let (header, data) = header_and_data;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1089
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1090
        if !is_inline {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1091
            transaction.add(&self.data_file, offset);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1092
            transaction
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1093
                .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
  1094
            let data_handle = data_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1095
                .as_mut()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1096
                .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
  1097
            if !header.is_empty() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1098
                data_handle.write_all(header)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1099
            }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1100
            data_handle.write_all(data)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1101
            match &mut self.delayed_buffer {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1102
                Some(buf) => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1103
                    buf.lock()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1104
                        .expect("propagate the panic")
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1105
                        .buffer
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1106
                        .write_all(entry)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1107
                        .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
  1108
                }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1109
                None => index_handle.write_all(entry)?,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1110
            }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1111
        } 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
  1112
            return Err(HgError::abort(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1113
                "invalid delayed write on inline revlog",
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1114
                exit_codes::ABORT,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1115
                None,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1116
            ));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1117
        } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1118
            offset += current_revision * entry.len();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1119
            transaction.add(&canonical_index_file, offset);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1120
            index_handle.write_all(entry)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1121
            index_handle.write_all(header)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1122
            index_handle.write_all(data)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1123
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1124
        let data_position = match data_handle {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1125
            Some(h) => Some(h.position()?),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1126
            None => None,
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
        Ok((index_handle.position()?, data_position))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1129
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1130
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1131
    /// 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
  1132
    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
  1133
        self.original_index_file
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1134
            .as_ref()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1135
            .map(ToOwned::to_owned)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1136
            .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
  1137
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1138
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1139
    /// 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
  1140
    fn diverted_index(&self) -> PathBuf {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1141
        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
  1142
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1143
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1144
    /// 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
  1145
    pub fn is_open(&self) -> bool {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1146
        self.segment_file.is_open()
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
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1149
    /// 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
  1150
    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
  1151
        assert!(!self.is_open());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1152
        if self.is_inline() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1153
            return Err(HgError::abort(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1154
                "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
  1155
                exit_codes::ABORT,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1156
                None,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1157
            ));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1158
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1159
        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
  1160
        {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1161
            // Delay or divert already happening
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1162
            return Ok(None);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1163
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1164
        if self.is_empty() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1165
            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
  1166
            self.index_file = self.diverted_index();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1167
            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
  1168
                self.vfs.unlink(&self.index_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1169
            }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1170
            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
  1171
        } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1172
            self.delayed_buffer =
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1173
                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
  1174
            Ok(None)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1175
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1176
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1177
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1178
    /// 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
  1179
    /// (on disk temporary file)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1180
    pub fn write_pending(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1181
        &mut self,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1182
    ) -> Result<(Option<PathBuf>, bool), HgError> {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1183
        assert!(!self.is_open());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1184
        if self.is_inline() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1185
            return Err(HgError::abort(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1186
                "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
  1187
                exit_codes::ABORT,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1188
                None,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1189
            ));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1190
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1191
        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
  1192
            return Ok((None, true));
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
        let mut any_pending = false;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1195
        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
  1196
        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
  1197
            self.vfs.unlink(&pending_index_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1198
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1199
        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
  1200
        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
  1201
            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
  1202
            index_file_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1203
                .seek(SeekFrom::End(0))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1204
                .when_writing_file(&pending_index_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1205
            let delayed_data =
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1206
                &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
  1207
            index_file_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1208
                .write_all(delayed_data)
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1209
                .when_writing_file(&pending_index_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1210
            any_pending = true;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1211
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1212
        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
  1213
        self.index_file = pending_index_file;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1214
        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
  1215
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1216
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1217
    /// 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
  1218
    /// delayed buffer.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1219
    /// 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
  1220
    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
  1221
        assert!(!self.is_open());
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1222
        if self.is_inline() {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1223
            return Err(HgError::abort(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1224
                "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
  1225
                exit_codes::ABORT,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1226
                None,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1227
            ));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1228
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1229
        match (
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1230
            self.delayed_buffer.as_ref(),
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1231
            self.original_index_file.as_ref(),
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
            (None, None) => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1234
                return Err(HgError::abort(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1235
                    "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
  1236
                    exit_codes::ABORT,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1237
                    None,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1238
                ));
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1239
            }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1240
            (Some(delay), None) => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1241
                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
  1242
                index_file_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1243
                    .seek(SeekFrom::End(0))
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1244
                    .when_writing_file(&self.index_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1245
                index_file_handle
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1246
                    .write_all(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1247
                        &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
  1248
                    )
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1249
                    .when_writing_file(&self.index_file)?;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1250
                self.delayed_buffer = None;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1251
            }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1252
            (None, Some(divert)) => {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1253
                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
  1254
                    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
  1255
                }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1256
                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
  1257
                self.original_index_file = None;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1258
            }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1259
            (Some(_), Some(_)) => unreachable!(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1260
                "{} 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
  1261
                self.canonical_index_file().display(),
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
        }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1264
        Ok(self.canonical_index_file())
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1265
    }
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
    /// `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
  1268
    /// [`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
  1269
    /// later.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1270
    #[doc(hidden)]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1271
    pub fn shared_index(&self) -> &Index {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1272
        &self.index
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1273
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1274
}
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1275
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1276
/// 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
  1277
/// 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
  1278
type UncompressedChunkCache =
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1279
    RefCell<LruMap<Revision, Arc<[u8]>, ByMemoryUsage>>;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1280
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1281
/// 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
  1282
/// 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
  1283
///
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1284
/// 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
  1285
/// avoid copies if possible.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1286
type SingleRevisionCache =
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1287
    (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
  1288
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1289
/// 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
  1290
/// 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
  1291
/// for performance reasons.
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1292
pub trait RevisionBuffer {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1293
    /// The owned buffer type to return
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1294
    type Target;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1295
    /// Copies the slice into the buffer
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1296
    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
  1297
    /// Returns the now finished owned buffer
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1298
    fn finish(self) -> Self::Target;
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1299
}
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1300
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1301
/// 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
  1302
/// 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
  1303
#[derive(Debug)]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1304
pub(super) struct CoreRevisionBuffer {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1305
    buf: Vec<u8>,
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1306
}
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1307
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1308
impl CoreRevisionBuffer {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1309
    pub fn new() -> Self {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1310
        Self { buf: vec![] }
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
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1313
    #[inline]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1314
    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
  1315
        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
  1316
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1317
}
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1318
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1319
impl RevisionBuffer for CoreRevisionBuffer {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1320
    type Target = Vec<u8>;
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
    #[inline]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1323
    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
  1324
        self.buf.extend_from_slice(slice);
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
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1327
    #[inline]
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1328
    fn finish(self) -> Self::Target {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1329
        self.buf
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
}
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1332
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1333
/// 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
  1334
pub fn hash(
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1335
    data: &[u8],
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1336
    p1_hash: &[u8],
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1337
    p2_hash: &[u8],
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1338
) -> [u8; NODE_BYTES_LENGTH] {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1339
    let mut hasher = Sha1::new();
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1340
    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
  1341
    if a > b {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1342
        hasher.update(b);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1343
        hasher.update(a);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1344
    } else {
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1345
        hasher.update(a);
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
    }
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1348
    hasher.update(data);
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1349
    *hasher.finalize().as_ref()
e01e84e5e426 rust-revlog: add a Rust-only `InnerRevlog`
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
  1350
}