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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use radix_common::prelude::*;
use radix_rust::prelude::borrow::*;
use radix_substate_store_interface::interface::*;

pub type UnmergeableSubstateDatabaseOverlay<'a, S> = SubstateDatabaseOverlay<&'a S, S>;
pub type MergeableSubstateDatabaseOverlay<'a, S> = SubstateDatabaseOverlay<&'a mut S, S>;
pub type OwnedSubstateDatabaseOverlay<S> = SubstateDatabaseOverlay<S, S>;

pub struct SubstateDatabaseOverlay<S, D> {
    /// The database overlay. All commits made to the database are written to the overlay. This
    /// covers new values and deletions too.
    overlay: StagingDatabaseUpdates,

    /// A mutable or immutable reference to the root database that this type overlays.
    /// It only needs to be mutable if you wish to commit to the root store.
    /// To be useful, `S` should implement at least `Borrow<D>`.
    root: S,

    /// The concrete type of the underlying substate database.
    substate_database_type: PhantomData<D>,
}

impl<'a, D> UnmergeableSubstateDatabaseOverlay<'a, D> {
    pub fn new_unmergeable(root_database: &'a D) -> Self {
        Self::new(root_database)
    }
}

impl<'a, D> MergeableSubstateDatabaseOverlay<'a, D> {
    pub fn new_mergeable(root_database: &'a mut D) -> Self {
        Self::new(root_database)
    }
}

impl<D> OwnedSubstateDatabaseOverlay<D> {
    pub fn new_owned(root_database: D) -> Self {
        Self::new(root_database)
    }
}

impl<S, D> SubstateDatabaseOverlay<S, D> {
    pub fn new(root_database: S) -> Self {
        Self {
            overlay: Default::default(),
            root: root_database,
            substate_database_type: PhantomData,
        }
    }

    pub fn deconstruct(self) -> (S, DatabaseUpdates) {
        (self.root, self.overlay.into())
    }

    pub fn database_updates(&self) -> DatabaseUpdates {
        self.overlay.clone().into()
    }

    pub fn into_database_updates(self) -> DatabaseUpdates {
        self.overlay.into()
    }
}

impl<S: Borrow<D>, D> SubstateDatabaseOverlay<S, D> {
    fn get_readable_root(&self) -> &D {
        self.root.borrow()
    }
}

impl<S: BorrowMut<D>, D> SubstateDatabaseOverlay<S, D> {
    fn get_writable_root(&mut self) -> &mut D {
        self.root.borrow_mut()
    }
}

impl<S: BorrowMut<D>, D: CommittableSubstateDatabase> SubstateDatabaseOverlay<S, D> {
    pub fn commit_overlay_into_root_store(&mut self) {
        let overlay = mem::replace(&mut self.overlay, StagingDatabaseUpdates::default());
        self.get_writable_root().commit(&overlay.into());
    }
}

impl<S: Borrow<D>, D: SubstateDatabase> SubstateDatabase for SubstateDatabaseOverlay<S, D> {
    fn get_substate(
        &self,
        partition_key @ DbPartitionKey {
            node_key,
            partition_num,
        }: &DbPartitionKey,
        sort_key: &DbSortKey,
    ) -> Option<DbSubstateValue> {
        let overlay_lookup_result = match self.overlay.node_updates.get(node_key) {
            // This particular node key exists in the overlay and probably has some partitions
            // written to the overlay.
            Some(StagingNodeDatabaseUpdates { partition_updates }) => {
                match partition_updates.get(partition_num) {
                    // This partition has some data written to the overlay
                    Some(StagingPartitionDatabaseUpdates::Delta { substate_updates }) => {
                        match substate_updates.get(sort_key) {
                            // The substate value is written to the overlay. It is a database set
                            // so we return the new value.
                            Some(DatabaseUpdate::Set(substate_value)) => {
                                OverlayLookupResult::Found(Some(substate_value))
                            }
                            // The substate value is written to the overlay. It is a database delete
                            // so we return a `Found(None)`.
                            Some(DatabaseUpdate::Delete) => OverlayLookupResult::Found(None),
                            // This particular substate was not written to the overlay and should be
                            // read from the underlying database.
                            None => OverlayLookupResult::NotFound,
                        }
                    }
                    Some(StagingPartitionDatabaseUpdates::Reset {
                        new_substate_values,
                    }) => match new_substate_values.get(sort_key) {
                        // The substate value is written to the overlay.
                        Some(substate_value) => OverlayLookupResult::Found(Some(substate_value)),
                        // In a partition reset we delete all substates in a partition and can also
                        // write new substates there. If the substate that we're looking for can't
                        // be found in the new substate values of a partition delete then it is
                        // one of the deleted substates. Therefore, the following will report that
                        // it has found the substate value in the overlay and that the substate
                        // does not exist.
                        None => OverlayLookupResult::Found(None),
                    },
                    // This particular partition for the specified node key does not exist in the
                    // overlay and should be read from the underlying database.
                    None => OverlayLookupResult::NotFound,
                }
            }
            // This particular node key does not exist in the overlay. The substate must be read
            // from the underlying database.
            None => OverlayLookupResult::NotFound,
        };

        match overlay_lookup_result {
            OverlayLookupResult::Found(substate_value) => substate_value.cloned(),
            OverlayLookupResult::NotFound => self
                .get_readable_root()
                .get_substate(partition_key, sort_key),
        }
    }

