下面列出了怎么用java.util.concurrent.ConcurrentNavigableMap的API类实例代码及写法,或者点击链接到github查看源代码。
private static void readDeltasInRange(WaveletDeltaRecordReader reader,
ConcurrentNavigableMap<HashedVersion, WaveletDeltaRecord> cachedDeltas,
HashedVersion startVersion, HashedVersion endVersion, Receiver<WaveletDeltaRecord> receiver)
throws IOException {
WaveletDeltaRecord delta = getDelta(reader, cachedDeltas, startVersion);
Preconditions.checkArgument(delta != null && delta.getAppliedAtVersion().equals(startVersion),
"invalid start version");
for (;;) {
if (!receiver.put(delta)) {
return;
}
if (delta.getResultingVersion().getVersion() >= endVersion.getVersion()) {
break;
}
delta = getDelta(reader, cachedDeltas, delta.getResultingVersion());
if (delta == null) {
break;
}
}
Preconditions.checkArgument(delta != null && delta.getResultingVersion().equals(endVersion),
"invalid end version");
}
/**
* Sets the active segment in the log.
* </p>
* Frees all segments that follow the active segment. Therefore, this should be
* used only after the active segment is conclusively determined.
* @param name the name of the log segment that is to be marked active.
* @throws IllegalArgumentException if there no segment with name {@code name}.
* @throws StoreException if there is any store exception while freeing segments.
*/
void setActiveSegment(String name) throws StoreException {
if (!segmentsByName.containsKey(name)) {
throw new IllegalArgumentException("There is no log segment with name: " + name);
}
ConcurrentNavigableMap<String, LogSegment> extraSegments = segmentsByName.tailMap(name, false);
Iterator<Map.Entry<String, LogSegment>> iterator = extraSegments.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, LogSegment> entry = iterator.next();
logger.info("Freeing extra segment with name [{}] ", entry.getValue().getName());
free(entry.getValue(), false);
remainingUnallocatedSegments.getAndIncrement();
iterator.remove();
}
logger.info("Setting active segment to [{}]", name);
LogSegment newActiveSegment = segmentsByName.get(name);
if (newActiveSegment != activeSegment) {
// If activeSegment needs to be changed, then drop buffer for old activeSegment and init buffer for new activeSegment.
activeSegment.dropBufferForAppend();
activeSegment = newActiveSegment;
activeSegment.initBufferForAppend();
}
}
/**
* pollLastEntry returns entries in order
*/
public void testDescendingPollLastEntry() {
ConcurrentNavigableMap map = dmap5();
Map.Entry e = map.pollLastEntry();
assertEquals(m5, e.getKey());
assertEquals("E", e.getValue());
e = map.pollLastEntry();
assertEquals(m4, e.getKey());
map.put(m5, "E");
e = map.pollLastEntry();
assertEquals(m5, e.getKey());
assertEquals("E", e.getValue());
e = map.pollLastEntry();
assertEquals(m3, e.getKey());
map.remove(m2);
e = map.pollLastEntry();
assertEquals(m1, e.getKey());
try {
e.setValue("E");
shouldThrow();
} catch (UnsupportedOperationException success) {}
e = map.pollLastEntry();
assertNull(e);
}
public void testSubMapContents2() {
ConcurrentNavigableMap map = map5();
SortedMap sm = map.subMap(two, three);
assertEquals(1, sm.size());
assertEquals(two, sm.firstKey());
assertEquals(two, sm.lastKey());
assertFalse(sm.containsKey(one));
assertTrue(sm.containsKey(two));
assertFalse(sm.containsKey(three));
assertFalse(sm.containsKey(four));
assertFalse(sm.containsKey(five));
Iterator i = sm.keySet().iterator();
Object k;
k = (Integer)(i.next());
assertEquals(two, k);
assertFalse(i.hasNext());
Iterator j = sm.keySet().iterator();
j.next();
j.remove();
assertFalse(map.containsKey(two));
assertEquals(4, map.size());
assertEquals(0, sm.size());
assertTrue(sm.isEmpty());
assertSame(sm.remove(three), null);
assertEquals(4, map.size());
}
/**
* pollFirstEntry returns entries in order
*/
public void testDescendingPollFirstEntry() {
ConcurrentNavigableMap map = dmap5();
Map.Entry e = map.pollFirstEntry();
assertEquals(m1, e.getKey());
assertEquals("A", e.getValue());
e = map.pollFirstEntry();
assertEquals(m2, e.getKey());
map.put(m1, "A");
e = map.pollFirstEntry();
assertEquals(m1, e.getKey());
assertEquals("A", e.getValue());
e = map.pollFirstEntry();
assertEquals(m3, e.getKey());
map.remove(m4);
e = map.pollFirstEntry();
assertEquals(m5, e.getKey());
try {
e.setValue("A");
shouldThrow();
} catch (UnsupportedOperationException success) {}
e = map.pollFirstEntry();
assertNull(e);
}
public void testSubMapContents2() {
ConcurrentNavigableMap map = map5();
SortedMap sm = map.subMap(two, three);
assertEquals(1, sm.size());
assertEquals(two, sm.firstKey());
assertEquals(two, sm.lastKey());
assertFalse(sm.containsKey(one));
assertTrue(sm.containsKey(two));
assertFalse(sm.containsKey(three));
assertFalse(sm.containsKey(four));
assertFalse(sm.containsKey(five));
Iterator i = sm.keySet().iterator();
Object k;
k = (Integer)(i.next());
assertEquals(two, k);
assertFalse(i.hasNext());
Iterator j = sm.keySet().iterator();
j.next();
j.remove();
assertFalse(map.containsKey(two));
assertEquals(4, map.size());
assertEquals(0, sm.size());
assertTrue(sm.isEmpty());
assertSame(sm.remove(three), null);
assertEquals(4, map.size());
}
public void testSubMapContents2() {
ConcurrentNavigableMap map = map5();
SortedMap sm = map.subMap(two, three);
assertEquals(1, sm.size());
assertEquals(two, sm.firstKey());
assertEquals(two, sm.lastKey());
assertFalse(sm.containsKey(one));
assertTrue(sm.containsKey(two));
assertFalse(sm.containsKey(three));
assertFalse(sm.containsKey(four));
assertFalse(sm.containsKey(five));
Iterator i = sm.keySet().iterator();
Object k;
k = (Integer)(i.next());
assertEquals(two, k);
assertFalse(i.hasNext());
Iterator j = sm.keySet().iterator();
j.next();
j.remove();
assertFalse(map.containsKey(two));
assertEquals(4, map.size());
assertEquals(0, sm.size());
assertTrue(sm.isEmpty());
assertSame(sm.remove(three), null);
assertEquals(4, map.size());
}
/**
* @param adapterType the type to check
* @return {@code true} in case the given type is a model (may be with a different adapter class)
*/
@SuppressWarnings("unchecked")
public <ModelType> boolean isModelClass(Class<ModelType> adapterType) {
String key = adapterType.getName();
// lookup in cache for models without adapter classes
ModelClass<ModelType> modelClass = (ModelClass<ModelType>)modelClasses.get(key);
if (modelClass!=null) {
return true;
}
// not found? look in cache with adapter classes
ConcurrentNavigableMap<String,ModelClass<?>> implementations = adapterImplementations.get(key);
if (implementations==null || implementations.isEmpty()) {
return false;
}
return true;
}
/**
* Returns a new map from Integers -5 to -1 to Strings "A"-"E".
*/
private static ConcurrentNavigableMap dmap5() {
ConcurrentSkipListMap map = new ConcurrentSkipListMap();
assertTrue(map.isEmpty());
map.put(m1, "A");
map.put(m5, "E");
map.put(m3, "C");
map.put(m2, "B");
map.put(m4, "D");
assertFalse(map.isEmpty());
assertEquals(5, map.size());
return map.descendingMap();
}
@Override
public void scan(final byte[] startKey, final byte[] endKey, final int limit,
@SuppressWarnings("unused") final boolean readOnlySafe, final boolean returnValue,
final KVStoreClosure closure) {
final Timer.Context timeCtx = getTimeContext("SCAN");
final List<KVEntry> entries = Lists.newArrayList();
final int maxCount = normalizeLimit(limit);
final ConcurrentNavigableMap<byte[], byte[]> subMap;
final byte[] realStartKey = BytesUtil.nullToEmpty(startKey);
if (endKey == null) {
subMap = this.defaultDB.tailMap(realStartKey);
} else {
subMap = this.defaultDB.subMap(realStartKey, endKey);
}
try {
for (final Map.Entry<byte[], byte[]> entry : subMap.entrySet()) {
entries.add(new KVEntry(entry.getKey(), returnValue ? entry.getValue() : null));
if (entries.size() >= maxCount) {
break;
}
}
setSuccess(closure, entries);
} catch (final Exception e) {
LOG.error("Fail to [SCAN], range: ['[{}, {})'], {}.", BytesUtil.toHex(startKey), BytesUtil.toHex(endKey),
StackTraceUtil.stackTrace(e));
setFailure(closure, "Fail to [SCAN]");
} finally {
timeCtx.stop();
}
}
/**
* higherEntry returns next entry.
*/
public void testDescendingHigherEntry() {
ConcurrentNavigableMap map = dmap5();
Map.Entry e1 = map.higherEntry(m3);
assertEquals(m4, e1.getKey());
Map.Entry e2 = map.higherEntry(zero);
assertEquals(m1, e2.getKey());
Map.Entry e3 = map.higherEntry(m5);
assertNull(e3);
Map.Entry e4 = map.higherEntry(m6);
assertNull(e4);
}
/**
* putAll adds all key-value pairs from the given map
*/
public void testPutAll() {
ConcurrentNavigableMap empty = map0();
ConcurrentNavigableMap map = map5();
empty.putAll(map);
assertEquals(5, empty.size());
assertTrue(empty.containsKey(one));
assertTrue(empty.containsKey(two));
assertTrue(empty.containsKey(three));
assertTrue(empty.containsKey(four));
assertTrue(empty.containsKey(five));
}
/**
* remove removes the correct key-value pair from the map
*/
public void testRemove() {
ConcurrentNavigableMap map = map5();
map.remove(five);
assertEquals(4, map.size());
assertFalse(map.containsKey(five));
}
public final ConcurrentNavigableMap<K, V> subMap(K fromKey,
boolean fromInclusive, K toKey, boolean toInclusive,
final int[] numNodesCompared) {
if (fromKey == null || toKey == null)
throw new NullPointerException();
return new SubMap<K, V>(this, fromKey, fromInclusive, toKey, toInclusive,
false, numNodesCompared);
}
/**
* remove(null) throws NPE
*/
public void testDescendingRemove1_NullPointerException() {
try {
ConcurrentNavigableMap c = dmap5();
c.remove(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* keySet.toArray returns contains all keys
*/
public void testKeySetToArray() {
ConcurrentNavigableMap map = map5();
Set s = map.keySet();
Object[] ar = s.toArray();
assertTrue(s.containsAll(Arrays.asList(ar)));
assertEquals(5, ar.length);
ar[0] = m10;
assertFalse(s.containsAll(Arrays.asList(ar)));
}
/**
* replace value succeeds when the given key mapped to expected value
*/
public void testReplaceValue2() {
ConcurrentNavigableMap map = map5();
assertEquals("A", map.get(one));
assertTrue(map.replace(one, "A", "Z"));
assertEquals("Z", map.get(one));
}
/**
* remove(null, x) throws NPE
*/
public void testRemove2_NullPointerException() {
try {
ConcurrentNavigableMap c = map5();
c.remove(null, "whatever");
shouldThrow();
} catch (NullPointerException success) {}
}
@Override
public ConcurrentNavigableMap<K, V> subMap(K fromKey,
boolean fromInclusive,
K toKey,
boolean toInclusive) {
throw new UnsupportedOperationException();
}
/**
* putIfAbsent(null, x) throws NPE
*/
public void testDescendingPutIfAbsent1_NullPointerException() {
try {
ConcurrentNavigableMap c = dmap5();
c.putIfAbsent(null, "whatever");
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* put(null,x) throws NPE
*/
public void testPut1_NullPointerException() {
try {
ConcurrentNavigableMap c = map5();
c.put(null, "whatever");
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* putAll adds all key-value pairs from the given map
*/
public void testPutAll() {
ConcurrentNavigableMap empty = map0();
ConcurrentNavigableMap map = map5();
empty.putAll(map);
assertEquals(5, empty.size());
assertTrue(empty.containsKey(one));
assertTrue(empty.containsKey(two));
assertTrue(empty.containsKey(three));
assertTrue(empty.containsKey(four));
assertTrue(empty.containsKey(five));
}
public final ConcurrentNavigableMap<K, V> headMap(K toKey, boolean inclusive,
final int[] numNodesCompared) {
if (toKey == null)
throw new NullPointerException();
return new SubMap<K, V>(this, null, false, toKey, inclusive, false,
numNodesCompared);
}
/**
* floorEntry returns preceding entry.
*/
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);
}
public void testConcurrentNavigableMap() throws Exception {
Type typeOfMap = new TypeToken<ConcurrentNavigableMap<Integer, String>>() {}.getType();
ConcurrentNavigableMap<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap);
assertEquals(1, map.size());
assertTrue(map.containsKey(123));
assertEquals("456", map.get(123));
String json = gson.toJson(map);
assertEquals("{\"123\":\"456\"}", json);
}
/**
* containsValue(null) throws NPE
*/
public void testDescendingContainsValue_NullPointerException() {
try {
ConcurrentNavigableMap c = dmap0();
c.containsValue(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* get returns the correct element at the given key,
* or null if not present
*/
public void testGet() {
ConcurrentNavigableMap map = map5();
assertEquals("A", (String)map.get(one));
ConcurrentNavigableMap empty = map0();
assertNull(empty.get(one));
}
/**
* Returns a new map from Integers 1-5 to Strings "A"-"E".
*/
private static ConcurrentNavigableMap map5() {
BTreeMap map = newBTreeMap();
assertTrue(map.isEmpty());
map.put(zero, "Z");
map.put(one, "A");
map.put(five, "E");
map.put(three, "C");
map.put(two, "B");
map.put(four, "D");
map.put(seven, "F");
assertFalse(map.isEmpty());
assertEquals(7, map.size());
return map.subMap(one, true, seven, false);
}
/**
* ceilingEntry returns next entry.
*/
public void testCeilingEntry() {
ConcurrentNavigableMap map = map5();
Map.Entry e1 = map.ceilingEntry(three);
assertEquals(three, e1.getKey());
Map.Entry e2 = map.ceilingEntry(zero);
assertEquals(one, e2.getKey());
Map.Entry e3 = map.ceilingEntry(five);
assertEquals(five, e3.getKey());
Map.Entry e4 = map.ceilingEntry(six);
assertNull(e4);
}
/**
* size returns the correct values
*/
public void testSize() {
ConcurrentNavigableMap map = map5();
ConcurrentNavigableMap empty = map0();
assertEquals(0, empty.size());
assertEquals(5, map.size());
}