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