Top Description Inners Fields Constructors Methods
org.apache.derby.impl.jdbc

pack-priv final Class TemporaryClob

extends Object
implements InternalClob
Class Inheritance
All Implemented Interfaces
org.apache.derby.impl.jdbc.InternalClob
Imports
java.io.BufferedInputStream, .EOFException, .FilterReader, .IOException, .InputStream, .Reader, .Writer, java.sql.SQLException, org.apache.derby.shared.common.error.StandardException, org.apache.derby.iapi.jdbc.CharacterStreamDescriptor, org.apache.derby.iapi.types.TypeId, org.apache.derby.iapi.util.UTF8Util

A Clob representation where the Clob is stored either in memory or on disk.

Character positions given as input to methods in this class are always 1-based. Byte positions are always 0-based.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
TemporaryClob.CharToBytePositionCache

A simple class to hold the byte position for a character position.

Field Summary

Modifier and TypeField and Description
private final LOBStreamControl
bytes

Underlying structure holding this Clobs raw bytes.

private long
cachedCharLength

Cached character length of the Clob.

private ConnectionChild
conChild

Connection child assoicated with this Clob.

private UTF8Reader
internalReader

Shared internal reader, closed when the Clob is released.

private final TemporaryClob.CharToBytePositionCache
posCache

Simple one-entry cache for character-byte position.

private boolean
released

Tells whether this Clob has been released or not.

private FilterReader
unclosableInternalReader

The internal reader wrapped so that it cannot be closed.

Constructor Summary

AccessConstructor and Description
pack-priv
TemporaryClob(ConnectionChild
connection object used to obtain synchronization object
conChild
)

Constructs a TemporaryClob object used to perform operations on a CLOB value.

pack-priv
TemporaryClob(String
initial value in String
data
,
ConnectionChild
connection object used to obtain synchronization object
conChild
)

Constructs a TemporaryClob object and initializes with a initial String.

Method Summary

Modifier and TypeMethod and Description
private final void
checkIfValid()

Makes sure the Clob has not been released.

pack-priv static InternalClob

Returns:

A read-write Clob.
cloneClobContent
(String
name of the assoicated database
dbName
,
ConnectionChild
assoiated connection child
conChild
,
InternalClob
the Clob whose content to clone
clob
)

Clones the content of another internal Clob.

pack-priv static InternalClob

Returns:

A read-write Clob.
cloneClobContent
(String
name of the assoicated database
dbName
,
ConnectionChild
assoiated connection child
conChild
,
InternalClob
the Clob whose content to clone
clob
,
long
number of chars in new InternalClob
length
)

Clones the content of another internal Clob.

private void
copyClobContent(InternalClob
the Clob to copy from
clob
)

Copies the content of another Clob into this one.

private void
copyClobContent(InternalClob
the Clob to copy from
clob
,
long
number of chars to copy
charLength
)

Copies the content of another Clob into this one.

private byte[]

Returns:

Byte array representing the string in modified UTF-8 encoding.
getByteFromString
(String
string to represent with modified UTF-8 encoding
str
)

Converts a string into the modified UTF-8 byte encoding.

public synchronized long

Returns:

Number of bytes in the CLOB value.
getByteLength
()

Returns the size of the Clob in bytes.

private long

Returns:

Stream position in bytes for the given character position.
getBytePosition
(final long
character position
charPos
)

Finds the corresponding byte position for the given UTF-8 character position, starting from the byte position startPos.

public synchronized long

Returns:

The length of the Clob in number of characters.
getCharLength
()

Implements org.apache.derby.impl.jdbc.InternalClob.getCharLength.

Returns number of characters in the Clob.

public synchronized long

Returns:

The number of characters in the Clob, or -1 if unknown.
getCharLengthIfKnown
()

Implements org.apache.derby.impl.jdbc.InternalClob.getCharLengthIfKnown.

Returns the cached character count for the Clob, if any.

private final CharacterStreamDescriptor

Returns:

A character stream descriptor.
getCSD
()

Returns a character stream descriptor for the stream.

public Reader
getInternalReader(long
character position. The first character is at position 1.
characterPosition
)

Implements org.apache.derby.impl.jdbc.InternalClob.getInternalReader.

Returns an internal reader for the Clob content, initialized at the specified character position.

public synchronized InputStream

Returns:

A stream serving the raw bytes of the stream, initialized at byte position 0.
getRawByteStream
()

Implements org.apache.derby.impl.jdbc.InternalClob.getRawByteStream.

Returns a stream serving the raw bytes of this Clob.

public synchronized Reader

Returns:

A Reader with the underlying CLOB value as source.
getReader
(long
initial position of the returned Reader in number of characters. Expected to be non-negative. The first character is at position 0.
pos
)

