1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use super::{Error, View, arena};
use core::alloc::Allocator;
pub(super) const MAX_MUTATIONS: usize = 2048;
/// One encoded mutation in an ordered, bounded batch.
#[derive(Clone, Copy)]
pub enum Mutation<'a> {
/// Insert or replace exact bytes.
Put(&'a [u8], &'a [u8]),
/// Delete if present; absence is a successful no-op.
Remove(&'a [u8]),
}
impl Mutation<'_> {
fn key(&self) -> &[u8] {
match self {
Self::Put(key, _) | Self::Remove(key) => key,
}
}
}
impl<const K: usize, const V: usize, A: Allocator + Clone, P: Allocator + Clone>
View<'_, K, V, A, P>
{
/// Apply at most 2,048 physical mutations after complete bounds/capacity preflight.
/// A DB entity mutation may expand into many row/index mutations; this is not
/// the consumer's entity-batch limit. Preflight is quadratic in batch length.
/// This supports rows and secondary entries in one collection. It does not
/// infer uniqueness/reference policy or span independently owned roots.
/// A host unwind still poisons the owner; installed trap rollback is runtime-owned.
///
/// # Errors
/// Excessive batch size, byte bounds, intermediate live-entry limit or allocator
/// refusal preserve every record. Duplicate keys follow sequential semantics.
pub fn apply(&mut self, mutations: &[Mutation<'_>]) -> Result<(), Error> {
if mutations.len() > MAX_MUTATIONS {
return Err(Error::Limit);
}
let mut live = self.len();
let mut peak = live;
let mut payload = arena::Plan::new(&self.map.image.payload);
for (position, mutation) in mutations.iter().enumerate() {
if mutation.key().len() > K
|| matches!(mutation, Mutation::Put(_, value) if value.len() > V)
{
return Err(Error::Limit);
}
let previous = mutations[..position]
.iter()
.rev()
.find(|prior| prior.key() == mutation.key())
.map_or_else(
|| {
let id = self.map.find(mutation.key());
(id != 0).then(|| self.map.node(id).value.length as usize)
},
|prior| match prior {
Mutation::Put(_, value) => Some(value.len()),
Mutation::Remove(_) => None,
},
);
let present = previous.is_some();
match (mutation, previous) {
(Mutation::Put(key, value), None) => {
payload.allocate(key.len())?;
payload.allocate(value.len())?;
}
(Mutation::Put(_, value), Some(old)) => payload.replace(old, value.len())?,
(Mutation::Remove(key), Some(old)) => {
payload.retire(key.len())?;
payload.retire(old)?;
}
_ => {}
}
match (mutation, present) {
(Mutation::Put(..), false) => live = live.checked_add(1).ok_or(Error::Limit)?,
(Mutation::Remove(..), true) => live -= 1,
_ => {}
}
if live > self.map.limit {
return Err(Error::Limit);
}
peak = peak.max(live);
}
let additional = peak.saturating_sub(self.map.image.nodes.len());
self.map
.image
.nodes
.try_reserve(additional)
.map_err(|_| Error::Allocation)?;
payload.reserve(&mut self.map.image.payload)?;
for mutation in mutations {
match mutation {
Mutation::Put(key, value) => {
self.insert(key, value)?;
}
Mutation::Remove(key) => {
self.remove_discard(key);
}
}
}
Ok(())
}
}