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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//! Merge operators: this crate's own trait and error (D282), adapted to the engine's.
use fmt;
use Arc;
/// A merge operator failure (bad operand encoding, overflow policy), returned by a
/// [`MergeOperator`]. A read that hits it fails with
/// [`ErrorCode::MergeFailed`](crate::ErrorCode::MergeFailed).
/// A user-defined merge, applied at read and compaction time to operands written blind
/// ([`RowMutation::merge`](crate::RowMutation::merge)). Register it with
/// [`Options::merge_operator`](crate::Options::merge_operator) and name it in a family
/// ([`Family::merge_operator`](crate::Family::merge_operator)).
///
/// Identified by [`MergeOperator::name`], which is stored in the family's options in the
/// file, so a binary that registers a different operator under that name is the only way to
/// misinterpret the data. Values and operands are stored values (tag byte included).
///
/// Operators are **associative folds**: the accumulator starts as a copy of the newest
/// operand, each older operand is folded in with [`MergeOperator::merge`], and
/// [`MergeOperator::finish`] applies the result to the base value (the newest put below the
/// operands) if one was found. Compaction that finds no base keeps the accumulator as a
/// single combined operand, so `older` passed to `merge` may itself combine several operands,
/// which associativity makes equivalent.
///
/// ```
/// use pigeonhole::{MergeError, MergeOperator};
///
/// /// Concatenates byte values, oldest first.
/// #[derive(Debug)]
/// struct Append;
///
/// impl MergeOperator for Append {
/// fn name(&self) -> &str {
/// "example.append"
/// }
/// fn merge(&self, acc: &mut Vec<u8>, older: &[u8]) -> Result<(), MergeError> {
/// // Both are `Bytes` values: tag 0x00, then the payload.
/// let newer = acc.split_off(1);
/// acc.extend_from_slice(&older[1..]);
/// acc.extend_from_slice(&newer);
/// Ok(())
/// }
/// fn finish(&self, base: Option<&[u8]>, acc: &mut Vec<u8>) -> Result<(), MergeError> {
/// if let Some(base) = base {
/// let newer = acc.split_off(1);
/// acc.extend_from_slice(&base[1..]);
/// acc.extend_from_slice(&newer);
/// }
/// Ok(())
/// }
/// }
///
/// let mut acc = b"\x00c".to_vec();
/// Append.merge(&mut acc, b"\x00b").unwrap();
/// Append.finish(Some(b"\x00a"), &mut acc).unwrap();
/// assert_eq!(acc, b"\x00abc");
/// ```
/// A [`MergeOperator`] as the engine's trait: one more call through a pointer per fold.
pub );