-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathstorage.rs
190 lines (175 loc) · 6.19 KB
/
storage.rs
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
use super::persistence::persister::PersisterKind;
use crate::configs::system::SystemConfig;
use crate::state::system::{PartitionState, StreamState, TopicState};
use crate::streaming::partitions::partition::{ConsumerOffset, Partition};
use crate::streaming::partitions::storage::FilePartitionStorage;
use crate::streaming::streams::storage::FileStreamStorage;
use crate::streaming::streams::stream::Stream;
use crate::streaming::systems::info::SystemInfo;
use crate::streaming::systems::storage::FileSystemInfoStorage;
use crate::streaming::topics::storage::FileTopicStorage;
use crate::streaming::topics::topic::Topic;
use iggy::consumer::ConsumerKind;
use iggy::error::IggyError;
#[cfg(test)]
use mockall::automock;
use std::fmt::Debug;
use std::future::Future;
use std::sync::Arc;
macro_rules! forward_async_methods {
(
$(
async fn $method_name:ident(
&self $(, $arg:ident : $arg_ty:ty )*
) -> $ret:ty ;
)*
) => {
$(
pub async fn $method_name(&self, $( $arg: $arg_ty ),* ) -> $ret {
match self {
Self::File(d) => d.$method_name($( $arg ),*).await,
#[cfg(test)]
Self::Mock(s) => s.$method_name($( $arg ),*).await,
}
}
)*
}
}
#[derive(Debug)]
pub enum SystemInfoStorageKind {
File(FileSystemInfoStorage),
#[cfg(test)]
Mock(MockSystemInfoStorage),
}
#[derive(Debug)]
pub enum StreamStorageKind {
File(FileStreamStorage),
#[cfg(test)]
Mock(MockStreamStorage),
}
#[derive(Debug)]
pub enum TopicStorageKind {
File(FileTopicStorage),
#[cfg(test)]
Mock(MockTopicStorage),
}
#[derive(Debug)]
pub enum PartitionStorageKind {
File(FilePartitionStorage),
#[cfg(test)]
Mock(MockPartitionStorage),
}
#[cfg_attr(test, automock)]
pub trait SystemInfoStorage: Send {
fn load(&self) -> impl Future<Output = Result<SystemInfo, IggyError>> + Send;
fn save(&self, system_info: &SystemInfo) -> impl Future<Output = Result<(), IggyError>> + Send;
}
#[cfg_attr(test, automock)]
pub trait StreamStorage: Send {
fn load(
&self,
stream: &mut Stream,
state: StreamState,
) -> impl Future<Output = Result<(), IggyError>> + Send;
fn save(&self, stream: &Stream) -> impl Future<Output = Result<(), IggyError>> + Send;
fn delete(&self, stream: &Stream) -> impl Future<Output = Result<(), IggyError>> + Send;
}
#[cfg_attr(test, automock)]
pub trait TopicStorage: Send {
fn load(
&self,
topic: &mut Topic,
state: TopicState,
) -> impl Future<Output = Result<(), IggyError>> + Send;
fn save(&self, topic: &Topic) -> impl Future<Output = Result<(), IggyError>> + Send;
fn delete(&self, topic: &Topic) -> impl Future<Output = Result<(), IggyError>> + Send;
}
#[cfg_attr(test, automock)]
pub trait PartitionStorage: Send {
fn load(
&self,
partition: &mut Partition,
state: PartitionState,
) -> impl Future<Output = Result<(), IggyError>> + Send;
fn save(&self, partition: &mut Partition)
-> impl Future<Output = Result<(), IggyError>> + Send;
fn delete(&self, partition: &Partition) -> impl Future<Output = Result<(), IggyError>> + Send;
fn save_consumer_offset(
&self,
offset: u64,
path: &str,
) -> impl Future<Output = Result<(), IggyError>> + Send;
fn load_consumer_offsets(
&self,
kind: ConsumerKind,
path: &str,
) -> impl Future<Output = Result<Vec<ConsumerOffset>, IggyError>> + Send;
fn delete_consumer_offsets(
&self,
path: &str,
) -> impl Future<Output = Result<(), IggyError>> + Send;
fn delete_consumer_offset(
&self,
path: &str,
) -> impl Future<Output = Result<(), IggyError>> + Send;
}
#[derive(Debug)]
pub struct SystemStorage {
pub info: Arc<SystemInfoStorageKind>,
pub stream: Arc<StreamStorageKind>,
pub topic: Arc<TopicStorageKind>,
pub partition: Arc<PartitionStorageKind>,
pub persister: Arc<PersisterKind>,
}
impl SystemStorage {
pub fn new(config: Arc<SystemConfig>, persister: Arc<PersisterKind>) -> Self {
Self {
info: Arc::new(SystemInfoStorageKind::File(FileSystemInfoStorage::new(
config.get_state_info_path(),
persister.clone(),
))),
stream: Arc::new(StreamStorageKind::File(FileStreamStorage)),
topic: Arc::new(TopicStorageKind::File(FileTopicStorage)),
partition: Arc::new(PartitionStorageKind::File(FilePartitionStorage::new(
persister.clone(),
))),
persister,
}
}
}
impl SystemInfoStorageKind {
forward_async_methods! {
async fn load(&self) -> Result<SystemInfo, IggyError>;
async fn save(&self, system_info: &SystemInfo) -> Result<(), IggyError>;
}
}
impl StreamStorageKind {
forward_async_methods! {
async fn load(&self, stream: &mut Stream, state: StreamState) -> Result<(), IggyError>;
async fn save(&self, stream: &Stream) -> Result<(), IggyError>;
async fn delete(&self, stream: &Stream) -> Result<(), IggyError>;
}
}
impl TopicStorageKind {
forward_async_methods! {
async fn load(&self, topic: &mut Topic, state: TopicState) -> Result<(), IggyError>;
async fn save(&self, topic: &Topic) -> Result<(), IggyError>;
async fn delete(&self, topic: &Topic) -> Result<(), IggyError>;
}
}
impl PartitionStorageKind {
forward_async_methods! {
async fn load(&self, partition: &mut Partition, state: PartitionState)
-> Result<(), IggyError>;
async fn save(&self, partition: &mut Partition) -> Result<(), IggyError>;
async fn delete(&self, partition: &Partition) -> Result<(), IggyError>;
async fn save_consumer_offset(&self, offset: u64, path: &str) -> Result<(), IggyError>;
async fn load_consumer_offsets(
&self,
kind: ConsumerKind,
path: &str
) -> Result<Vec<ConsumerOffset>, IggyError>;
async fn delete_consumer_offsets(&self, path: &str) -> Result<(), IggyError>;
async fn delete_consumer_offset(&self, path: &str) -> Result<(), IggyError>;
}
}