use anyhow::Result;
use futures::future::{Either, Ready, ready};
use serde::{Deserialize, Serialize};
use std::{
pin::Pin,
task::{Context, Poll},
};
pub use crate::worker::{ImportMetadataResponseAwaiter, SerializedResponseAwaiter};
pub use crate::{BlockId, SequenceHash};
pub use kvbm_common::LogicalLayoutHandle;
pub use kvbm_physical::manager::{LayoutHandle, SerializedLayout};
pub struct SerializedLayoutResponse {
awaiter: Either<Ready<Result<SerializedLayout>>, SerializedResponseAwaiter>,
}
impl SerializedLayoutResponse {
pub fn ready(layout: SerializedLayout) -> Self {
Self {
awaiter: Either::Left(ready(Ok(layout))),
}
}
pub fn from_boxed(awaiter: SerializedResponseAwaiter) -> Self {
Self {
awaiter: Either::Right(awaiter),
}
}
pub fn could_yield(&self) -> bool {
matches!(self.awaiter, Either::Right(_))
}
}
impl std::future::IntoFuture for SerializedLayoutResponse {
type Output = Result<SerializedLayout>;
type IntoFuture = Either<Ready<Result<SerializedLayout>>, SerializedResponseAwaiter>;
fn into_future(self) -> Self::IntoFuture {
self.awaiter
}
}
pub struct ImportMetadataResponse {
awaiter: Either<Ready<Result<Vec<LayoutHandle>>>, ImportMetadataResponseAwaiter>,
}
impl ImportMetadataResponse {
pub fn ready(handles: Vec<LayoutHandle>) -> Self {
Self {
awaiter: Either::Left(ready(Ok(handles))),
}
}
pub fn from_boxed(awaiter: ImportMetadataResponseAwaiter) -> Self {
Self {
awaiter: Either::Right(awaiter),
}
}
pub fn could_yield(&self) -> bool {
matches!(self.awaiter, Either::Right(_))
}
}
impl std::future::IntoFuture for ImportMetadataResponse {
type Output = Result<Vec<LayoutHandle>>;
type IntoFuture = Either<Ready<Result<Vec<LayoutHandle>>>, ImportMetadataResponseAwaiter>;
fn into_future(self) -> Self::IntoFuture {
self.awaiter
}
}
pub struct ConnectRemoteResponse {
awaiter: ConnectRemoteAwaiter,
}
pub enum ConnectRemoteAwaiter {
Ready(Ready<Result<()>>),
Event(::velo::EventAwaiter),
}
impl std::future::Future for ConnectRemoteAwaiter {
type Output = Result<()>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.get_mut() {
Self::Ready(ready) => Pin::new(ready).poll(cx),
Self::Event(waiter) => Pin::new(waiter).poll(cx),
}
}
}
impl ConnectRemoteResponse {
pub fn ready() -> Self {
Self {
awaiter: ConnectRemoteAwaiter::Ready(ready(Ok(()))),
}
}
pub fn from_awaiter(awaiter: ::velo::EventAwaiter) -> Self {
Self {
awaiter: ConnectRemoteAwaiter::Event(awaiter),
}
}
pub fn could_yield(&self) -> bool {
matches!(self.awaiter, ConnectRemoteAwaiter::Event(_))
}
}
impl std::future::IntoFuture for ConnectRemoteResponse {
type Output = Result<()>;
type IntoFuture = ConnectRemoteAwaiter;
fn into_future(self) -> Self::IntoFuture {
self.awaiter
}
}
#[derive(Serialize, Deserialize, Clone)]
pub enum RemoteDescriptor {
Layout {
handle: LayoutHandle,
block_ids: Vec<BlockId>,
},
Object {
keys: Vec<SequenceHash>,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LeaderLayoutConfig {
pub rank: usize,
pub host_block_count: usize,
pub disk_block_count: Option<usize>,
#[serde(default)]
pub object: Option<kvbm_config::ObjectConfig>,
#[serde(default)]
pub parallelism: kvbm_config::ParallelismMode,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkerLayoutResponse {
pub metadata: SerializedLayout,
pub created_layouts: Vec<LogicalLayoutHandle>,
}