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
// Copyright (c) 2024-present, fjall-rs
// This source code is licensed under both the Apache 2.0 and MIT License
// (found in the LICENSE-* files in the repository)
use crateUserValue;
use RefUnwindSafe;
/// A user-defined merge operator for commutative LSM operations.
///
/// Merge operators enable efficient read-modify-write operations by storing
/// partial updates (operands) that are lazily combined during reads and
/// compaction, avoiding the need for explicit read-modify-write cycles.
///
/// # Implementor contract
///
/// The merge function must be **deterministic and stable across multiple
/// passes**. The `base_value` may itself be the result of a previous merge
/// (e.g., from compaction or an earlier read resolution) rather than the
/// original stored value. Repeated merging must produce identical bytes
/// for the same logical state.
///
/// # Examples
///
/// A simple counter merge operator that sums integer operands:
///
/// ```
/// use lsm_tree::{MergeOperator, UserValue};
///
/// struct CounterMerge;
///
/// impl MergeOperator for CounterMerge {
/// fn merge(
/// &self,
/// _key: &[u8],
/// base_value: Option<&[u8]>,
/// operands: &[&[u8]],
/// ) -> lsm_tree::Result<UserValue> {
/// let mut counter: i64 = match base_value {
/// Some(bytes) if bytes.len() == 8 => i64::from_le_bytes(
/// bytes.try_into().expect("checked length"),
/// ),
/// Some(_) => return Err(lsm_tree::Error::MergeOperator),
/// None => 0,
/// };
///
/// for operand in operands {
/// if operand.len() != 8 {
/// return Err(lsm_tree::Error::MergeOperator);
/// }
/// counter += i64::from_le_bytes(
/// (*operand).try_into().expect("checked length"),
/// );
/// }
///
/// Ok(counter.to_le_bytes().to_vec().into())
/// }
/// }
/// ```