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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::ledger_counters::LedgerCounterBumps;
use diem_types::transaction::Version;
use schemadb::SchemaBatch;
use std::collections::HashMap;

/// Structure that collects changes to be made to the DB in one transaction.
///
/// To be specific it carries a batch of db alternations and counter increases that'll be converted
/// to DB alternations on "sealing". This is required to be converted to `SealedChangeSet` before
/// committing to the DB.
pub(crate) struct ChangeSet {
    /// A batch of db alternations.
    pub batch: SchemaBatch,
    /// Counter bumps to be made on commit.
    counter_bumps: HashMap<Version, LedgerCounterBumps>,
}

impl ChangeSet {
    /// Constructor.
    pub fn new() -> Self {
        Self {
            batch: SchemaBatch::new(),
            counter_bumps: HashMap::new(),
        }
    }

    pub fn counter_bumps(&mut self, version: Version) -> &mut LedgerCounterBumps {
        self.counter_bumps
            .entry(version)
            .or_insert_with(LedgerCounterBumps::new)
    }

    #[cfg(test)]
    pub fn new_with_bumps(counter_bumps: HashMap<Version, LedgerCounterBumps>) -> Self {
        Self {
            batch: SchemaBatch::new(),
            counter_bumps,
        }
    }
}

/// ChangeSet that's ready to be committed to the DB.
///
/// This is a wrapper type just to make sure `ChangeSet` to be committed is sealed properly.
pub(crate) struct SealedChangeSet {
    /// A batch of db alternations.
    pub batch: SchemaBatch,
}