Skip to main content

ItemReader

Trait ItemReader 

Source
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§

Source

fn read(&self) -> ItemReaderResult<I>

Reads an item from the reader.

§Returns
  • Ok(Some(item)) when an item is successfully read
  • Ok(None) when there are no more items to read (end of data)
  • Err(BatchError) when an error occurs during reading

Implementors§

Source§

impl ItemReader<Person> for PersonReader

Available on crate feature fake only.
Source§

impl<I> ItemReader<<I as EntityTrait>::Model> for OrmItemReader<'_, I>

Available on crate feature 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:

  1. First read: Loads the first page of data
  2. Subsequent reads: Returns items from the buffer
  3. Page boundary: When the buffer is exhausted, loads the next page
  4. End of data: Returns None when no more data is available

§State Management

  • offset: Tracks the absolute position across all pages
  • current_page: Tracks which page we’re currently reading from
  • buffer: Holds the current page’s data

The reader maintains these invariants:

  • offset always represents the next item to be read
  • current_page represents the page currently loaded in the buffer
  • The buffer contains items for the current page only
Source§

impl<I> ItemReader<I> for MySqlRdbcItemReader<'_, I>
where for<'r> I: FromRow<'r, MySqlRow> + Send + Unpin + Clone,

Available on crate feature rdbc only.
Source§

impl<I> ItemReader<I> for PostgresRdbcItemReader<'_, I>
where for<'r> I: FromRow<'r, PgRow> + Send + Unpin + Clone,

Available on crate feature 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.

Source§

impl<I> ItemReader<I> for SqliteRdbcItemReader<'_, I>
where for<'r> I: FromRow<'r, SqliteRow> + Send + Unpin + Clone,

Available on crate feature rdbc only.
Source§

impl<I: DeserializeOwned + Clone + WithObjectId + Send + Sync> ItemReader<I> for MongodbItemReader<'_, I>

Available on crate feature mongodb only.
Source§

impl<I: DeserializeOwned, R: Read> ItemReader<I> for CsvItemReader<R>

Available on crate feature csv only.
Source§

impl<I: DeserializeOwned, R: Read> ItemReader<I> for JsonItemReader<I, R>

Available on crate feature json only.
Source§

impl<R: Read, I: DeserializeOwned> ItemReader<I> for XmlItemReader<R, I>

Available on crate feature xml only.