Implements org.apache.derby.impl.jdbc.InternalClob.getReader.

Constructs and returns a Reader.

public long

Returns:

Update count.
getUpdateCount
()

Implements org.apache.derby.impl.jdbc.InternalClob.getUpdateCount.

Returns the update count of this Clob.

public synchronized Writer

Returns:

A Writer to write to the CLOB value.
getWriter
(long
the initial position in bytes for the Writer
pos
)

Implements org.apache.derby.impl.jdbc.InternalClob.getWriter.

Constructs and returns a Writer for the CLOB value.

public synchronized long

Returns:

Number of characters inserted.
insertString
(String
the string to insert
str
,
long
the character position to insert the string at
insertionPoint
)

Implements org.apache.derby.impl.jdbc.InternalClob.insertString.

Inserts a string at the given position.

public synchronized boolean

Returns:

true if released, false if not.
isReleased
()

Implements org.apache.derby.impl.jdbc.InternalClob.isReleased.

Tells if this Clob has been released.

public boolean

Returns:

true
isWritable
()

Implements org.apache.derby.impl.jdbc.InternalClob.isWritable.

Tells if this Clob is intended to be writable.

public synchronized void
release()

Implements org.apache.derby.impl.jdbc.InternalClob.release.

Releases this Clob by freeing assoicated resources.

public synchronized void
truncate(long
the new length, in characters, of the Clob
newCharLength
)

Implements org.apache.derby.impl.jdbc.InternalClob.truncate.

Truncate the Clob to the specifiec size.

private final void
updateInternalState(long
the position where the Clob change started
charChangePosition
)

Updates the internal state after a modification has been performed on the Clob content.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bytesback to summary
private final LOBStreamControl bytes

Underlying structure holding this Clobs raw bytes.

cachedCharLengthback to summary
private long cachedCharLength

Cached character length of the Clob.

A value of 0 is interpreted as unknown length, even though it is a valid value. If the length is requested and the value is zero, an attempt to obtain the length is made by draining the source.

conChildback to summary
private ConnectionChild conChild

Connection child assoicated with this Clob.

Currently only used for synchronization in *some* streams associated with the Clob. This suggests something is off wrt. synchronization.

internalReaderback to summary
private UTF8Reader internalReader

Shared internal reader, closed when the Clob is released. This is a performance optimization, and the stream is shared between "one time" operations, for instance getSubString calls. Often a subset, or the whole, of the Clob is read subsequently and then this optimization avoids repositioning costs (the store does not support random access for LOBs)

Note

Do not publish this reader to the end-user.

posCacheback to summary
private final TemporaryClob.CharToBytePositionCache posCache

Simple one-entry cache for character-byte position.

releasedback to summary
private boolean released

Tells whether this Clob has been released or not.

unclosableInternalReaderback to summary
private FilterReader unclosableInternalReader

The internal reader wrapped so that it cannot be closed.

Constructor Detail

TemporaryClobback to summary
pack-priv TemporaryClob(ConnectionChild conChild)

Constructs a TemporaryClob object used to perform operations on a CLOB value.

Parameters
conChild:ConnectionChild

connection object used to obtain synchronization object

Exceptions
NullPointerException:
if conChild is null
TemporaryClobback to summary
pack-priv TemporaryClob(String data, ConnectionChild conChild) throws IOException, StandardException

Constructs a TemporaryClob object and initializes with a initial String.

Parameters
data:String

initial value in String

conChild:ConnectionChild

connection object used to obtain synchronization object

Method Detail

checkIfValidback to summary
private final void checkIfValid()

Makes sure the Clob has not been released.

All operations are invalid on a released Clob.

Exceptions
IllegalStateException:
if the Clob has been released
cloneClobContentback to summary
pack-priv static InternalClob cloneClobContent(String dbName, ConnectionChild conChild, InternalClob clob) throws IOException, SQLException

Clones the content of another internal Clob.

Parameters
dbName:String

name of the assoicated database

conChild:ConnectionChild

assoiated connection child

clob:InternalClob

the Clob whose content to clone

Returns:InternalClob

A read-write Clob.

Exceptions
IOException:
if accessing the I/O resources fail (read or write)
SQLException:
if accessing underlying resources fail
cloneClobContentback to summary
pack-priv static InternalClob cloneClobContent(String dbName, ConnectionChild conChild, InternalClob clob, long length) throws IOException, SQLException

Clones the content of another internal Clob.

Parameters
dbName:String

name of the assoicated database

conChild:ConnectionChild

assoiated connection child

clob:InternalClob

the Clob whose content to clone

length:long

number of chars in new InternalClob

Returns:InternalClob

A read-write Clob.

