coven_sync/
file_storage.rs1use crate::{
5 files::{Files, FilesInner, FilesMode},
6 FileReadError,
7};
8use coven_database::DbError;
9use coven_storage::Storage;
10use std::sync::Arc;
11use tokio::sync::watch;
12
13#[derive(Clone)]
17pub(crate) enum StorageState {
18 Connected(Arc<dyn Storage>),
20 Connecting,
23 Released,
26 Closed,
28}
29
30pub(super) struct StorageSlot {
34 storage: Option<Arc<dyn Storage>>,
35 attempting: bool,
37 closed: bool,
38 connection: u64,
40}
41
42impl StorageSlot {
43 pub(super) fn opened(storage: Option<Arc<dyn Storage>>) -> Self {
46 Self { storage, attempting: false, closed: false, connection: 0 }
47 }
48
49 pub(super) fn state(&self) -> StorageState {
50 if self.closed {
51 StorageState::Closed
52 } else if let Some(storage) = &self.storage {
53 StorageState::Connected(storage.clone())
54 } else if self.attempting {
55 StorageState::Connecting
56 } else {
57 StorageState::Released
58 }
59 }
60
61 pub(super) fn connection(&self) -> u64 {
62 self.connection
63 }
64
65 pub(super) fn connect(&mut self, storage: Option<Arc<dyn Storage>>) {
67 self.storage = storage;
68 self.connection += 1;
69 }
70
71 pub(super) fn close(&mut self) {
72 self.storage = None;
73 self.closed = true;
74 }
75
76 pub(super) fn closing(&mut self) {
79 self.closed = true;
80 }
81}
82
83impl<M: FilesMode> FilesInner<M> {
84 pub(crate) fn storage_state(&self) -> StorageState {
86 self.storage.borrow().state()
87 }
88
89 pub(crate) async fn settled(&self) -> Result<StorageState, FileReadError> {
94 let mut changes = self.storage.subscribe();
95 loop {
96 let closing = self.hold_wake.notified();
99 tokio::pin!(closing);
100 closing.as_mut().enable();
101 self.check_open()?;
102 let state = changes.borrow_and_update().state();
103 if !matches!(state, StorageState::Connecting) {
104 return Ok(state);
105 }
106 #[cfg(any(test, feature = "test-utils"))]
107 let _waiting = crate::files::Waiting::new(&self.reads_awaiting_connection);
108 tokio::select! {
109 () = closing => {}
110 changed = changes.changed() => {
111 if changed.is_err() {
112 return Err(DbError::StoreClosed.into());
113 }
114 }
115 }
116 }
117 }
118}
119
120pub(super) fn connected(state: StorageState) -> Result<Arc<dyn Storage>, FileReadError> {
123 match state {
124 StorageState::Connected(storage) => Ok(storage),
125 StorageState::Connecting | StorageState::Released => Err(FileReadError::NoStorage),
126 StorageState::Closed => Err(DbError::StoreClosed.into()),
127 }
128}
129
130impl<M: FilesMode> Files<M> {
131 #[cfg(any(test, feature = "test-utils"))]
134 pub fn reads_awaiting_connection(&self) -> watch::Receiver<usize> {
135 self.inner.reads_awaiting_connection.subscribe()
136 }
137}
138
139impl Files {
140 pub fn expect_connection(&self) -> ConnectionComing {
145 self.inner.storage.send_modify(|slot| slot.attempting = true);
146 ConnectionComing { storage: self.inner.storage.clone() }
147 }
148}
149
150impl Files {
151 pub fn closing(&self) {
155 self.inner.storage.send_modify(|slot| slot.closing());
156 }
157}
158
159#[must_use = "the connection is expected only while this lives"]
162pub struct ConnectionComing {
163 storage: watch::Sender<StorageSlot>,
164}
165
166impl Drop for ConnectionComing {
167 fn drop(&mut self) {
168 self.storage.send_if_modified(|slot| std::mem::take(&mut slot.attempting));
169 }
170}