use of org.h2.mvstore.db.TransactionStore in project h2database by h2database.
the class TestTransactionStore method testTransactionAge.
private void testTransactionAge() throws Exception {
MVStore s;
TransactionStore ts;
s = MVStore.open(null);
ts = new TransactionStore(s);
ts.init();
ts.setMaxTransactionId(16);
ArrayList<Transaction> openList = new ArrayList<>();
for (int i = 0, j = 1; i < 64; i++) {
Transaction t = ts.begin();
openList.add(t);
assertEquals(j, t.getId());
j++;
if (j > 16) {
j = 1;
}
if (openList.size() >= 16) {
t = openList.remove(0);
t.commit();
}
}
s = MVStore.open(null);
ts = new TransactionStore(s);
ts.init();
ts.setMaxTransactionId(16);
ArrayList<Transaction> fifo = New.arrayList();
int open = 0;
for (int i = 0; i < 64; i++) {
Transaction t = null;
if (open >= 16) {
try {
t = ts.begin();
fail();
} catch (IllegalStateException e) {
// expected - too many open
}
Transaction first = fifo.remove(0);
first.commit();
open--;
}
t = ts.begin();
t.openMap("data").put(i, i);
fifo.add(t);
open++;
}
s.close();
}
use of org.h2.mvstore.db.TransactionStore in project h2database by h2database.
the class TestTransactionStore method testConcurrentTransactionsReadCommitted.
private void testConcurrentTransactionsReadCommitted() {
MVStore s = MVStore.open(null);
TransactionStore ts = new TransactionStore(s);
ts.init();
Transaction tx1, tx2;
TransactionMap<String, String> m1, m2;
tx1 = ts.begin();
m1 = tx1.openMap("test");
m1.put("1", "Hi");
m1.put("3", ".");
tx1.commit();
tx1 = ts.begin();
m1 = tx1.openMap("test");
m1.put("1", "Hello");
m1.put("2", "World");
m1.remove("3");
tx1.commit();
// start new transaction to read old data
tx2 = ts.begin();
m2 = tx2.openMap("test");
// start transaction tx1, update/delete/add
tx1 = ts.begin();
m1 = tx1.openMap("test");
m1.put("1", "Hallo");
m1.remove("2");
m1.put("3", "!");
assertEquals("Hello", m2.get("1"));
assertEquals("World", m2.get("2"));
assertNull(m2.get("3"));
tx1.commit();
assertEquals("Hallo", m2.get("1"));
assertNull(m2.get("2"));
assertEquals("!", m2.get("3"));
tx1 = ts.begin();
m1 = tx1.openMap("test");
m1.put("2", "World");
assertNull(m2.get("2"));
assertFalse(m2.tryRemove("2"));
assertFalse(m2.tryPut("2", "Welt"));
tx2 = ts.begin();
m2 = tx2.openMap("test");
assertNull(m2.get("2"));
m1.remove("2");
assertNull(m2.get("2"));
tx1.commit();
tx1 = ts.begin();
m1 = tx1.openMap("test");
assertNull(m1.get("2"));
m1.put("2", "World");
m1.put("2", "Welt");
tx1.rollback();
tx1 = ts.begin();
m1 = tx1.openMap("test");
assertNull(m1.get("2"));
ts.close();
s.close();
}
use of org.h2.mvstore.db.TransactionStore in project h2database by h2database.
the class TestTransactionStore method testConcurrentUpdate.
private void testConcurrentUpdate() {
MVStore s;
TransactionStore ts;
s = MVStore.open(null);
ts = new TransactionStore(s);
ts.init();
Transaction tx1 = ts.begin();
TransactionMap<Integer, Integer> map1 = tx1.openMap("data");
map1.put(1, 10);
Transaction tx2 = ts.begin();
TransactionMap<Integer, Integer> map2 = tx2.openMap("data");
try {
map2.put(1, 20);
fail();
} catch (IllegalStateException e) {
assertEquals(DataUtils.ERROR_TRANSACTION_LOCKED, DataUtils.getErrorCode(e.getMessage()));
}
assertEquals(10, map1.get(1).intValue());
assertNull(map2.get(1));
tx1.commit();
assertEquals(10, map2.get(1).intValue());
s.close();
}
use of org.h2.mvstore.db.TransactionStore in project h2database by h2database.
the class TestTransactionStore method testHCLFKey.
private void testHCLFKey() {
MVStore s = MVStore.open(null);
final TransactionStore ts = new TransactionStore(s);
ts.init();
Transaction t = ts.begin();
ObjectDataType keyType = new ObjectDataType();
TransactionMap<Long, Long> map = t.openMap("test", keyType, keyType);
// firstKey()
assertNull(map.firstKey());
// lastKey()
assertNull(map.lastKey());
map.put(10L, 100L);
map.put(20L, 200L);
map.put(30L, 300L);
map.put(40L, 400L);
t.commit();
t = ts.begin();
map = t.openMap("test", keyType, keyType);
map.put(15L, 150L);
// The same transaction
assertEquals((Object) 15L, map.higherKey(10L));
t = ts.begin();
map = t.openMap("test", keyType, keyType);
// Another transaction
// higherKey()
assertEquals((Object) 20L, map.higherKey(10L));
assertEquals((Object) 20L, map.higherKey(15L));
assertNull(map.higherKey(40L));
// ceilingKey()
assertEquals((Object) 10L, map.ceilingKey(10L));
assertEquals((Object) 20L, map.ceilingKey(15L));
assertEquals((Object) 40L, map.ceilingKey(40L));
assertNull(map.higherKey(45L));
// lowerKey()
assertNull(map.lowerKey(10L));
assertEquals((Object) 10L, map.lowerKey(15L));
assertEquals((Object) 10L, map.lowerKey(20L));
assertEquals((Object) 20L, map.lowerKey(25L));
// floorKey()
assertNull(map.floorKey(5L));
assertEquals((Object) 10L, map.floorKey(10L));
assertEquals((Object) 10L, map.floorKey(15L));
assertEquals((Object) 30L, map.floorKey(35L));
s.close();
}
use of org.h2.mvstore.db.TransactionStore in project h2database by h2database.
the class TestTransactionStore method testCountWithOpenTransactions.
private void testCountWithOpenTransactions() {
MVStore s;
TransactionStore ts;
s = MVStore.open(null);
ts = new TransactionStore(s);
ts.init();
Transaction tx1 = ts.begin();
TransactionMap<Integer, Integer> map1 = tx1.openMap("data");
int size = 150;
for (int i = 0; i < size; i++) {
map1.put(i, i * 10);
}
tx1.commit();
tx1 = ts.begin();
map1 = tx1.openMap("data");
Transaction tx2 = ts.begin();
TransactionMap<Integer, Integer> map2 = tx2.openMap("data");
Random r = new Random(1);
for (int i = 0; i < size * 3; i++) {
assertEquals("op: " + i, size, (int) map1.sizeAsLong());
// keep the first 10%, and add 10%
int k = size / 10 + r.nextInt(size);
if (r.nextBoolean()) {
map2.remove(k);
} else {
map2.put(k, i);
}
}
s.close();
}
Aggregations