use of java.util.concurrent.ConcurrentNavigableMap in project cdap by caskdata.
the class InMemoryTableService method swap.
public static synchronized boolean swap(String tableName, byte[] row, byte[] column, byte[] oldValue, byte[] newValue) {
ConcurrentNavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, Update>>> table = tables.get(tableName);
// get the correct row from the table, create it if it doesn't exist
NavigableMap<byte[], NavigableMap<Long, Update>> rowMap = table.get(row);
Update existingValue = null;
if (rowMap != null) {
NavigableMap<Long, Update> columnMap = rowMap.get(column);
if (columnMap != null) {
existingValue = columnMap.lastEntry().getValue();
}
}
// verify existing value matches
if (oldValue == null && existingValue != null) {
return false;
}
if (oldValue != null && (existingValue == null || !Bytes.equals(oldValue, existingValue.getBytes()))) {
return false;
}
// write new value
if (newValue == null) {
if (rowMap != null) {
rowMap.remove(column);
}
} else {
if (rowMap == null) {
rowMap = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
table.put(row, rowMap);
}
NavigableMap<Long, Update> columnMap = rowMap.get(column);
if (columnMap == null) {
columnMap = Maps.newTreeMap();
rowMap.put(column, columnMap);
}
PutValue newPut = new PutValue(newValue);
columnMap.put(System.currentTimeMillis(), newPut);
}
return true;
}
use of java.util.concurrent.ConcurrentNavigableMap in project mapdb by jankotek.
the class ConcurrentSkipListSubMapTest method testGet.
/**
* get returns the correct element at the given key,
* or null if not present
*/
@Test
public void testGet() {
ConcurrentNavigableMap map = map5();
assertEquals("A", (String) map.get(one));
ConcurrentNavigableMap empty = map0();
assertNull(empty.get(one));
}
use of java.util.concurrent.ConcurrentNavigableMap in project mapdb by jankotek.
the class ConcurrentSkipListSubMapTest method testLastKey.
/**
* lastKey returns last key
*/
@Test
public void testLastKey() {
ConcurrentNavigableMap map = map5();
assertEquals(five, map.lastKey());
}
use of java.util.concurrent.ConcurrentNavigableMap in project mapdb by jankotek.
the class ConcurrentSkipListSubMapTest method testReplaceValue2.
/**
* replace value succeeds when the given key mapped to expected value
*/
@Test
public void testReplaceValue2() {
if (isReadOnly())
return;
ConcurrentNavigableMap map = map5();
assertEquals("A", map.get(one));
assertTrue(map.replace(one, "A", "Z"));
assertEquals("Z", map.get(one));
}
use of java.util.concurrent.ConcurrentNavigableMap in project mapdb by jankotek.
the class ConcurrentSkipListSubMapTest method testDescendingFloorEntry.
/**
* floorEntry returns preceding entry.
*/
@Test
public void testDescendingFloorEntry() {
ConcurrentNavigableMap map = dmap5();
Map.Entry e1 = map.floorEntry(m3);
assertEquals(m3, e1.getKey());
Map.Entry e2 = map.floorEntry(m6);
assertEquals(m5, e2.getKey());
Map.Entry e3 = map.floorEntry(m1);
assertEquals(m1, e3.getKey());
Map.Entry e4 = map.floorEntry(zero);
assertNull(e4);
}
Aggregations