use of org.h2.mvstore.db.TransactionStore.TransactionMap in project h2database by h2database.
the class MVSecondaryIndex method find.
private Cursor find(Session session, SearchRow first, boolean bigger, SearchRow last) {
ValueArray min = convertToKey(first);
if (min != null) {
min.getList()[keyColumns - 1] = ValueLong.MIN;
}
TransactionMap<Value, Value> map = getMap(session);
if (bigger && min != null) {
// search for the next: first skip 1, then 2, 4, 8, until
// we have a higher key; then skip 4, 2,...
// (binary search), until 1
int offset = 1;
while (true) {
ValueArray v = (ValueArray) map.relativeKey(min, offset);
if (v != null) {
boolean foundHigher = false;
for (int i = 0; i < keyColumns - 1; i++) {
int idx = columnIds[i];
Value b = first.getValue(idx);
if (b == null) {
break;
}
Value a = v.getList()[i];
if (database.compare(a, b) > 0) {
foundHigher = true;
break;
}
}
if (!foundHigher) {
offset += offset;
min = v;
continue;
}
}
if (offset > 1) {
offset /= 2;
continue;
}
if (map.get(v) == null) {
min = (ValueArray) map.higherKey(min);
if (min == null) {
break;
}
continue;
}
min = v;
break;
}
if (min == null) {
return new MVStoreCursor(session, Collections.<Value>emptyList().iterator(), null);
}
}
return new MVStoreCursor(session, map.keyIterator(min), last);
}
use of org.h2.mvstore.db.TransactionStore.TransactionMap in project h2database by h2database.
the class MVSecondaryIndex method remove.
@Override
public void remove(Session session) {
TransactionMap<Value, Value> map = getMap(session);
if (!map.isClosed()) {
Transaction t = session.getTransaction();
t.removeMap(map);
}
}
use of org.h2.mvstore.db.TransactionStore.TransactionMap 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.TransactionMap 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.TransactionMap 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();
}
Aggregations