Unverified 提交 5365e664 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov 提交者: GitHub

Merge pull request #1118 from katzyn/tempResult

Add external storage of temporary results for MVStore mode
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.mvstore.db;
import org.h2.engine.Database;
import org.h2.expression.Expression;
import org.h2.message.DbException;
import org.h2.mvstore.Cursor;
import org.h2.mvstore.MVMap;
import org.h2.mvstore.MVMap.Builder;
import org.h2.result.ResultExternal;
import org.h2.value.Value;
import org.h2.value.ValueArray;
import org.h2.value.ValueLong;
/**
* Plain temporary result.
*/
class MVPlainTempResult extends MVTempResult {
/**
* The type of the values in the main map and keys in the index.
*/
private final ValueDataType valueType;
/**
* Map with identities of rows as keys rows as values.
*/
private final MVMap<ValueLong, ValueArray> map;
/**
* Counter for the identities of rows. A separate counter is used instead of
* {@link #rowCount} because rows due to presence of {@link #removeRow(Value[])}
* method to ensure that each row will have an own identity.
*/
private long counter;
/**
* Optional index. This index is created only if {@link #contains(Value[])}
* method is invoked. Only the root result should have an index if required.
*/
private MVMap<ValueArray, Boolean> index;
/**
* Cursor for the {@link #next()} method.
*/
private Cursor<ValueLong, ValueArray> cursor;
/**
* Creates a shallow copy of the result.
*
* @param parent
* parent result
*/
private MVPlainTempResult(MVPlainTempResult parent) {
super(parent);
this.valueType = null;
this.map = parent.map;
}
/**
* Creates a new plain temporary result.
*
* @param database
* database
* @param expressions
* column expressions
*/
MVPlainTempResult(Database database, Expression[] expressions) {
super(database);
ValueDataType keyType = new ValueDataType(null, null, null);
valueType = new ValueDataType(database.getCompareMode(), database, new int[expressions.length]);
Builder<ValueLong, ValueArray> builder = new MVMap.Builder<ValueLong, ValueArray>().keyType(keyType)
.valueType(valueType);
map = store.openMap("tmp", builder);
}
@Override
public int addRow(Value[] values) {
assert parent == null && index == null;
map.put(ValueLong.get(counter++), ValueArray.get(values));
return ++rowCount;
}
@Override
public boolean contains(Value[] values) {
// Only parent result maintains the index
if (parent != null) {
return parent.contains(values);
}
if (index == null) {
createIndex();
}
return index.containsKey(ValueArray.get(values));
}
private void createIndex() {
Builder<ValueArray, Boolean> builder = new MVMap.Builder<ValueArray, Boolean>().keyType(valueType);
index = store.openMap("idx", builder);
Cursor<ValueLong, ValueArray> c = map.cursor(null);
while (c.hasNext()) {
c.next();
index.putIfAbsent(c.getValue(), true);
}
}
@Override
public synchronized ResultExternal createShallowCopy() {
if (parent != null) {
return parent.createShallowCopy();
}
if (closed) {
return null;
}
childCount++;
return new MVPlainTempResult(this);
}
@Override
public Value[] next() {
if (cursor == null) {
cursor = map.cursor(null);
}
if (!cursor.hasNext()) {
return null;
}
cursor.next();
return cursor.getValue().getList();
}
@Override
public int removeRow(Value[] values) {
throw DbException.getUnsupportedException("removeRow()");
}
@Override
public void reset() {
cursor = null;
}
}
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.mvstore.db;
import java.util.BitSet;
import org.h2.engine.Database;
import org.h2.expression.Expression;
import org.h2.mvstore.Cursor;
import org.h2.mvstore.MVMap;
import org.h2.mvstore.MVMap.Builder;
import org.h2.result.ResultExternal;
import org.h2.result.SortOrder;
import org.h2.value.Value;
import org.h2.value.ValueArray;
/**
* Sorted temporary result.
*
* <p>
* This result is used for distinct and/or sorted results.
* </p>
*/
class MVSortedTempResult extends MVTempResult {
/**
* Whether this result is distinct.
*/
private final boolean distinct;
/**
* Mapping of indexes of columns to its positions in the store, or {@code null}
* if columns are not reordered.
*/
private final int[] indexes;
/**
* Map with rows as keys and counts of duplicate rows as values. If this map is
* distinct all values are 1.
*/
private final MVMap<ValueArray, Long> map;
/**
* Cursor for the {@link #next()} method.
*/
private Cursor<ValueArray, Long> cursor;
/**
* Current value for the {@link #next()} method. Used in non-distinct results
* with duplicate rows.
*/
private Value[] current;
/**
* Count of remaining duplicate rows for the {@link #next()} method. Used in
* non-distinct results.
*/
private long valueCount;
/**
* Creates a shallow copy of the result.
*
* @param parent
* parent result
*/
private MVSortedTempResult(MVSortedTempResult parent) {
super(parent);
this.distinct = parent.distinct;
this.indexes = parent.indexes;
this.map = parent.map;
this.rowCount = parent.rowCount;
}
/**
* Creates a new sorted temporary result.
*
* @param database
* database
* @param expressions
* column expressions
* @param distinct
* whether this result should be distinct
* @param sort
* sort order, or {@code null} if this result does not
* need any sorting
*/
MVSortedTempResult(Database database, Expression[] expressions, boolean distinct, SortOrder sort) {
super(database);
this.distinct = distinct;
int length = expressions.length;
int[] sortTypes = new int[length];
int[] indexes;
if (sort != null) {
/*
* If sorting is specified we need to reorder columns in requested order and set
* sort types (ASC, DESC etc) for them properly.
*/
indexes = new int[length];
int[] colIndex = sort.getQueryColumnIndexes();
int len = colIndex.length;
// This set is used to remember columns that are already included
BitSet used = new BitSet();
for (int i = 0; i < len; i++) {
int idx = colIndex[i];
assert !used.get(idx);
used.set(idx);
indexes[i] = idx;
sortTypes[i] = sort.getSortTypes()[i];
}
/*
* Because this result may have more columns than specified in sorting we need
* to add all remaining columns to the mapping of columns. A default sorting
* order (ASC / 0) will be used for them.
*/
int idx = 0;
for (int i = len; i < length; i++) {
idx = used.nextClearBit(idx);
indexes[i] = idx;
idx++;
}
/*
* Sometimes columns may be not reordered. Because reordering of columns
* slightly slows down other methods we check whether columns are really
* reordered or have the same order.
*/
sameOrder: {
for (int i = 0; i < length; i++) {
if (indexes[i] != i) {
// Columns are reordered
break sameOrder;
}
}
/*
* Columns are not reordered, set this field to null to disable reordering in
* other methods.
*/
indexes = null;
}
} else {
// Columns are not reordered if sort order is not specified
indexes = null;
}
this.indexes = indexes;
ValueDataType keyType = new ValueDataType(database.getCompareMode(), database, sortTypes);
Builder<ValueArray, Long> builder = new MVMap.Builder<ValueArray, Long>().keyType(keyType);
map = store.openMap("tmp", builder);
}
@Override
public int addRow(Value[] values) {
assert parent == null;
ValueArray key = getKey(values);
if (distinct) {
// Add a row and increment the counter only if row does not exist
if (map.putIfAbsent(key, 1L) == null) {
rowCount++;
}
} else {
// Try to set counter to 1 first if such row does not exist yet
Long old = map.putIfAbsent(key, 1L);
if (old != null) {
// This rows is already in the map, increment its own counter
map.put(key, old + 1);
}
rowCount++;
}
return rowCount;
}
@Override
public boolean contains(Value[] values) {
return map.containsKey(getKey(values));
}
@Override
public synchronized ResultExternal createShallowCopy() {
if (parent != null) {
return parent.createShallowCopy();
}
if (closed) {
return null;
}
childCount++;
return new MVSortedTempResult(this);
}
/**
* Reorder values if required and convert them into {@link ValueArray}.
*
* @param values
* values
* @return ValueArray for maps
*/
private ValueArray getKey(Value[] values) {
if (indexes != null) {
Value[] r = new Value[indexes.length];
for (int i = 0; i < indexes.length; i++) {
r[indexes[i]] = values[i];
}
values = r;
}
return ValueArray.get(values);
}
/**
* Reorder values back if required.
*
* @param key
* reordered values
* @return original values
*/
private Value[] getValue(Value[] key) {
if (indexes != null) {
Value[] r = new Value[indexes.length];
for (int i = 0; i < indexes.length; i++) {
r[i] = key[indexes[i]];
}
key = r;
}
return key;
}
@Override
public Value[] next() {
if (cursor == null) {
cursor = map.cursor(null);
current = null;
valueCount = 0L;
}
// If we have multiple rows with the same values return them all
if (--valueCount > 0) {
/*
* Underflow in valueCount is hypothetically possible after a lot of invocations
* (not really possible in practice), but current will be null anyway.
*/
return current;
}
if (!cursor.hasNext()) {
// Set current to null to be sure
current = null;
return null;
}
// Read the next row
current = getValue(cursor.next().getList());
/*
* If valueCount is greater than 1 that is possible for non-distinct results the
* following invocations of next() will use this.current and this.valueCount.
*/
valueCount = cursor.getValue();
return current;
}
@Override
public int removeRow(Value[] values) {
assert parent == null;
ValueArray key = getKey(values);
if (distinct) {
// If an entry was removed decrement the counter
if (map.remove(key) != null) {
rowCount--;
}
} else {
Long old = map.remove(key);
if (old != null) {
long l = old;
if (l > 1) {
/*
* We have more than one such row. Decrement its counter by 1 and put this row
* back into map.
*/
map.put(key, l - 1);
}
rowCount--;
}
}
return rowCount;
}
@Override
public void reset() {
cursor = null;
current = null;
valueCount = 0L;
}
}
...@@ -75,12 +75,7 @@ public class MVTableEngine implements TableEngine { ...@@ -75,12 +75,7 @@ public class MVTableEngine implements TableEngine {
} }
if (key != null) { if (key != null) {
encrypted = true; encrypted = true;
char[] password = new char[key.length / 2]; builder.encryptionKey(decodePassword(key));
for (int i = 0; i < password.length; i++) {
password[i] = (char) (((key[i + i] & 255) << 16) |
((key[i + i + 1]) & 255));
}
builder.encryptionKey(password);
} }
if (db.getSettings().compressData) { if (db.getSettings().compressData) {
builder.compress(); builder.compress();
...@@ -101,6 +96,15 @@ public class MVTableEngine implements TableEngine { ...@@ -101,6 +96,15 @@ public class MVTableEngine implements TableEngine {
return store; return store;
} }
static char[] decodePassword(byte[] key) {
char[] password = new char[key.length / 2];
for (int i = 0; i < password.length; i++) {
password[i] = (char) (((key[i + i] & 255) << 16) |
((key[i + i + 1]) & 255));
}
return password;
}
@Override @Override
public TableBase createTable(CreateTableData data) { public TableBase createTable(CreateTableData data) {
Database db = data.session.getDatabase(); Database db = data.session.getDatabase();
......
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.mvstore.db;
import java.io.IOException;
import java.lang.ref.Reference;
import java.util.ArrayList;
import org.h2.engine.Constants;
import org.h2.engine.Database;
import org.h2.expression.Expression;
import org.h2.message.DbException;
import org.h2.mvstore.MVStore;
import org.h2.mvstore.MVStore.Builder;
import org.h2.result.ResultExternal;
import org.h2.result.SortOrder;
import org.h2.store.fs.FileUtils;
import org.h2.util.TempFileDeleter;
import org.h2.value.Value;
/**
* Temporary result.
*
* <p>
* A separate MVStore in a temporary file is used for each result. The file is
* removed when this result and all its copies are closed.
* {@link TempFileDeleter} is also used to delete this file if results are not
* closed properly.
* </p>
*/
public abstract class MVTempResult implements ResultExternal {
private static final class CloseImpl implements AutoCloseable {
/**
* MVStore.
*/
private final MVStore store;
/**
* File name.
*/
private final String fileName;
CloseImpl(MVStore store, String fileName) {
this.store = store;
this.fileName = fileName;
}
@Override
public void close() throws Exception {
store.closeImmediately();
FileUtils.tryDelete(fileName);
}
}
/**
* Creates MVStore-based temporary result.
*
* @param database
* database
* @param expressions
* expressions
* @param distinct
* is output distinct
* @param sort
* sort order, or {@code null}
* @return temporary result
*/
public static ResultExternal of(Database database, Expression[] expressions, boolean distinct, SortOrder sort) {
return distinct || sort != null ? new MVSortedTempResult(database, expressions, distinct, sort)
: new MVPlainTempResult(database, expressions);
}
/**
* MVStore.
*/
final MVStore store;
/**
* Count of rows. Used only in a root results, copies always have 0 value.
*/
int rowCount;
/**
* Parent store for copies. If {@code null} this result is a root result.
*/
final MVTempResult parent;
/**
* Count of child results.
*/
int childCount;
/**
* Whether this result is closed.
*/
boolean closed;
/**
* Temporary file deleter.
*/
private final TempFileDeleter tempFileDeleter;
/**
* Closeable to close the storage.
*/
private final CloseImpl closeable;
/**
* Reference to the record in the temporary file deleter.
*/
private final Reference<?> fileRef;
/**
* Creates a shallow copy of the result.
*
* @param parent
* parent result
*/
MVTempResult(MVTempResult parent) {
this.parent = parent;
this.store = parent.store;
this.tempFileDeleter = null;
this.closeable = null;
this.fileRef = null;
}
/**
* Creates a new temporary result.
*
* @param database
* database
*/
MVTempResult(Database database) {
try {
String fileName = FileUtils.createTempFile("h2tmp", Constants.SUFFIX_TEMP_FILE, false, true);
Builder builder = new MVStore.Builder().fileName(fileName);
byte[] key = database.getFileEncryptionKey();
if (key != null) {
builder.encryptionKey(MVTableEngine.decodePassword(key));
}
store = builder.open();
tempFileDeleter = database.getTempFileDeleter();
closeable = new CloseImpl(store, fileName);
fileRef = tempFileDeleter.addFile(closeable, this);
} catch (IOException e) {
throw DbException.convert(e);
}
parent = null;
}
@Override
public int addRows(ArrayList<Value[]> rows) {
for (Value[] row : rows) {
addRow(row);
}
return rowCount;
}
@Override
public synchronized void close() {
if (closed) {
return;
}
closed = true;
if (parent != null) {
parent.closeChild();
} else {
if (childCount == 0) {
delete();
}
}
}
private synchronized void closeChild() {
if (--childCount == 0 && closed) {
delete();
}
}
private void delete() {
tempFileDeleter.deleteFile(fileRef, closeable);
}
@Override
public void done() {
// Do nothing
}
}
...@@ -15,6 +15,7 @@ import org.h2.engine.Session; ...@@ -15,6 +15,7 @@ import org.h2.engine.Session;
import org.h2.engine.SessionInterface; import org.h2.engine.SessionInterface;
import org.h2.expression.Expression; import org.h2.expression.Expression;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.mvstore.db.MVTempResult;
import org.h2.util.Utils; import org.h2.util.Utils;
import org.h2.util.ValueHashMap; import org.h2.util.ValueHashMap;
import org.h2.value.DataType; import org.h2.value.DataType;
...@@ -290,7 +291,10 @@ public class LocalResult implements ResultInterface, ResultTarget { ...@@ -290,7 +291,10 @@ public class LocalResult implements ResultInterface, ResultTarget {
} }
private void createExternalResult() { private void createExternalResult() {
external = new ResultTempTable(session, expressions, distinct, sort); Database database = session.getDatabase();
external = database.getMvStore() != null
? MVTempResult.of(database, expressions, distinct, sort)
: new ResultTempTable(session, expressions, distinct, sort);
} }
/** /**
......
...@@ -278,19 +278,7 @@ public class ResultTempTable implements ResultExternal { ...@@ -278,19 +278,7 @@ public class ResultTempTable implements ResultExternal {
} else { } else {
idx = table.getScanIndex(session); idx = table.getScanIndex(session);
} }
if (session.getDatabase().getMvStore() != null) { resultCursor = idx.find(session, null, null);
// sometimes the transaction is already committed,
// in which case we can't use the session
if (idx.getRowCount(session) == 0 && rowCount > 0) {
// this means querying is not transactional
resultCursor = idx.find((Session) null, null, null);
} else {
// the transaction is still open
resultCursor = idx.find(session, null, null);
}
} else {
resultCursor = idx.find(session, null, null);
}
} }
if (!resultCursor.next()) { if (!resultCursor.next()) {
return null; return null;
......
...@@ -21,7 +21,7 @@ import org.h2.store.fs.FileUtils; ...@@ -21,7 +21,7 @@ import org.h2.store.fs.FileUtils;
public class TempFileDeleter { public class TempFileDeleter {
private final ReferenceQueue<Object> queue = new ReferenceQueue<>(); private final ReferenceQueue<Object> queue = new ReferenceQueue<>();
private final HashMap<PhantomReference<?>, String> refMap = new HashMap<>(); private final HashMap<PhantomReference<?>, Object> refMap = new HashMap<>();
private TempFileDeleter() { private TempFileDeleter() {
// utility class // utility class
...@@ -32,43 +32,59 @@ public class TempFileDeleter { ...@@ -32,43 +32,59 @@ public class TempFileDeleter {
} }
/** /**
* Add a file to the list of temp files to delete. The file is deleted once * Add a file or a closeable to the list of temporary objects to delete. The
* the file object is garbage collected. * file is deleted once the file object is garbage collected.
* *
* @param fileName the file name * @param resource the file name or the closeable
* @param file the object to monitor * @param monitor the object to monitor
* @return the reference that can be used to stop deleting the file * @return the reference that can be used to stop deleting the file or closing the closeable
*/ */
public synchronized Reference<?> addFile(String fileName, Object file) { public synchronized Reference<?> addFile(Object resource, Object monitor) {
IOUtils.trace("TempFileDeleter.addFile", fileName, file); if (!(resource instanceof String) && !(resource instanceof AutoCloseable)) {
PhantomReference<?> ref = new PhantomReference<>(file, queue); throw DbException.getUnsupportedException("Unsupported resource " + resource);
refMap.put(ref, fileName); }
IOUtils.trace("TempFileDeleter.addFile",
resource instanceof String ? (String) resource : "-", monitor);
PhantomReference<?> ref = new PhantomReference<>(monitor, queue);
refMap.put(ref, resource);
deleteUnused(); deleteUnused();
return ref; return ref;
} }
/** /**
* Delete the given file now. This will remove the reference from the list. * Delete the given file or close the closeable now. This will remove the
* reference from the list.
* *
* @param ref the reference as returned by addFile * @param ref the reference as returned by addFile
* @param fileName the file name * @param resource the file name or closeable
*/ */
public synchronized void deleteFile(Reference<?> ref, String fileName) { public synchronized void deleteFile(Reference<?> ref, Object resource) {
if (ref != null) { if (ref != null) {
String f2 = refMap.remove(ref); Object f2 = refMap.remove(ref);
if (f2 != null) { if (f2 != null) {
if (SysProperties.CHECK) { if (SysProperties.CHECK) {
if (fileName != null && !f2.equals(fileName)) { if (resource != null && !f2.equals(resource)) {
DbException.throwInternalError("f2:" + f2 + " f:" + fileName); DbException.throwInternalError("f2:" + f2 + " f:" + resource);
} }
} }
fileName = f2; resource = f2;
} }
} }
if (fileName != null && FileUtils.exists(fileName)) { if (resource instanceof String) {
String fileName = (String) resource;
if (FileUtils.exists(fileName)) {
try {
IOUtils.trace("TempFileDeleter.deleteFile", fileName, null);
FileUtils.tryDelete(fileName);
} catch (Exception e) {
// TODO log such errors?
}
}
} else if (resource instanceof AutoCloseable) {
AutoCloseable closeable = (AutoCloseable) resource;
try { try {
IOUtils.trace("TempFileDeleter.deleteFile", fileName, null); IOUtils.trace("TempFileDeleter.deleteCloseable", "-", null);
FileUtils.tryDelete(fileName); closeable.close();
} catch (Exception e) { } catch (Exception e) {
// TODO log such errors? // TODO log such errors?
} }
...@@ -76,17 +92,17 @@ public class TempFileDeleter { ...@@ -76,17 +92,17 @@ public class TempFileDeleter {
} }
/** /**
* Delete all registered temp files. * Delete all registered temp resources.
*/ */
public void deleteAll() { public void deleteAll() {
for (String tempFile : new ArrayList<>(refMap.values())) { for (Object resource : new ArrayList<>(refMap.values())) {
deleteFile(null, tempFile); deleteFile(null, resource);
} }
deleteUnused(); deleteUnused();
} }
/** /**
* Delete all unused files now. * Delete all unused resources now.
*/ */
public void deleteUnused() { public void deleteUnused() {
while (queue != null) { while (queue != null) {
...@@ -99,20 +115,21 @@ public class TempFileDeleter { ...@@ -99,20 +115,21 @@ public class TempFileDeleter {
} }
/** /**
* This method is called if a file should no longer be deleted if the object * This method is called if a file should no longer be deleted or a resource
* is garbage collected. * should no longer be closed if the object is garbage collected.
* *
* @param ref the reference as returned by addFile * @param ref the reference as returned by addFile
* @param fileName the file name * @param resource file name or closeable
*/ */
public void stopAutoDelete(Reference<?> ref, String fileName) { public void stopAutoDelete(Reference<?> ref, Object resource) {
IOUtils.trace("TempFileDeleter.stopAutoDelete", fileName, ref); IOUtils.trace("TempFileDeleter.stopAutoDelete",
resource instanceof String ? (String) resource : "-", ref);
if (ref != null) { if (ref != null) {
String f2 = refMap.remove(ref); Object f2 = refMap.remove(ref);
if (SysProperties.CHECK) { if (SysProperties.CHECK) {
if (f2 == null || !f2.equals(fileName)) { if (f2 == null || !f2.equals(resource)) {
DbException.throwInternalError("f2:" + f2 + DbException.throwInternalError("f2:" + f2 +
" " + (f2 == null ? "" : f2) + " f:" + fileName); " " + (f2 == null ? "" : f2) + " f:" + resource);
} }
} }
} }
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
*/ */
package org.h2.test.db; package org.h2.test.db;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
...@@ -12,6 +14,7 @@ import java.sql.SQLException; ...@@ -12,6 +14,7 @@ import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.sql.Types; import java.sql.Types;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet; import java.util.BitSet;
import org.h2.store.FileLister; import org.h2.store.FileLister;
...@@ -38,6 +41,7 @@ public class TestBigResult extends TestBase { ...@@ -38,6 +41,7 @@ public class TestBigResult extends TestBase {
} }
testLargeSubquery(); testLargeSubquery();
testSortingAndDistinct(); testSortingAndDistinct();
testLOB();
testLargeUpdateDelete(); testLargeUpdateDelete();
testCloseConnectionDelete(); testCloseConnectionDelete();
testOrderGroup(); testOrderGroup();
...@@ -289,6 +293,66 @@ public class TestBigResult extends TestBase { ...@@ -289,6 +293,66 @@ public class TestBigResult extends TestBase {
assertFalse(rs.next()); assertFalse(rs.next());
} }
private void testLOB() throws SQLException {
deleteDb("bigResult");
Connection conn = getConnection("bigResult");
Statement stat = conn.createStatement();
stat.execute("SET MAX_MEMORY_ROWS " + 1);
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, VALUE BLOB NOT NULL)");
PreparedStatement ps = conn.prepareStatement("INSERT INTO TEST VALUES (?, ?)");
int length = 1_000_000;
byte[] data = new byte[length];
for (int i = 1; i <= 10; i++) {
ps.setInt(1, i);
Arrays.fill(data, (byte) i);
ps.setBytes(2, data);
ps.executeUpdate();
}
Blob[] blobs = new Blob[10];
ResultSet rs = stat.executeQuery("SELECT * FROM TEST");
for (int i = 1; i <= 10; i++) {
assertTrue(rs.next());
assertEquals(i, rs.getInt(1));
blobs[i - 1] = rs.getBlob(2);
}
assertFalse(rs.next());
rs.close();
for (int i = 1; i <= 10; i++) {
Blob b = blobs[i - 1];
byte[] bytes = b.getBytes(1, (int) b.length());
Arrays.fill(data, (byte) i);
assertEquals(data, bytes);
b.free();
}
stat.execute("DROP TABLE TEST");
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, VALUE CLOB NOT NULL)");
ps = conn.prepareStatement("INSERT INTO TEST VALUES (?, ?)");
char[] cdata = new char[length];
for (int i = 1; i <= 10; i++) {
ps.setInt(1, i);
Arrays.fill(cdata, (char) i);
ps.setString(2, new String(cdata));
ps.executeUpdate();
}
Clob[] clobs = new Clob[10];
rs = stat.executeQuery("SELECT * FROM TEST");
for (int i = 1; i <= 10; i++) {
assertTrue(rs.next());
assertEquals(i, rs.getInt(1));
clobs[i - 1] = rs.getClob(2);
}
assertFalse(rs.next());
rs.close();
for (int i = 1; i <= 10; i++) {
Clob c = clobs[i - 1];
String string = c.getSubString(1, (int) c.length());
Arrays.fill(cdata, (char) i);
assertEquals(new String(cdata), string);
c.free();
}
conn.close();
}
private void testLargeUpdateDelete() throws SQLException { private void testLargeUpdateDelete() throws SQLException {
deleteDb("bigResult"); deleteDb("bigResult");
Connection conn = getConnection("bigResult"); Connection conn = getConnection("bigResult");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论