use of org.h2.command.dml.Replace in project h2database by h2database.
the class Page method setKey.
/**
* Replace the key at an index in this page.
*
* @param index the index
* @param key the new key
*/
public void setKey(int index, Object key) {
// this is slightly slower:
// keys = Arrays.copyOf(keys, keys.length);
keys = keys.clone();
if (isPersistent()) {
Object old = keys[index];
DataType keyType = map.getKeyType();
int mem = keyType.getMemory(key);
if (old != null) {
mem -= keyType.getMemory(old);
}
addMemory(mem);
}
keys[index] = key;
}
use of org.h2.command.dml.Replace in project h2database by h2database.
the class TestValue method testCastTrim.
private void testCastTrim() {
Value v;
String spaces = new String(new char[100]).replace((char) 0, ' ');
v = ValueArray.get(new Value[] { ValueString.get("hello"), ValueString.get("world") });
assertEquals(10, v.getPrecision());
assertEquals(5, v.convertPrecision(5, true).getPrecision());
v = ValueArray.get(new Value[] { ValueString.get(""), ValueString.get("") });
assertEquals(0, v.getPrecision());
assertEquals("('')", v.convertPrecision(1, true).toString());
v = ValueBytes.get(spaces.getBytes());
assertEquals(100, v.getPrecision());
assertEquals(10, v.convertPrecision(10, false).getPrecision());
assertEquals(10, v.convertPrecision(10, false).getBytes().length);
assertEquals(32, v.convertPrecision(10, false).getBytes()[9]);
assertEquals(10, v.convertPrecision(10, true).getPrecision());
final Value vd = ValueDecimal.get(new BigDecimal("1234567890.123456789"));
assertEquals(19, vd.getPrecision());
assertEquals("1234567890.1234567", vd.convertPrecision(10, true).getString());
new AssertThrows(ErrorCode.NUMERIC_VALUE_OUT_OF_RANGE_1) {
@Override
public void test() {
vd.convertPrecision(10, false);
}
};
v = ValueLobDb.createSmallLob(Value.CLOB, spaces.getBytes(), 100);
assertEquals(100, v.getPrecision());
assertEquals(10, v.convertPrecision(10, false).getPrecision());
assertEquals(10, v.convertPrecision(10, false).getString().length());
assertEquals(" ", v.convertPrecision(10, false).getString());
assertEquals(10, v.convertPrecision(10, true).getPrecision());
v = ValueLobDb.createSmallLob(Value.BLOB, spaces.getBytes(), 100);
assertEquals(100, v.getPrecision());
assertEquals(10, v.convertPrecision(10, false).getPrecision());
assertEquals(10, v.convertPrecision(10, false).getBytes().length);
assertEquals(32, v.convertPrecision(10, false).getBytes()[9]);
assertEquals(10, v.convertPrecision(10, true).getPrecision());
ResultSet rs = new SimpleResultSet();
v = ValueResultSet.get(rs);
assertEquals(Integer.MAX_VALUE, v.getPrecision());
assertEquals(Integer.MAX_VALUE, v.convertPrecision(10, false).getPrecision());
assertTrue(rs == v.convertPrecision(10, false).getObject());
assertFalse(rs == v.convertPrecision(10, true).getObject());
assertEquals(Integer.MAX_VALUE, v.convertPrecision(10, true).getPrecision());
v = ValueString.get(spaces);
assertEquals(100, v.getPrecision());
assertEquals(10, v.convertPrecision(10, false).getPrecision());
assertEquals(" ", v.convertPrecision(10, false).getString());
assertEquals(" ", v.convertPrecision(10, true).getString());
}
use of org.h2.command.dml.Replace in project h2database by h2database.
the class TestObjectDataType method testCommonValues.
private void testCommonValues() {
BigInteger largeBigInt = BigInteger.probablePrime(200, new Random(1));
ObjectDataType ot = new ObjectDataType();
Object[] array = { false, true, Byte.MIN_VALUE, (byte) -1, (byte) 0, (byte) 1, Byte.MAX_VALUE, Short.MIN_VALUE, (short) -1, (short) 0, (short) 1, Short.MAX_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE + 1, -1000, -100, -1, 0, 1, 2, 14, 15, 16, 17, 100, Integer.MAX_VALUE - 1, Integer.MAX_VALUE, Long.MIN_VALUE, Long.MIN_VALUE + 1, -1000L, -1L, 0L, 1L, 2L, 14L, 15L, 16L, 17L, 100L, Long.MAX_VALUE - 1, Long.MAX_VALUE, largeBigInt.negate(), BigInteger.valueOf(-1), BigInteger.ZERO, BigInteger.ONE, BigInteger.TEN, largeBigInt, Float.NEGATIVE_INFINITY, -Float.MAX_VALUE, -1f, -0f, 0f, Float.MIN_VALUE, 1f, Float.MAX_VALUE, Float.POSITIVE_INFINITY, Float.NaN, Double.NEGATIVE_INFINITY, -Double.MAX_VALUE, -1d, -0d, 0d, Double.MIN_VALUE, 1d, Double.MAX_VALUE, Double.POSITIVE_INFINITY, Double.NaN, BigDecimal.valueOf(Double.MAX_VALUE).negate(), new BigDecimal(largeBigInt).negate(), BigDecimal.valueOf(-100.0), BigDecimal.ZERO, BigDecimal.ONE, BigDecimal.TEN, BigDecimal.valueOf(Long.MAX_VALUE), new BigDecimal(largeBigInt), BigDecimal.valueOf(Double.MAX_VALUE), Character.MIN_VALUE, '0', 'a', Character.MAX_VALUE, "", " ", " ", "123456789012345", "1234567890123456", new String(new char[100]).replace((char) 0, 'x'), new String(new char[100000]).replace((char) 0, 'x'), "y", "\u1234", "\u2345", "\u6789", "\uffff", new UUID(Long.MIN_VALUE, Long.MIN_VALUE), new UUID(Long.MIN_VALUE, 0), new UUID(0, 0), new UUID(Long.MAX_VALUE, Long.MAX_VALUE), new java.util.Date(0), new java.util.Date(1000), new java.util.Date(4000), new java.util.Date(5000), new boolean[0], new boolean[] { false, false }, new boolean[] { true }, new byte[0], new byte[1], new byte[15], new byte[16], new byte[10000], new byte[] { (byte) 1 }, new byte[] { (byte) 0xff }, new short[0], new short[] { -1 }, new short[] { 1 }, new char[0], new char[1], new char[10000], new char[] { (char) 1 }, new int[0], new int[1], new int[15], new int[16], new int[10000], new int[] { (byte) 1 }, new long[0], new long[1], new long[15], new long[16], new long[10000], new long[] { (byte) 1 }, new float[0], new float[] { Float.NEGATIVE_INFINITY }, new float[1], new float[] { Float.POSITIVE_INFINITY }, new double[0], new double[] { Double.NEGATIVE_INFINITY }, new double[1], new double[] { Double.POSITIVE_INFINITY }, new Object[0], new Object[100], new Object[] { 1 }, new Object[] { 0.0, "Hello", null, Double.NaN }, new String[] { "Hello", null }, new String[] { "World" }, new java.sql.Date[] {}, new Timestamp[] {}, new Timestamp[] { null }, new Timestamp(2000), new Timestamp(3000) };
Object otherType = false;
Object last = null;
for (Object x : array) {
test(otherType, x);
if (last != null) {
int comp = ot.compare(x, last);
if (comp <= 0) {
ot.compare(x, last);
fail(x.getClass().getSimpleName() + ": " + x.toString() + " " + comp);
}
assertTrue(x.toString(), ot.compare(last, x) < 0);
}
if (last != null && last.getClass() != x.getClass()) {
otherType = last;
}
last = x;
}
Random r = new Random(1);
for (int i = 0; i < 1000; i++) {
Object x = array[r.nextInt(array.length)];
Object y = array[r.nextInt(array.length)];
int comp = ot.compare(x, y);
if (comp != 0) {
assertEquals("x:" + x + " y:" + y, -comp, ot.compare(y, x));
}
}
}
use of org.h2.command.dml.Replace in project h2database by h2database.
the class TestMVStore method testAtomicOperations.
private void testAtomicOperations() {
String fileName = getBaseDir() + "/" + getTestName();
FileUtils.delete(fileName);
MVStore s;
MVMap<Integer, byte[]> m;
s = new MVStore.Builder().fileName(fileName).open();
m = s.openMap("data");
// putIfAbsent
assertNull(m.putIfAbsent(1, new byte[1]));
assertEquals(1, m.putIfAbsent(1, new byte[2]).length);
assertEquals(1, m.get(1).length);
// replace
assertNull(m.replace(2, new byte[2]));
assertNull(m.get(2));
assertEquals(1, m.replace(1, new byte[2]).length);
assertEquals(2, m.replace(1, new byte[3]).length);
assertEquals(3, m.replace(1, new byte[1]).length);
// replace with oldValue
assertFalse(m.replace(1, new byte[2], new byte[10]));
assertTrue(m.replace(1, new byte[1], new byte[2]));
assertTrue(m.replace(1, new byte[2], new byte[1]));
// remove
assertFalse(m.remove(1, new byte[2]));
assertTrue(m.remove(1, new byte[1]));
s.close();
FileUtils.delete(fileName);
}
use of org.h2.command.dml.Replace in project h2database by h2database.
the class TestMVStore method testLargerThan2G.
private void testLargerThan2G() {
if (!config.big) {
return;
}
String fileName = getBaseDir() + "/" + getTestName();
FileUtils.delete(fileName);
MVStore store = new MVStore.Builder().cacheSize(16).fileName(fileName).open();
try {
MVMap<Integer, String> map = store.openMap("test");
long last = System.nanoTime();
String data = new String(new char[2500]).replace((char) 0, 'x');
for (int i = 0; ; i++) {
map.put(i, data);
if (i % 10000 == 0) {
store.commit();
long time = System.nanoTime();
if (time - last > TimeUnit.SECONDS.toNanos(2)) {
long mb = store.getFileStore().size() / 1024 / 1024;
trace(mb + "/4500");
if (mb > 4500) {
break;
}
last = time;
}
}
}
store.commit();
store.close();
} finally {
store.closeImmediately();
}
FileUtils.delete(fileName);
}
Aggregations