    fn list_entries_from(
        &self,
        partition_key @ DbPartitionKey {
            node_key,
            partition_num,
        }: &DbPartitionKey,
        from_sort_key: Option<&DbSortKey>,
    ) -> Box<dyn Iterator<Item = PartitionEntry> + '_> {
        // This function iterates over entries of the specified partition. Therefore, we don't need
        // to think about other partitions here. We first check if there are any partition updates
        // for the specified partition. If there is not, no overlaying is needed and we can just
        // return the iterator of the root store.
        let from_sort_key = from_sort_key.cloned();
        match self.overlay.node_updates.get(node_key) {
            // There is a partition update in the overlay.
            Some(StagingNodeDatabaseUpdates { partition_updates }) => {
                match partition_updates.get(partition_num) {
                    // The partition was reset. None of the substates of this partition that exist
                    // in the root store "exist" anymore. We just need an iterator over the new
                    // substates in the reset action.
                    Some(StagingPartitionDatabaseUpdates::Reset {
                        new_substate_values,
                    }) => {
                        match from_sort_key {
                            // A `from_sort_key` is specified. Only return sort keys that are larger
                            // than or equal to the from sort key. We do this through BTreeMap's
                            // range function instead of doing filtering. We're able to do this
                            // since a `BTreeMap`'s keys are always sorted.
                            Some(from_sort_key) => {
                                Box::new(new_substate_values.range(from_sort_key..).map(
                                    |(sort_key, substate_value)| {
                                        (sort_key.clone(), substate_value.clone())
                                    },
                                ))
                            }
                            // No `from_sort_key` is specified. Start iterating from the beginning.
                            None => Box::new(new_substate_values.iter().map(
                                |(sort_key, substate_value)| {
                                    (sort_key.clone(), substate_value.clone())
                                },
                            )),
                        }
                    }
                    // There are some changes that need to be overlayed.
                    Some(StagingPartitionDatabaseUpdates::Delta { substate_updates }) => {
                        let underlying = self
                            .get_readable_root()
                            .list_entries_from(partition_key, from_sort_key.as_ref());

                        match from_sort_key {
                            // A `from_sort_key` is specified. Only return sort keys that are larger
                            // than or equal to the from sort key. We do this through BTreeMap's
                            // range function instead of doing filtering. We're able to do this
                            // since a `BTreeMap`'s keys are always sorted.
                            Some(from_sort_key) => {
                                let overlaying = substate_updates.range(from_sort_key..).map(
                                    |(sort_key, database_update)| match database_update {
                                        DatabaseUpdate::Set(substate_value) => {
                                            (sort_key.clone(), Some(substate_value.clone()))
                                        }
                                        DatabaseUpdate::Delete => (sort_key.clone(), None),
                                    },
                                );
                                Box::new(OverlayingIterator::new(underlying, overlaying))
                            }
                            // No `from_sort_key` is specified. Start iterating from the beginning.
                            None => {
                                let overlaying =
                                    substate_updates.iter().map(|(sort_key, database_update)| {
                                        match database_update {
                                            DatabaseUpdate::Set(substate_value) => {
                                                (sort_key.clone(), Some(substate_value.clone()))
                                            }
                                            DatabaseUpdate::Delete => (sort_key.clone(), None),
                                        }
                                    });
                                Box::new(OverlayingIterator::new(underlying, overlaying))
                            }
                        }
                    }
                    // Overlay doesn't contain anything for the provided partition number. Return an
                    // iterator over the data in the root store.
                    None => self
                        .get_readable_root()
                        .list_entries_from(partition_key, from_sort_key.as_ref()),
                }
            }
            // Overlay doesn't contain anything for the provided node key. Return an iterator over
            // the data in the root store.
            None => self
                .get_readable_root()
                .list_entries_from(partition_key, from_sort_key.as_ref()),
        }
    }
}