Exceptions
IOException:
if accessing the I/O resources fail (read or write)
SQLException:
if accessing underlying resources fail
copyClobContentback to summary
private void copyClobContent(InternalClob clob) throws IOException, SQLException

Copies the content of another Clob into this one.

Parameters
clob:InternalClob

the Clob to copy from

Exceptions
IOException:
if accessing I/O resources fail (both read and write)
SQLException:
if accessing underlying resources fail
copyClobContentback to summary
private void copyClobContent(InternalClob clob, long charLength) throws IOException, SQLException

Copies the content of another Clob into this one.

Parameters
clob:InternalClob

the Clob to copy from

charLength:long

number of chars to copy

Exceptions
IOException:
if accessing I/O resources fail (both read and write)
SQLException:
if accessing underlying resources fail
EOFException:
if the length of the stream is shorter than the specified length
getByteFromStringback to summary
private byte[] getByteFromString(String str)

Converts a string into the modified UTF-8 byte encoding.

Parameters
str:String

string to represent with modified UTF-8 encoding

Returns:byte[]

Byte array representing the string in modified UTF-8 encoding.

getByteLengthback to summary
public synchronized long getByteLength() throws IOException

Returns the size of the Clob in bytes.

Returns:long

Number of bytes in the CLOB value.

Exceptions
IOException:
if accessing the underlying I/O resources fail
getBytePositionback to summary
private long getBytePosition(final long charPos) throws IOException

Finds the corresponding byte position for the given UTF-8 character position, starting from the byte position startPos. See comments in SQLChar.readExternal for more notes on processing the UTF8 format.

Parameters
charPos:long

character position

Returns:long

Stream position in bytes for the given character position.

Exceptions
IOException:
if accessing underlying I/O resources fail
EOFException:
if the character position specified is greater than the Clob length +1
getCharLengthback to summary
public synchronized long getCharLength() throws IOException

Implements org.apache.derby.impl.jdbc.InternalClob.getCharLength.

Returns number of characters in the Clob.

Returns:long

The length of the Clob in number of characters.

Exceptions
IOException:
if accessing the underlying I/O resources fail
getCharLengthIfKnownback to summary
public synchronized long getCharLengthIfKnown()

Implements org.apache.derby.impl.jdbc.InternalClob.getCharLengthIfKnown.

Returns the cached character count for the Clob, if any.

Returns:long

The number of characters in the Clob, or -1 if unknown.

getCSDback to summary
private final CharacterStreamDescriptor getCSD() throws IOException

Returns a character stream descriptor for the stream.

All streams from the underlying source (LOBStreamControl) are position aware and can be moved to a specific byte position cheaply. The maximum length is not really needed, nor known, at the moment, so the maximum allowed Clob length in Derby is used.

Returns:CharacterStreamDescriptor

A character stream descriptor.

Exceptions
IOException:
if obtaining the length of the stream fails
getInternalReaderback to summary
public Reader getInternalReader(long characterPosition) throws IOException, SQLException

Implements org.apache.derby.impl.jdbc.InternalClob.getInternalReader.

Doc from org.apache.derby.impl.jdbc.InternalClob.getInternalReader.

Returns an internal reader for the Clob content, initialized at the specified character position.

This method can return a shared reader object, avoiding instantiation and repositioning costs for internal operations where the stream itself is not published to the end-user. One such example is Clob.getSubString.

Parameters
characterPosition:long

character position. The first character is at position 1.

Returns:Reader

A Reader serving the content of the Clob.

Exceptions
IOException:
if accessing underlying I/O resources fail
SQLException:
if accessing underlying resources fail
See Also
getReader
getRawByteStreamback to summary
public synchronized InputStream getRawByteStream() throws IOException

Implements org.apache.derby.impl.jdbc.InternalClob.getRawByteStream.

Returns a stream serving the raw bytes of this Clob.

The stream is managed by the underlying byte store, and can serve bytes both from memory and from a file on disk.

Returns:InputStream

A stream serving the raw bytes of the stream, initialized at byte position 0.

Exceptions
IOException:
if obtaining the stream fails
getReaderback to summary
public synchronized Reader getReader(long pos) throws IOException, SQLException

Implements org.apache.derby.impl.jdbc.InternalClob.getReader.

Constructs and returns a Reader.

Parameters
pos:long

initial position of the returned Reader in number of characters. Expected to be non-negative. The first character is at position 0.

Returns:Reader

A Reader with the underlying CLOB value as source.

Exceptions
IOException:

Doc from org.apache.derby.impl.jdbc.InternalClob.getReader.

if accessing underlying I/O resources fail

SQLException:
if the specified position is too big
getUpdateCountback to summary
public long getUpdateCount()

