org.openstreetmap.osmosis.core.store
Class SegmentedObjectStore<T extends Storeable>

java.lang.Object
  extended by org.openstreetmap.osmosis.core.store.SegmentedObjectStore<T>
Type Parameters:
T - The object type to be stored.
All Implemented Interfaces:
Completable, Releasable

public class SegmentedObjectStore<T extends Storeable>
extends java.lang.Object
implements Completable

Provides a store for writing objects to a file for later retrieval. The number of objects is limited only by disk space.

This class supports chunking where the stream is broken into segments. This is achieved by calling the closeChunk method between add calls.

This store is only suitable for single-threaded use because it does not provide per-thread readers.

Author:
Brett Henderson

Constructor Summary
SegmentedObjectStore(ObjectSerializationFactory serializationFactory, java.lang.String storageFilePrefix, boolean useCompression)
          Creates a new instance.
 
Method Summary
 void add(T data)
          Adds the specified object to the store.
 long closeChunk()
          Closes the current object stream and creates a new one.
 void complete()
          Ensures that all information is fully persisted.
 ReleasableIterator<T> iterate()
          Returns an iterator for reading objects from the underlying data store.
 ReleasableIterator<T> iterate(long streamOffset, long maxObjectCount)
          Returns an iterator for reading objects from the underlying data store.
 void release()
          Performs resource cleanup tasks such as closing files, or database connections.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SegmentedObjectStore

public SegmentedObjectStore(ObjectSerializationFactory serializationFactory,
                            java.lang.String storageFilePrefix,
                            boolean useCompression)
Creates a new instance.

Parameters:
serializationFactory - The factory defining the object serialisation implementation.
storageFilePrefix - The prefix of the storage file.
useCompression - If true, the storage file will be compressed.
Method Detail

add

public void add(T data)
Adds the specified object to the store.

Parameters:
data - The object to be added.

closeChunk

public long closeChunk()
Closes the current object stream and creates a new one. This allows read operations to begin at offsets within the file. This can only be called while adding to the store, not once reads are begun. Read operations must begin at offsets created by this method.

Returns:
The start position of the new chunk within the file.

iterate

public ReleasableIterator<T> iterate()
Returns an iterator for reading objects from the underlying data store.

Returns:
An iterator for reading objects from the data store. This iterator must be released after use.

iterate

public ReleasableIterator<T> iterate(long streamOffset,
                                     long maxObjectCount)
Returns an iterator for reading objects from the underlying data store.

Parameters:
streamOffset - The location in the underlying stream to begin reading.
maxObjectCount - The maximum number of objects to be returned, -1 for unlimited.
Returns:
An iterator for reading objects from the data store. This iterator must be released after use.

complete

public void complete()
Ensures that all information is fully persisted. This includes database commits, file buffer flushes, etc. Implementations must call complete on any nested Completable objects. Where the releasable method of a Releasable class should be called within a finally block, this method should typically be the final statement within the try block.

Specified by:
complete in interface Completable

release

public void release()
Performs resource cleanup tasks such as closing files, or database connections. This must be called after all processing is complete and may be called multiple times. Implementations must call release on any nested Releasable objects. It should be called within a finally block to ensure it is called in exception scenarios.

Specified by:
release in interface Releasable