mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-21 18:24:20 +01:00
Merge 'eliminate the need for another Once in Completion' from Pedro Muniz
I added the `Once` before so fix a bug, but it was a bit hackery. We can `get_or_init` to achieve the same purpose, and the code becomes much cleaner. `get_or_init` guarantees the init will happen only once as well. Reviewed-by: Preston Thorpe <preston@turso.tech> Reviewed-by: bit-aloo (@Shourya742) Closes #3578
This commit is contained in:
@@ -3,7 +3,6 @@ use crate::storage::sqlite3_ondisk::WAL_FRAME_HEADER_SIZE;
|
|||||||
use crate::{BufferPool, CompletionError, Result};
|
use crate::{BufferPool, CompletionError, Result};
|
||||||
use bitflags::bitflags;
|
use bitflags::bitflags;
|
||||||
use cfg_block::cfg_block;
|
use cfg_block::cfg_block;
|
||||||
use parking_lot::Once;
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::ptr::NonNull;
|
use std::ptr::NonNull;
|
||||||
@@ -143,8 +142,6 @@ struct CompletionInner {
|
|||||||
// Thread safe with OnceLock
|
// Thread safe with OnceLock
|
||||||
result: std::sync::OnceLock<Option<CompletionError>>,
|
result: std::sync::OnceLock<Option<CompletionError>>,
|
||||||
needs_link: bool,
|
needs_link: bool,
|
||||||
/// before calling callback we check if done is true
|
|
||||||
done: Once,
|
|
||||||
/// Optional parent group this completion belongs to
|
/// Optional parent group this completion belongs to
|
||||||
parent: OnceLock<Arc<GroupCompletionInner>>,
|
parent: OnceLock<Arc<GroupCompletionInner>>,
|
||||||
}
|
}
|
||||||
@@ -298,7 +295,6 @@ impl Completion {
|
|||||||
completion_type,
|
completion_type,
|
||||||
result: OnceLock::new(),
|
result: OnceLock::new(),
|
||||||
needs_link: false,
|
needs_link: false,
|
||||||
done: Once::new(),
|
|
||||||
parent: OnceLock::new(),
|
parent: OnceLock::new(),
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
@@ -310,7 +306,6 @@ impl Completion {
|
|||||||
completion_type,
|
completion_type,
|
||||||
result: OnceLock::new(),
|
result: OnceLock::new(),
|
||||||
needs_link: true,
|
needs_link: true,
|
||||||
done: Once::new(),
|
|
||||||
parent: OnceLock::new(),
|
parent: OnceLock::new(),
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
@@ -420,7 +415,7 @@ impl Completion {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn callback(&self, result: Result<i32, CompletionError>) {
|
fn callback(&self, result: Result<i32, CompletionError>) {
|
||||||
self.inner.done.call_once(|| {
|
self.inner.result.get_or_init(|| {
|
||||||
match &self.inner.completion_type {
|
match &self.inner.completion_type {
|
||||||
CompletionType::Read(r) => r.callback(result),
|
CompletionType::Read(r) => r.callback(result),
|
||||||
CompletionType::Write(w) => w.callback(result),
|
CompletionType::Write(w) => w.callback(result),
|
||||||
@@ -428,10 +423,6 @@ impl Completion {
|
|||||||
CompletionType::Truncate(t) => t.callback(result),
|
CompletionType::Truncate(t) => t.callback(result),
|
||||||
CompletionType::Group(g) => g.callback(result),
|
CompletionType::Group(g) => g.callback(result),
|
||||||
};
|
};
|
||||||
self.inner
|
|
||||||
.result
|
|
||||||
.set(result.err())
|
|
||||||
.expect("result must be set only once");
|
|
||||||
|
|
||||||
if let Some(group) = self.inner.parent.get() {
|
if let Some(group) = self.inner.parent.get() {
|
||||||
// Capture first error in group
|
// Capture first error in group
|
||||||
@@ -447,6 +438,8 @@ impl Completion {
|
|||||||
}
|
}
|
||||||
// TODO: remove self from parent group
|
// TODO: remove self from parent group
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result.err()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -459,15 +452,6 @@ impl Completion {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// only call this method if you are sure that the completion is
|
|
||||||
/// a WriteCompletion, panics otherwise
|
|
||||||
pub fn as_write(&self) -> &WriteCompletion {
|
|
||||||
match self.inner.completion_type {
|
|
||||||
CompletionType::Write(ref w) => w,
|
|
||||||
_ => unreachable!(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Link this completion to a group completion (internal use only)
|
/// Link this completion to a group completion (internal use only)
|
||||||
fn link_internal(&mut self, group: &Completion) {
|
fn link_internal(&mut self, group: &Completion) {
|
||||||
let group_inner = match &group.inner.completion_type {
|
let group_inner = match &group.inner.completion_type {
|
||||||
|
|||||||
Reference in New Issue
Block a user