Search in sources :

Example 1 with ValueLobDb

use of org.h2.value.ValueLobDb in project h2database by h2database.

the class LobStorageBackend method copyLob.

@Override
public ValueLobDb copyLob(ValueLobDb old, int tableId, long length) {
    int type = old.getType();
    long oldLobId = old.getLobId();
    assertNotHolds(conn.getSession());
    // see locking discussion at the top
    synchronized (database) {
        synchronized (conn.getSession()) {
            try {
                init();
                ValueLobDb v = null;
                if (!old.isRecoveryReference()) {
                    long lobId = getNextLobId();
                    String sql = "INSERT INTO " + LOB_MAP + "(LOB, SEQ, POS, HASH, BLOCK) " + "SELECT ?, SEQ, POS, HASH, BLOCK FROM " + LOB_MAP + " WHERE LOB = ?";
                    PreparedStatement prep = prepare(sql);
                    prep.setLong(1, lobId);
                    prep.setLong(2, oldLobId);
                    prep.executeUpdate();
                    reuse(sql, prep);
                    sql = "INSERT INTO " + LOBS + "(ID, BYTE_COUNT, TABLE) " + "SELECT ?, BYTE_COUNT, ? FROM " + LOBS + " WHERE ID = ?";
                    prep = prepare(sql);
                    prep.setLong(1, lobId);
                    prep.setLong(2, tableId);
                    prep.setLong(3, oldLobId);
                    prep.executeUpdate();
                    reuse(sql, prep);
                    v = ValueLobDb.create(type, database, tableId, lobId, null, length);
                } else {
                    // Recovery process, no need to copy LOB using normal
                    // infrastructure
                    v = ValueLobDb.create(type, database, tableId, oldLobId, null, length);
                }
                return v;
            } catch (SQLException e) {
                throw DbException.convert(e);
            }
        }
    }
}
Also used : ValueLobDb(org.h2.value.ValueLobDb) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement)

Example 2 with ValueLobDb

use of org.h2.value.ValueLobDb in project h2database by h2database.

the class LobStorageMap method createLob.

private ValueLobDb createLob(InputStream in, int type) throws IOException {
    byte[] streamStoreId;
    try {
        streamStoreId = streamStore.put(in);
    } catch (Exception e) {
        throw DbException.convertToIOException(e);
    }
    long lobId = generateLobId();
    long length = streamStore.length(streamStoreId);
    int tableId = LobStorageFrontend.TABLE_TEMP;
    Object[] value = { streamStoreId, tableId, length, 0 };
    lobMap.put(lobId, value);
    Object[] key = { streamStoreId, lobId };
    refMap.put(key, Boolean.TRUE);
    ValueLobDb lob = ValueLobDb.create(type, database, tableId, lobId, null, length);
    if (TRACE) {
        trace("create " + tableId + "/" + lobId);
    }
    return lob;
}
Also used : ValueLobDb(org.h2.value.ValueLobDb) IOException(java.io.IOException) DbException(org.h2.message.DbException)

Example 3 with ValueLobDb

use of org.h2.value.ValueLobDb in project h2database by h2database.

the class LobStorageMap method createClob.

@Override
public Value createClob(Reader reader, long maxLength) {
    init();
    int type = Value.CLOB;
    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 ValueLobDb.createSmallLob(type, utf8);
        }
        if (maxLength < 0) {
            maxLength = Long.MAX_VALUE;
        }
        CountingReaderInputStream in = new CountingReaderInputStream(reader, maxLength);
        ValueLobDb lob = createLob(in, type);
        // the length is not correct
        lob = ValueLobDb.create(type, database, lob.getTableId(), lob.getLobId(), null, in.getLength());
        return lob;
    } catch (IllegalStateException e) {
        throw DbException.get(ErrorCode.OBJECT_CLOSED, e);
    } catch (IOException e) {
        throw DbException.convertIOException(e, null);
    }
}
Also used : ValueLobDb(org.h2.value.ValueLobDb) IOException(java.io.IOException)

Example 4 with ValueLobDb

use of org.h2.value.ValueLobDb in project h2database by h2database.

the class LobStorageMap method copyLob.

@Override
public ValueLobDb copyLob(ValueLobDb old, int tableId, long length) {
    init();
    int type = old.getType();
    long oldLobId = old.getLobId();
    long oldLength = old.getPrecision();
    if (oldLength != length) {
        throw DbException.throwInternalError("Length is different");
    }
    Object[] value = lobMap.get(oldLobId);
    value = value.clone();
    byte[] streamStoreId = (byte[]) value[0];
    long lobId = generateLobId();
    value[1] = tableId;
    lobMap.put(lobId, value);
    Object[] key = { streamStoreId, lobId };
    refMap.put(key, Boolean.TRUE);
    ValueLobDb lob = ValueLobDb.create(type, database, tableId, lobId, null, length);
    if (TRACE) {
        trace("copy " + old.getTableId() + "/" + old.getLobId() + " > " + tableId + "/" + lobId);
    }
    return lob;
}
Also used : ValueLobDb(org.h2.value.ValueLobDb)

Example 5 with ValueLobDb

use of org.h2.value.ValueLobDb in project h2database by h2database.

the class TcpServerThread method writeValue.

private void writeValue(Value v) throws IOException {
    if (v.getType() == Value.CLOB || v.getType() == Value.BLOB) {
        if (v instanceof ValueLobDb) {
            ValueLobDb lob = (ValueLobDb) v;
            if (lob.isStored()) {
                long id = lob.getLobId();
                lobs.put(id, new CachedInputStream(null));
            }
        }
    }
    transfer.writeValue(v);
}
Also used : ValueLobDb(org.h2.value.ValueLobDb)

Aggregations

ValueLobDb (org.h2.value.ValueLobDb)10 IOException (java.io.IOException)3 SQLException (java.sql.SQLException)3 BigDecimal (java.math.BigDecimal)2 BigInteger (java.math.BigInteger)2 ResultSet (java.sql.ResultSet)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 DataHandler (org.h2.store.DataHandler)2 SimpleResultSet (org.h2.tools.SimpleResultSet)2 Value (org.h2.value.Value)2 ValueLob (org.h2.value.ValueLob)2 ValueResultSet (org.h2.value.ValueResultSet)2 ValueString (org.h2.value.ValueString)2 ValueTime (org.h2.value.ValueTime)2 ValueTimestamp (org.h2.value.ValueTimestamp)2 ValueTimestampTimeZone (org.h2.value.ValueTimestampTimeZone)2 ValueUuid (org.h2.value.ValueUuid)2 BufferedReader (java.io.BufferedReader)1 PreparedStatement (java.sql.PreparedStatement)1 Timestamp (java.sql.Timestamp)1