ERXFetchSpecificationBatchIterator Class Reference

Inherits java::util::Iterator, and java::util::Enumeration.

Collaboration diagram for ERXFetchSpecificationBatchIterator:

Collaboration graph
[legend]

List of all members.

Public Member Functions

int batchCount ()
int batchSize ()
NSArray batchWithIndex (int index)
NSArray batchWithRange (NSRange requestedRange)
int count ()
int currentBatchIndex ()
int currentObjectFetchCount ()
EOEditingContext editingContext ()
 ERXFetchSpecificationBatchIterator (EOFetchSpecification fetchSpecification, NSArray pkeys, EOEditingContext ec, int batchSize)
 ERXFetchSpecificationBatchIterator (EOFetchSpecification fetchSpecification, EOEditingContext ec, int batchSize)
 ERXFetchSpecificationBatchIterator (EOFetchSpecification fetchSpecification, EOEditingContext ec)
 ERXFetchSpecificationBatchIterator (EOFetchSpecification fetchSpecification)
boolean filtersBatches ()
boolean hasMoreElements ()
boolean hasNext ()
boolean hasNextBatch ()
Object next ()
NSArray nextBatch ()
Object nextElement ()
void remove ()
void reset ()
void setBatchSize (int batchSize)
void setEditingContext (EOEditingContext ec)
void setFiltersBatches (boolean newValue)

Static Public Attributes

static final int DefaultBatchSize = 250
static final Logger log = Logger.getLogger(ERXFetchSpecificationBatchIterator.class)

Protected Member Functions

NSArray _fetchNextBatch ()
boolean _hasMoreToFetch ()
EOFetchSpecification batchFetchSpecificationForQualifier (EOQualifier qualifier)
NSArray primaryKeys ()

Protected Attributes

int batchSize
NSMutableArray cachedBatch
int currentObjectFetchCount
EOEditingContext editingContext
EOFetchSpecification fetchSpecification
String primaryKeyAttributeName
NSArray primaryKeys
boolean shouldFilterBatches

Private Member Functions

NSRange _rangeForBatchIndex (int index)
NSRange _rangeForOffset (int start)


Detailed Description

The goal of the fetch specification batch iterator is to have the ability to iterate through a fetch specification that might fetch one million enterprise objects. Fetching all of the objects into a single editing context is prohibitive in the amount of memory needed and in the time taken to process all of the rows.
The iterator allows one to iterate through the fetched objects only hydrating those objects need in small bite size pieces. The iterator also allows you to swap out editing contexts between calls to nextBatch(), which will allow the garbage collector to collect the old editing context and the previous batch of enterprise objects.
For your convenience, this class also implements Iterator and Enumeration, so you can use it as such.
Be aware that the batch size is primarily intended to govern the number of objects requested from the database at once, and may differ from the number of objects returned by nextBatch(), for instance if the batch size is changed after fetching, or if filtersBatches() is set to true.

Constructor & Destructor Documentation

ERXFetchSpecificationBatchIterator ( EOFetchSpecification  fetchSpecification  ) 

Constructs a fetch specification iterator for a given fetch specification with the default batch size. Note you will have to set an editingContext on the iterator before calling the nextBatch method.

Parameters:
fetchSpecification to iterate through

ERXFetchSpecificationBatchIterator ( EOFetchSpecification  fetchSpecification,
EOEditingContext  ec 
)

Constructs a fetch specification iterator for a given fetch specification with the default batch size. All objects will be fetched from the given editing context. Note that you can switch out different editing contexts between calls to nextBatch

Parameters:
fetchSpecification to iterate through
ec editing context to fetch against

ERXFetchSpecificationBatchIterator ( EOFetchSpecification  fetchSpecification,
EOEditingContext  ec,
int  batchSize 
)

Constructs a fetch specification iterator for a given fetch specification and a batch size. All objects will be fetched from the given editing context. Note that you can switch out different editing contexts between calls to nextBatch

Parameters:
fetchSpecification to iterate through
ec editing context to fetch against
batchSize number of objects to fetch in a given batch

ERXFetchSpecificationBatchIterator ( EOFetchSpecification  fetchSpecification,
NSArray  pkeys,
EOEditingContext  ec,
int  batchSize 
)

Constructs a fetch specification iterator for a fetch specification, an optional set of pre-fetched primary keys and a batch size. All objects will be fetched from the given editing context. Note that you can switch out different editing contexts between calls to nextBatch.

Note: if no ec is supplied a new one is initialized.

Parameters:
fetchSpecification to iterate through
pkeys primary keys to iterate through
ec editing context to fetch against
batchSize number of objects to fetch in a given batch


Member Function Documentation

NSArray _fetchNextBatch (  )  [protected]

Fetches the next batch unconditionally. Subclasses can override this rather than nextBatch(), to get automatic support for the Iterator and Enumeration interfaces.

