pub trait ItemReader<I> {
// Required method
fn read(&self) -> ItemReaderResult<I>;
}Expand description
A trait for reading items.
This trait defines the contract for components that read items from a data source. It is one of the fundamental building blocks of the batch processing pipeline.
§Design Pattern
This follows the Strategy Pattern, allowing different reading strategies to be interchangeable while maintaining a consistent interface.
§Implementation Note
Implementors of this trait should:
- Return
Ok(Some(item))when an item is successfully read - Return
Ok(None)when there are no more items to read (end of data) - Return
Err(BatchError)when an error occurs during reading
§Example
use spring_batch_rs::core::item::{ItemReader, ItemReaderResult};
use spring_batch_rs::error::BatchError;
struct StringReader {
items: Vec<String>,
position: usize,
}
impl ItemReader<String> for StringReader {
fn read(&mut self) -> ItemReaderResult<String> {
if self.position < self.items.len() {
let item = self.items[self.position].clone();
self.position += 1;
Ok(Some(item))
} else {
Ok(None) // End of data
}
}
}Required Methods§
Sourcefn read(&self) -> ItemReaderResult<I>
fn read(&self) -> ItemReaderResult<I>
Reads an item from the reader.
§Returns
Ok(Some(item))when an item is successfully readOk(None)when there are no more items to read (end of data)Err(BatchError)when an error occurs during reading
Implementors§
impl ItemReader<Person> for PersonReader
fake only.impl<I> ItemReader<<I as EntityTrait>::Model> for OrmItemReader<'_, I>
orm only.Implementation of ItemReader trait for OrmItemReader.
This implementation provides a way to read items from a database using SeaORM with support for pagination. It uses an internal buffer to store the results of database queries and keeps track of the current offset to determine when a new page of data needs to be fetched.
§Reading Strategy
The reader implements a lazy-loading strategy:
- First read: Loads the first page of data
- Subsequent reads: Returns items from the buffer
- Page boundary: When the buffer is exhausted, loads the next page
- End of data: Returns None when no more data is available
§State Management
offset: Tracks the absolute position across all pagescurrent_page: Tracks which page we’re currently reading frombuffer: Holds the current page’s data
The reader maintains these invariants:
offsetalways represents the next item to be readcurrent_pagerepresents the page currently loaded in the buffer- The buffer contains items for the current page only
impl<I> ItemReader<I> for MySqlRdbcItemReader<'_, I>
rdbc only.impl<I> ItemReader<I> for PostgresRdbcItemReader<'_, I>
rdbc only.Implementation of ItemReader trait for PostgresRdbcItemReader.
This implementation provides a way to read items from a PostgreSQL database with support for pagination. It uses an internal buffer to store the results of database queries and keeps track of the current offset to determine when a new page of data needs to be fetched.
The implementation handles both paginated and non-paginated reading modes transparently, making it suitable for various batch processing scenarios.
impl<I> ItemReader<I> for SqliteRdbcItemReader<'_, I>
rdbc only.impl<I: DeserializeOwned + Clone + WithObjectId + Send + Sync> ItemReader<I> for MongodbItemReader<'_, I>
mongodb only.impl<I: DeserializeOwned, R: Read> ItemReader<I> for CsvItemReader<R>
csv only.impl<I: DeserializeOwned, R: Read> ItemReader<I> for JsonItemReader<I, R>
json only.impl<R: Read, I: DeserializeOwned> ItemReader<I> for XmlItemReader<R, I>
xml only.