use of org.h2.mvstore.db.TransactionStore.TransactionMap 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();
}
use of org.h2.mvstore.db.TransactionStore.TransactionMap in project h2database by h2database.
the class TestTransactionStore method testStopWhileCommitting.
private void testStopWhileCommitting() throws Exception {
String fileName = getBaseDir() + "/testStopWhileCommitting.h3";
FileUtils.delete(fileName);
Random r = new Random(0);
for (int i = 0; i < 10; ) {
MVStore s;
TransactionStore ts;
Transaction tx;
TransactionMap<Integer, String> m;
s = MVStore.open(fileName);
ts = new TransactionStore(s);
ts.init();
tx = ts.begin();
s.setReuseSpace(false);
m = tx.openMap("test");
final String value = "x" + i;
for (int j = 0; j < 1000; j++) {
m.put(j, value);
}
final AtomicInteger state = new AtomicInteger();
final MVStore store = s;
final MVMap<Integer, String> other = s.openMap("other");
Task task = new Task() {
@Override
public void call() throws Exception {
for (int i = 0; !stop; i++) {
state.set(i);
other.put(i, value);
store.commit();
}
}
};
task.execute();
// wait for the task to start
while (state.get() < 1) {
Thread.yield();
}
// commit while writing in the task
tx.commit();
// wait for the task to stop
task.get();
store.close();
s = MVStore.open(fileName);
// roll back a bit, until we have some undo log entries
assertTrue(s.hasMap("undoLog"));
for (int back = 0; back < 100; back++) {
int minus = r.nextInt(10);
s.rollbackTo(Math.max(0, s.getCurrentVersion() - minus));
MVMap<?, ?> undo = s.openMap("undoLog");
if (undo.size() > 0) {
break;
}
}
// re-open the store, because we have opened
// the undoLog map with the wrong data type
s.close();
s = MVStore.open(fileName);
ts = new TransactionStore(s);
List<Transaction> list = ts.getOpenTransactions();
if (list.size() != 0) {
tx = list.get(0);
if (tx.getStatus() == Transaction.STATUS_COMMITTING) {
i++;
}
}
s.close();
FileUtils.delete(fileName);
assertFalse(FileUtils.exists(fileName));
}
}
use of org.h2.mvstore.db.TransactionStore.TransactionMap in project h2database by h2database.
the class MVPrimaryIndex method add.
@Override
public void add(Session session, Row row) {
if (mainIndexColumn == -1) {
if (row.getKey() == 0) {
row.setKey(lastKey.incrementAndGet());
}
} else {
long c = row.getValue(mainIndexColumn).getLong();
row.setKey(c);
}
if (mvTable.getContainsLargeObject()) {
for (int i = 0, len = row.getColumnCount(); i < len; i++) {
Value v = row.getValue(i);
Value v2 = v.copy(database, getId());
if (v2.isLinkedToTable()) {
session.removeAtCommitStop(v2);
}
if (v != v2) {
row.setValue(i, v2);
}
}
}
TransactionMap<Value, Value> map = getMap(session);
Value key = ValueLong.get(row.getKey());
Value old = map.getLatest(key);
if (old != null) {
String sql = "PRIMARY KEY ON " + table.getSQL();
if (mainIndexColumn >= 0 && mainIndexColumn < indexColumns.length) {
sql += "(" + indexColumns[mainIndexColumn].getSQL() + ")";
}
DbException e = DbException.get(ErrorCode.DUPLICATE_KEY_1, sql);
e.setSource(this);
throw e;
}
try {
map.put(key, ValueArray.get(row.getValueList()));
} catch (IllegalStateException e) {
throw mvTable.convertException(e);
}
// syntax
if (row.getKey() > lastKey.get()) {
lastKey.set(row.getKey());
}
}
use of org.h2.mvstore.db.TransactionStore.TransactionMap in project h2database by h2database.
the class MVSpatialIndex method remove.
@Override
public void remove(Session session, Row row) {
SpatialKey key = getKey(row);
if (key.isNull()) {
return;
}
TransactionMap<SpatialKey, Value> map = getMap(session);
try {
Value old = map.remove(key);
if (old == null) {
old = map.remove(key);
throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1, getSQL() + ": " + row.getKey());
}
} catch (IllegalStateException e) {
throw mvTable.convertException(e);
}
}
use of org.h2.mvstore.db.TransactionStore.TransactionMap in project h2database by h2database.
the class MVSpatialIndex method findByGeometry.
@Override
public Cursor findByGeometry(TableFilter filter, SearchRow first, SearchRow last, SearchRow intersection) {
Session session = filter.getSession();
if (intersection == null) {
return find(session, first, last);
}
Iterator<SpatialKey> cursor = spatialMap.findIntersectingKeys(getKey(intersection));
TransactionMap<SpatialKey, Value> map = getMap(session);
Iterator<SpatialKey> it = map.wrapIterator(cursor, false);
return new MVStoreCursor(session, it);
}
Aggregations