Skip to main content

grid_sdk/commits/store/
mod.rs

1// Copyright 2018-2021 Cargill Incorporated
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#[cfg(feature = "diesel")]
16pub(in crate) mod diesel;
17mod error;
18
19#[cfg(feature = "diesel")]
20pub use self::diesel::{DieselCommitStore, DieselConnectionCommitStore};
21pub use error::CommitStoreError;
22
23/// Represents a Grid commit
24#[derive(Clone, Debug, Serialize, PartialEq)]
25pub struct Commit {
26    pub commit_id: String,
27    pub commit_num: i64,
28    pub service_id: Option<String>,
29}
30
31#[derive(Clone, Debug, Serialize, PartialEq)]
32pub struct ChainRecord {
33    pub start_commit_num: i64,
34    pub end_commit_num: i64,
35    pub service_id: Option<String>,
36}
37
38/// A change that has been applied to state, represented in terms of a key/value pair
39#[derive(Clone, Eq, PartialEq)]
40pub enum StateChange {
41    Set { key: String, value: Vec<u8> },
42    Delete { key: String },
43}
44
45/// A notification that some source has committed a set of changes to state
46#[derive(Clone)]
47pub struct CommitEvent {
48    /// An identifier for specifying where the event came from
49    pub service_id: Option<String>,
50    /// An identifier that is unique among events from the source
51    pub id: String,
52    /// May be used to provide ordering of commits from the source. If `None`, ordering is not
53    /// explicitly provided, so it must be inferred from the order in which events are received.
54    pub height: Option<u64>,
55    /// All state changes that are included in the commit
56    pub state_changes: Vec<StateChange>,
57}
58
59pub trait CommitStore {
60    /// Adds an commit to the underlying storage
61    ///
62    /// # Arguments
63    ///
64    ///  * `commit` - The commit to be added
65    fn add_commit(&self, commit: Commit) -> Result<(), CommitStoreError>;
66
67    /// Gets a commit from the underlying storage
68    ///
69    /// # Arguments
70    ///
71    ///  * `commit_num` - The commit to be fetched
72    fn get_commit_by_commit_num(&self, commit_num: i64)
73        -> Result<Option<Commit>, CommitStoreError>;
74
75    /// Gets the current commit ID from the underlying storage
76    fn get_current_commit_id(&self) -> Result<Option<String>, CommitStoreError>;
77
78    /// Gets all the current commits on services.
79    ///
80    /// This returns the latest commit values for all commits where `commit.service_id` is not
81    /// `None`.
82    fn get_current_service_commits(&self) -> Result<Vec<Commit>, CommitStoreError>;
83
84    /// Gets the next commit number from the underlying storage
85    fn get_next_commit_num(&self) -> Result<i64, CommitStoreError>;
86
87    /// Resolves a fork
88    ///
89    /// # Arguments
90    ///
91    ///  * `commit_num` - The commit to be fetched
92    fn resolve_fork(&self, commit_num: i64) -> Result<(), CommitStoreError>;
93
94    /// Creates a commit model from a commit event
95    ///
96    /// # Arguments
97    ///
98    ///  * `event` - The commit event to be processed
99    fn create_db_commit_from_commit_event(
100        &self,
101        event: &CommitEvent,
102    ) -> Result<Option<Commit>, CommitStoreError>;
103}
104
105impl<CS> CommitStore for Box<CS>
106where
107    CS: CommitStore + ?Sized,
108{
109    fn add_commit(&self, commit: Commit) -> Result<(), CommitStoreError> {
110        (**self).add_commit(commit)
111    }
112
113    fn get_commit_by_commit_num(
114        &self,
115        commit_num: i64,
116    ) -> Result<Option<Commit>, CommitStoreError> {
117        (**self).get_commit_by_commit_num(commit_num)
118    }
119
120    fn get_current_commit_id(&self) -> Result<Option<String>, CommitStoreError> {
121        (**self).get_current_commit_id()
122    }
123
124    fn get_current_service_commits(&self) -> Result<Vec<Commit>, CommitStoreError> {
125        (**self).get_current_service_commits()
126    }
127
128    fn get_next_commit_num(&self) -> Result<i64, CommitStoreError> {
129        (**self).get_next_commit_num()
130    }
131
132    fn resolve_fork(&self, commit_num: i64) -> Result<(), CommitStoreError> {
133        (**self).resolve_fork(commit_num)
134    }
135
136    fn create_db_commit_from_commit_event(
137        &self,
138        event: &CommitEvent,
139    ) -> Result<Option<Commit>, CommitStoreError> {
140        (**self).create_db_commit_from_commit_event(event)
141    }
142}