//
executor.submit(new Runnable() {
public void run() {
try {
Transaction writer = pageFile.tx();
Index<String, Long> index = openIndex(writer);
try {
for (int i = 0; i < 1000; i++) {
index.put("" + i, Long.valueOf(i));
}
preCommitLatch.countDown();
if (preCommitLatch.await(10, TimeUnit.SECONDS)) {
try {
writer.commit();
System.out.println("Committed from 1.");
} catch (OptimisticUpdateException ex) {
System.out.println("Replaying from 1...");
run();
}
} else {
throw new RuntimeException();
}
} catch (InterruptedException ex) {
error.set(ex);
}
} catch (Exception ex) {
ex.printStackTrace();
error.compareAndSet(null, ex);
} finally {
commitLatch.countDown();
}
}
});
//
executor.submit(new Runnable() {
public void run() {
try {
Transaction writer = pageFile.tx();
Index<String, Long> index = openIndex(writer);
try {
for (int i = 1000; i < 2000; i++) {
index.put("" + i, Long.valueOf(i));
}
preCommitLatch.countDown();
if (preCommitLatch.await(10, TimeUnit.SECONDS)) {
try {
writer.commit();
System.out.println("Committed from 2.");
} catch (OptimisticUpdateException ex) {
System.out.println("Replaying from 2...");
run();
}
} else {
throw new RuntimeException();
}
} catch (InterruptedException ex) {
error.set(ex);
}
} catch (Exception ex) {
ex.printStackTrace();
error.compareAndSet(null, ex);
} finally {
commitLatch.countDown();
}
}
});
assertTrue(commitLatch.await(60, TimeUnit.SECONDS));
if (error.get() == null) {
Transaction checker = pageFile.tx();
Index<String, Long> index = openIndex(checker);
for (int i = 0; i < 2000; i++) {
assertEquals(Long.valueOf(i), index.get("" + i));
}
checker.commit();
} else {
throw (Exception) error.get();
}
//
executor.shutdownNow();