commonware_storage/qmdb/any/operation/update/
ordered.rs1use crate::qmdb::{
2 any::{
3 operation::{update::sealed::Sealed, Update as UpdateTrait},
4 value::{FixedEncoding, ValueEncoding, VariableEncoding},
5 FixedValue, VariableValue,
6 },
7 operation::Key,
8};
9use commonware_codec::{
10 Encode as _, EncodeSize, Error as CodecError, FixedSize, Read, ReadExt as _, Write,
11};
12use commonware_formatting::hex;
13use commonware_runtime::{Buf, BufMut};
14use commonware_utils::Array;
15use std::fmt;
16
17#[derive(Clone, PartialEq, Debug, Eq)]
18pub struct Update<K: Key, V: ValueEncoding> {
19 pub key: K,
20 pub value: V::Value,
21 pub next_key: K,
22}
23
24#[cfg(feature = "arbitrary")]
25impl<K: Key, V: ValueEncoding> arbitrary::Arbitrary<'_> for Update<K, V>
26where
27 K: for<'a> arbitrary::Arbitrary<'a>,
28 V::Value: for<'a> arbitrary::Arbitrary<'a>,
29{
30 fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
31 Ok(Self {
32 key: u.arbitrary()?,
33 value: u.arbitrary()?,
34 next_key: u.arbitrary()?,
35 })
36 }
37}
38
39impl<K: Key, V: ValueEncoding> Sealed for Update<K, V> {}
40
41impl<K: Key, V: ValueEncoding> UpdateTrait for Update<K, V> {
42 type Key = K;
43 type Value = V::Value;
44 type ValueEncoding = V;
45 type Cached = K;
46
47 const STAGES_DELETES: bool = false;
50
51 const STAGES_ANCESTORS: Option<K> = None;
54
55 fn key(&self) -> &K {
56 &self.key
57 }
58
59 fn value(&self) -> &V::Value {
60 &self.value
61 }
62
63 fn cached(&self) -> K {
64 self.next_key.clone()
65 }
66
67 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68 write!(
69 f,
70 "[key:{} next_key:{} value:{}]",
71 hex(&self.key),
72 hex(&self.next_key),
73 hex(&self.value.encode())
74 )
75 }
76}
77
78impl<K: Array, V: FixedValue> FixedSize for Update<K, FixedEncoding<V>> {
79 const SIZE: usize = K::SIZE + V::SIZE + K::SIZE;
80}
81
82impl<K, V> Write for Update<K, V>
83where
84 K: Key + Write,
85 V: ValueEncoding,
86 V::Value: Write,
87{
88 fn write(&self, buf: &mut impl BufMut) {
89 self.key.write(buf);
90 self.value.write(buf);
91 self.next_key.write(buf);
92 }
93}
94
95impl<K: Array, V: FixedValue> Read for Update<K, FixedEncoding<V>> {
96 type Cfg = ();
97
98 fn read_cfg(buf: &mut impl Buf, cfg: &Self::Cfg) -> Result<Self, CodecError> {
99 let key = K::read(buf)?;
100 let value = V::read_cfg(buf, cfg)?;
101 let next_key = K::read(buf)?;
102 Ok(Self {
103 key,
104 value,
105 next_key,
106 })
107 }
108}
109
110impl<K, V> EncodeSize for Update<K, VariableEncoding<V>>
111where
112 K: Key + EncodeSize,
113 V: VariableValue,
114{
115 fn encode_size(&self) -> usize {
116 self.key.encode_size() + self.value.encode_size() + self.next_key.encode_size()
117 }
118}
119
120impl<K, V> Read for Update<K, VariableEncoding<V>>
121where
122 K: Key + Read,
123 V: VariableValue,
124{
125 type Cfg = (<K as Read>::Cfg, <V as Read>::Cfg);
126
127 fn read_cfg(buf: &mut impl Buf, cfg: &Self::Cfg) -> Result<Self, CodecError> {
128 let key = K::read_cfg(buf, &cfg.0)?;
129 let value = V::read_cfg(buf, &cfg.1)?;
130 let next_key = K::read_cfg(buf, &cfg.0)?;
131 Ok(Self {
132 key,
133 value,
134 next_key,
135 })
136 }
137}