Unverified 提交 11b7e7cd authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #1233 from grandinj/simplify_old_lob

Simplify old lob ValueLob class
......@@ -584,7 +584,6 @@ public class Data {
writeByte((byte) type);
if (v instanceof ValueLob) {
ValueLob lob = (ValueLob) v;
lob.convertToFileIfRequired(handler);
byte[] small = lob.getSmall();
if (small == null) {
int t = -1;
......@@ -1013,7 +1012,6 @@ public class Data {
int len = 1;
if (v instanceof ValueLob) {
ValueLob lob = (ValueLob) v;
lob.convertToFileIfRequired(handler);
byte[] small = lob.getSmall();
if (small == null) {
int t = -1;
......
......@@ -214,7 +214,7 @@ public class Recover extends Tool implements DataHandler {
/**
* INTERNAL
*/
public static Value.ValueBlob readBlobDb(Connection conn, long lobId,
public static ValueLobDb readBlobDb(Connection conn, long lobId,
long precision) {
DataHandler h = ((JdbcConnection) conn).getSession().getDataHandler();
verifyPageStore(h);
......@@ -235,7 +235,7 @@ public class Recover extends Tool implements DataHandler {
/**
* INTERNAL
*/
public static Value.ValueClob readClobDb(Connection conn, long lobId,
public static ValueLobDb readClobDb(Connection conn, long lobId,
long precision) {
DataHandler h = ((JdbcConnection) conn).getSession().getDataHandler();
verifyPageStore(h);
......
......@@ -995,10 +995,11 @@ public class DataType {
return Value.DECIMAL;
} else if (ResultSet.class.isAssignableFrom(x)) {
return Value.RESULT_SET;
} else if (Value.ValueBlob.class.isAssignableFrom(x)) {
} else if (ValueLobDb.class.isAssignableFrom(x)) {
return Value.BLOB;
} else if (Value.ValueClob.class.isAssignableFrom(x)) {
return Value.CLOB;
// FIXME no way to distinguish between these 2 types
// } else if (ValueLobDb.class.isAssignableFrom(x)) {
// return Value.CLOB;
} else if (Date.class.isAssignableFrom(x)) {
return Value.DATE;
} else if (Time.class.isAssignableFrom(x)) {
......
......@@ -1395,18 +1395,4 @@ public abstract class Value {
return null;
}
/**
* A "binary large object".
*/
public interface ValueClob {
// this is a marker interface
}
/**
* A "character large object".
*/
public interface ValueBlob {
// this is a marker interface
}
}
......@@ -39,10 +39,12 @@ import org.h2.util.Utils;
* Small objects are kept in memory and stored in the record.
* Large objects are either stored in the database, or in temporary files.
*/
public class ValueLobDb extends Value implements Value.ValueClob,
Value.ValueBlob {
public class ValueLobDb extends Value {
private final int type;
/**
* the value type (Value.BLOB or CLOB)
*/
private final int valueType;
private final long lobId;
private final byte[] hmac;
private final byte[] small;
......@@ -66,7 +68,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
private ValueLobDb(int type, DataHandler handler, int tableId, long lobId,
byte[] hmac, long precision) {
this.type = type;
this.valueType = type;
this.handler = handler;
this.tableId = tableId;
this.lobId = lobId;
......@@ -78,7 +80,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
}
private ValueLobDb(int type, byte[] small, long precision) {
this.type = type;
this.valueType = type;
this.small = small;
this.precision = precision;
this.lobId = 0;
......@@ -94,7 +96,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
*/
private ValueLobDb(DataHandler handler, Reader in, long remaining)
throws IOException {
this.type = Value.CLOB;
this.valueType = Value.CLOB;
this.handler = handler;
this.small = null;
this.lobId = 0;
......@@ -126,7 +128,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
*/
private ValueLobDb(DataHandler handler, byte[] buff, int len, InputStream in,
long remaining) throws IOException {
this.type = Value.BLOB;
this.valueType = Value.BLOB;
this.handler = handler;
this.small = null;
this.lobId = 0;
......@@ -167,7 +169,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
/**
* Create a LOB value.
*
* @param type the type
* @param type the type (Value.BLOB or CLOB)
* @param handler the data handler
* @param tableId the table id
* @param id the lob id
......@@ -194,7 +196,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
*/
@Override
public Value convertTo(int t, int precision, Mode mode, Object column, String[] enumerators) {
if (t == type) {
if (t == valueType) {
return this;
} else if (t == Value.CLOB) {
if (handler != null) {
......@@ -248,7 +250,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
} else if (small.length > database.getMaxLengthInplaceLob()) {
LobStorageInterface s = database.getLobStorage();
Value v;
if (type == Value.BLOB) {
if (valueType == Value.BLOB) {
v = s.createBlob(getInputStream(), getPrecision());
} else {
v = s.createClob(getReader(), getPrecision());
......@@ -272,7 +274,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
@Override
public int getType() {
return type;
return valueType;
}
@Override
......@@ -285,7 +287,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
int len = precision > Integer.MAX_VALUE || precision == 0 ?
Integer.MAX_VALUE : (int) precision;
try {
if (type == Value.CLOB) {
if (valueType == Value.CLOB) {
if (small != null) {
return new String(small, StandardCharsets.UTF_8);
}
......@@ -305,7 +307,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
@Override
public byte[] getBytes() {
if (type == CLOB) {
if (valueType == CLOB) {
// convert hex to string
return super.getBytes();
}
......@@ -315,7 +317,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
@Override
public byte[] getBytesNoCopy() {
if (type == CLOB) {
if (valueType == CLOB) {
// convert hex to string
return super.getBytesNoCopy();
}
......@@ -337,7 +339,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
// it in the database file
return (int) (precision ^ (precision >>> 32));
}
if (type == CLOB) {
if (valueType == CLOB) {
hash = getString().hashCode();
} else {
hash = Utils.getByteArrayHash(getBytes());
......@@ -357,7 +359,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
return 0;
}
}
if (type == Value.CLOB) {
if (valueType == Value.CLOB) {
return Integer.signum(getString().compareTo(v.getString()));
}
byte[] v2 = v.getBytesNoCopy();
......@@ -366,7 +368,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
@Override
public Object getObject() {
if (type == Value.CLOB) {
if (valueType == Value.CLOB) {
return getReader();
}
return getInputStream();
......@@ -379,7 +381,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
@Override
public Reader getReader(long oneBasedOffset, long length) {
return ValueLob.rangeReader(getReader(), oneBasedOffset, length, type == Value.CLOB ? precision : -1);
return ValueLob.rangeReader(getReader(), oneBasedOffset, length, valueType == Value.CLOB ? precision : -1);
}
@Override
......@@ -392,7 +394,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
return new BufferedInputStream(new FileStoreInputStream(store,
handler, false, alwaysClose), Constants.IO_BUFFER_SIZE);
}
long byteCount = (type == Value.BLOB) ? precision : -1;
long byteCount = (valueType == Value.BLOB) ? precision : -1;
try {
return handler.getLobStorage().getInputStream(this, hmac, byteCount);
} catch (IOException e) {
......@@ -413,7 +415,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
inputStream = new BufferedInputStream(new FileStoreInputStream(store,
handler, false, alwaysClose), Constants.IO_BUFFER_SIZE);
} else {
byteCount = (type == Value.BLOB) ? precision : -1;
byteCount = (valueType == Value.BLOB) ? precision : -1;
try {
inputStream = handler.getLobStorage().getInputStream(this, hmac, byteCount);
} catch (IOException e) {
......@@ -430,7 +432,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
if (p > Integer.MAX_VALUE || p <= 0) {
p = -1;
}
if (type == Value.BLOB) {
if (valueType == Value.BLOB) {
prep.setBinaryStream(parameterIndex, getInputStream(), (int) p);
} else {
prep.setCharacterStream(parameterIndex, getReader(), (int) p);
......@@ -440,7 +442,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
@Override
public String getSQL() {
String s;
if (type == Value.CLOB) {
if (valueType == Value.CLOB) {
s = getString();
return StringUtils.quoteStringSQL(s);
}
......@@ -455,7 +457,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
return getSQL();
}
StringBuilder buff = new StringBuilder();
if (type == Value.CLOB) {
if (valueType == Value.CLOB) {
buff.append("SPACE(").append(getPrecision());
} else {
buff.append("CAST(REPEAT('00', ").append(getPrecision()).append(") AS BINARY");
......@@ -650,13 +652,13 @@ public class ValueLobDb extends Value implements Value.ValueClob,
return this;
}
ValueLobDb lob;
if (type == CLOB) {
if (valueType == CLOB) {
if (handler == null) {
try {
int p = MathUtils.convertLongToInt(precision);
String s = IOUtils.readStringAndClose(getReader(), p);
byte[] data = s.getBytes(StandardCharsets.UTF_8);
lob = ValueLobDb.createSmallLob(type, data, s.length());
lob = ValueLobDb.createSmallLob(valueType, data, s.length());
} catch (IOException e) {
throw DbException.convertIOException(e, null);
}
......@@ -668,7 +670,7 @@ public class ValueLobDb extends Value implements Value.ValueClob,
try {
int p = MathUtils.convertLongToInt(precision);
byte[] data = IOUtils.readBytesAndClose(getInputStream(), p);
lob = ValueLobDb.createSmallLob(type, data, data.length);
lob = ValueLobDb.createSmallLob(valueType, data, data.length);
} catch (IOException e) {
throw DbException.convertIOException(e, null);
}
......
......@@ -265,8 +265,9 @@ public class TestValue extends TestDb {
testDataType(Value.NULL, Void.class);
testDataType(Value.DECIMAL, BigDecimal.class);
testDataType(Value.RESULT_SET, ResultSet.class);
testDataType(Value.BLOB, Value.ValueBlob.class);
testDataType(Value.CLOB, Value.ValueClob.class);
testDataType(Value.BLOB, ValueLobDb.class);
// see FIXME in DataType.getTypeFromClass
//testDataType(Value.CLOB, Value.ValueClob.class);
testDataType(Value.DATE, Date.class);
testDataType(Value.TIME, Time.class);
testDataType(Value.TIMESTAMP, Timestamp.class);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论