use of org.h2.mvstore.db.TransactionStore in project h2database by h2database.
the class TestTransactionStore method testKeyIterator.
private void testKeyIterator() {
MVStore s = MVStore.open(null);
TransactionStore ts = new TransactionStore(s);
ts.init();
Transaction tx, tx2;
TransactionMap<String, String> m, m2;
Iterator<String> it, it2;
tx = ts.begin();
m = tx.openMap("test");
m.put("1", "Hello");
m.put("2", "World");
m.put("3", ".");
tx.commit();
tx2 = ts.begin();
m2 = tx2.openMap("test");
m2.remove("2");
m2.put("3", "!");
m2.put("4", "?");
tx = ts.begin();
m = tx.openMap("test");
it = m.keyIterator(null);
assertTrue(it.hasNext());
assertEquals("1", it.next());
assertTrue(it.hasNext());
assertEquals("2", it.next());
assertTrue(it.hasNext());
assertEquals("3", it.next());
assertFalse(it.hasNext());
it2 = m2.keyIterator(null);
assertTrue(it2.hasNext());
assertEquals("1", it2.next());
assertTrue(it2.hasNext());
assertEquals("3", it2.next());
assertTrue(it2.hasNext());
assertEquals("4", it2.next());
assertFalse(it2.hasNext());
s.close();
}
use of org.h2.mvstore.db.TransactionStore in project h2database by h2database.
the class TestTransactionStore method testSavepoint.
private void testSavepoint() {
MVStore s = MVStore.open(null);
TransactionStore ts = new TransactionStore(s);
ts.init();
Transaction tx;
TransactionMap<String, String> m;
tx = ts.begin();
m = tx.openMap("test");
m.put("1", "Hello");
m.put("2", "World");
m.put("1", "Hallo");
m.remove("2");
m.put("3", "!");
long logId = tx.setSavepoint();
m.put("1", "Hi");
m.put("2", ".");
m.remove("3");
tx.rollbackToSavepoint(logId);
assertEquals("Hallo", m.get("1"));
assertNull(m.get("2"));
assertEquals("!", m.get("3"));
tx.rollback();
tx = ts.begin();
m = tx.openMap("test");
assertNull(m.get("1"));
assertNull(m.get("2"));
assertNull(m.get("3"));
ts.close();
s.close();
}
use of org.h2.mvstore.db.TransactionStore in project h2database by h2database.
the class TestTransactionStore method testStoreMultiThreadedReads.
private static void testStoreMultiThreadedReads() throws Exception {
MVStore s = MVStore.open(null);
final TransactionStore ts = new TransactionStore(s);
ts.init();
Transaction t = ts.begin();
TransactionMap<Integer, Integer> mapA = t.openMap("a");
mapA.put(1, 0);
t.commit();
Task task = new Task() {
@Override
public void call() throws Exception {
for (int i = 0; !stop; i++) {
Transaction tx = ts.begin();
TransactionMap<Integer, Integer> mapA = tx.openMap("a");
while (!mapA.tryPut(1, i)) {
// repeat
}
tx.commit();
// map B transaction
// the other thread will get a map A uncommitted value,
// but by the time it tries to walk back to the committed
// value, the undoLog has changed
tx = ts.begin();
TransactionMap<Integer, Integer> mapB = tx.openMap("b");
// put a new value to the map; this will cause a map B
// undoLog entry to be created with a null pre-image value
mapB.tryPut(i, -i);
// this is where the real race condition occurs:
// some other thread might get the B log entry
// for this transaction rather than the uncommitted A log
// entry it is expecting
tx.commit();
}
}
};
task.execute();
try {
for (int i = 0; i < 10000; i++) {
Transaction tx = ts.begin();
mapA = tx.openMap("a");
if (mapA.get(1) == null) {
throw new AssertionError("key not found");
}
tx.commit();
}
} finally {
task.get();
}
ts.close();
}
use of org.h2.mvstore.db.TransactionStore in project h2database by h2database.
the class TestTransactionStore method testGetModifiedMaps.
private void testGetModifiedMaps() {
MVStore s = MVStore.open(null);
TransactionStore ts = new TransactionStore(s);
ts.init();
Transaction tx;
TransactionMap<String, String> m1, m2, m3;
long sp;
tx = ts.begin();
m1 = tx.openMap("m1");
m2 = tx.openMap("m2");
m3 = tx.openMap("m3");
assertFalse(tx.getChanges(0).hasNext());
tx.commit();
tx = ts.begin();
m1 = tx.openMap("m1");
m2 = tx.openMap("m2");
m3 = tx.openMap("m3");
m1.put("1", "100");
sp = tx.setSavepoint();
m2.put("1", "100");
m3.put("1", "100");
Iterator<Change> it = tx.getChanges(sp);
assertTrue(it.hasNext());
Change c;
c = it.next();
assertEquals("m3", c.mapName);
assertEquals("1", c.key.toString());
assertNull(c.value);
assertTrue(it.hasNext());
c = it.next();
assertEquals("m2", c.mapName);
assertEquals("1", c.key.toString());
assertNull(c.value);
assertFalse(it.hasNext());
it = tx.getChanges(0);
assertTrue(it.hasNext());
c = it.next();
assertEquals("m3", c.mapName);
assertEquals("1", c.key.toString());
assertNull(c.value);
assertTrue(it.hasNext());
c = it.next();
assertEquals("m2", c.mapName);
assertEquals("1", c.key.toString());
assertNull(c.value);
assertTrue(it.hasNext());
c = it.next();
assertEquals("m1", c.mapName);
assertEquals("1", c.key.toString());
assertNull(c.value);
assertFalse(it.hasNext());
tx.rollbackToSavepoint(sp);
it = tx.getChanges(0);
assertTrue(it.hasNext());
c = it.next();
assertEquals("m1", c.mapName);
assertEquals("1", c.key.toString());
assertNull(c.value);
assertFalse(it.hasNext());
tx.commit();
s.close();
}
Aggregations