| 1 |
use std::ops::Deref; |
| 2 |
|
| 3 |
use hg::revlog::deltas::estimate_combined_deltas_size; |
| 4 |
use hg::revlog::deltas::optimize_base; |
| 5 |
use hg::utils::filter_map_results; |
| 6 |
use hg::Revision; |
| 7 |
use pyo3::buffer::PyBuffer; |
| 8 |
use pyo3::prelude::*; |
| 9 |
use pyo3::types::PyIterator; |
| 10 |
use pyo3::types::PyList; |
| 11 |
|
| 12 |
use crate::utils::new_submodule; |
| 13 |
use crate::utils::PyBufferDeref; |
| 14 |
|
| 15 |
fn get_vec_of_non_empty_chunks( |
| 16 |
chunks: &Bound<'_, PyList>, |
| 17 |
) -> PyResult<Vec<PyBufferDeref>> { |
| 18 |
filter_map_results( |
| 19 |
chunks.iter().map(|b| b.extract()), |
| 20 |
|chunk: PyBuffer<u8>| { |
| 21 |
if chunk.len_bytes() == 0 { |
| 22 |
Ok(None) |
| 23 |
} else { |
| 24 |
Ok(Some(PyBufferDeref::new(chunk))) |
| 25 |
} |
| 26 |
}, |
| 27 |
) |
| 28 |
.collect() |
| 29 |
} |
| 30 |
|
| 31 |
fn get_iter_of_rev_chunk_pairs<'a>( |
| 32 |
deltas: &Bound<'a, PyIterator>, |
| 33 |
) -> PyResult<impl Iterator<Item = (Revision, PyBufferDeref)> + use<'a>> { |
| 34 |
Ok(deltas.clone().map(|t| { |
| 35 |
let p = t.expect("iterating over rev+chunk pairs failed"); |
| 36 |
let rev_py = |
| 37 |
p.get_item(0).expect("failed to read `rev` in rev+chunk pair"); |
| 38 |
let chunk_py = |
| 39 |
p.get_item(1).expect("failed to read `chunk` in rev+chunk pair"); |
| 40 |
( |
| 41 |
Revision(rev_py.extract().expect("failed to convert rev to i32")), |
| 42 |
PyBufferDeref::new( |
| 43 |
chunk_py |
| 44 |
.extract() |
| 45 |
.expect("failed to convert chunk to PyBuffer"), |
| 46 |
), |
| 47 |
) |
| 48 |
})) |
| 49 |
} |
| 50 |
|
| 51 |
#[pyfunction] |
| 52 |
#[pyo3(name = "estimate_combined_deltas_size")] |
| 53 |
fn estimate_combined_deltas_size_python( |
| 54 |
deltas: &Bound<'_, PyList>, |
| 55 |
) -> PyResult<i32> { |
| 56 |
let chunks = get_vec_of_non_empty_chunks(deltas)?; |
| 57 |
Ok(match &chunks[..] { |
| 58 |
[] => 0, |
| 59 |
[single] => single.deref().len(), |
| 60 |
_ => estimate_combined_deltas_size( |
| 61 |
chunks.iter().map(|d| d.deref()).collect(), |
| 62 |
), |
| 63 |
} |
| 64 |
.try_into() |
| 65 |
.expect("estimated size doesn't fit a i32")) |
| 66 |
} |
| 67 |
|
| 68 |
#[pyfunction] |
| 69 |
#[pyo3(name = "optimize_base")] |
| 70 |
fn optimize_base_python( |
| 71 |
delta: &[u8], |
| 72 |
next_deltas: &Bound<'_, PyIterator>, |
| 73 |
max_size: usize, |
| 74 |
) -> PyResult<Option<i32>> { |
| 75 |
let mut deltas = get_iter_of_rev_chunk_pairs(next_deltas)? |
| 76 |
.filter(|t| !t.1.deref().is_empty()); |
| 77 |
Ok(optimize_base(delta, &mut deltas, max_size).map(|r| r.0)) |
| 78 |
} |
| 79 |
|
| 80 |
pub fn init_module<'py>( |
| 81 |
py: Python<'py>, |
| 82 |
package: &str, |
| 83 |
) -> PyResult<Bound<'py, PyModule>> { |
| 84 |
let m = new_submodule(py, package, "deltas")?; |
| 85 |
m.add_function(wrap_pyfunction!( |
| 86 |
estimate_combined_deltas_size_python, |
| 87 |
&m |
| 88 |
)?)?; |
| 89 |
m.add_function(wrap_pyfunction!(optimize_base_python, &m)?)?; |
| 90 |
Ok(m) |
| 91 |
} |