22
33use super :: Immutable ;
44use crate :: {
5- journal:: authenticated,
5+ journal:: { authenticated, contiguous :: Mutable , Error as JournalError } ,
66 merkle:: { Family , Location , Position } ,
7- qmdb:: { any:: VariableValue , immutable:: operation:: Operation , operation:: Key } ,
7+ qmdb:: { any:: ValueEncoding , immutable:: operation:: Operation , operation:: Key , Error } ,
88 translator:: Translator ,
9- Context ,
9+ Context , Persistable ,
1010} ;
11+ use commonware_codec:: EncodeShared ;
1112use commonware_cryptography:: { Digest , Hasher as CHasher } ;
1213use std:: { collections:: BTreeMap , sync:: Arc } ;
1314
@@ -33,17 +34,17 @@ pub struct UnmerkleizedBatch<F, H, K, V>
3334where
3435 F : Family ,
3536 K : Key ,
36- V : VariableValue ,
37+ V : ValueEncoding ,
3738 H : CHasher ,
3839{
3940 /// Authenticated journal batch for computing the speculative Merkle root.
4041 journal_batch : authenticated:: UnmerkleizedBatch < F , H , Operation < K , V > > ,
4142
4243 /// Pending mutations.
43- mutations : BTreeMap < K , V > ,
44+ mutations : BTreeMap < K , V :: Value > ,
4445
4546 /// Uncommitted key-level changes accumulated by prior batches in the chain.
46- base_diff : Arc < BTreeMap < K , DiffEntry < F , V > > > ,
47+ base_diff : Arc < BTreeMap < K , DiffEntry < F , V :: Value > > > ,
4748
4849 /// Total operation count before this batch (committed DB + prior batches).
4950 /// This batch's i-th operation lands at location `base_size + i`.
@@ -55,12 +56,12 @@ where
5556
5657/// A speculative batch of operations whose root digest has been computed,
5758/// in contrast to [`UnmerkleizedBatch`].
58- pub struct MerkleizedBatch < F : Family , D : Digest , K : Key , V : VariableValue > {
59+ pub struct MerkleizedBatch < F : Family , D : Digest , K : Key , V : ValueEncoding > {
5960 /// Journal batch (Merkle state + accumulated operation segments).
6061 journal : authenticated:: MerkleizedBatch < F , D , Operation < K , V > > ,
6162
6263 /// All uncommitted key-level changes from the batch chain.
63- diff : Arc < BTreeMap < K , DiffEntry < F , V > > > ,
64+ diff : Arc < BTreeMap < K , DiffEntry < F , V :: Value > > > ,
6465
6566 /// Total operation count after this batch.
6667 total_size : u64 ,
@@ -70,7 +71,7 @@ pub struct MerkleizedBatch<F: Family, D: Digest, K: Key, V: VariableValue> {
7071}
7172
7273/// An owned changeset that can be applied to the database.
73- pub struct Changeset < F : Family , K : Key , D : Digest , V : VariableValue > {
74+ pub struct Changeset < F : Family , K : Key , D : Digest , V : ValueEncoding > {
7475 /// The finalized authenticated journal batch (Merkle changeset + item chain).
7576 pub ( super ) journal_finalized : authenticated:: Changeset < F , D , Operation < K , V > > ,
7677
@@ -88,13 +89,19 @@ impl<F, H, K, V> UnmerkleizedBatch<F, H, K, V>
8889where
8990 F : Family ,
9091 K : Key ,
91- V : VariableValue ,
92+ V : ValueEncoding ,
9293 H : CHasher ,
94+ Operation < K , V > : EncodeShared ,
9395{
9496 /// Create a batch from a committed DB (no parent chain).
95- pub ( super ) fn new < E , T > ( immutable : & Immutable < F , E , K , V , H , T > , journal_size : u64 ) -> Self
97+ pub ( super ) fn new < E , C , T > (
98+ immutable : & Immutable < F , E , K , V , C , H , T > ,
99+ journal_size : u64 ,
100+ ) -> Self
96101 where
97102 E : Context ,
103+ C : Mutable < Item = Operation < K , V > > + Persistable < Error = JournalError > ,
104+ C :: Item : EncodeShared ,
98105 T : Translator ,
99106 {
100107 Self {
@@ -108,21 +115,23 @@ where
108115
109116 /// Set a key to a value.
110117 ///
111- /// Duplicate keys are not supported. The key must not already exist in the database or in any
112- /// ancestor batch in the chain. Setting a key that already exists is undefined behavior.
113- pub fn set ( mut self , key : K , value : V ) -> Self {
118+ /// The key must not already exist in the database or in any ancestor batch
119+ /// in the chain. Setting a key that already exists causes undefined behavior.
120+ pub fn set ( mut self , key : K , value : V :: Value ) -> Self {
114121 self . mutations . insert ( key, value) ;
115122 self
116123 }
117124
118125 /// Read through: mutations -> base diff -> committed DB.
119- pub async fn get < E , T > (
126+ pub async fn get < E , C , T > (
120127 & self ,
121128 key : & K ,
122- db : & Immutable < F , E , K , V , H , T > ,
123- ) -> Result < Option < V > , crate :: qmdb :: Error < F > >
129+ db : & Immutable < F , E , K , V , C , H , T > ,
130+ ) -> Result < Option < V :: Value > , Error < F > >
124131 where
125132 E : Context ,
133+ C : Mutable < Item = Operation < K , V > > + Persistable < Error = JournalError > ,
134+ C :: Item : EncodeShared ,
126135 T : Translator ,
127136 {
128137 // Check this batch's pending mutations.
@@ -138,12 +147,12 @@ where
138147 }
139148
140149 /// Resolve mutations into operations, merkleize, and return a [`MerkleizedBatch`].
141- pub fn merkleize ( self , metadata : Option < V > ) -> MerkleizedBatch < F , H :: Digest , K , V > {
150+ pub fn merkleize ( self , metadata : Option < V :: Value > ) -> MerkleizedBatch < F , H :: Digest , K , V > {
142151 let base = self . base_size ;
143152
144153 // Build operations: one Set per key (BTreeMap iterates in sorted order), then Commit.
145154 let mut ops: Vec < Operation < K , V > > = Vec :: with_capacity ( self . mutations . len ( ) + 1 ) ;
146- let mut diff: BTreeMap < K , DiffEntry < F , V > > = BTreeMap :: new ( ) ;
155+ let mut diff: BTreeMap < K , DiffEntry < F , V :: Value > > = BTreeMap :: new ( ) ;
147156
148157 for ( key, value) in self . mutations {
149158 let loc = Location :: new ( base + ops. len ( ) as u64 ) ;
@@ -179,20 +188,25 @@ where
179188 }
180189}
181190
182- impl < F : Family , D : Digest , K : Key , V : VariableValue > MerkleizedBatch < F , D , K , V > {
191+ impl < F : Family , D : Digest , K : Key , V : ValueEncoding > MerkleizedBatch < F , D , K , V >
192+ where
193+ Operation < K , V > : EncodeShared ,
194+ {
183195 /// Return the speculative root.
184196 pub fn root ( & self ) -> D {
185197 self . journal . root ( )
186198 }
187199
188200 /// Read through: diff -> committed DB.
189- pub async fn get < E , H , T > (
201+ pub async fn get < E , C , H , T > (
190202 & self ,
191203 key : & K ,
192- db : & Immutable < F , E , K , V , H , T > ,
193- ) -> Result < Option < V > , crate :: qmdb :: Error < F > >
204+ db : & Immutable < F , E , K , V , C , H , T > ,
205+ ) -> Result < Option < V :: Value > , Error < F > >
194206 where
195207 E : Context ,
208+ C : Mutable < Item = Operation < K , V > > + Persistable < Error = JournalError > ,
209+ C :: Item : EncodeShared ,
196210 H : CHasher < Digest = D > ,
197211 T : Translator ,
198212 {
@@ -277,12 +291,14 @@ impl<F: Family, D: Digest, K: Key, V: VariableValue> MerkleizedBatch<F, D, K, V>
277291 }
278292}
279293
280- impl < F , E , K , V , H , T > Immutable < F , E , K , V , H , T >
294+ impl < F , E , K , V , C , H , T > Immutable < F , E , K , V , C , H , T >
281295where
282296 F : Family ,
283297 E : Context ,
284298 K : Key ,
285- V : VariableValue ,
299+ V : ValueEncoding ,
300+ C : Mutable < Item = Operation < K , V > > + Persistable < Error = JournalError > ,
301+ C :: Item : EncodeShared ,
286302 H : CHasher ,
287303 T : Translator ,
288304{
0 commit comments