Implements org.apache.derby.impl.jdbc.InternalClob.getUpdateCount.

Returns the update count of this Clob.

Returns:long

Update count.

getWriterback to summary
public synchronized Writer getWriter(long pos) throws IOException, SQLException

Implements org.apache.derby.impl.jdbc.InternalClob.getWriter.

Constructs and returns a Writer for the CLOB value.

Parameters
pos:long

the initial position in bytes for the Writer

Returns:Writer

A Writer to write to the CLOB value.

Exceptions
IOException:

Doc from org.apache.derby.impl.jdbc.InternalClob.getWriter.

if writing to the Clob fails

SQLException:
if the specified position is invalid
insertStringback to summary
public synchronized long insertString(String str, long insertionPoint) throws IOException, SQLException

Implements org.apache.derby.impl.jdbc.InternalClob.insertString.

Inserts a string at the given position.

Parameters
str:String

the string to insert

insertionPoint:long

the character position to insert the string at

Returns:long

Number of characters inserted.

Exceptions
IOException:
if accessing the underlying I/O resources fail
SQLException:
if accessing the underlying resources fail
EOFException:
if the position is larger than the Clob length +1
isReleasedback to summary
public synchronized boolean isReleased()

Implements org.apache.derby.impl.jdbc.InternalClob.isReleased.

Tells if this Clob has been released.

Returns:boolean

true if released, false if not.

isWritableback to summary
public boolean isWritable()

Implements org.apache.derby.impl.jdbc.InternalClob.isWritable.

Tells if this Clob is intended to be writable.

Returns:boolean

true

releaseback to summary
public synchronized void release() throws IOException

Implements org.apache.derby.impl.jdbc.InternalClob.release.

Releases this Clob by freeing assoicated resources.

Exceptions
IOException:
if accessing underlying I/O resources fail
truncateback to summary
public synchronized void truncate(long newCharLength) throws IOException, SQLException

Implements org.apache.derby.impl.jdbc.InternalClob.truncate.

Truncate the Clob to the specifiec size.

Parameters
newCharLength:long

the new length, in characters, of the Clob

Exceptions
IOException:
if accessing the underlying I/O resources fails
SQLException:

Doc from org.apache.derby.impl.jdbc.InternalClob.truncate.

if accessing underlying resources fail

updateInternalStateback to summary
private final void updateInternalState(long charChangePosition)

Updates the internal state after a modification has been performed on the Clob content.

Currently the state update consists of dicarding the internal reader to stop it from delivering stale data, to reset the byte/char position cache if necessary, and to reset the cached length.

Parameters
charChangePosition:long

the position where the Clob change started

org.apache.derby.impl.jdbc back to summary

private Class TemporaryClob.CharToBytePositionCache

extends Object
Class Inheritance
  • java.lang.Object
  • org.apache.derby.impl.jdbc.TemporaryClob.CharToBytePositionCache

A simple class to hold the byte position for a character position.

The implementation is very simple and is basically intended to speed up writing a sequence of consequtive characters one character at a time. Even though this should be avoided if possible, the penalty of updating a large Clob this way and finding the correct byte position by navigating from the start of the byte stream each time is so severe that a simple caching mechanism should be in place. Note that for other encodings than UTF-8, this might not be a problem if the mapping between character position and byte position is one-to-one.

Note that to ensure consistency between character and byte positions, access to this class must be synchronized externally to avoid caller 1 getting the character position, then caller 2 updates the cached values and then caller 1 gets the updated byte position.

Field Summary

Modifier and TypeField and Description
private long
private long

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv long

Returns:

The byte position for the last cached character position.
getBytePos
()

Returns the last cached byte position.

pack-priv long

Returns:

The last cached character position.
getCharPos
()

Returns the last cached character position.

pack-priv void
reset()

Resets the position cache.

pack-priv void
updateCachedPos(long
the character position to cache the byte position for
charPos
,
long
byte position for the specified character position
bytePos
)

Updates the position cache.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bytePosback to summary
private long bytePos
charPosback to summary
private long charPos

Constructor Detail

CharToBytePositionCacheback to summary
pack-priv CharToBytePositionCache()

Method Detail

getBytePosback to summary
pack-priv long getBytePos()

Returns the last cached byte position.

Returns:long

The byte position for the last cached character position.

getCharPosback to summary
pack-priv long getCharPos()

Returns the last cached character position.

Returns:long

The last cached character position.

resetback to summary
pack-priv void reset()

Resets the position cache.

updateCachedPosback to summary
pack-priv void updateCachedPos(long charPos, long bytePos)

Updates the position cache.

Parameters
charPos:long

the character position to cache the byte position for

bytePos:long

byte position for the specified character position