| 1 |
// revlog.rs |
| 2 |
// |
| 3 |
// Copyright 2019-2020 Georges Racinet <[email protected]> |
| 4 |
// 2020-2024 Raphaël Gomès <[email protected]> |
| 5 |
// 2024 Georges Racinet <[email protected]> |
| 6 |
// |
| 7 |
// This software may be used and distributed according to the terms of the |
| 8 |
// GNU General Public License version 2 or any later version. |
| 9 |
#![allow(non_snake_case)] |
| 10 |
use std::collections::HashMap; |
| 11 |
use std::collections::HashSet; |
| 12 |
use std::os::fd::AsRawFd; |
| 13 |
use std::sync::atomic::AtomicUsize; |
| 14 |
use std::sync::atomic::Ordering; |
| 15 |
use std::sync::Arc; |
| 16 |
use std::sync::RwLock; |
| 17 |
use std::sync::RwLockReadGuard; |
| 18 |
use std::sync::RwLockWriteGuard; |
| 19 |
|
| 20 |
use hg::dyn_bytes::DynBytes; |
| 21 |
use hg::errors::HgError; |
| 22 |
use hg::revlog::index::Index; |
| 23 |
use hg::revlog::index::IndexHeader; |
| 24 |
use hg::revlog::index::Phase; |
| 25 |
use hg::revlog::index::RevisionDataParams; |
| 26 |
use hg::revlog::index::SnapshotsCache; |
| 27 |
use hg::revlog::index::INDEX_ENTRY_SIZE; |
| 28 |
use hg::revlog::inner_revlog::InnerRevlog as CoreInnerRevlog; |
| 29 |
use hg::revlog::nodemap::NodeMap; |
| 30 |
use hg::revlog::nodemap::NodeMapError; |
| 31 |
use hg::revlog::nodemap::NodeTree as CoreNodeTree; |
| 32 |
use hg::revlog::options::RevlogOpenOptions; |
| 33 |
use hg::revlog::path_encode::PathEncoding; |
| 34 |
use hg::revlog::RevlogError; |
| 35 |
use hg::revlog::RevlogIndex; |
| 36 |
use hg::revlog::RevlogType; |
| 37 |
use hg::utils::files::get_bytes_from_path; |
| 38 |
use hg::utils::files::get_path_from_bytes; |
| 39 |
use hg::utils::u32_u; |
| 40 |
use hg::utils::RawData; |
| 41 |
use hg::vfs::EncodedVfs; |
| 42 |
use hg::vfs::VfsImpl; |
| 43 |
use hg::BaseRevision; |
| 44 |
use hg::Revision; |
| 45 |
use hg::UncheckedRevision; |
| 46 |
use hg::NULL_REVISION; |
| 47 |
use pyo3::buffer::PyBuffer; |
| 48 |
use pyo3::conversion::IntoPyObject; |
| 49 |
use pyo3::exceptions::PyIndexError; |
| 50 |
use pyo3::exceptions::PyTypeError; |
| 51 |
use pyo3::exceptions::PyValueError; |
| 52 |
use pyo3::prelude::*; |
| 53 |
use pyo3::types::PyBool; |
| 54 |
use pyo3::types::PyBytes; |
| 55 |
use pyo3::types::PyBytesMethods; |
| 56 |
use pyo3::types::PyDict; |
| 57 |
use pyo3::types::PyList; |
| 58 |
use pyo3::types::PySet; |
| 59 |
use pyo3::types::PyTuple; |
| 60 |
use pyo3::IntoPyObjectExt; |
| 61 |
use pyo3_sharedref::PyShareable; |
| 62 |
use pyo3_sharedref::SharedByPyObject; |
| 63 |
|
| 64 |
use crate::exceptions::graph_error; |
| 65 |
use crate::exceptions::map_lock_error; |
| 66 |
use crate::exceptions::map_try_lock_error; |
| 67 |
use crate::exceptions::nodemap_error; |
| 68 |
use crate::exceptions::rev_not_in_index; |
| 69 |
use crate::exceptions::revlog_error; |
| 70 |
use crate::exceptions::revlog_error_bare; |
| 71 |
use crate::exceptions::revlog_error_from_msg; |
| 72 |
use crate::node::node_from_py_bytes; |
| 73 |
use crate::node::node_prefix_from_py_bytes; |
| 74 |
use crate::node::py_node_for_rev; |
| 75 |
use crate::revision::check_revision; |
| 76 |
use crate::revision::rev_pyiter_collect; |
| 77 |
use crate::revision::rev_pyiter_collect_or_else; |
| 78 |
use crate::revision::revs_py_list; |
| 79 |
use crate::revision::revs_py_set; |
| 80 |
use crate::revision::PyRevision; |
| 81 |
use crate::transaction::PyTransaction; |
| 82 |
use crate::utils::new_submodule; |
| 83 |
use crate::utils::take_buffer_with_slice; |
| 84 |
use crate::utils::with_pybytes_buffer; |
| 85 |
use crate::utils::PyBytesDeref; |
| 86 |
|
| 87 |
mod config; |
| 88 |
use config::*; |
| 89 |
mod index; |
| 90 |
use index::py_tuple_to_revision_data_params; |
| 91 |
use index::revision_data_params_to_py_tuple; |
| 92 |
pub use index::PySharedIndex; |
| 93 |
|
| 94 |
#[pyclass] |
| 95 |
struct ReadingContextManager { |
| 96 |
inner_revlog: Py<InnerRevlog>, |
| 97 |
} |
| 98 |
|
| 99 |
#[pymethods] |
| 100 |
impl ReadingContextManager { |
| 101 |
fn __enter__(slf: PyRef<'_, Self>) -> PyResult<()> { |
| 102 |
let inner_bound = slf.inner_revlog.bind(slf.py()); |
| 103 |
let shareable = &inner_bound.borrow().irl; |
| 104 |
// Safety: the owner is correct and we won't use `share()` anyway |
| 105 |
let core_irl = |
| 106 |
unsafe { shareable.borrow_with_owner(inner_bound) }.read(); |
| 107 |
core_irl |
| 108 |
.enter_reading_context() |
| 109 |
.map_err(revlog_error_from_msg) |
| 110 |
.inspect_err(|_e| { |
| 111 |
// `__exit__` is not called from Python if `__enter__` fails |
| 112 |
core_irl.exit_reading_context(); |
| 113 |
}) |
| 114 |
} |
| 115 |
|
| 116 |
#[pyo3(signature = (*_args))] |
| 117 |
fn __exit__(slf: PyRef<'_, Self>, _args: &Bound<'_, PyTuple>) { |
| 118 |
let inner_bound = slf.inner_revlog.bind(slf.py()); |
| 119 |
let shareable = &inner_bound.borrow().irl; |
| 120 |
// Safety: the owner is correct and we won't use `share()` anyway |
| 121 |
let core_irl_ref = unsafe { shareable.borrow_with_owner(inner_bound) }; |
| 122 |
core_irl_ref.read().exit_reading_context(); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
#[pyclass] |
| 127 |
struct WritingContextManager { |
| 128 |
inner_revlog: Py<InnerRevlog>, |
| 129 |
transaction: RwLock<PyTransaction>, |
| 130 |
data_end: Option<usize>, |
| 131 |
} |
| 132 |
|
| 133 |
#[pymethods] |
| 134 |
impl WritingContextManager { |
| 135 |
fn __enter__(slf: PyRefMut<'_, Self>) -> PyResult<()> { |
| 136 |
let inner_bound = slf.inner_revlog.bind(slf.py()); |
| 137 |
let shareable = &inner_bound.borrow_mut().irl; |
| 138 |
// Safety: the owner is correct and we won't use `share()` anyway |
| 139 |
let mut core_irl = |
| 140 |
unsafe { shareable.borrow_with_owner(inner_bound) }.write(); |
| 141 |
core_irl |
| 142 |
.enter_writing_context( |
| 143 |
slf.data_end, |
| 144 |
&mut *slf |
| 145 |
.transaction |
| 146 |
.try_write() |
| 147 |
.expect("transaction should be protected by the GIL"), |
| 148 |
) |
| 149 |
.map_err(revlog_error_from_msg) |
| 150 |
.inspect_err(|_e| { |
| 151 |
// `__exit__` is not called from Python if `__enter__` fails |
| 152 |
core_irl.exit_writing_context(); |
| 153 |
}) |
| 154 |
} |
| 155 |
|
| 156 |
#[pyo3(signature = (*_args))] |
| 157 |
fn __exit__(slf: PyRef<'_, Self>, _args: &Bound<'_, PyTuple>) { |
| 158 |
let inner_bound = slf.inner_revlog.bind(slf.py()); |
| 159 |
let shareable = &inner_bound.borrow().irl; |
| 160 |
// Safety: the owner is correct and we won't use `share()` anyway |
| 161 |
let core_irl_ref = unsafe { shareable.borrow_with_owner(inner_bound) }; |
| 162 |
core_irl_ref.write().exit_writing_context(); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
struct PySnapshotsCache<'a, 'py: 'a>(&'a Bound<'py, PyDict>); |
| 167 |
|
| 168 |
impl PySnapshotsCache<'_, '_> { |
| 169 |
fn insert_for_with_py_result( |
| 170 |
&self, |
| 171 |
rev: BaseRevision, |
| 172 |
value: BaseRevision, |
| 173 |
) -> PyResult<()> { |
| 174 |
match self.0.get_item(rev)? { |
| 175 |
Some(obj) => obj.cast::<PySet>()?.add(value), |
| 176 |
None => { |
| 177 |
let set = PySet::new(self.0.py(), vec![value])?; |
| 178 |
self.0.set_item(rev, set) |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
impl SnapshotsCache for PySnapshotsCache<'_, '_> { |
| 185 |
fn insert_for( |
| 186 |
&mut self, |
| 187 |
rev: BaseRevision, |
| 188 |
value: BaseRevision, |
| 189 |
) -> Result<(), RevlogError> { |
| 190 |
self.insert_for_with_py_result(rev, value).map_err(|_| { |
| 191 |
RevlogError::Other(HgError::unsupported( |
| 192 |
"Error in Python caches handling", |
| 193 |
)) |
| 194 |
}) |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
// Only used from Python *tests* |
| 199 |
#[doc(hidden)] |
| 200 |
#[pyclass] |
| 201 |
pub struct PyFileHandle { |
| 202 |
inner_file: std::os::fd::RawFd, |
| 203 |
} |
| 204 |
|
| 205 |
#[pymethods] |
| 206 |
impl PyFileHandle { |
| 207 |
#[new] |
| 208 |
fn new(handle: std::os::fd::RawFd) -> Self { |
| 209 |
Self { inner_file: handle } |
| 210 |
} |
| 211 |
|
| 212 |
fn tell(&self, py: Python<'_>) -> PyResult<Py<PyAny>> { |
| 213 |
let locals = PyDict::new(py); |
| 214 |
locals.set_item("os", py.import("os")?)?; |
| 215 |
locals.set_item("fd", self.inner_file)?; |
| 216 |
let f = py.eval(c"os.fdopen(fd)", None, Some(&locals))?; |
| 217 |
|
| 218 |
// Prevent Python from closing the file after garbage collecting. |
| 219 |
// This is fine since Rust is still holding on to the actual File. |
| 220 |
// (and also because it's only used in tests). |
| 221 |
std::mem::forget(f.clone()); |
| 222 |
|
| 223 |
locals.set_item("f", f)?; |
| 224 |
let res = py.eval(c"f.tell()", None, Some(&locals))?; |
| 225 |
Ok(res.unbind()) |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
#[pyclass] |
| 230 |
#[allow(dead_code)] |
| 231 |
pub(crate) struct InnerRevlog { |
| 232 |
pub(crate) irl: PyShareable<CoreInnerRevlog>, |
| 233 |
docket: Option<Py<PyAny>>, |
| 234 |
// Holds a reference to the mmap'ed persistent nodemap data |
| 235 |
nodemap_mmap: Option<PyBuffer<u8>>, |
| 236 |
// Holds a reference to the mmap'ed persistent index data |
| 237 |
index_mmap: Option<PyBuffer<u8>>, |
| 238 |
revision_cache: Option<Py<PyAny>>, |
| 239 |
head_revs_py_list: Option<Py<PyList>>, |
| 240 |
head_node_ids_py_list: Option<Py<PyList>>, |
| 241 |
nodemap_queries: AtomicUsize, |
| 242 |
} |
| 243 |
|
| 244 |
#[pymethods] |
| 245 |
impl InnerRevlog { |
| 246 |
#[new] |
| 247 |
// The Python side has authority on this signature. |
| 248 |
#[allow(clippy::too_many_arguments)] |
| 249 |
fn new( |
| 250 |
vfs_base: &Bound<'_, PyBytes>, |
| 251 |
vfs_is_readonly: bool, |
| 252 |
index_data: &Bound<'_, PyAny>, |
| 253 |
index_file: &Bound<'_, PyBytes>, |
| 254 |
data_file: &Bound<'_, PyBytes>, |
| 255 |
sidedata_file: &Bound<'_, PyAny>, |
| 256 |
inline: bool, |
| 257 |
data_config: &Bound<'_, PyAny>, |
| 258 |
delta_config: &Bound<'_, PyAny>, |
| 259 |
feature_config: &Bound<'_, PyAny>, |
| 260 |
chunk_cache: &Bound<'_, PyAny>, |
| 261 |
default_compression_header: &Bound<'_, PyAny>, |
| 262 |
revlog_type: usize, |
| 263 |
encoding: i64, |
| 264 |
) -> PyResult<Self> { |
| 265 |
// Let clippy accept the unused arguments. This is a bit better than |
| 266 |
// a blank `allow` directive |
| 267 |
let _ = sidedata_file; |
| 268 |
let _ = chunk_cache; |
| 269 |
let _ = default_compression_header; |
| 270 |
|
| 271 |
let index_file = get_path_from_bytes(index_file.as_bytes()).to_owned(); |
| 272 |
let data_file = get_path_from_bytes(data_file.as_bytes()).to_owned(); |
| 273 |
let revlog_type = |
| 274 |
RevlogType::try_from(revlog_type).map_err(revlog_error_from_msg)?; |
| 275 |
let data_config = extract_data_config(data_config, revlog_type)?; |
| 276 |
let delta_config = extract_delta_config(delta_config, revlog_type)?; |
| 277 |
let feature_config = |
| 278 |
extract_feature_config(feature_config, revlog_type)?; |
| 279 |
let options = RevlogOpenOptions::new( |
| 280 |
inline, |
| 281 |
data_config, |
| 282 |
delta_config, |
| 283 |
feature_config, |
| 284 |
); |
| 285 |
|
| 286 |
// Safety: we keep the buffer around inside the returned instance as |
| 287 |
// `index_mmap` |
| 288 |
let (buf, bytes) = unsafe { take_buffer_with_slice(index_data)? }; |
| 289 |
let index = Index::new(DynBytes::new(bytes), options.index_header()) |
| 290 |
.map_err(revlog_error_from_msg)?; |
| 291 |
|
| 292 |
let base = get_path_from_bytes(vfs_base.as_bytes()).to_owned(); |
| 293 |
let core = CoreInnerRevlog::new( |
| 294 |
match encoding { |
| 295 |
0 => Box::new(VfsImpl::new( |
| 296 |
base, |
| 297 |
vfs_is_readonly, |
| 298 |
PathEncoding::None, |
| 299 |
)), |
| 300 |
1 => Box::new(EncodedVfs::new( |
| 301 |
base, |
| 302 |
vfs_is_readonly, |
| 303 |
PathEncoding::DotEncode, |
| 304 |
)), |
| 305 |
2 => Box::new(EncodedVfs::new( |
| 306 |
base, |
| 307 |
vfs_is_readonly, |
| 308 |
PathEncoding::Plain, |
| 309 |
)), |
| 310 |
_ => unreachable!("unknown encoding {}", encoding), |
| 311 |
}, |
| 312 |
index, |
| 313 |
index_file, |
| 314 |
data_file, |
| 315 |
data_config, |
| 316 |
delta_config, |
| 317 |
feature_config, |
| 318 |
revlog_type, |
| 319 |
None, |
| 320 |
); |
| 321 |
Ok(Self { |
| 322 |
irl: core.into(), |
| 323 |
docket: None, |
| 324 |
nodemap_mmap: None, |
| 325 |
index_mmap: buf.into(), |
| 326 |
head_revs_py_list: None, |
| 327 |
head_node_ids_py_list: None, |
| 328 |
revision_cache: None, |
| 329 |
nodemap_queries: AtomicUsize::new(0), |
| 330 |
}) |
| 331 |
} |
| 332 |
|
| 333 |
#[getter] |
| 334 |
fn has_revdiff_extra(&self) -> bool { |
| 335 |
true |
| 336 |
} |
| 337 |
|
| 338 |
#[getter] |
| 339 |
fn canonical_index_file( |
| 340 |
slf: &Bound<'_, Self>, |
| 341 |
py: Python<'_>, |
| 342 |
) -> PyResult<Py<PyBytes>> { |
| 343 |
Self::with_core_read(slf, |_self_ref, irl| { |
| 344 |
let path = irl.canonical_index_file(); |
| 345 |
Ok(PyBytes::new(py, &get_bytes_from_path(path)).into()) |
| 346 |
}) |
| 347 |
} |
| 348 |
|
| 349 |
#[getter] |
| 350 |
fn is_delaying(slf: &Bound<'_, Self>) -> PyResult<bool> { |
| 351 |
Self::with_core_read(slf, |_self_ref, irl| Ok(irl.is_delaying())) |
| 352 |
} |
| 353 |
|
| 354 |
#[getter] |
| 355 |
fn inline(slf: &Bound<'_, Self>) -> PyResult<bool> { |
| 356 |
Self::with_core_read(slf, |_self_ref, irl| Ok(irl.is_inline())) |
| 357 |
} |
| 358 |
|
| 359 |
#[setter] |
| 360 |
fn set_inline(slf: &Bound<'_, Self>, inline: bool) -> PyResult<()> { |
| 361 |
Self::with_core_write(slf, |_self_ref, mut irl| { |
| 362 |
irl.inline = inline; |
| 363 |
Ok(()) |
| 364 |
}) |
| 365 |
} |
| 366 |
|
| 367 |
#[getter] |
| 368 |
fn is_writing(slf: &Bound<'_, Self>) -> PyResult<bool> { |
| 369 |
Self::with_core_read(slf, |_self_ref, irl| Ok(irl.is_writing())) |
| 370 |
} |
| 371 |
|
| 372 |
#[getter] |
| 373 |
fn is_open(slf: &Bound<'_, Self>) -> PyResult<bool> { |
| 374 |
Self::with_core_read(slf, |_self_ref, irl| Ok(irl.is_open())) |
| 375 |
} |
| 376 |
|
| 377 |
fn get_cached_text( |
| 378 |
slf: &Bound<'_, Self>, |
| 379 |
py: Python<'_>, |
| 380 |
rev: PyRevision, |
| 381 |
) -> PyResult<Py<PyAny>> { |
| 382 |
if let Some(tuple) = slf.borrow().inner_get_cached_text(py, rev)? { |
| 383 |
return tuple.into_py_any(py); |
| 384 |
} |
| 385 |
let maybe_tuple = Self::with_core_read(slf, |_self_ref, irl| { |
| 386 |
if let Some(cached) = irl.get_rev_cache() { |
| 387 |
// Ok because even if the Revision is invalid, it will just |
| 388 |
// result in a cache miss. |
| 389 |
if cached.rev == Revision(rev.0) { |
| 390 |
// TODO: we should stop returning PyByte and start returning |
| 391 |
// memoryview to avoid the memcopy here. |
| 392 |
let bytes = PyBytes::new(py, &cached); |
| 393 |
let tuple = PyTuple::new( |
| 394 |
py, |
| 395 |
&[ |
| 396 |
rev.into_py_any(py)?, |
| 397 |
bytes.into_py_any(py)?, |
| 398 |
false.into_py_any(py)?, |
| 399 |
], |
| 400 |
)?; |
| 401 |
return Ok(Some(tuple.into_py_any(py)?)); |
| 402 |
} |
| 403 |
} |
| 404 |
Ok(None) |
| 405 |
})?; |
| 406 |
if let Some(tuple) = maybe_tuple { |
| 407 |
slf.borrow_mut().revision_cache = Some(tuple.clone_ref(py)); |
| 408 |
Ok(tuple) |
| 409 |
} else { |
| 410 |
Ok(py.None()) |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
/// the inner part of getting a PyTuple from the cache. |
| 415 |
fn inner_get_cached_text( |
| 416 |
&self, |
| 417 |
py: Python<'_>, |
| 418 |
rev: PyRevision, |
| 419 |
) -> Result<Option<Py<PyTuple>>, PyErr> { |
| 420 |
if let Some(cache) = &self.revision_cache { |
| 421 |
let tuple: &Bound<'_, PyTuple> = cache.cast_bound(py)?; |
| 422 |
let cached_rev: PyRevision = tuple.get_item(0)?.extract()?; |
| 423 |
if rev == cached_rev { |
| 424 |
let a = tuple.as_unbound(); |
| 425 |
let ret: Py<PyTuple> = a.clone_ref(py); |
| 426 |
Ok(Some(ret)) |
| 427 |
} else { |
| 428 |
Ok(None) |
| 429 |
} |
| 430 |
} else { |
| 431 |
Ok(None) |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
fn cache_revision_text( |
| 436 |
slf: &Bound<'_, Self>, |
| 437 |
py: Python<'_>, |
| 438 |
rev: PyRevision, |
| 439 |
data: Py<PyBytes>, |
| 440 |
validated: bool, |
| 441 |
) -> PyResult<()> { |
| 442 |
let mut self_ref = slf.borrow_mut(); |
| 443 |
let replace = match &mut self_ref.revision_cache { |
| 444 |
None => true, |
| 445 |
Some(cache) => { |
| 446 |
let tuple: &Bound<'_, PyTuple> = cache.cast_bound(py)?; |
| 447 |
let cached_rev: PyRevision = tuple.get_item(0)?.extract()?; |
| 448 |
let validated: bool = tuple.get_item(2)?.extract()?; |
| 449 |
rev != cached_rev || !validated |
| 450 |
} |
| 451 |
}; |
| 452 |
if replace { |
| 453 |
self_ref.revision_cache = Some( |
| 454 |
PyTuple::new( |
| 455 |
py, |
| 456 |
&[ |
| 457 |
rev.into_py_any(py)?, |
| 458 |
data.clone_ref(py).into_py_any(py)?, |
| 459 |
validated.into_py_any(py)?, |
| 460 |
], |
| 461 |
)? |
| 462 |
.into_py_any(py)?, |
| 463 |
); |
| 464 |
drop(self_ref); |
| 465 |
|
| 466 |
// Ok because Python only sets this if the revision has been |
| 467 |
// checked |
| 468 |
let rev = Revision(rev.0); |
| 469 |
Self::with_core_read(slf, |_self_ref, irl| { |
| 470 |
irl.set_rev_cache( |
| 471 |
rev, |
| 472 |
Arc::new(PyBytesDeref::new(py, data.clone_ref(py))), |
| 473 |
); |
| 474 |
Ok(()) |
| 475 |
})?; |
| 476 |
} |
| 477 |
Ok(()) |
| 478 |
} |
| 479 |
|
| 480 |
fn clear_cached_text( |
| 481 |
slf: &Bound<'_, Self>, |
| 482 |
py: Python<'_>, |
| 483 |
rev: PyRevision, |
| 484 |
) -> PyResult<()> { |
| 485 |
let mut self_ref = slf.borrow_mut(); |
| 486 |
let clear = if let Some(cache) = &self_ref.revision_cache { |
| 487 |
let tuple: &Bound<'_, PyTuple> = cache.cast_bound(py)?; |
| 488 |
let cached_rev: PyRevision = tuple.get_item(0)?.extract()?; |
| 489 |
rev == cached_rev |
| 490 |
} else { |
| 491 |
false |
| 492 |
}; |
| 493 |
if clear { |
| 494 |
self_ref.revision_cache.take(); |
| 495 |
} |
| 496 |
drop(self_ref); |
| 497 |
// call it unconditionnaly in case the python and the rust cache |
| 498 |
// diverged. |
| 499 |
Self::with_core_read(slf, |_self_ref, irl| { |
| 500 |
irl.clear_rev_cache(Revision(rev.0)); |
| 501 |
Ok(()) |
| 502 |
})?; |
| 503 |
Ok(()) |
| 504 |
} |
| 505 |
|
| 506 |
#[getter] |
| 507 |
fn index_file( |
| 508 |
slf: &Bound<'_, Self>, |
| 509 |
py: Python<'_>, |
| 510 |
) -> PyResult<Py<PyBytes>> { |
| 511 |
Self::with_core_read(slf, |_self_ref, irl| { |
| 512 |
let path = get_bytes_from_path(&irl.index_file); |
| 513 |
Ok(PyBytes::new(py, &path).unbind()) |
| 514 |
}) |
| 515 |
} |
| 516 |
|
| 517 |
#[setter] |
| 518 |
fn set_index_file( |
| 519 |
slf: &Bound<'_, Self>, |
| 520 |
path: &Bound<'_, PyBytes>, |
| 521 |
) -> PyResult<()> { |
| 522 |
Self::with_core_write(slf, |_self_ref, mut irl| { |
| 523 |
let path = get_path_from_bytes(path.as_bytes()); |
| 524 |
path.clone_into(&mut irl.index_file); |
| 525 |
Ok(()) |
| 526 |
}) |
| 527 |
} |
| 528 |
|
| 529 |
// This is only used in Python *tests* |
| 530 |
#[getter] |
| 531 |
#[doc(hidden)] |
| 532 |
fn _writinghandles( |
| 533 |
slf: &Bound<'_, Self>, |
| 534 |
py: Python<'_>, |
| 535 |
) -> PyResult<Py<PyAny>> { |
| 536 |
Self::with_core_read(slf, |_self_ref, irl| { |
| 537 |
let handles = irl.python_writing_handles(); |
| 538 |
match handles.as_ref() { |
| 539 |
None => Ok(py.None()), |
| 540 |
Some(handles) => { |
| 541 |
let index_handle = PyFileHandle::new( |
| 542 |
handles.index_handle.file.as_raw_fd(), |
| 543 |
); |
| 544 |
let data_handle = handles |
| 545 |
.data_handle |
| 546 |
.as_ref() |
| 547 |
.map(|h| PyFileHandle::new(h.file.as_raw_fd())); |
| 548 |
Ok(PyTuple::new( |
| 549 |
py, |
| 550 |
&[ |
| 551 |
index_handle.into_py_any(py)?, |
| 552 |
data_handle.into_py_any(py)?, |
| 553 |
py.None(), // Sidedata handle |
| 554 |
], |
| 555 |
)? |
| 556 |
.unbind() |
| 557 |
.into()) |
| 558 |
} |
| 559 |
} |
| 560 |
}) |
| 561 |
} |
| 562 |
|
| 563 |
fn clear_cache(slf: &Bound<'_, Self>) -> PyResult<Py<PyAny>> { |
| 564 |
assert!(!Self::is_delaying(slf)?); |
| 565 |
let mut self_ref = slf.borrow_mut(); |
| 566 |
self_ref.revision_cache.take(); |
| 567 |
self_ref.nodemap_queries.store(0, Ordering::Relaxed); |
| 568 |
drop(self_ref); |
| 569 |
|
| 570 |
Self::with_core_write(slf, |_self_ref, mut irl| { |
| 571 |
irl.clear_cache(); |
| 572 |
Ok(slf.py().None()) |
| 573 |
}) |
| 574 |
} |
| 575 |
|
| 576 |
fn seen_file_size(slf: &Bound<'_, Self>, size: u32) -> PyResult<Py<PyAny>> { |
| 577 |
Self::with_core_read(slf, |_self_ref, irl| { |
| 578 |
irl.seen_file_size(u32_u(size)); |
| 579 |
Ok(slf.py().None()) |
| 580 |
}) |
| 581 |
} |
| 582 |
|
| 583 |
fn record_uncompressed_chunk( |
| 584 |
slf: &Bound<'_, Self>, |
| 585 |
rev: PyRevision, |
| 586 |
data: &Bound<'_, PyAny>, |
| 587 |
) -> PyResult<Py<PyAny>> { |
| 588 |
let data = PyBuffer::<u8>::get(data)?.to_vec(slf.py())?; |
| 589 |
Self::with_core_read(slf, |_self_ref, irl| { |
| 590 |
if let Some(rev) = irl.index.check_revision(rev.into()) { |
| 591 |
let data: RawData = RawData::from(data.to_owned()); |
| 592 |
irl.record_uncompressed_chunk(rev, data); |
| 593 |
}; |
| 594 |
Ok(slf.py().None()) |
| 595 |
}) |
| 596 |
} |
| 597 |
|
| 598 |
fn issnapshot(slf: &Bound<'_, Self>, rev: PyRevision) -> PyResult<bool> { |
| 599 |
Self::_index_issnapshot(slf, rev) |
| 600 |
} |
| 601 |
|
| 602 |
#[pyo3(signature = (rev, stoprev=None))] |
| 603 |
fn _deltachain( |
| 604 |
slf: &Bound<'_, Self>, |
| 605 |
py: Python<'_>, |
| 606 |
rev: PyRevision, |
| 607 |
stoprev: Option<PyRevision>, |
| 608 |
) -> PyResult<Py<PyTuple>> { |
| 609 |
Self::_index_deltachain(slf, py, rev, stoprev) |
| 610 |
} |
| 611 |
|
| 612 |
fn compress( |
| 613 |
slf: &Bound<'_, Self>, |
| 614 |
py: Python<'_>, |
| 615 |
data: &Bound<'_, PyAny>, |
| 616 |
) -> PyResult<Py<PyTuple>> { |
| 617 |
Self::with_core_read(slf, |_self_ref, irl| { |
| 618 |
// Safety: we only hold on to the data for as long as `_buf` |
| 619 |
// is alive |
| 620 |
let (_buf, data) = unsafe { take_buffer_with_slice(data)? }; |
| 621 |
let compressed = |
| 622 |
irl.compress(&data).map_err(revlog_error_from_msg)?; |
| 623 |
let compressed = compressed.as_deref(); |
| 624 |
let header = if compressed.is_some() { |
| 625 |
PyBytes::new(py, &b""[..]) |
| 626 |
} else { |
| 627 |
PyBytes::new(py, &b"u"[..]) |
| 628 |
}; |
| 629 |
Ok(PyTuple::new( |
| 630 |
py, |
| 631 |
&[header, PyBytes::new(py, compressed.unwrap_or(&data))], |
| 632 |
)? |
| 633 |
.unbind()) |
| 634 |
}) |
| 635 |
} |
| 636 |
|
| 637 |
#[pyo3(signature = (tr, header, new_index_file_path=None))] |
| 638 |
fn split_inline( |
| 639 |
slf: &Bound<'_, Self>, |
| 640 |
py: Python<'_>, |
| 641 |
tr: Py<PyAny>, |
| 642 |
header: i32, |
| 643 |
new_index_file_path: Option<&Bound<'_, PyBytes>>, |
| 644 |
) -> PyResult<Py<PyBytes>> { |
| 645 |
// Also unused in Python, TODO clean this up. |
| 646 |
let _ = tr; |
| 647 |
|
| 648 |
Self::with_core_write(slf, |_self_ref, mut irl| { |
| 649 |
let new_index_file_path = new_index_file_path |
| 650 |
.map(|path| get_path_from_bytes(path.as_bytes()).to_owned()); |
| 651 |
let header = IndexHeader::parse(&header.to_be_bytes()) |
| 652 |
.expect("invalid header bytes"); |
| 653 |
let old_path = irl |
| 654 |
.split_inline(header, new_index_file_path) |
| 655 |
.map_err(revlog_error_from_msg)?; |
| 656 |
Ok(PyBytes::new(py, &get_bytes_from_path(old_path)).unbind()) |
| 657 |
}) |
| 658 |
} |
| 659 |
|
| 660 |
fn get_segment_for_revs( |
| 661 |
slf: &Bound<'_, Self>, |
| 662 |
py: Python<'_>, |
| 663 |
startrev: PyRevision, |
| 664 |
endrev: PyRevision, |
| 665 |
) -> PyResult<Py<PyTuple>> { |
| 666 |
Self::with_core_read(slf, |_self_ref, irl| { |
| 667 |
// Here both revisions only come from revlog code, so we assume |
| 668 |
// them to be valid. |
| 669 |
// Panics will alert the offending programmer if not. |
| 670 |
let (offset, data) = irl |
| 671 |
.get_segment_for_revs(Revision(startrev.0), Revision(endrev.0)) |
| 672 |
.map_err(revlog_error_from_msg)?; |
| 673 |
let data = PyBytes::new(py, &data); |
| 674 |
Ok(PyTuple::new( |
| 675 |
py, |
| 676 |
&[offset.into_py_any(py)?, data.into_py_any(py)?], |
| 677 |
)? |
| 678 |
.unbind()) |
| 679 |
}) |
| 680 |
} |
| 681 |
|
| 682 |
fn raw_text( |
| 683 |
slf: &Bound<'_, Self>, |
| 684 |
py: Python<'_>, |
| 685 |
rev: PyRevision, |
| 686 |
) -> PyResult<Py<PyTuple>> { |
| 687 |
if let Some(tuple) = slf.borrow().inner_get_cached_text(py, rev)? { |
| 688 |
return Ok(tuple); |
| 689 |
} |
| 690 |
Self::with_core_read(slf, |_self_ref, irl| { |
| 691 |
let mut py_bytes = PyBytes::new(py, &[]).unbind(); |
| 692 |
irl.raw_text(Revision(rev.0), |size, f| { |
| 693 |
py_bytes = with_pybytes_buffer(py, size, f)?; |
| 694 |
Ok(()) |
| 695 |
}) |
| 696 |
.map_err(revlog_error_from_msg)?; |
| 697 |
Ok(PyTuple::new( |
| 698 |
py, |
| 699 |
&[ |
| 700 |
rev.into_py_any(py)?, |
| 701 |
py_bytes.into_py_any(py)?, |
| 702 |
false.into_py_any(py)?, |
| 703 |
], |
| 704 |
)? |
| 705 |
.unbind()) |
| 706 |
}) |
| 707 |
} |
| 708 |
|
| 709 |
/// return a binary delta between two revisions (with an optional extra |
| 710 |
/// delta) |
| 711 |
fn rev_diff( |
| 712 |
slf: &Bound<'_, Self>, |
| 713 |
py: Python<'_>, |
| 714 |
rev_1: PyRevision, |
| 715 |
rev_2: PyRevision, |
| 716 |
extra_delta: Option<&[u8]>, |
| 717 |
) -> PyResult<Py<PyBytes>> { |
| 718 |
let rev_1: UncheckedRevision = rev_1.into(); |
| 719 |
let rev_2: UncheckedRevision = rev_2.into(); |
| 720 |
Self::with_core_read(slf, |_self_ref, irl| { |
| 721 |
let idx = &irl.index; |
| 722 |
let rev_1 = check_revision(idx, rev_1)?; |
| 723 |
let rev_2 = check_revision(idx, rev_2)?; |
| 724 |
let bytes = if let Some(delta) = extra_delta { |
| 725 |
irl.rev_delta_extra(rev_1, rev_2, delta) |
| 726 |
.map_err(revlog_error_from_msg)? |
| 727 |
} else { |
| 728 |
irl.rev_delta(rev_1, rev_2).map_err(revlog_error_from_msg)? |
| 729 |
}; |
| 730 |
let py_bytes: Py<PyBytes> = PyBytes::new(py, &bytes).unbind(); |
| 731 |
Ok(py_bytes) |
| 732 |
}) |
| 733 |
} |
| 734 |
|
| 735 |
#[allow(clippy::too_many_arguments)] |
| 736 |
#[pyo3(signature = ( |
| 737 |
transaction, |
| 738 |
entry, |
| 739 |
data, |
| 740 |
_link, |
| 741 |
offset, |
| 742 |
_sidedata, |
| 743 |
_sidedata_offset, |
| 744 |
index_end, |
| 745 |
data_end, |
| 746 |
_sidedata_end |
| 747 |
))] |
| 748 |
fn write_entry( |
| 749 |
slf: &Bound<'_, Self>, |
| 750 |
py: Python<'_>, |
| 751 |
transaction: Py<PyAny>, |
| 752 |
entry: &Bound<'_, PyBytes>, |
| 753 |
data: &Bound<'_, PyTuple>, |
| 754 |
// TODO remove and also from Python |
| 755 |
_link: Py<PyAny>, |
| 756 |
offset: usize, |
| 757 |
// Other underscore args are for revlog-v2, which is unimplemented |
| 758 |
_sidedata: Py<PyAny>, |
| 759 |
_sidedata_offset: u64, |
| 760 |
index_end: Option<u64>, |
| 761 |
data_end: Option<u64>, |
| 762 |
_sidedata_end: Option<u64>, |
| 763 |
) -> PyResult<Py<PyTuple>> { |
| 764 |
Self::with_core_write(slf, |_self_ref, mut irl| { |
| 765 |
let transaction = PyTransaction::new(transaction); |
| 766 |
let header = data.get_borrowed_item(0)?; |
| 767 |
let header = header.cast::<PyBytes>()?; |
| 768 |
let data = data.get_borrowed_item(1)?; |
| 769 |
let data = data.cast::<PyBytes>()?; |
| 770 |
let (idx_pos, data_pos) = irl |
| 771 |
.write_entry( |
| 772 |
transaction, |
| 773 |
entry.as_bytes(), |
| 774 |
(header.as_bytes(), data.as_bytes()), |
| 775 |
offset, |
| 776 |
index_end, |
| 777 |
data_end, |
| 778 |
) |
| 779 |
.map_err(revlog_error_from_msg)?; |
| 780 |
let tuple = PyTuple::new( |
| 781 |
py, |
| 782 |
[idx_pos.into_py_any(py)?, data_pos.into_py_any(py)?], |
| 783 |
)?; |
| 784 |
Ok(tuple.unbind()) |
| 785 |
}) |
| 786 |
} |
| 787 |
|
| 788 |
fn delay( |
| 789 |
slf: &Bound<'_, Self>, |
| 790 |
py: Python<'_>, |
| 791 |
) -> PyResult<Option<Py<PyBytes>>> { |
| 792 |
Self::with_core_write(slf, |_self_ref, mut irl| { |
| 793 |
let path = irl.delay().map_err(revlog_error_from_msg)?; |
| 794 |
Ok(path.map(|p| PyBytes::new(py, &get_bytes_from_path(p)).unbind())) |
| 795 |
}) |
| 796 |
} |
| 797 |
|
| 798 |
fn write_pending( |
| 799 |
slf: &Bound<'_, Self>, |
| 800 |
py: Python<'_>, |
| 801 |
) -> PyResult<Py<PyTuple>> { |
| 802 |
Self::with_core_write(slf, |_self_ref, mut irl| { |
| 803 |
let (path, any_pending) = |
| 804 |
irl.write_pending().map_err(revlog_error_from_msg)?; |
| 805 |
let maybe_path = match path { |
| 806 |
Some(path) => PyBytes::new(py, &get_bytes_from_path(path)) |
| 807 |
.unbind() |
| 808 |
.into_any(), |
| 809 |
None => py.None(), |
| 810 |
}; |
| 811 |
Ok(PyTuple::new(py, [maybe_path, any_pending.into_py_any(py)?])? |
| 812 |
.unbind()) |
| 813 |
}) |
| 814 |
} |
| 815 |
|
| 816 |
fn finalize_pending( |
| 817 |
slf: &Bound<'_, Self>, |
| 818 |
py: Python<'_>, |
| 819 |
) -> PyResult<Py<PyBytes>> { |
| 820 |
Self::with_core_write(slf, |_self_ref, mut irl| { |
| 821 |
let path = irl.finalize_pending().map_err(revlog_error_from_msg)?; |
| 822 |
Ok(PyBytes::new(py, &get_bytes_from_path(path)).unbind()) |
| 823 |
}) |
| 824 |
} |
| 825 |
|
| 826 |
fn _chunk( |
| 827 |
slf: &Bound<'_, Self>, |
| 828 |
py: Python<'_>, |
| 829 |
rev: PyRevision, |
| 830 |
) -> PyResult<Py<PyBytes>> { |
| 831 |
Self::with_core_read(slf, |_self_ref, irl| { |
| 832 |
let chunk = irl |
| 833 |
.chunk_for_rev(Revision(rev.0)) |
| 834 |
.map_err(revlog_error_from_msg)?; |
| 835 |
Ok(PyBytes::new(py, &chunk).unbind()) |
| 836 |
}) |
| 837 |
} |
| 838 |
|
| 839 |
fn reading(slf: &Bound<'_, Self>) -> PyResult<ReadingContextManager> { |
| 840 |
Ok(ReadingContextManager { inner_revlog: slf.clone().unbind() }) |
| 841 |
} |
| 842 |
|
| 843 |
#[pyo3(signature = (transaction, data_end=None, sidedata_end=None))] |
| 844 |
fn writing( |
| 845 |
slf: &Bound<'_, Self>, |
| 846 |
transaction: Py<PyAny>, |
| 847 |
data_end: Option<usize>, |
| 848 |
sidedata_end: Option<usize>, |
| 849 |
) -> PyResult<WritingContextManager> { |
| 850 |
// Only useful in revlog v2 |
| 851 |
let _ = sidedata_end; |
| 852 |
Ok(WritingContextManager { |
| 853 |
inner_revlog: slf.clone().unbind(), |
| 854 |
transaction: RwLock::new(PyTransaction::new(transaction)), |
| 855 |
data_end, |
| 856 |
}) |
| 857 |
} |
| 858 |
|
| 859 |
// |
| 860 |
// -- forwarded index methods -- |
| 861 |
// |
| 862 |
|
| 863 |
fn _index_get_rev( |
| 864 |
slf: &Bound<'_, Self>, |
| 865 |
node: &Bound<'_, PyBytes>, |
| 866 |
) -> PyResult<Option<PyRevision>> { |
| 867 |
let node = node_from_py_bytes(node)?; |
| 868 |
|
| 869 |
Self::with_core_read(slf, |_, irl| { |
| 870 |
Ok(irl |
| 871 |
.rev_from_node_prefix(node.into()) |
| 872 |
.map_err(revlog_error)? |
| 873 |
.map(Into::into)) |
| 874 |
}) |
| 875 |
} |
| 876 |
|
| 877 |
/// same as `_index_get_rev()` but raises a bare `error.RevlogError` if |
| 878 |
/// node is not found. |
| 879 |
/// |
| 880 |
/// No need to repeat `node` in the exception, `mercurial/revlog.py` |
| 881 |
/// will catch and rewrap with it |
| 882 |
fn _index_rev( |
| 883 |
slf: &Bound<'_, Self>, |
| 884 |
node: &Bound<'_, PyBytes>, |
| 885 |
) -> PyResult<PyRevision> { |
| 886 |
Self::_index_get_rev(slf, node)?.ok_or_else(revlog_error_bare) |
| 887 |
} |
| 888 |
|
| 889 |
/// return True if the node exist in the index |
| 890 |
fn _index_has_node( |
| 891 |
slf: &Bound<'_, Self>, |
| 892 |
node: &Bound<'_, PyBytes>, |
| 893 |
) -> PyResult<bool> { |
| 894 |
Self::_index_get_rev(slf, node).map(|opt| opt.is_some()) |
| 895 |
} |
| 896 |
|
| 897 |
/// find length of shortest hex nodeid of a binary ID |
| 898 |
fn _index_shortest( |
| 899 |
slf: &Bound<'_, Self>, |
| 900 |
node: &Bound<'_, PyBytes>, |
| 901 |
) -> PyResult<usize> { |
| 902 |
Self::with_core_read(slf, |_, irl| { |
| 903 |
match irl.unique_prefix_len_node(node_from_py_bytes(node)?) { |
| 904 |
Ok(Some(l)) => Ok(l), |
| 905 |
Ok(None) => Err(revlog_error_bare()), |
| 906 |
Err(e) => Err(revlog_error(e)), |
| 907 |
} |
| 908 |
}) |
| 909 |
} |
| 910 |
|
| 911 |
fn _index_partialmatch<'py>( |
| 912 |
slf: &Bound<'py, Self>, |
| 913 |
node: &Bound<'py, PyBytes>, |
| 914 |
) -> PyResult<Option<Bound<'py, PyBytes>>> { |
| 915 |
Self::with_core_read(slf, |_, irl| { |
| 916 |
Ok(irl |
| 917 |
.rev_from_node_prefix(node_prefix_from_py_bytes(node)?) |
| 918 |
.map_err(revlog_error)? |
| 919 |
.map(|rev| py_node_for_rev(slf.py(), &irl.index, rev))) |
| 920 |
}) |
| 921 |
} |
| 922 |
|
| 923 |
/// append an index entry |
| 924 |
fn _index_append( |
| 925 |
slf: &Bound<'_, Self>, |
| 926 |
tup: &Bound<'_, PyTuple>, |
| 927 |
) -> PyResult<()> { |
| 928 |
Self::with_core_write(slf, |_, mut irl| { |
| 929 |
irl.index_append(py_tuple_to_revision_data_params(tup)?) |
| 930 |
.map_err(revlog_error_from_msg)?; |
| 931 |
|
| 932 |
Ok(()) |
| 933 |
}) |
| 934 |
} |
| 935 |
|
| 936 |
/// Removes one or several entries from the index. |
| 937 |
/// |
| 938 |
/// Historically, on the Mercurial revlog index, `__delitem__` has always |
| 939 |
/// been both for `del idx[r1]` and `del idx[r1:r2]`. In both cases, |
| 940 |
/// all entries starting from `r1` are removed anyway. |
| 941 |
fn _index___delitem__( |
| 942 |
slf: &Bound<'_, Self>, |
| 943 |
arg: &Bound<'_, PyAny>, |
| 944 |
) -> PyResult<()> { |
| 945 |
let start = if let Ok(rev) = arg.extract() { |
| 946 |
UncheckedRevision(rev) |
| 947 |
} else { |
| 948 |
// here we could cast to `PySlice` and use `indices()`, *but* |
| 949 |
// the PyO3 based version could not do that, and |
| 950 |
// `indices()` does some resolving that makes it not equivalent, |
| 951 |
// e.g., `idx[-1::]` has `start=0`. As we are currently in |
| 952 |
// transition, we keep it the old way (hoping it was consistent |
| 953 |
// with the C index). |
| 954 |
let start = arg.getattr("start")?; |
| 955 |
UncheckedRevision(start.extract()?) |
| 956 |
}; |
| 957 |
|
| 958 |
Self::with_core_write(slf, |_, mut irl| { |
| 959 |
// In the case of a slice, the check is possibly already done by |
| 960 |
// `slice.indices`, which is itself an FFI wrapper for CPython's |
| 961 |
// `PySlice_GetIndicesEx` |
| 962 |
// (Python integration tests will tell us) |
| 963 |
let start = irl.index.check_revision(start).ok_or_else(|| { |
| 964 |
nodemap_error(NodeMapError::RevisionNotInIndex(start)) |
| 965 |
})?; |
| 966 |
irl.index.remove(start).map_err(revlog_error_from_msg)?; |
| 967 |
irl.nodemap_invalidate().map_err(nodemap_error)?; |
| 968 |
Ok(()) |
| 969 |
}) |
| 970 |
} |
| 971 |
|
| 972 |
/// return the gca set of the given revs |
| 973 |
#[pyo3(signature = (*revs))] |
| 974 |
fn _index_ancestors( |
| 975 |
slf: &Bound<'_, Self>, |
| 976 |
revs: &Bound<'_, PyTuple>, |
| 977 |
) -> PyResult<Py<PyAny>> { |
| 978 |
Self::with_index_read(slf, |idx| { |
| 979 |
let revs: Vec<_> = rev_pyiter_collect(revs, idx)?; |
| 980 |
Ok(PyList::new( |
| 981 |
slf.py(), |
| 982 |
idx.ancestors(&revs) |
| 983 |
.map_err(graph_error)? |
| 984 |
.into_iter() |
| 985 |
.map(PyRevision::from), |
| 986 |
)? |
| 987 |
.into_any() |
| 988 |
.unbind()) |
| 989 |
}) |
| 990 |
} |
| 991 |
|
| 992 |
/// return the heads of the common ancestors of the given revs |
| 993 |
#[pyo3(signature = (*revs))] |
| 994 |
fn _index_commonancestorsheads( |
| 995 |
slf: &Bound<'_, Self>, |
| 996 |
revs: &Bound<'_, PyTuple>, |
| 997 |
) -> PyResult<Py<PyList>> { |
| 998 |
Self::with_index_read(slf, |idx| { |
| 999 |
let revs: Vec<_> = rev_pyiter_collect(revs, idx)?; |
| 1000 |
revs_py_list( |
| 1001 |
slf.py(), |
| 1002 |
idx.common_ancestor_heads(&revs).map_err(graph_error)?, |
| 1003 |
) |
| 1004 |
}) |
| 1005 |
} |
| 1006 |
|
| 1007 |
/// Clear the index caches and inner py_class data. |
| 1008 |
/// It is Python's responsibility to call `update_nodemap_data` again. |
| 1009 |
fn _index_clearcaches(slf: &Bound<'_, Self>) -> PyResult<()> { |
| 1010 |
Self::with_index_write(slf, |idx| { |
| 1011 |
idx.clear_caches(); |
| 1012 |
Ok(()) |
| 1013 |
})?; |
| 1014 |
|
| 1015 |
// Very important to drop this in addition to the `nodemap_mmap`, |
| 1016 |
// otherwise the nodemap in core would point to a dangling mmap. |
| 1017 |
// TODO this shows that our way of holding on to mmap buffers is unsafe |
| 1018 |
// since safe code can trigger UB by messing up drop ordering. |
| 1019 |
Self::with_core_write(slf, |_, mut irl| { |
| 1020 |
let index_file = irl.canonical_index_file(); |
| 1021 |
irl.nodemap_set(None, index_file); |
| 1022 |
Ok(()) |
| 1023 |
})?; |
| 1024 |
let mut self_ref = slf.borrow_mut(); |
| 1025 |
self_ref.docket.take(); |
| 1026 |
self_ref.nodemap_mmap.take(); |
| 1027 |
self_ref.head_revs_py_list.take(); |
| 1028 |
self_ref.head_node_ids_py_list.take(); |
| 1029 |
Ok(()) |
| 1030 |
} |
| 1031 |
|
| 1032 |
/// return the raw binary string representing a revision |
| 1033 |
fn _index_entry_binary( |
| 1034 |
slf: &Bound<'_, Self>, |
| 1035 |
rev: PyRevision, |
| 1036 |
) -> PyResult<Py<PyBytes>> { |
| 1037 |
let rev: UncheckedRevision = rev.into(); |
| 1038 |
Self::with_index_read(slf, |idx| { |
| 1039 |
let rev = check_revision(idx, rev)?; |
| 1040 |
Ok(PyBytes::new(slf.py(), idx.entry_binary(rev)).unbind()) |
| 1041 |
}) |
| 1042 |
} |
| 1043 |
|
| 1044 |
/// return a binary packed version of the header |
| 1045 |
fn _index_pack_header( |
| 1046 |
slf: &Bound<'_, Self>, |
| 1047 |
header: i32, |
| 1048 |
) -> PyResult<Py<PyBytes>> { |
| 1049 |
let packed = |
| 1050 |
Self::with_index_read(slf, |idx| Ok(idx.pack_header(header)))?; |
| 1051 |
Ok(PyBytes::new(slf.py(), &packed).unbind()) |
| 1052 |
} |
| 1053 |
|
| 1054 |
/// compute phases |
| 1055 |
fn _index_computephasesmapsets( |
| 1056 |
slf: &Bound<'_, Self>, |
| 1057 |
py: Python<'_>, |
| 1058 |
roots: &Bound<'_, PyDict>, |
| 1059 |
) -> PyResult<Py<PyTuple>> { |
| 1060 |
let (len, phase_maps) = Self::with_index_read(slf, |idx| { |
| 1061 |
let extracted_roots: PyResult<HashMap<Phase, Vec<Revision>>> = |
| 1062 |
roots |
| 1063 |
.iter() |
| 1064 |
.map(|(phase, revs)| { |
| 1065 |
let phase = Phase::try_from(phase.extract::<usize>()?) |
| 1066 |
.map_err(revlog_error)?; |
| 1067 |
let revs: Vec<Revision> = |
| 1068 |
rev_pyiter_collect(&revs, idx)?; |
| 1069 |
Ok((phase, revs)) |
| 1070 |
}) |
| 1071 |
.collect(); |
| 1072 |
idx.compute_phases_map_sets(extracted_roots?).map_err(graph_error) |
| 1073 |
})?; |
| 1074 |
// Ugly hack, but temporary (!) |
| 1075 |
const IDX_TO_PHASE_NUM: [usize; 4] = [1, 2, 32, 96]; |
| 1076 |
let py_phase_maps = PyDict::new(py); |
| 1077 |
for (i, roots) in phase_maps.into_iter().enumerate() { |
| 1078 |
py_phase_maps.set_item( |
| 1079 |
IDX_TO_PHASE_NUM[i], |
| 1080 |
revs_py_set(py, roots)?.into_any(), |
| 1081 |
)?; |
| 1082 |
} |
| 1083 |
Ok((len, py_phase_maps).into_pyobject(py)?.unbind()) |
| 1084 |
} |
| 1085 |
|
| 1086 |
/// reachableroots |
| 1087 |
#[pyo3(signature = (*args))] |
| 1088 |
fn _index_reachableroots2( |
| 1089 |
slf: &Bound<'_, Self>, |
| 1090 |
py: Python<'_>, |
| 1091 |
args: &Bound<'_, PyTuple>, |
| 1092 |
) -> PyResult<Py<PyList>> { |
| 1093 |
// TODO what was the point of having a signature with variable args? |
| 1094 |
let min_root = UncheckedRevision(args.get_item(0)?.extract()?); |
| 1095 |
let heads = args.get_item(1)?; |
| 1096 |
let roots = args.get_item(2)?; |
| 1097 |
let include_path: bool = args.get_item(3)?.extract()?; |
| 1098 |
|
| 1099 |
let as_set = Self::with_index_read(slf, |idx| { |
| 1100 |
let heads = rev_pyiter_collect_or_else(&heads, idx, |_rev| { |
| 1101 |
PyIndexError::new_err("head out of range") |
| 1102 |
})?; |
| 1103 |
let roots: Result<_, _> = roots |
| 1104 |
.try_iter()? |
| 1105 |
.map(|r| { |
| 1106 |
r.and_then(|o| match o.extract::<PyRevision>() { |
| 1107 |
Ok(r) => Ok(UncheckedRevision(r.0)), |
| 1108 |
Err(e) => Err(e), |
| 1109 |
}) |
| 1110 |
}) |
| 1111 |
.collect(); |
| 1112 |
idx.reachable_roots(min_root, heads, roots?, include_path) |
| 1113 |
.map_err(graph_error) |
| 1114 |
})?; |
| 1115 |
|
| 1116 |
revs_py_list(py, as_set) |
| 1117 |
} |
| 1118 |
|
| 1119 |
#[pyo3(signature = (*args))] |
| 1120 |
fn _index_headrevs( |
| 1121 |
slf: &Bound<'_, Self>, |
| 1122 |
py: Python<'_>, |
| 1123 |
args: &Bound<'_, PyTuple>, |
| 1124 |
) -> PyResult<Py<PyList>> { |
| 1125 |
let (filtered_revs, stop_rev) = match args.len() { |
| 1126 |
0 => Ok((None, None)), |
| 1127 |
1 => Ok((Some(args.get_item(0)?), None)), |
| 1128 |
2 => Ok((Some(args.get_item(0)?), Some(args.get_item(1)?))), |
| 1129 |
_ => Err(PyTypeError::new_err("too many arguments")), |
| 1130 |
}?; |
| 1131 |
let stop_rev = |
| 1132 |
stop_rev.map(|o| o.extract::<Option<i32>>()).transpose()?.flatten(); |
| 1133 |
let filtered_revs = filtered_revs.filter(|o| !o.is_none()); |
| 1134 |
|
| 1135 |
let (from_core, stop_rev) = Self::with_index_read(slf, |idx| { |
| 1136 |
let stop_rev = stop_rev |
| 1137 |
// should this not just be the normal checking? |
| 1138 |
.filter(|rev| 0 <= *rev && *rev < idx.len() as BaseRevision) |
| 1139 |
.map(Revision); |
| 1140 |
|
| 1141 |
let from_core = if let Some(filtered_revs) = filtered_revs { |
| 1142 |
let filtered_revs = rev_pyiter_collect(&filtered_revs, idx)?; |
| 1143 |
idx.head_revs_advanced( |
| 1144 |
&filtered_revs, |
| 1145 |
stop_rev, |
| 1146 |
stop_rev.is_none(), |
| 1147 |
) |
| 1148 |
} else if stop_rev.is_some() { |
| 1149 |
idx.head_revs_advanced(&HashSet::new(), stop_rev, false) |
| 1150 |
} else { |
| 1151 |
idx.head_revs_shortcut() |
| 1152 |
} |
| 1153 |
.map_err(graph_error)?; |
| 1154 |
Ok((from_core, stop_rev)) |
| 1155 |
})?; |
| 1156 |
|
| 1157 |
if stop_rev.is_some() { |
| 1158 |
// we don't cache result for now |
| 1159 |
let new_heads = |
| 1160 |
from_core.expect("this case should not be cached yet"); |
| 1161 |
|
| 1162 |
revs_py_list(py, new_heads) |
| 1163 |
} else { |
| 1164 |
if let Some(new_heads) = from_core { |
| 1165 |
Self::cache_new_heads_py_list(slf, new_heads)?; |
| 1166 |
} |
| 1167 |
|
| 1168 |
Ok(slf |
| 1169 |
.borrow() |
| 1170 |
.head_revs_py_list |
| 1171 |
.as_ref() |
| 1172 |
.expect("head revs should be cached") |
| 1173 |
.clone_ref(py)) |
| 1174 |
} |
| 1175 |
} |
| 1176 |
|
| 1177 |
/// get head nodeids |
| 1178 |
fn _index_head_node_ids( |
| 1179 |
slf: &Bound<'_, Self>, |
| 1180 |
py: Python<'_>, |
| 1181 |
) -> PyResult<Py<PyList>> { |
| 1182 |
let (head_revs, head_nodes) = Self::with_index_read(slf, |idx| { |
| 1183 |
// We don't use the shortcut here, as it's actually slower to loop |
| 1184 |
// through the cached `PyList` than to re-do the whole |
| 1185 |
// conversion for large lists, which are the performance |
| 1186 |
// sensitive ones anyway. |
| 1187 |
let head_revs = idx.head_revs().map_err(graph_error)?; |
| 1188 |
let head_nodes = PyList::new( |
| 1189 |
py, |
| 1190 |
head_revs.iter().map(|r| { |
| 1191 |
PyBytes::new(py, idx.node(*r).as_bytes()).unbind() |
| 1192 |
}), |
| 1193 |
)? |
| 1194 |
.unbind(); |
| 1195 |
Ok((head_revs, head_nodes)) |
| 1196 |
})?; |
| 1197 |
|
| 1198 |
Self::cache_new_heads_py_list(slf, head_revs)?; |
| 1199 |
Ok(head_nodes) |
| 1200 |
} |
| 1201 |
|
| 1202 |
/// get diff in head revisions |
| 1203 |
fn _index_headrevsdiff( |
| 1204 |
slf: &Bound<'_, Self>, |
| 1205 |
py: Python<'_>, |
| 1206 |
begin: PyRevision, |
| 1207 |
end: PyRevision, |
| 1208 |
) -> PyResult<Py<PyTuple>> { |
| 1209 |
let begin: BaseRevision = begin.0 - 1; |
| 1210 |
let end: BaseRevision = end.0 - 1; |
| 1211 |
let (removed, added) = Self::with_index_read(slf, |idx| { |
| 1212 |
idx.head_revs_diff( |
| 1213 |
check_revision(idx, begin)?, |
| 1214 |
check_revision(idx, end)?, |
| 1215 |
) |
| 1216 |
.map_err(graph_error) |
| 1217 |
})?; |
| 1218 |
let py_removed = revs_py_list(py, removed)?; |
| 1219 |
let py_added = revs_py_list(py, added)?; |
| 1220 |
Ok((py_removed, py_added).into_pyobject(py)?.unbind()) |
| 1221 |
} |
| 1222 |
|
| 1223 |
/// True if the object is a snapshot |
| 1224 |
fn _index_issnapshot( |
| 1225 |
slf: &Bound<'_, Self>, |
| 1226 |
rev: PyRevision, |
| 1227 |
) -> PyResult<bool> { |
| 1228 |
let rev: UncheckedRevision = rev.into(); |
| 1229 |
let rev = Self::with_index_read(slf, |idx| check_revision(idx, rev))?; |
| 1230 |
Self::with_core_read(slf, |_self_ref, irl| { |
| 1231 |
irl.is_snapshot(rev) |
| 1232 |
.map_err(|e| PyValueError::new_err(e.to_string())) |
| 1233 |
}) |
| 1234 |
} |
| 1235 |
|
| 1236 |
/// Gather snapshot data in a cache dict |
| 1237 |
fn _index_findsnapshots( |
| 1238 |
slf: &Bound<'_, Self>, |
| 1239 |
cache: &Bound<'_, PyDict>, |
| 1240 |
start_rev: PyRevision, |
| 1241 |
end_rev: PyRevision, |
| 1242 |
) -> PyResult<()> { |
| 1243 |
let mut cache = PySnapshotsCache(cache); |
| 1244 |
Self::with_index_read(slf, |idx| { |
| 1245 |
idx.find_snapshots(start_rev.into(), end_rev.into(), &mut cache) |
| 1246 |
.map_err(revlog_error) |
| 1247 |
})?; |
| 1248 |
Ok(()) |
| 1249 |
} |
| 1250 |
|
| 1251 |
/// determine revisions with deltas to reconstruct fulltext |
| 1252 |
#[pyo3(signature = (rev, stop_rev))] |
| 1253 |
fn _index_deltachain( |
| 1254 |
slf: &Bound<'_, Self>, |
| 1255 |
py: Python<'_>, |
| 1256 |
rev: PyRevision, |
| 1257 |
stop_rev: Option<PyRevision>, |
| 1258 |
) -> PyResult<Py<PyTuple>> { |
| 1259 |
let rev: UncheckedRevision = rev.into(); |
| 1260 |
let stop_rev: Option<UncheckedRevision> = stop_rev.map(Into::into); |
| 1261 |
|
| 1262 |
let (chain, stopped) = Self::with_index_read(slf, |idx| { |
| 1263 |
let rev = idx.check_revision(rev).ok_or_else(|| { |
| 1264 |
nodemap_error(NodeMapError::RevisionNotInIndex(rev)) |
| 1265 |
})?; |
| 1266 |
let stop_rev = stop_rev |
| 1267 |
.map(|r| { |
| 1268 |
idx.check_revision(r).ok_or_else(|| { |
| 1269 |
nodemap_error(NodeMapError::RevisionNotInIndex( |
| 1270 |
rev.into(), |
| 1271 |
)) |
| 1272 |
}) |
| 1273 |
}) |
| 1274 |
.transpose()?; |
| 1275 |
idx.delta_chain(rev, stop_rev) |
| 1276 |
.map_err(|e| PyValueError::new_err(e.to_string())) |
| 1277 |
})?; |
| 1278 |
|
| 1279 |
let py_chain = revs_py_list(py, chain)?.into_any(); |
| 1280 |
let py_stopped = |
| 1281 |
PyBool::new(py, stopped).to_owned().unbind().into_any(); |
| 1282 |
Ok((py_chain, py_stopped).into_pyobject(py)?.unbind()) |
| 1283 |
} |
| 1284 |
|
| 1285 |
/// slice planned chunk read to reach a density threshold |
| 1286 |
fn _index_slicechunktodensity( |
| 1287 |
slf: &Bound<'_, Self>, |
| 1288 |
py: Python<'_>, |
| 1289 |
revs: &Bound<'_, PyAny>, |
| 1290 |
target_density: f64, |
| 1291 |
min_gap_size: usize, |
| 1292 |
) -> PyResult<Py<PyAny>> { |
| 1293 |
Self::with_index_read(slf, |idx| { |
| 1294 |
let revs: Vec<_> = rev_pyiter_collect(revs, idx)?; |
| 1295 |
let as_nested_vec = |
| 1296 |
idx.slice_chunk_to_density(&revs, target_density, min_gap_size); |
| 1297 |
let res_len = as_nested_vec.len(); |
| 1298 |
|
| 1299 |
// cannot build the outer sequence from iterator, because |
| 1300 |
// `rev_py_list()` returns `Result<T>` instead of `T`. |
| 1301 |
let mut res = Vec::with_capacity(res_len); |
| 1302 |
for chunk in as_nested_vec { |
| 1303 |
res.push(revs_py_list(py, chunk.iter().copied())?.into_any()); |
| 1304 |
} |
| 1305 |
// This is just to do the same as C, not sure why it does this |
| 1306 |
Ok(if res_len == 1 { |
| 1307 |
PyTuple::new(py, res)?.unbind().into_any() |
| 1308 |
} else { |
| 1309 |
PyList::new(py, res)?.unbind().into_any() |
| 1310 |
}) |
| 1311 |
}) |
| 1312 |
} |
| 1313 |
|
| 1314 |
fn _index___len__(slf: &Bound<'_, Self>) -> PyResult<usize> { |
| 1315 |
Self::with_index_read(slf, |idx| Ok(idx.len())) |
| 1316 |
} |
| 1317 |
|
| 1318 |
fn _index___getitem__( |
| 1319 |
slf: &Bound<'_, Self>, |
| 1320 |
py: Python<'_>, |
| 1321 |
key: &Bound<'_, PyAny>, |
| 1322 |
) -> PyResult<Py<PyAny>> { |
| 1323 |
Self::with_index_read(slf, |idx| { |
| 1324 |
match key.extract::<BaseRevision>() { |
| 1325 |
Ok(key_as_int) => { |
| 1326 |
let entry_params = if key_as_int == NULL_REVISION.0 { |
| 1327 |
RevisionDataParams::default() |
| 1328 |
} else { |
| 1329 |
let rev = UncheckedRevision(key_as_int); |
| 1330 |
match idx.entry_as_params(rev) { |
| 1331 |
Some(e) => e, |
| 1332 |
None => { |
| 1333 |
return Err(PyIndexError::new_err( |
| 1334 |
"revlog index out of range", |
| 1335 |
)); |
| 1336 |
} |
| 1337 |
} |
| 1338 |
}; |
| 1339 |
Ok(revision_data_params_to_py_tuple(py, entry_params)? |
| 1340 |
.into_any() |
| 1341 |
.unbind()) |
| 1342 |
} |
| 1343 |
// Case when key is a binary Node ID (lame: we're re-unlocking) |
| 1344 |
_ => Self::_index_get_rev(slf, key.cast::<PyBytes>()?)? |
| 1345 |
.map_or_else( |
| 1346 |
|| Ok(py.None()), |
| 1347 |
|py_rev| Ok(py_rev.into_pyobject(py)?.unbind().into()), |
| 1348 |
), |
| 1349 |
} |
| 1350 |
}) |
| 1351 |
} |
| 1352 |
|
| 1353 |
/// Returns the full nodemap bytes to be written as-is to disk |
| 1354 |
fn _index_nodemap_data_all( |
| 1355 |
slf: &Bound<'_, Self>, |
| 1356 |
py: Python<'_>, |
| 1357 |
) -> PyResult<Py<PyBytes>> { |
| 1358 |
Self::with_core_write(slf, |_, irl| { |
| 1359 |
let mut nt = CoreNodeTree::load_bytes(Box::<Vec<_>>::default(), 0); |
| 1360 |
nt.catch_up_to_index(&irl.index, NULL_REVISION) |
| 1361 |
.map_err(nodemap_error)?; |
| 1362 |
|
| 1363 |
let (readonly, bytes) = nt.into_readonly_and_added_bytes(); |
| 1364 |
assert_eq!(readonly.len(), 0); |
| 1365 |
|
| 1366 |
let bytes = PyBytes::new(py, &bytes); |
| 1367 |
Ok(bytes.unbind()) |
| 1368 |
}) |
| 1369 |
} |
| 1370 |
|
| 1371 |
/// Returns the last saved docket along with the size of any changed data |
| 1372 |
/// (in number of blocks), and said data as bytes. |
| 1373 |
fn _index_nodemap_data_incremental( |
| 1374 |
slf: &Bound<'_, Self>, |
| 1375 |
py: Python<'_>, |
| 1376 |
) -> PyResult<Py<PyAny>> { |
| 1377 |
let mut self_ref = slf.borrow_mut(); |
| 1378 |
let docket = &mut self_ref.docket; |
| 1379 |
let docket = match docket.as_ref() { |
| 1380 |
Some(d) => d.clone_ref(py), |
| 1381 |
None => return Ok(py.None()), |
| 1382 |
}; |
| 1383 |
drop(self_ref); |
| 1384 |
|
| 1385 |
Self::with_core_write(slf, |_, mut irl| { |
| 1386 |
let (changed, data) = irl.nodemap_incremental_data(); |
| 1387 |
|
| 1388 |
Ok(PyTuple::new( |
| 1389 |
py, |
| 1390 |
[ |
| 1391 |
docket, |
| 1392 |
changed.into_py_any(py)?, |
| 1393 |
PyBytes::new(py, &data).into_py_any(py)?, |
| 1394 |
], |
| 1395 |
)? |
| 1396 |
.unbind() |
| 1397 |
.into_any()) |
| 1398 |
}) |
| 1399 |
} |
| 1400 |
|
| 1401 |
/// Update the nodemap from the new (mmaped) data. |
| 1402 |
/// The docket is kept as a reference for later incremental calls. |
| 1403 |
fn _index_update_nodemap_data( |
| 1404 |
slf: &Bound<'_, Self>, |
| 1405 |
py: Python<'_>, |
| 1406 |
docket: &Bound<'_, PyAny>, |
| 1407 |
nm_data: &Bound<'_, PyAny>, |
| 1408 |
) -> PyResult<Py<PyAny>> { |
| 1409 |
// Safety: we keep the buffer around inside the class as `nodemap_mmap` |
| 1410 |
let (buf, bytes) = unsafe { take_buffer_with_slice(nm_data)? }; |
| 1411 |
let len = buf.item_count(); |
| 1412 |
let data_tip = |
| 1413 |
docket.getattr("tip_rev")?.extract::<BaseRevision>()?.into(); |
| 1414 |
|
| 1415 |
let mut nt = CoreNodeTree::load_bytes(bytes, len); |
| 1416 |
|
| 1417 |
Self::with_index_read(slf, |idx| { |
| 1418 |
let data_tip = idx.check_revision(data_tip).ok_or_else(|| { |
| 1419 |
nodemap_error(NodeMapError::RevisionNotInIndex(data_tip)) |
| 1420 |
})?; |
| 1421 |
|
| 1422 |
nt.catch_up_to_index(idx, Revision(data_tip.0 + 1)) |
| 1423 |
.map_err(nodemap_error)?; |
| 1424 |
|
| 1425 |
Ok(py.None()) |
| 1426 |
})?; |
| 1427 |
|
| 1428 |
{ |
| 1429 |
let mut self_ref = slf.borrow_mut(); |
| 1430 |
self_ref.docket.replace(docket.clone().unbind()); |
| 1431 |
self_ref.nodemap_mmap = Some(buf); |
| 1432 |
} |
| 1433 |
Self::with_core_write(slf, |_, mut irl| { |
| 1434 |
let index_file = irl.canonical_index_file(); |
| 1435 |
irl.nodemap_set(Some(nt), index_file); |
| 1436 |
Ok(()) |
| 1437 |
})?; |
| 1438 |
|
| 1439 |
Ok(py.None()) |
| 1440 |
} |
| 1441 |
|
| 1442 |
#[getter] |
| 1443 |
fn _index_entry_size(&self) -> usize { |
| 1444 |
INDEX_ENTRY_SIZE |
| 1445 |
} |
| 1446 |
|
| 1447 |
#[getter] |
| 1448 |
fn _index_rust_ext_compat(&self) -> i32 { |
| 1449 |
1 |
| 1450 |
} |
| 1451 |
|
| 1452 |
#[getter] |
| 1453 |
fn _index_is_rust(&self) -> bool { |
| 1454 |
true |
| 1455 |
} |
| 1456 |
} |
| 1457 |
|
| 1458 |
impl InnerRevlog { |
| 1459 |
/// Take the lock on `slf.irl` for reading and call a closure. |
| 1460 |
/// |
| 1461 |
/// This serves the purpose to keep the needed intermediate [`PyRef`] |
| 1462 |
/// that must be obtained to access the data from the [`Bound`] reference |
| 1463 |
/// and of which the locked [`CoreInnerRevlog`] depends. |
| 1464 |
/// This also provides releasing of the [`PyRef`] as soon as the closure |
| 1465 |
/// is done, which is crucial if the caller needs to obtain a [`PyRefMut`] |
| 1466 |
/// later on. |
| 1467 |
/// |
| 1468 |
/// In the closure, we hand back the intermediate [`PyRef`] that |
| 1469 |
/// has been generated so that the closure can access more attributes. |
| 1470 |
fn with_core_read<'py, T>( |
| 1471 |
slf: &Bound<'py, Self>, |
| 1472 |
f: impl FnOnce( |
| 1473 |
&PyRef<'py, Self>, |
| 1474 |
RwLockReadGuard<CoreInnerRevlog>, |
| 1475 |
) -> PyResult<T>, |
| 1476 |
) -> PyResult<T> { |
| 1477 |
let self_ref = slf.borrow(); |
| 1478 |
// Safety: the owner is the right one. We will anyway |
| 1479 |
// not actually `share` it. Perhaps pyo3-sharedref should provide |
| 1480 |
// something less scary for this kind of usage. |
| 1481 |
let shareable_ref = unsafe { self_ref.irl.borrow_with_owner(slf) }; |
| 1482 |
let guard = shareable_ref.try_read().map_err(map_try_lock_error)?; |
| 1483 |
f(&self_ref, guard) |
| 1484 |
} |
| 1485 |
|
| 1486 |
/// Take the lock on `slf.irl` for writing and call a closure. |
| 1487 |
/// |
| 1488 |
/// See [`Self::with_core_read`] for more explanations. |
| 1489 |
fn with_core_write<'py, T>( |
| 1490 |
slf: &Bound<'py, Self>, |
| 1491 |
f: impl FnOnce( |
| 1492 |
&PyRef<'py, Self>, |
| 1493 |
RwLockWriteGuard<CoreInnerRevlog>, |
| 1494 |
) -> PyResult<T>, |
| 1495 |
) -> PyResult<T> { |
| 1496 |
let self_ref = slf.borrow(); |
| 1497 |
// Safety: the owner is the right one. We will anyway |
| 1498 |
// not actually `share` it. Perhaps pyo3-sharedref should provide |
| 1499 |
// something less scary for this kind of usage. |
| 1500 |
let shareable_ref = unsafe { self_ref.irl.borrow_with_owner(slf) }; |
| 1501 |
let guard = shareable_ref.try_write().map_err(map_try_lock_error)?; |
| 1502 |
f(&self_ref, guard) |
| 1503 |
} |
| 1504 |
|
| 1505 |
fn with_index_read<T>( |
| 1506 |
slf: &Bound<'_, Self>, |
| 1507 |
f: impl FnOnce(&Index) -> PyResult<T>, |
| 1508 |
) -> PyResult<T> { |
| 1509 |
Self::with_core_read(slf, |_, guard| f(&guard.index)) |
| 1510 |
} |
| 1511 |
|
| 1512 |
fn with_index_write<T>( |
| 1513 |
slf: &Bound<'_, Self>, |
| 1514 |
f: impl FnOnce(&mut Index) -> PyResult<T>, |
| 1515 |
) -> PyResult<T> { |
| 1516 |
Self::with_core_write(slf, |_, mut guard| f(&mut guard.index)) |
| 1517 |
} |
| 1518 |
|
| 1519 |
fn cache_new_heads_py_list( |
| 1520 |
slf: &Bound<'_, Self>, |
| 1521 |
new_heads: Vec<Revision>, |
| 1522 |
) -> PyResult<Py<PyList>> { |
| 1523 |
let py = slf.py(); |
| 1524 |
let new_heads_py_list = revs_py_list(py, new_heads)?; |
| 1525 |
slf.borrow_mut().head_revs_py_list = |
| 1526 |
Some(new_heads_py_list.clone_ref(py)); |
| 1527 |
// TODO is returning really useful? |
| 1528 |
Ok(new_heads_py_list) |
| 1529 |
} |
| 1530 |
} |
| 1531 |
|
| 1532 |
#[pyclass] |
| 1533 |
struct NodeTree { |
| 1534 |
nt: RwLock<CoreNodeTree>, |
| 1535 |
index: SharedByPyObject<PySharedIndex>, |
| 1536 |
} |
| 1537 |
|
| 1538 |
#[pymethods] |
| 1539 |
impl NodeTree { |
| 1540 |
#[new] |
| 1541 |
// The share/mapping should be set apart to become the PyO3 homolog of |
| 1542 |
// `py_rust_index_to_graph` |
| 1543 |
fn new(index_proxy: &Bound<'_, PyAny>) -> PyResult<Self> { |
| 1544 |
let py_irl = index_proxy.getattr("inner")?; |
| 1545 |
let py_irl_ref = py_irl.cast::<InnerRevlog>()?.borrow(); |
| 1546 |
let shareable_irl = &py_irl_ref.irl; |
| 1547 |
|
| 1548 |
// Safety: the owner is the actual one and we do not leak any |
| 1549 |
// internal reference. |
| 1550 |
let index = unsafe { |
| 1551 |
shareable_irl.share_map(&py_irl, |irl| (&irl.index).into()) |
| 1552 |
}; |
| 1553 |
let nt = CoreNodeTree::default(); // in-RAM, fully mutable |
| 1554 |
|
| 1555 |
Ok(Self { nt: nt.into(), index }) |
| 1556 |
} |
| 1557 |
|
| 1558 |
/// Tell whether the NodeTree is still valid |
| 1559 |
/// |
| 1560 |
/// In case of mutation of the index, the given results are not |
| 1561 |
/// guaranteed to be correct, and in fact, the methods borrowing |
| 1562 |
/// the inner index would fail because of `PySharedRef` poisoning |
| 1563 |
/// (generation-based guard), same as iterating on a `dict` that has |
| 1564 |
/// been meanwhile mutated. |
| 1565 |
fn is_invalidated(&self, py: Python<'_>) -> PyResult<bool> { |
| 1566 |
// Safety: we don't leak any reference derived from self.index, as |
| 1567 |
// we only check errors |
| 1568 |
let result = unsafe { self.index.try_borrow(py) }; |
| 1569 |
// two cases for result to be an error: |
| 1570 |
// - the index has previously been mutably borrowed |
| 1571 |
// - there is currently a mutable borrow |
| 1572 |
// in both cases this amounts for previous results related to |
| 1573 |
// the index to still be valid. |
| 1574 |
Ok(result.is_err()) |
| 1575 |
} |
| 1576 |
|
| 1577 |
fn insert(&self, py: Python<'_>, rev: PyRevision) -> PyResult<()> { |
| 1578 |
// Safety: we don't leak any reference derived from self.index, |
| 1579 |
// as `nt.insert` does not store direct references |
| 1580 |
let idx = &*unsafe { self.index.try_borrow(py)? }; |
| 1581 |
|
| 1582 |
let rev = check_revision(idx, rev)?; |
| 1583 |
if rev == NULL_REVISION { |
| 1584 |
return Err(rev_not_in_index(rev.into())); |
| 1585 |
} |
| 1586 |
|
| 1587 |
let entry = idx.inner().get_entry(rev); |
| 1588 |
let mut nt = self.nt.write().map_err(map_lock_error)?; |
| 1589 |
nt.insert(idx, entry.hash(), rev).map_err(nodemap_error) |
| 1590 |
} |
| 1591 |
|
| 1592 |
fn shortest( |
| 1593 |
&self, |
| 1594 |
py: Python<'_>, |
| 1595 |
node: &Bound<'_, PyBytes>, |
| 1596 |
) -> PyResult<usize> { |
| 1597 |
let nt = self.nt.read().map_err(map_lock_error)?; |
| 1598 |
// Safety: we don't leak any reference derived from self.index |
| 1599 |
// as returned type is Copy |
| 1600 |
let idx = &*unsafe { self.index.try_borrow(py)? }; |
| 1601 |
nt.unique_prefix_len_node(idx, &node_from_py_bytes(node)?) |
| 1602 |
.map_err(nodemap_error)? |
| 1603 |
.ok_or_else(revlog_error_bare) |
| 1604 |
} |
| 1605 |
|
| 1606 |
/// Lookup by node hex prefix in the NodeTree, returning revision number. |
| 1607 |
/// |
| 1608 |
/// This is not part of the classical NodeTree API, but is good enough |
| 1609 |
/// for unit testing, as in `test-rust-revlog.py`. |
| 1610 |
fn prefix_rev_lookup( |
| 1611 |
&self, |
| 1612 |
py: Python<'_>, |
| 1613 |
node_prefix: &Bound<'_, PyBytes>, |
| 1614 |
) -> PyResult<Option<PyRevision>> { |
| 1615 |
let prefix = node_prefix_from_py_bytes(node_prefix)?; |
| 1616 |
let nt = self.nt.read().map_err(map_lock_error)?; |
| 1617 |
// Safety: we don't leak any reference derived from self.index |
| 1618 |
// as returned type is Copy |
| 1619 |
let idx = &*unsafe { self.index.try_borrow(py)? }; |
| 1620 |
Ok(nt.find_bin(idx, prefix).map_err(nodemap_error)?.map(|r| r.into())) |
| 1621 |
} |
| 1622 |
} |
| 1623 |
|
| 1624 |
pub fn init_module<'py>( |
| 1625 |
py: Python<'py>, |
| 1626 |
package: &str, |
| 1627 |
) -> PyResult<Bound<'py, PyModule>> { |
| 1628 |
let m = new_submodule(py, package, "revlog")?; |
| 1629 |
m.add_class::<InnerRevlog>()?; |
| 1630 |
m.add_class::<NodeTree>()?; |
| 1631 |
m.add_class::<ReadingContextManager>()?; |
| 1632 |
Ok(m) |
| 1633 |
} |