WIP
This commit is contained in:
@@ -15,11 +15,23 @@ use netpod::DtMs;
|
||||
use netpod::TsNano;
|
||||
use query::api4::events::EventsSubQuery;
|
||||
use std::future::Future;
|
||||
use std::ops::Range;
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
use std::task::Context;
|
||||
use std::task::Poll;
|
||||
|
||||
#[allow(unused)]
|
||||
macro_rules! trace_emit { ($($arg:tt)*) => ( if true { trace!($($arg)*); } ) }
|
||||
|
||||
pub fn off_max() -> u64 {
|
||||
1000
|
||||
}
|
||||
|
||||
pub fn part_len(bin_len: DtMs) -> DtMs {
|
||||
DtMs::from_ms_u64(bin_len.ms() * off_max())
|
||||
}
|
||||
|
||||
pub struct EventsReading {
|
||||
stream: Pin<Box<dyn Stream<Item = Sitemty<ChannelEvents>> + Send>>,
|
||||
}
|
||||
@@ -43,11 +55,19 @@ pub trait EventsReadProvider: Send + Sync {
|
||||
}
|
||||
|
||||
pub struct CacheReading {
|
||||
fut: Pin<Box<dyn Future<Output = Result<BinsDim0<f32>, Box<dyn std::error::Error + Send>>> + Send>>,
|
||||
fut: Pin<Box<dyn Future<Output = Result<BinsDim0<f32>, streams::timebin::cached::reader::Error>> + Send>>,
|
||||
}
|
||||
|
||||
impl CacheReading {
|
||||
pub fn new(
|
||||
fut: Pin<Box<dyn Future<Output = Result<BinsDim0<f32>, streams::timebin::cached::reader::Error>> + Send>>,
|
||||
) -> Self {
|
||||
Self { fut }
|
||||
}
|
||||
}
|
||||
|
||||
impl Future for CacheReading {
|
||||
type Output = Result<BinsDim0<f32>, Box<dyn std::error::Error + Send>>;
|
||||
type Output = Result<BinsDim0<f32>, streams::timebin::cached::reader::Error>;
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||
self.fut.poll_unpin(cx)
|
||||
@@ -73,7 +93,7 @@ impl Future for CacheWriting {
|
||||
}
|
||||
|
||||
pub trait CacheReadProvider: Send + Sync {
|
||||
fn read(&self, series: u64, range: BinnedRange<TsNano>) -> CacheReading;
|
||||
fn read(&self, series: u64, bin_len: DtMs, msp: u64, offs: Range<u32>) -> CacheReading;
|
||||
fn write(&self, series: u64, bins: BinsDim0<f32>) -> CacheWriting;
|
||||
}
|
||||
|
||||
@@ -87,17 +107,28 @@ pub enum Error {
|
||||
}
|
||||
|
||||
pub struct CachedReader {
|
||||
series: u64,
|
||||
range: BinnedRange<TsNano>,
|
||||
ts1next: TsNano,
|
||||
bin_len: DtMs,
|
||||
cache_read_provider: Arc<dyn CacheReadProvider>,
|
||||
reading: Option<Pin<Box<dyn Future<Output = Result<BinsDim0<f32>, Error>> + Send>>>,
|
||||
}
|
||||
|
||||
impl CachedReader {
|
||||
pub fn new(
|
||||
series: u64,
|
||||
bin_len: DtMs,
|
||||
range: BinnedRange<TsNano>,
|
||||
cache_read_provider: Arc<dyn CacheReadProvider>,
|
||||
) -> Result<Self, Error> {
|
||||
let ret = Self { cache_read_provider };
|
||||
let ret = Self {
|
||||
series,
|
||||
ts1next: range.nano_beg(),
|
||||
bin_len: range.bin_len.to_dt_ms(),
|
||||
range,
|
||||
cache_read_provider,
|
||||
reading: None,
|
||||
};
|
||||
Ok(ret)
|
||||
}
|
||||
}
|
||||
@@ -113,8 +144,42 @@ impl Stream for CachedReader {
|
||||
// Change the worker interface:
|
||||
// We should already compute here the msp and off because we must here implement the loop logic.
|
||||
// Therefore worker interface should not accept BinnedRange, but msp and off range.
|
||||
error!("TODO CachedReader impl split reads over known ranges");
|
||||
// Ready(Some(Err(Error::TodoImpl)))
|
||||
Ready(None)
|
||||
loop {
|
||||
break if let Some(fut) = self.reading.as_mut() {
|
||||
match fut.poll_unpin(cx) {
|
||||
Ready(x) => {
|
||||
self.reading = None;
|
||||
match x {
|
||||
Ok(bins) => {
|
||||
use items_0::WithLen;
|
||||
trace_emit!(
|
||||
"- - - - - - - - - - - - emit cached bins {} bin_len {}",
|
||||
bins.len(),
|
||||
self.bin_len
|
||||
);
|
||||
Ready(Some(Ok(bins)))
|
||||
}
|
||||
Err(e) => Ready(Some(Err(e))),
|
||||
}
|
||||
}
|
||||
Pending => Pending,
|
||||
}
|
||||
} else {
|
||||
if self.ts1next < self.range.nano_end() {
|
||||
let div = part_len(self.bin_len).ns();
|
||||
let msp = self.ts1next.ns() / div;
|
||||
let off = (self.ts1next.ns() - div * msp) / self.bin_len.ns();
|
||||
let off2 = (self.range.nano_end().ns() - div * msp) / self.bin_len.ns();
|
||||
let off2 = off2.min(off_max());
|
||||
self.ts1next = TsNano::from_ns(self.bin_len.ns() * off2 + div * msp);
|
||||
let offs = off as u32..off2 as u32;
|
||||
let fut = self.cache_read_provider.read(self.series, self.bin_len, msp, offs);
|
||||
self.reading = Some(Box::pin(fut));
|
||||
continue;
|
||||
} else {
|
||||
Ready(None)
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user