Skip to content

Commit

Permalink
impl admin methods for flushing
Browse files Browse the repository at this point in the history
  • Loading branch information
makemake-kbo committed Jun 28, 2024
1 parent df0bc4d commit 669be06
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/admin/methods.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
db_flush,
admin::error::AdminError,
database::types::RequestBus,
Rpc,
Expand Down Expand Up @@ -105,21 +106,16 @@ pub async fn execute_method(
#[allow(unreachable_code)]
async fn admin_blutgang_quit(cache: RequestBus) -> Result<Value, AdminError> {
// We're doing something not-good so flush everything to disk
// let _ = cache.flush_async().await;
// Drop cache so we get the print profile on drop thing before we quit
// We have to get the raw pointer
// TODO: This still doesnt work!
// unsafe {
// ptr::drop_in_place(Arc::into_raw(cache) as *mut Db);
// }
let _ = db_flush!(cache);

std::process::exit(0);
Ok(Value::Null)
}

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

let rx = json!({
Expand Down
32 changes: 32 additions & 0 deletions src/database/accept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ pub async fn database_processing(mut rax: mpsc::UnboundedReceiver<DbRequest>, ca
RequestKind::Read(k) => cache.get(k),
RequestKind::Write(k, v) => cache.insert(k, v),
RequestKind::Batch(b) => cache.apply_batch(b).map(|_| None),
RequestKind::Flush() => {
// TODO: return proper stats!
let _ = cache.flush().unwrap();
Ok(None)
},
};

if result.is_err() {
Expand Down Expand Up @@ -96,3 +101,30 @@ macro_rules! db_batch {
rx
}};
}

/// Macro for flushing the DB
///
/// Returns `Option<InlineArray>`, where the result is `None` if
/// there was an error or data isn't present, or `Some` if the operation
/// completed successfully.
#[macro_export]
macro_rules! db_flush {
(
$channel:expr
) => {{
use $crate::database::types::{
DbRequest,
RequestKind,
};

use tokio::sync::oneshot;

let (tx, rx) = oneshot::channel();
let req = DbRequest::new(RequestKind::Flush(), tx);

let _ = $channel.send(req);

rx
}};
}

1 change: 1 addition & 0 deletions src/database/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub enum RequestKind {
Read(Vec<u8>),
Write(Vec<u8>, InlineArray),
Batch(sled::Batch),
Flush(),
}

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

0 comments on commit 669be06

Please sign in to comment.