Search in sources :

Example 6 with ValueClob

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);
}
Also used : LobData(org.h2.value.lob.LobData) LobDataInMemory(org.h2.value.lob.LobDataInMemory) LobDataDatabase(org.h2.value.lob.LobDataDatabase) LobDataFetchOnDemand(org.h2.value.lob.LobDataFetchOnDemand)

Example 7 with ValueClob

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);
    }
}
Also used : LobData(org.h2.value.lob.LobData) ValueUuid(org.h2.value.ValueUuid) LobDataDatabase(org.h2.value.lob.LobDataDatabase) ValueRow(org.h2.value.ValueRow) ValueTimestampTimeZone(org.h2.value.ValueTimestampTimeZone) ValueBlob(org.h2.value.ValueBlob) DataReader(org.h2.store.DataReader) Reader(java.io.Reader) ValueTimeTimeZone(org.h2.value.ValueTimeTimeZone) ValueClob(org.h2.value.ValueClob) ValueInterval(org.h2.value.ValueInterval) ValueBigint(org.h2.value.ValueBigint) ValueTinyint(org.h2.value.ValueTinyint) ValueSmallint(org.h2.value.ValueSmallint) ValueTimestamp(org.h2.value.ValueTimestamp) Value(org.h2.value.Value) ValueArray(org.h2.value.ValueArray)

Example 8 with ValueClob

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);
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) LobData(org.h2.value.lob.LobData) CountingReaderInputStream(org.h2.store.CountingReaderInputStream) ValueBlob(org.h2.value.ValueBlob) ValueClob(org.h2.value.ValueClob) IOException(java.io.IOException)

Example 9 with ValueClob

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();
    }
}
Also used : LobDataInMemory(org.h2.value.lob.LobDataInMemory) LobStorageInterface(org.h2.store.LobStorageInterface) LobDataDatabase(org.h2.value.lob.LobDataDatabase)

Example 10 with ValueClob

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);
    }
}
Also used : Reader(java.io.Reader) RangeReader(org.h2.store.RangeReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Aggregations

ValueBlob (org.h2.value.ValueBlob)13 ValueClob (org.h2.value.ValueClob)13 LobDataDatabase (org.h2.value.lob.LobDataDatabase)13 IOException (java.io.IOException)12 LobData (org.h2.value.lob.LobData)10 ValueBigint (org.h2.value.ValueBigint)7 ValueSmallint (org.h2.value.ValueSmallint)7 ValueTinyint (org.h2.value.ValueTinyint)7 BufferedReader (java.io.BufferedReader)6 BigDecimal (java.math.BigDecimal)6 BigInteger (java.math.BigInteger)6 DataUtils.readString (org.h2.mvstore.DataUtils.readString)6 MVStore (org.h2.mvstore.MVStore)6 RangeReader (org.h2.store.RangeReader)6 LobDataInMemory (org.h2.value.lob.LobDataInMemory)6 Reader (java.io.Reader)4 Value (org.h2.value.Value)4 ValueInterval (org.h2.value.ValueInterval)4 ValueTimeTimeZone (org.h2.value.ValueTimeTimeZone)4 ValueTimestamp (org.h2.value.ValueTimestamp)4