Search in sources :

Example 81 with ConcurrentNavigableMap

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;
}
Also used : PutValue(co.cask.cdap.data2.dataset2.lib.table.PutValue) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) NavigableMap(java.util.NavigableMap) Update(co.cask.cdap.data2.dataset2.lib.table.Update)

Example 82 with ConcurrentNavigableMap

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));
}
Also used : ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) Test(org.junit.Test)

Example 83 with ConcurrentNavigableMap

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());
}
Also used : ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) Test(org.junit.Test)

Example 84 with ConcurrentNavigableMap

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));
}
Also used : ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) Test(org.junit.Test)

Example 85 with ConcurrentNavigableMap

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);
}
Also used : Map(java.util.Map) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) NavigableMap(java.util.NavigableMap) SortedMap(java.util.SortedMap) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) Test(org.junit.Test)

Aggregations

ConcurrentNavigableMap (java.util.concurrent.ConcurrentNavigableMap)156 Test (org.junit.Test)147 SortedMap (java.util.SortedMap)25 Map (java.util.Map)19 NavigableMap (java.util.NavigableMap)19 Iterator (java.util.Iterator)12 Set (java.util.Set)10 Collection (java.util.Collection)4 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)4 Update (co.cask.cdap.data2.dataset2.lib.table.Update)3 ArrayList (java.util.ArrayList)2 IncrementValue (co.cask.cdap.data2.dataset2.lib.table.IncrementValue)1 PutValue (co.cask.cdap.data2.dataset2.lib.table.PutValue)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 HashMap (java.util.HashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1