Skip to content

Commit 669be06

Browse files
committed
impl admin methods for flushing
1 parent df0bc4d commit 669be06

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

src/admin/methods.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{
2+
db_flush,
23
admin::error::AdminError,
34
database::types::RequestBus,
45
Rpc,
@@ -105,21 +106,16 @@ pub async fn execute_method(
105106
#[allow(unreachable_code)]
106107
async fn admin_blutgang_quit(cache: RequestBus) -> Result<Value, AdminError> {
107108
// We're doing something not-good so flush everything to disk
108-
// let _ = cache.flush_async().await;
109-
// Drop cache so we get the print profile on drop thing before we quit
110-
// We have to get the raw pointer
111-
// TODO: This still doesnt work!
112-
// unsafe {
113-
// ptr::drop_in_place(Arc::into_raw(cache) as *mut Db);
114-
// }
109+
let _ = db_flush!(cache);
110+
115111
std::process::exit(0);
116112
Ok(Value::Null)
117113
}
118114

119115
/// Flushes sled cache to disk
120116
async fn admin_flush_cache(cache: RequestBus) -> Result<Value, AdminError> {
121117
let time = Instant::now();
122-
// let _ = cache.flush_async().await;
118+
let _ = db_flush!(cache);
123119
let time = time.elapsed();
124120

125121
let rx = json!({

src/database/accept.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ pub async fn database_processing(mut rax: mpsc::UnboundedReceiver<DbRequest>, ca
1818
RequestKind::Read(k) => cache.get(k),
1919
RequestKind::Write(k, v) => cache.insert(k, v),
2020
RequestKind::Batch(b) => cache.apply_batch(b).map(|_| None),
21+
RequestKind::Flush() => {
22+
// TODO: return proper stats!
23+
let _ = cache.flush().unwrap();
24+
Ok(None)
25+
},
2126
};
2227

2328
if result.is_err() {
@@ -96,3 +101,30 @@ macro_rules! db_batch {
96101
rx
97102
}};
98103
}
104+
105+
/// Macro for flushing the DB
106+
///
107+
/// Returns `Option<InlineArray>`, where the result is `None` if
108+
/// there was an error or data isn't present, or `Some` if the operation
109+
/// completed successfully.
110+
#[macro_export]
111+
macro_rules! db_flush {
112+
(
113+
$channel:expr
114+
) => {{
115+
use $crate::database::types::{
116+
DbRequest,
117+
RequestKind,
118+
};
119+
120+
use tokio::sync::oneshot;
121+
122+
let (tx, rx) = oneshot::channel();
123+
let req = DbRequest::new(RequestKind::Flush(), tx);
124+
125+
let _ = $channel.send(req);
126+
127+
rx
128+
}};
129+
}
130+

src/database/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub enum RequestKind {
1818
Read(Vec<u8>),
1919
Write(Vec<u8>, InlineArray),
2020
Batch(sled::Batch),
21+
Flush(),
2122
}
2223

2324
/// Contains data to be sent to the DB thread for processing.

0 commit comments

Comments
 (0)