org.gcube.contentmanagement.baselayer.rdbmsImpl
Class SubstringStream

java.lang.Object
  extended by org.gcube.contentmanagement.baselayer.BaseLayerStream
      extended by org.gcube.contentmanagement.baselayer.rdbmsImpl.SubstringStream

public class SubstringStream
extends BaseLayerStream

Stream for reading BLOBs from a database connection in several chunks using the SQL SUBSTRING method. This has been introduced to handle the inconvenient situation that some database drivers, e.g. MySQL Connector/J send the entire BLOB when it is read as a stream to the client and the client has to keep it in main memory, which frequently results in an OutOfMemoryError. This class helps by reading only smaller parts of huge BLOBs into a buffer, which is refilled as needed by contacting the database again. This may increases the network load signifanctly, slow down the overall time, and requires to keep a read-only transaction open for a longer time. In order to tweak performance, the size of the buffer that is used should be adjusted to the memory available in the system and the actual read requests. It would be much more elegant, if the database driver itself provided such kind of handling instead of expected the client to have sufficient main memory.

Author:
michaelspringmann

Field Summary
static int SUGGESTED_MINIMAL_BUFFER_SIZE
          Since reading chunks from the database has considerable overhead, each chunk should use a reasonable size.
 
Fields inherited from class org.gcube.contentmanagement.baselayer.BaseLayerStream
END_OF_STREAM, NO_LIMIT, STREAM_LENGTH_IMPLICITE, URL_CONNECTION_LENGTH_UNKNOWN
 
Constructor Summary
SubstringStream(java.sql.Connection c, java.lang.String contentID, BasicStorageHints hints)
          Opens a substring stream for reading on a given connection
 
Method Summary
 
Methods inherited from class org.gcube.contentmanagement.baselayer.BaseLayerStream
dispose, getBufferedStream, getBufferSize, getDefaultBufferSize, getLength, getLimit, getStream, handleStartOffset, hasImpliciteLength, isBuffered, needsLimitTreatment, setBuffered, setBufferSize, setDefaultBufferSize, setLimit, setLimit, toFixedLengthStream, writeToStreamRespectingLimit
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SUGGESTED_MINIMAL_BUFFER_SIZE

public static final int SUGGESTED_MINIMAL_BUFFER_SIZE
Since reading chunks from the database has considerable overhead, each chunk should use a reasonable size. Default is to read at least one megabyte each time (if the remaining part of the blob is that big).

See Also:
Constant Field Values
Constructor Detail

SubstringStream

public SubstringStream(java.sql.Connection c,
                       java.lang.String contentID,
                       BasicStorageHints hints)
                throws BaseLayerException
Opens a substring stream for reading on a given connection

Parameters:
c - the connection to be used
contentID - the raw content to be retrieved
hints - the hints to be used
Throws:
BaseLayerException - if something goes wrong