coven_database/
journal.rs1use super::FileWrite;
7use crate::{
8 DbError, NewOperation, OperationId, OperationRecord, OperationUpdate, RetrySubject, StoreState,
9};
10
11pub struct Journal<'write> {
13 files: &'write FileWrite<'write>,
14}
15
16impl<'write> Journal<'write> {
17 pub(crate) fn new(files: &'write FileWrite<'write>) -> Self {
18 Self { files }
19 }
20
21 pub fn operation(&self, id: OperationId) -> Result<Option<OperationRecord>, DbError> {
24 crate::operation::find(self.files.database, id)
25 }
26
27 pub fn operations(&self, kind: &str) -> Result<Vec<OperationRecord>, DbError> {
29 crate::operation::of_kind(self.files.database, kind)
30 }
31
32 pub fn advance_operation(&self, update: &OperationUpdate) -> Result<(), DbError> {
36 crate::operation::advance(self.files.database, update)
37 }
38
39 pub fn finish_operation(&self, id: OperationId) -> Result<(), DbError> {
43 if crate::operation::finish(self.files.database, id)? != 1 {
44 return Err(DbError::OperationChanged(id));
45 }
46 Ok(())
47 }
48
49 pub fn store_state(&self) -> Result<StoreState, DbError> {
52 crate::store_state_tables::read(self.files.database)
53 }
54
55 pub fn applied_slot(&self) -> Result<u64, DbError> {
57 crate::sequence::applied(self.files.database)
58 }
59
60 pub fn start_operation(&self, operation: &NewOperation) -> Result<OperationId, DbError> {
63 crate::operation::insert(self.files.database, operation)
64 }
65
66 pub fn start_app_operation(
70 &self,
71 operation: &NewOperation,
72 kind: &str,
73 ) -> Result<OperationId, DbError> {
74 crate::operation::insert_for_app(self.files.database, operation, kind)
75 }
76
77 pub(crate) fn acknowledge(&self, id: OperationId) -> Result<(), DbError> {
82 crate::operation::acknowledge(self.files.database, id)
83 }
84
85 pub fn operation_pending<P: crate::PendingValue>(
87 &self,
88 id: OperationId,
89 ) -> Result<Option<P>, DbError> {
90 crate::pending::operation(self.files.database, id)
91 }
92
93 pub fn pending_can_retry<P: crate::PendingValue>(
96 &self,
97 subject: &P::Subject,
98 ) -> Result<bool, DbError> {
99 crate::pending::retry::<P>(self.files.database, self.files.call, subject)
100 }
101
102 pub fn clear_pending<S: serde::Serialize>(&self, subject: &S) -> Result<(), DbError> {
104 crate::pending::clear(self.files.database, subject)
105 }
106
107 pub fn clear_retry(&self, subject: RetrySubject) -> Result<(), DbError> {
109 crate::retry::clear(self.files.database, subject)
110 }
111}
112
113#[cfg(test)]
114#[path = "journal_tests.rs"]
115mod tests;