Struct pravega_client::index::reader::IndexReader
source · pub struct IndexReader { /* private fields */ }
Expand description
Index Reader reads the Index Record from Stream.
The Stream has to be fixed size single segment stream like byte stream.
§Examples
use pravega_client_config::ClientConfigBuilder;
use pravega_client::client_factory::ClientFactory;
use pravega_client_shared::ScopedStream;
use futures_util::pin_mut;
use futures_util::StreamExt;
use std::io::Write;
use tokio;
// Suppose the existing Fields in the stream is like below.
// #[derive(Fields, Debug, PartialOrd, PartialEq)]
// struct MyFields {
// id: u64,
// timestamp: u64,
// }
#[tokio::main]
async fn main() {
// assuming Pravega controller is running at endpoint `localhost:9090`
let config = ClientConfigBuilder::default()
.controller_uri("localhost:9090")
.build()
.expect("creating config");
let client_factory = ClientFactory::new(config);
// assuming scope:myscope, stream:mystream exist.
let stream = ScopedStream::from("myscope/mystream");
let mut index_reader = client_factory.create_index_reader(stream).await;
// search data
let offset = index_reader.search_offset(("id", 10)).await.expect("get offset");
// read data
let s = index_reader.read(offset, u64::MAX).expect("get read slice");
pin_mut!(s);
while let Some(res) = s.next().await {
// do something with the read result
res.expect("read next event");
}
}
Implementations§
source§impl IndexReader
impl IndexReader
sourcepub async fn search_offset(
&self,
field: (&'static str, u64)
) -> Result<u64, IndexReaderError>
pub async fn search_offset( &self, field: (&'static str, u64) ) -> Result<u64, IndexReaderError>
Given an Field (name, v), find the offset of the first record that contains the given Field that has value >= v.
Note that if there are multiple entries that have the same Field name and value, this method will find and return the first one. If the value of searching field is smaller than the first readable Record’s field in the stream, the first record data will be returned. If the value of searching field is larger than the latest Record, a FieldNotFound error will be returned.
sourcepub fn read<'stream, 'reader: 'stream>(
&'reader self,
start_offset: u64,
end_offset: u64
) -> Result<impl Stream<Item = Result<Vec<u8>, IndexReaderError>> + 'stream, IndexReaderError>
pub fn read<'stream, 'reader: 'stream>( &'reader self, start_offset: u64, end_offset: u64 ) -> Result<impl Stream<Item = Result<Vec<u8>, IndexReaderError>> + 'stream, IndexReaderError>
Reads records starting from the given offset.
This method returns a slice of stream that implements an iterator. Application can iterate on
this slice to get the data. When next()
is invoked on the iterator, a read request
will be issued by the underlying reader.
If we want to do tail read instead of reading just a slice of the data, we can set end_offset to be u64::MAX.
sourcepub async fn first_record_data(&self) -> Result<Vec<u8>, IndexReaderError>
pub async fn first_record_data(&self) -> Result<Vec<u8>, IndexReaderError>
Data in the first readable record.
sourcepub async fn last_record_data(&self) -> Result<Vec<u8>, IndexReaderError>
pub async fn last_record_data(&self) -> Result<Vec<u8>, IndexReaderError>
Data in the last record.
sourcepub async fn head_offset(&self) -> Result<u64, IndexReaderError>
pub async fn head_offset(&self) -> Result<u64, IndexReaderError>
Get the readable head offset.
sourcepub async fn tail_offset(&self) -> Result<u64, IndexReaderError>
pub async fn tail_offset(&self) -> Result<u64, IndexReaderError>
Get the tail offset.
Auto Trait Implementations§
impl !Freeze for IndexReader
impl !RefUnwindSafe for IndexReader
impl Send for IndexReader
impl Sync for IndexReader
impl Unpin for IndexReader
impl !UnwindSafe for IndexReader
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request