impl<S, D> CommittableSubstateDatabase for SubstateDatabaseOverlay<S, D> {
    fn commit(&mut self, database_updates: &DatabaseUpdates) {
        merge_database_updates(&mut self.overlay, database_updates.clone())
    }
}

impl<S: Borrow<D>, D: ListableSubstateDatabase> ListableSubstateDatabase
    for SubstateDatabaseOverlay<S, D>
{
    fn list_partition_keys(&self) -> Box<dyn Iterator<Item = DbPartitionKey> + '_> {
        let overlying = self
            .overlay
            .node_updates
            .iter()
            .flat_map(
                |(node_key, StagingNodeDatabaseUpdates { partition_updates })| {
                    partition_updates
                        .keys()
                        .map(|partition_num| DbPartitionKey {
                            node_key: node_key.clone(),
                            partition_num: *partition_num,
                        })
                },
            )
            .map(|partition_key| (partition_key, Some(())));
        let underlying = self
            .get_readable_root()
            .list_partition_keys()
            .map(|partition_key| (partition_key, ()));

        Box::new(OverlayingIterator::new(underlying, overlying).map(|(value, _)| value))
    }
}

pub enum OverlayLookupResult<T> {
    Found(T),
    NotFound,
}

fn merge_database_updates(this: &mut StagingDatabaseUpdates, other: DatabaseUpdates) {
    for (
        other_node_key,
        NodeDatabaseUpdates {
            partition_updates: other_partition_updates,
        },
    ) in other.node_updates.into_iter()
    {
        // Check if the other node key exists in `this` database updates.
        match this.node_updates.get_mut(&other_node_key) {
            // The node key exists in `this` database updates.
            Some(StagingNodeDatabaseUpdates {
                partition_updates: this_partition_updates,
            }) => {
                for (other_partition_num, other_partition_database_updates) in
                    other_partition_updates.into_iter()
                {
                    // Check if the partition num exists in `this` database updates
                    match this_partition_updates.get_mut(&other_partition_num) {
                        // The partition exists in both `this` and `other` and now we must combine
                        // both the partition database updates together
                        Some(this_partition_database_updates) => {
                            match (
                                this_partition_database_updates,
                                other_partition_database_updates,
                            ) {
                                // This and other are both `Delta`. We insert all entries in the
                                // other state updates into this substate updates. This will also
                                // override anything in `this` with anything in `other`.
                                (
                                    StagingPartitionDatabaseUpdates::Delta {
                                        substate_updates: this_substate_updates,
                                    },
                                    PartitionDatabaseUpdates::Delta {
                                        substate_updates: other_substate_updates,
                                    },
                                ) => this_substate_updates.extend(other_substate_updates),
                                // We need to apply the delta on the reset. 
                                (
                                    StagingPartitionDatabaseUpdates::Reset {
                                        new_substate_values: this_new_substate_values,
                                    },
                                    PartitionDatabaseUpdates::Delta {
                                        substate_updates: other_substate_updates,
                                    },
                                ) => {
                                    for (other_sort_key, other_database_update) in
                                        other_substate_updates.into_iter()
                                    {
                                        match other_database_update {
                                            DatabaseUpdate::Set(other_substate_value) => {
                                                this_new_substate_values
                                                    .insert(other_sort_key, other_substate_value);
                                            }
                                            DatabaseUpdate::Delete => {
                                                this_new_substate_values.remove(&other_sort_key);
                                            }
                                        }
                                    }
                                }
                                // Whatever the current state is, if the other database update is
                                // a partition reset then it takes precedence.
                                (
                                    this_partition_database_updates,
                                    other_partition_database_updates @ PartitionDatabaseUpdates::Reset { .. },
                                ) => {
                                    *this_partition_database_updates = other_partition_database_updates.into();
                                }
                            }
                        }
                        // The partition num does not exist in `this` database updates. This merge
                        // is simple, just insert it.
                        None => {
                            this_partition_updates.insert(
                                other_partition_num,
                                other_partition_database_updates.into(),
                            );
                        }
                    }
                }
            }
            // The node key does not exist in `this` database updates. This merge is simple, just
            // insert it.
            None => {
                this.node_updates.insert(
                    other_node_key,
                    NodeDatabaseUpdates {
                        partition_updates: other_partition_updates,
                    }
                    .into(),
                );
            }
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Sbor, Default)]
struct StagingDatabaseUpdates {
    node_updates: BTreeMap<DbNodeKey, StagingNodeDatabaseUpdates>,
}

impl From<StagingDatabaseUpdates> for DatabaseUpdates {
    fn from(value: StagingDatabaseUpdates) -> Self {
        Self {
            node_updates: value
                .node_updates
                .into_iter()
                .map(|(key, value)| (key, NodeDatabaseUpdates::from(value)))
                .collect(),
        }
    }
}

impl From<DatabaseUpdates> for StagingDatabaseUpdates {
    fn from(value: DatabaseUpdates) -> Self {
        Self {
            node_updates: value
                .node_updates
                .into_iter()
                .map(|(key, value)| (key, StagingNodeDatabaseUpdates::from(value)))
                .collect(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Sbor, Default)]
struct StagingNodeDatabaseUpdates {
    partition_updates: BTreeMap<DbPartitionNum, StagingPartitionDatabaseUpdates>,
}

impl From<StagingNodeDatabaseUpdates> for NodeDatabaseUpdates {
    fn from(value: StagingNodeDatabaseUpdates) -> Self {
        Self {
            partition_updates: value
                .partition_updates
                .into_iter()
                .map(|(key, value)| (key, PartitionDatabaseUpdates::from(value)))
                .collect(),
        }
    }
}

impl From<NodeDatabaseUpdates> for StagingNodeDatabaseUpdates {
    fn from(value: NodeDatabaseUpdates) -> Self {
        Self {
            partition_updates: value
                .partition_updates
                .into_iter()
                .map(|(key, value)| (key, StagingPartitionDatabaseUpdates::from(value)))
                .collect(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Sbor)]
enum StagingPartitionDatabaseUpdates {
    Delta {
        substate_updates: BTreeMap<DbSortKey, DatabaseUpdate>,
    },

    Reset {
        new_substate_values: BTreeMap<DbSortKey, DbSubstateValue>,
    },
}

impl From<StagingPartitionDatabaseUpdates> for PartitionDatabaseUpdates {
    fn from(value: StagingPartitionDatabaseUpdates) -> Self {
        match value {
            StagingPartitionDatabaseUpdates::Delta { substate_updates } => Self::Delta {
                substate_updates: substate_updates.into_iter().collect(),
            },
            StagingPartitionDatabaseUpdates::Reset {
                new_substate_values,
            } => Self::Reset {
                new_substate_values: new_substate_values.into_iter().collect(),
            },
        }
    }
}

impl From<PartitionDatabaseUpdates> for StagingPartitionDatabaseUpdates {
    fn from(value: PartitionDatabaseUpdates) -> Self {
        match value {
            PartitionDatabaseUpdates::Delta { substate_updates } => Self::Delta {
                substate_updates: substate_updates.into_iter().collect(),
            },
            PartitionDatabaseUpdates::Reset {
                new_substate_values,
            } => Self::Reset {
                new_substate_values: new_substate_values.into_iter().collect(),
            },
        }
    }
}