use of org.h2.value.ValueClob in project h2database by h2database.
the class ValueClob method compareTypeSafe.
@Override
public int compareTypeSafe(Value v, CompareMode mode, CastDataProvider provider) {
if (v == this) {
return 0;
}
ValueClob v2 = (ValueClob) v;
LobData lobData = this.lobData, lobData2 = v2.lobData;
if (lobData.getClass() == lobData2.getClass()) {
if (lobData instanceof LobDataInMemory) {
return Integer.signum(getString().compareTo(v2.getString()));
} else if (lobData instanceof LobDataDatabase) {
if (((LobDataDatabase) lobData).getLobId() == ((LobDataDatabase) lobData2).getLobId()) {
return 0;
}
} else if (lobData instanceof LobDataFetchOnDemand) {
if (((LobDataFetchOnDemand) lobData).getLobId() == ((LobDataFetchOnDemand) lobData2).getLobId()) {
return 0;
}
}
}
return compare(this, v2);
}
use of org.h2.value.ValueClob in project apollo by salesforce.
the class StreamTransfer method writeValue.
/**
* Write a value.
*
* @param v the value
* @throws IOException on failure
*/
public void writeValue(Value v, DataOutputStream out) throws IOException {
int type = v.getValueType();
switch(type) {
case Value.NULL:
writeInt(NULL, out);
break;
case Value.BINARY:
if (version >= Constants.TCP_PROTOCOL_VERSION_20) {
writeInt(BINARY, out);
writeBytes(v.getBytesNoCopy(), out);
break;
}
// $FALL-THROUGH$
case Value.VARBINARY:
writeInt(VARBINARY, out);
writeBytes(v.getBytesNoCopy(), out);
break;
case Value.JAVA_OBJECT:
writeInt(JAVA_OBJECT, out);
writeBytes(v.getBytesNoCopy(), out);
break;
case Value.UUID:
{
writeInt(UUID, out);
ValueUuid uuid = (ValueUuid) v;
writeLong(uuid.getHigh(), out);
writeLong(uuid.getLow(), out);
break;
}
case Value.BOOLEAN:
writeInt(BOOLEAN, out);
writeBoolean(v.getBoolean(), out);
break;
case Value.TINYINT:
writeInt(TINYINT, out);
writeByte(v.getByte(), out);
break;
case Value.TIME:
writeInt(TIME, out);
writeLong(((ValueTime) v).getNanos(), out);
break;
case Value.TIME_TZ:
{
ValueTimeTimeZone t = (ValueTimeTimeZone) v;
if (version >= Constants.TCP_PROTOCOL_VERSION_19) {
writeInt(TIME_TZ, out);
writeLong(t.getNanos(), out);
writeInt(t.getTimeZoneOffsetSeconds(), out);
} else {
writeInt(TIME, out);
/*
* Don't call SessionRemote.currentTimestamp(), it may require own remote call
* and old server will not return custom time zone anyway.
*/
ValueTimestampTimeZone current = DateTimeUtils.currentTimestamp(DateTimeUtils.getTimeZone());
writeLong(DateTimeUtils.normalizeNanosOfDay(t.getNanos() + (t.getTimeZoneOffsetSeconds() - current.getTimeZoneOffsetSeconds()) * DateTimeUtils.NANOS_PER_DAY), out);
}
break;
}
case Value.DATE:
writeInt(DATE, out);
writeLong(((ValueDate) v).getDateValue(), out);
break;
case Value.TIMESTAMP:
{
writeInt(TIMESTAMP, out);
ValueTimestamp ts = (ValueTimestamp) v;
writeLong(ts.getDateValue(), out);
writeLong(ts.getTimeNanos(), out);
break;
}
case Value.TIMESTAMP_TZ:
{
writeInt(TIMESTAMP_TZ, out);
ValueTimestampTimeZone ts = (ValueTimestampTimeZone) v;
writeLong(ts.getDateValue(), out);
writeLong(ts.getTimeNanos(), out);
int timeZoneOffset = ts.getTimeZoneOffsetSeconds();
writeInt(//
version >= Constants.TCP_PROTOCOL_VERSION_19 ? timeZoneOffset : timeZoneOffset / 60, out);
break;
}
case Value.DECFLOAT:
if (version >= Constants.TCP_PROTOCOL_VERSION_20) {
writeInt(DECFLOAT, out);
writeString(v.getString(), out);
break;
}
// $FALL-THROUGH$
case Value.NUMERIC:
writeInt(NUMERIC, out);
writeString(v.getString(), out);
break;
case Value.DOUBLE:
writeInt(DOUBLE, out);
writeDouble(v.getDouble(), out);
break;
case Value.REAL:
writeInt(REAL, out);
writeFloat(v.getFloat(), out);
break;
case Value.INTEGER:
writeInt(INTEGER, out);
writeInt(v.getInt(), out);
break;
case Value.BIGINT:
writeInt(BIGINT, out);
writeLong(v.getLong(), out);
break;
case Value.SMALLINT:
writeInt(SMALLINT, out);
if (version >= Constants.TCP_PROTOCOL_VERSION_20) {
writeShort(v.getShort(), out);
} else {
writeInt(v.getShort(), out);
}
break;
case Value.VARCHAR:
writeInt(VARCHAR, out);
writeString(v.getString(), out);
break;
case Value.VARCHAR_IGNORECASE:
writeInt(VARCHAR_IGNORECASE, out);
writeString(v.getString(), out);
break;
case Value.CHAR:
writeInt(CHAR, out);
writeString(v.getString(), out);
break;
case Value.BLOB:
{
writeInt(BLOB, out);
ValueBlob lob = (ValueBlob) v;
LobData lobData = lob.getLobData();
long length = lob.octetLength();
if (lobData instanceof LobDataDatabase) {
LobDataDatabase lobDataDatabase = (LobDataDatabase) lobData;
writeLong(-1, out);
writeInt(lobDataDatabase.getTableId(), out);
writeLong(lobDataDatabase.getLobId(), out);
writeBytes(calculateLobMac(lobDataDatabase.getLobId()), out);
writeLong(length, out);
break;
}
if (length < 0) {
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1, "length=" + length);
}
writeLong(length, out);
long written = IOUtils.copyAndCloseInput(lob.getInputStream(), out);
if (written != length) {
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1, "length:" + length + " written:" + written);
}
writeInt(LOB_MAGIC, out);
break;
}
case Value.CLOB:
{
writeInt(CLOB, out);
ValueClob lob = (ValueClob) v;
LobData lobData = lob.getLobData();
long charLength = lob.charLength();
if (lobData instanceof LobDataDatabase) {
LobDataDatabase lobDataDatabase = (LobDataDatabase) lobData;
writeLong(-1, out);
writeInt(lobDataDatabase.getTableId(), out);
writeLong(lobDataDatabase.getLobId(), out);
writeBytes(calculateLobMac(lobDataDatabase.getLobId()), out);
if (version >= Constants.TCP_PROTOCOL_VERSION_20) {
writeLong(lob.octetLength(), out);
}
writeLong(charLength, out);
break;
}
if (charLength < 0) {
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1, "length=" + charLength);
}
writeLong(charLength, out);
Reader reader = lob.getReader();
Data.copyString(reader, out);
writeInt(LOB_MAGIC, out);
break;
}
case Value.ARRAY:
{
writeInt(ARRAY, out);
ValueArray va = (ValueArray) v;
Value[] list = va.getList();
int len = list.length;
writeInt(len, out);
for (Value value : list) {
writeValue(value, out);
}
break;
}
case Value.ROW:
{
writeInt(version >= Constants.TCP_PROTOCOL_VERSION_18 ? ROW : ARRAY, out);
ValueRow va = (ValueRow) v;
Value[] list = va.getList();
int len = list.length;
writeInt(len, out);
for (Value value : list) {
writeValue(value, out);
}
break;
}
case Value.ENUM:
{
writeInt(ENUM, out);
writeInt(v.getInt(), out);
if (version < Constants.TCP_PROTOCOL_VERSION_20) {
writeString(v.getString(), out);
}
break;
}
case Value.GEOMETRY:
writeInt(GEOMETRY, out);
writeBytes(v.getBytesNoCopy(), out);
break;
case Value.INTERVAL_YEAR:
case Value.INTERVAL_MONTH:
case Value.INTERVAL_DAY:
case Value.INTERVAL_HOUR:
case Value.INTERVAL_MINUTE:
if (version >= Constants.TCP_PROTOCOL_VERSION_18) {
ValueInterval interval = (ValueInterval) v;
int ordinal = type - Value.INTERVAL_YEAR;
if (interval.isNegative()) {
ordinal = ~ordinal;
}
writeInt(INTERVAL, out);
writeByte((byte) ordinal, out);
writeLong(interval.getLeading(), out);
} else {
writeInt(VARCHAR, out);
writeString(v.getString(), out);
}
break;
case Value.INTERVAL_SECOND:
case Value.INTERVAL_YEAR_TO_MONTH:
case Value.INTERVAL_DAY_TO_HOUR:
case Value.INTERVAL_DAY_TO_MINUTE:
case Value.INTERVAL_DAY_TO_SECOND:
case Value.INTERVAL_HOUR_TO_MINUTE:
case Value.INTERVAL_HOUR_TO_SECOND:
case Value.INTERVAL_MINUTE_TO_SECOND:
if (version >= Constants.TCP_PROTOCOL_VERSION_18) {
ValueInterval interval = (ValueInterval) v;
int ordinal = type - Value.INTERVAL_YEAR;
if (interval.isNegative()) {
ordinal = ~ordinal;
}
writeInt(INTERVAL, out);
writeByte((byte) ordinal, out);
writeLong(interval.getLeading(), out);
writeLong(interval.getRemaining(), out);
} else {
writeInt(VARCHAR, out);
writeString(v.getString(), out);
}
break;
case Value.JSON:
{
writeInt(JSON, out);
writeBytes(v.getBytesNoCopy(), out);
break;
}
default:
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1, "type=" + type);
}
}
use of org.h2.value.ValueClob in project SpringStudy by myounghaklee.
the class LobStorageMap method createClob.
@Override
public ValueClob createClob(Reader reader, long maxLength) {
MVStore.TxCounter txCounter = mvStore.registerVersionUsage();
try {
// we multiple by 3 here to get the worst-case size in bytes
if (maxLength != -1 && maxLength * 3 <= database.getMaxLengthInplaceLob()) {
char[] small = new char[(int) maxLength];
int len = IOUtils.readFully(reader, small, (int) maxLength);
if (len > maxLength) {
throw new IllegalStateException("len > blobLength, " + len + " > " + maxLength);
}
byte[] utf8 = new String(small, 0, len).getBytes(StandardCharsets.UTF_8);
if (utf8.length > database.getMaxLengthInplaceLob()) {
throw new IllegalStateException("len > maxinplace, " + utf8.length + " > " + database.getMaxLengthInplaceLob());
}
return ValueClob.createSmall(utf8, len);
}
if (maxLength < 0) {
maxLength = Long.MAX_VALUE;
}
CountingReaderInputStream in = new CountingReaderInputStream(reader, maxLength);
ValueBlob blob = createBlob(in);
LobData lobData = blob.getLobData();
return new ValueClob(lobData, blob.octetLength(), in.getLength());
} catch (IllegalStateException e) {
throw DbException.get(ErrorCode.OBJECT_CLOSED, e);
} catch (IOException e) {
throw DbException.convertIOException(e, null);
} finally {
mvStore.deregisterVersionUsage(txCounter);
}
}
use of org.h2.value.ValueClob in project SpringStudy by myounghaklee.
the class ValueClob method copy.
@Override
public ValueLob copy(DataHandler database, int tableId) {
if (lobData instanceof LobDataInMemory) {
byte[] small = ((LobDataInMemory) lobData).getSmall();
if (small.length > database.getMaxLengthInplaceLob()) {
LobStorageInterface s = database.getLobStorage();
ValueClob v = s.createClob(getReader(), charLength);
ValueLob v2 = v.copy(database, tableId);
v.remove();
return v2;
}
return this;
} else if (lobData instanceof LobDataDatabase) {
return database.getLobStorage().copyLob(this, tableId);
} else {
throw new UnsupportedOperationException();
}
}
use of org.h2.value.ValueClob in project SpringStudy by myounghaklee.
the class ValueClob method compare.
/**
* Compares two CLOB values directly.
*
* @param v1
* first CLOB value
* @param v2
* second CLOB value
* @return result of comparison
*/
private static int compare(ValueClob v1, ValueClob v2) {
long minPrec = Math.min(v1.charLength, v2.charLength);
try (Reader reader1 = v1.getReader();
Reader reader2 = v2.getReader()) {
char[] buf1 = new char[BLOCK_COMPARISON_SIZE];
char[] buf2 = new char[BLOCK_COMPARISON_SIZE];
for (; minPrec >= BLOCK_COMPARISON_SIZE; minPrec -= BLOCK_COMPARISON_SIZE) {
if (IOUtils.readFully(reader1, buf1, BLOCK_COMPARISON_SIZE) != BLOCK_COMPARISON_SIZE || IOUtils.readFully(reader2, buf2, BLOCK_COMPARISON_SIZE) != BLOCK_COMPARISON_SIZE) {
throw DbException.getUnsupportedException("Invalid LOB");
}
int cmp = Bits.compareNotNull(buf1, buf2);
if (cmp != 0) {
return cmp;
}
}
for (; ; ) {
int c1 = reader1.read(), c2 = reader2.read();
if (c1 < 0) {
return c2 < 0 ? 0 : -1;
}
if (c2 < 0) {
return 1;
}
if (c1 != c2) {
return c1 < c2 ? -1 : 1;
}
}
} catch (IOException ex) {
throw DbException.convert(ex);
}
}
Aggregations