use crate::{
Runtime,
dynamic::WeightTrait,
trace::{Batch, BatchLocation, BatchReader, Builder, Cursor},
};
pub(super) fn copy_to_builder<B, Output, C, K, V, T, R>(builder: &mut B, mut cursor: C)
where
B: Builder<Output>,
Output: Batch<Key = K, Val = V, Time = T, R = R>,
C: Cursor<K, V, T, R>,
K: ?Sized,
V: ?Sized,
R: WeightTrait + ?Sized,
{
while cursor.key_valid() {
while cursor.val_valid() {
cursor.map_times(&mut |time, diff| builder.push_time_diff(time, diff));
builder.push_val(cursor.val());
cursor.step_val();
}
builder.push_key(cursor.key());
cursor.step_key();
}
}
pub(super) enum BuildTo {
Memory,
Storage,
Threshold(usize),
}
impl BuildTo {
pub fn for_capacity(key_capacity: usize, value_capacity: usize) -> Self {
match Runtime::min_step_storage_bytes().unwrap_or(usize::MAX) {
usize::MAX => {
Self::Memory
}
min_step_storage_bytes
if key_capacity
.saturating_add(value_capacity)
.saturating_mul(32)
>= min_step_storage_bytes =>
{
Self::Storage
}
min_step_storage_bytes => {
Self::Threshold(min_step_storage_bytes)
}
}
}
}
impl From<BatchLocation> for BuildTo {
fn from(location: BatchLocation) -> Self {
match location {
BatchLocation::Memory => Self::Memory,
BatchLocation::Storage => Self::Storage,
}
}
}
pub fn pick_merge_destination<'a, B, I>(
batches: I,
dst_hint: Option<BatchLocation>,
) -> BatchLocation
where
B: BatchReader,
I: IntoIterator<Item = &'a B>,
{
if let Some(location) = dst_hint {
return location;
}
match Runtime::min_merge_storage_bytes().unwrap_or(usize::MAX) {
0 => BatchLocation::Storage,
usize::MAX => BatchLocation::Memory,
min_storage_bytes => {
let mut size = 0;
for b in batches {
size += b.approximate_byte_size();
if size >= min_storage_bytes {
return BatchLocation::Storage;
}
}
BatchLocation::Memory
}
}
}
pub fn pick_insert_destination<B>(batch: &B) -> BatchLocation
where
B: BatchReader,
{
match Runtime::min_insert_storage_bytes().unwrap_or(usize::MAX) {
0 => BatchLocation::Storage,
usize::MAX => BatchLocation::Memory,
min_storage_bytes => {
if batch.approximate_byte_size() >= min_storage_bytes {
BatchLocation::Storage
} else {
BatchLocation::Memory
}
}
}
}