use of org.h2.value.ValueLobDb in project h2database by h2database.
the class ValueLobDb 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 ValueLobDb 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 {
boolean compress = handler.getLobCompressionAlgorithm(Value.CLOB) != null;
long remaining = Long.MAX_VALUE;
if (length >= 0 && length < remaining) {
remaining = length;
}
int len = getBufferSize(handler, compress, 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()) {
byte[] small = new String(buff, 0, len).getBytes(StandardCharsets.UTF_8);
return ValueLobDb.createSmallLob(Value.CLOB, small, len);
}
reader.reset();
return new ValueLobDb(handler, reader, remaining);
} catch (IOException e) {
throw DbException.convertIOException(e, null);
}
}
use of org.h2.value.ValueLobDb in project h2database by h2database.
the class Data method writeValue.
/**
* Append a value.
*
* @param v the value
*/
public void writeValue(Value v) {
int start = pos;
if (v == ValueNull.INSTANCE) {
data[pos++] = 0;
return;
}
int type = v.getType();
switch(type) {
case Value.BOOLEAN:
writeByte((byte) (v.getBoolean() ? BOOLEAN_TRUE : BOOLEAN_FALSE));
break;
case Value.BYTE:
writeByte((byte) type);
writeByte(v.getByte());
break;
case Value.SHORT:
writeByte((byte) type);
writeShortInt(v.getShort());
break;
case Value.ENUM:
case Value.INT:
{
int x = v.getInt();
if (x < 0) {
writeByte((byte) INT_NEG);
writeVarInt(-x);
} else if (x < 16) {
writeByte((byte) (INT_0_15 + x));
} else {
writeByte((byte) type);
writeVarInt(x);
}
break;
}
case Value.LONG:
{
long x = v.getLong();
if (x < 0) {
writeByte((byte) LONG_NEG);
writeVarLong(-x);
} else if (x < 8) {
writeByte((byte) (LONG_0_7 + x));
} else {
writeByte((byte) type);
writeVarLong(x);
}
break;
}
case Value.DECIMAL:
{
BigDecimal x = v.getBigDecimal();
if (BigDecimal.ZERO.equals(x)) {
writeByte((byte) DECIMAL_0_1);
} else if (BigDecimal.ONE.equals(x)) {
writeByte((byte) (DECIMAL_0_1 + 1));
} else {
int scale = x.scale();
BigInteger b = x.unscaledValue();
int bits = b.bitLength();
if (bits <= 63) {
if (scale == 0) {
writeByte((byte) DECIMAL_SMALL_0);
writeVarLong(b.longValue());
} else {
writeByte((byte) DECIMAL_SMALL);
writeVarInt(scale);
writeVarLong(b.longValue());
}
} else {
writeByte((byte) type);
writeVarInt(scale);
byte[] bytes = b.toByteArray();
writeVarInt(bytes.length);
write(bytes, 0, bytes.length);
}
}
break;
}
case Value.TIME:
if (STORE_LOCAL_TIME) {
writeByte((byte) LOCAL_TIME);
ValueTime t = (ValueTime) v;
long nanos = t.getNanos();
long millis = nanos / 1_000_000;
nanos -= millis * 1_000_000;
writeVarLong(millis);
writeVarLong(nanos);
} else {
writeByte((byte) type);
writeVarLong(DateTimeUtils.getTimeLocalWithoutDst(v.getTime()));
}
break;
case Value.DATE:
{
if (STORE_LOCAL_TIME) {
writeByte((byte) LOCAL_DATE);
long x = ((ValueDate) v).getDateValue();
writeVarLong(x);
} else {
writeByte((byte) type);
long x = DateTimeUtils.getTimeLocalWithoutDst(v.getDate());
writeVarLong(x / MILLIS_PER_MINUTE);
}
break;
}
case Value.TIMESTAMP:
{
if (STORE_LOCAL_TIME) {
writeByte((byte) LOCAL_TIMESTAMP);
ValueTimestamp ts = (ValueTimestamp) v;
long dateValue = ts.getDateValue();
writeVarLong(dateValue);
long nanos = ts.getTimeNanos();
long millis = nanos / 1_000_000;
nanos -= millis * 1_000_000;
writeVarLong(millis);
writeVarLong(nanos);
} else {
Timestamp ts = v.getTimestamp();
writeByte((byte) type);
writeVarLong(DateTimeUtils.getTimeLocalWithoutDst(ts));
writeVarInt(ts.getNanos() % 1_000_000);
}
break;
}
case Value.TIMESTAMP_TZ:
{
ValueTimestampTimeZone ts = (ValueTimestampTimeZone) v;
writeByte((byte) type);
writeVarLong(ts.getDateValue());
writeVarLong(ts.getTimeNanos());
writeVarInt(ts.getTimeZoneOffsetMins());
break;
}
case Value.GEOMETRY:
// fall though
case Value.JAVA_OBJECT:
{
writeByte((byte) type);
byte[] b = v.getBytesNoCopy();
int len = b.length;
writeVarInt(len);
write(b, 0, len);
break;
}
case Value.BYTES:
{
byte[] b = v.getBytesNoCopy();
int len = b.length;
if (len < 32) {
writeByte((byte) (BYTES_0_31 + len));
write(b, 0, len);
} else {
writeByte((byte) type);
writeVarInt(len);
write(b, 0, len);
}
break;
}
case Value.UUID:
{
writeByte((byte) type);
ValueUuid uuid = (ValueUuid) v;
writeLong(uuid.getHigh());
writeLong(uuid.getLow());
break;
}
case Value.STRING:
{
String s = v.getString();
int len = s.length();
if (len < 32) {
writeByte((byte) (STRING_0_31 + len));
writeStringWithoutLength(s, len);
} else {
writeByte((byte) type);
writeString(s);
}
break;
}
case Value.STRING_IGNORECASE:
case Value.STRING_FIXED:
writeByte((byte) type);
writeString(v.getString());
break;
case Value.DOUBLE:
{
double x = v.getDouble();
if (x == 1.0d) {
writeByte((byte) (DOUBLE_0_1 + 1));
} else {
long d = Double.doubleToLongBits(x);
if (d == ValueDouble.ZERO_BITS) {
writeByte((byte) DOUBLE_0_1);
} else {
writeByte((byte) type);
writeVarLong(Long.reverse(d));
}
}
break;
}
case Value.FLOAT:
{
float x = v.getFloat();
if (x == 1.0f) {
writeByte((byte) (FLOAT_0_1 + 1));
} else {
int f = Float.floatToIntBits(x);
if (f == ValueFloat.ZERO_BITS) {
writeByte((byte) FLOAT_0_1);
} else {
writeByte((byte) type);
writeVarInt(Integer.reverse(f));
}
}
break;
}
case Value.BLOB:
case Value.CLOB:
{
writeByte((byte) type);
if (v instanceof ValueLob) {
ValueLob lob = (ValueLob) v;
lob.convertToFileIfRequired(handler);
byte[] small = lob.getSmall();
if (small == null) {
int t = -1;
if (!lob.isLinkedToTable()) {
t = -2;
}
writeVarInt(t);
writeVarInt(lob.getTableId());
writeVarInt(lob.getObjectId());
writeVarLong(lob.getPrecision());
writeByte((byte) (lob.isCompressed() ? 1 : 0));
if (t == -2) {
writeString(lob.getFileName());
}
} else {
writeVarInt(small.length);
write(small, 0, small.length);
}
} else {
ValueLobDb lob = (ValueLobDb) v;
byte[] small = lob.getSmall();
if (small == null) {
writeVarInt(-3);
writeVarInt(lob.getTableId());
writeVarLong(lob.getLobId());
writeVarLong(lob.getPrecision());
} else {
writeVarInt(small.length);
write(small, 0, small.length);
}
}
break;
}
case Value.ARRAY:
{
writeByte((byte) type);
Value[] list = ((ValueArray) v).getList();
writeVarInt(list.length);
for (Value x : list) {
writeValue(x);
}
break;
}
case Value.RESULT_SET:
{
writeByte((byte) type);
try {
ResultSet rs = ((ValueResultSet) v).getResultSet();
rs.beforeFirst();
ResultSetMetaData meta = rs.getMetaData();
int columnCount = meta.getColumnCount();
writeVarInt(columnCount);
for (int i = 0; i < columnCount; i++) {
writeString(meta.getColumnName(i + 1));
writeVarInt(meta.getColumnType(i + 1));
writeVarInt(meta.getPrecision(i + 1));
writeVarInt(meta.getScale(i + 1));
}
while (rs.next()) {
writeByte((byte) 1);
for (int i = 0; i < columnCount; i++) {
int t = DataType.getValueTypeFromResultSet(meta, i + 1);
Value val = DataType.readValue(null, rs, i + 1, t);
writeValue(val);
}
}
writeByte((byte) 0);
rs.beforeFirst();
} catch (SQLException e) {
throw DbException.convert(e);
}
break;
}
default:
DbException.throwInternalError("type=" + v.getType());
}
if (SysProperties.CHECK2) {
if (pos - start != getValueLen(v, handler)) {
throw DbException.throwInternalError("value size error: got " + (pos - start) + " expected " + getValueLen(v, handler));
}
}
}
use of org.h2.value.ValueLobDb in project h2database by h2database.
the class Recover method readBlobDb.
/**
* INTERNAL
*/
public static Value.ValueBlob readBlobDb(Connection conn, long lobId, long precision) {
DataHandler h = ((JdbcConnection) conn).getSession().getDataHandler();
verifyPageStore(h);
ValueLobDb lob = ValueLobDb.create(Value.BLOB, h, LobStorageFrontend.TABLE_TEMP, lobId, null, precision);
lob.setRecoveryReference(true);
return lob;
}
use of org.h2.value.ValueLobDb in project h2database by h2database.
the class Recover method getSQL.
private String getSQL(String column, Value v) {
if (v instanceof ValueLob) {
ValueLob lob = (ValueLob) v;
byte[] small = lob.getSmall();
if (small == null) {
String file = lob.getFileName();
String type = lob.getType() == Value.BLOB ? "BLOB" : "CLOB";
if (lob.isCompressed()) {
dumpLob(file, true);
file += ".comp";
}
return "READ_" + type + "('" + file + ".txt')";
}
} else if (v instanceof ValueLobDb) {
ValueLobDb lob = (ValueLobDb) v;
byte[] small = lob.getSmall();
if (small == null) {
int type = lob.getType();
long id = lob.getLobId();
long precision = lob.getPrecision();
String m;
String columnType;
if (type == Value.BLOB) {
columnType = "BLOB";
m = "READ_BLOB";
} else {
columnType = "CLOB";
m = "READ_CLOB";
}
if (lobMaps) {
m += "_MAP";
} else {
m += "_DB";
}
columnTypeMap.put(column, columnType);
return m + "(" + id + ", " + precision + ")";
}
}
return v.getSQL();
}
use of org.h2.value.ValueLobDb in project h2database by h2database.
the class Recover method readClobDb.
/**
* INTERNAL
*/
public static Value.ValueClob readClobDb(Connection conn, long lobId, long precision) {
DataHandler h = ((JdbcConnection) conn).getSession().getDataHandler();
verifyPageStore(h);
ValueLobDb lob = ValueLobDb.create(Value.CLOB, h, LobStorageFrontend.TABLE_TEMP, lobId, null, precision);
lob.setRecoveryReference(true);
return lob;
}
Aggregations