Returns:
next batch

boolean _hasMoreToFetch (  )  [protected]

NSRange _rangeForBatchIndex ( int  index  )  [private]

NSRange _rangeForOffset ( int  start  )  [private]

int batchCount (  ) 

Gets the number of batches for a given iterator.

Returns:
number of objects / batch size rounded up

EOFetchSpecification batchFetchSpecificationForQualifier ( EOQualifier  qualifier  )  [protected]

int batchSize (  ) 

Gets the batch size.

Returns:
number of enterprise objects to fetch a batch.

NSArray batchWithIndex ( int  index  ) 

Returns the batch corresponding to the given index, that is, the batch beginning at batchSize() * index. Note that if the batch size has been changed after fetching, the batches return by nextBatch() may not line up with the batches returned by this method.

Calling this method does not affect the position of the iterator.

Parameters:
index index of batch to retrieve
Returns:
batch of enterprise objects

NSArray batchWithRange ( NSRange  requestedRange  ) 

Returns the batch corresponding to the given range.

If the supplied range does not fall within the available range, the results returned correspond to the intersection of the two.

If no items are found, the supplied range does not intersect the available range, or the supplied range has length zero, then an empty array is returned.

Calling this method does not affect the position of the iterator.

Parameters:
requestedRange range of batch to retrieve
Returns:
batch of enterprise objects

int count (  ) 

Gets the number of objects.

Returns:
number of objects

int currentBatchIndex (  ) 

Gets the current batch index.

Returns:
number of batches fetched thus far

Gets the current number of objects fetched thus far.

Returns:
current number of objects fetched.

EOEditingContext editingContext (  ) 

Gets the currently set editing context.

Returns:
editing context used to fetch against

boolean filtersBatches (  ) 

If true, each batch will be filtered based on the original qualifier.

See also:
setFiltersBatches
Returns:
whether batches will be re-filtered

boolean hasMoreElements (  ) 

Implementation of the Enumeration interface

boolean hasNext (  ) 

Implementation of the Iterator interface

boolean hasNextBatch (  ) 

Determines if the iterator has another batch.

Returns:
if ok to call nextBatch()

Object next (  ) 

Implementation of the Iterator interface

NSArray nextBatch (  ) 

Gets the next batch of enterprise objects for the given fetch specification. Note that the editing context that is set will be used to fetch against. You can swap out a different editing context before calling this method to reduce memory consumption. (However, if you are mixing calls to this method with calls to next() or nextElement(), this method may return a partial batch of already-cached objects, in the editing context which was in place at the time they were fetched.)

Returns:
batch of enterprise objects

Object nextElement (  ) 

Implementation of the Enumeration interface

NSArray primaryKeys (  )  [protected]

Method used to fetch the primary keys of the objects for the given fetch specification. Note the sort orderings for the fetch specification are respected.

Returns:
array of primary keys to iterate over

void remove (  ) 

Implementation of the Iterator interface

void reset (  ) 

Resets the batch iterator so it will refetch its primary keys again.

void setBatchSize ( int  batchSize  ) 

Sets the batch size.

Parameters:
batchSize to be set.

void setEditingContext ( EOEditingContext  ec  ) 

Sets the editing context used to fetch objects against. It is perfectly fine to change editing contexts between fetching the next batch.

Parameters:
ec editing context used to fetch against

void setFiltersBatches ( boolean  newValue  ) 

If set to true, each batch fetched will be filtered based on the qualifier attached to the original fetch specification. The is useful to cover the case in which the objects may have changed in important ways between the time their primary keys were retrieved and the time they were fetched. Note that when filtering is on, empty arrays may be returned from nextBatch(), and null may be returned from next() and nextElement().

Note that not all qualifiers can be applied in-memory, so this should not bet set to true if such a qualifier is being used.

Defaults to false.

Parameters:
newValue whether batches should be re-filtered


Member Data Documentation

int batchSize [protected]

holds the selected batch size

holds array of fetched but not-yet-returned objects; used by the Iterator and Enumeration interfaces

int currentObjectFetchCount [protected]

holds the number of objects fetched

final int DefaultBatchSize = 250 [static]

holds the default batch size, any bigger than this an Oracle has a fit

EOEditingContext editingContext [protected]

holds a reference to the selected editing context

EOFetchSpecification fetchSpecification [protected]

holds a reference to the fetch spec to iterate over

final Logger log = Logger.getLogger(ERXFetchSpecificationBatchIterator.class) [static]

logging support

holds the name of the primary key attribute corresponding to the entity being iterated over

NSArray primaryKeys [protected]

holds an array of primary key values to iterate through

boolean shouldFilterBatches [protected]

determines whether we should re-apply the original qualifier to each batch of objects fetched


The documentation for this class was generated from the following file:

Generated on Sat May 26 06:43:10 2012 for Project Wonder by  doxygen 1.5.8