Search in sources :

Example 11 with ValueClob

use of org.h2.value.ValueClob in project SpringStudy by myounghaklee.

the class ValueClob method createTempClob.

/**
 * Create a temporary CLOB value from a stream.
 *
 * @param in
 *            the reader
 * @param length
 *            the number of characters to read, or -1 for no limit
 * @param handler
 *            the data handler
 * @return the lob value
 */
public static ValueClob createTempClob(Reader in, long length, DataHandler handler) {
    if (length >= 0) {
        // blocks the network level
        try {
            in = new RangeReader(in, 0, length);
        } catch (IOException e) {
            throw DbException.convert(e);
        }
    }
    BufferedReader reader;
    if (in instanceof BufferedReader) {
        reader = (BufferedReader) in;
    } else {
        reader = new BufferedReader(in, Constants.IO_BUFFER_SIZE);
    }
    try {
        long remaining = Long.MAX_VALUE;
        if (length >= 0 && length < remaining) {
            remaining = length;
        }
        int len = ValueLob.getBufferSize(handler, remaining);
        char[] buff;
        if (len >= Integer.MAX_VALUE) {
            String data = IOUtils.readStringAndClose(reader, -1);
            buff = data.toCharArray();
            len = buff.length;
        } else {
            buff = new char[len];
            reader.mark(len);
            len = IOUtils.readFully(reader, buff, len);
        }
        if (len <= handler.getMaxLengthInplaceLob()) {
            return ValueClob.createSmall(new String(buff, 0, len));
        }
        reader.reset();
        return createTemporary(handler, reader, remaining);
    } catch (IOException e) {
        throw DbException.convertIOException(e, null);
    }
}
Also used : RangeReader(org.h2.store.RangeReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Example 12 with ValueClob

use of org.h2.value.ValueClob in project 468H2Project by lukeunderwood42.

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 13 with ValueClob

use of org.h2.value.ValueClob in project 468H2Project by lukeunderwood42.

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)

Example 14 with ValueClob

use of org.h2.value.ValueClob in project 468H2Project by lukeunderwood42.

the class ValueClob method convertPrecision.

/**
 * Convert the precision to the requested value.
 *
 * @param precision
 *            the new precision
 * @return the truncated or this value
 */
ValueClob convertPrecision(long precision) {
    if (this.charLength <= precision) {
        return this;
    }
    ValueClob lob;
    DataHandler handler = lobData.getDataHandler();
    if (handler != null) {
        lob = createTempClob(getReader(), precision, handler);
    } else {
        try {
            lob = createSmall(IOUtils.readStringAndClose(getReader(), MathUtils.convertLongToInt(precision)));
        } catch (IOException e) {
            throw DbException.convertIOException(e, null);
        }
    }
    return lob;
}
Also used : DataHandler(org.h2.store.DataHandler) IOException(java.io.IOException)

Example 15 with ValueClob

use of org.h2.value.ValueClob in project 468H2Project by lukeunderwood42.

the class ValueClob method createTemporary.

/**
 * Create a CLOB in a temporary file.
 */
private static ValueClob createTemporary(DataHandler handler, Reader in, long remaining) throws IOException {
    String fileName = ValueLob.createTempLobFileName(handler);
    FileStore tempFile = handler.openFile(fileName, "rw", false);
    tempFile.autoDelete();
    long octetLength = 0L, charLength = 0L;
    try (FileStoreOutputStream out = new FileStoreOutputStream(tempFile, null)) {
        char[] buff = new char[Constants.IO_BUFFER_SIZE];
        while (true) {
            int len = ValueLob.getBufferSize(handler, remaining);
            len = IOUtils.readFully(in, buff, len);
            if (len == 0) {
                break;
            }
            // TODO reduce memory allocation
            byte[] data = new String(buff, 0, len).getBytes(StandardCharsets.UTF_8);
            out.write(data);
            octetLength += data.length;
            charLength += len;
        }
    }
    return new ValueClob(new LobDataFile(handler, fileName, tempFile), octetLength, charLength);
}
Also used : FileStore(org.h2.store.FileStore) LobDataFile(org.h2.value.lob.LobDataFile) FileStoreOutputStream(org.h2.store.FileStoreOutputStream)

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