下面列出了java.util.NavigableMap#remove ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void compactJournalToSnapshot(long index) {
logger.info("Compact journal to index: {}...", index);
try {
Snapshot snapshot = snapshots.get(index);
if (null != snapshot) {
JournalSnapshot journalSnapshot = snapshot.getJournalSnapshot();
logger.info("Compact journal entries, journal snapshot: {}, journal: {}...", journalSnapshot, journal);
journal.compact(snapshot.getJournalSnapshot());
logger.info("Compact journal finished, journal: {}.", journal);
NavigableMap<Long, Snapshot> headMap = snapshots.headMap(index, false);
while (!headMap.isEmpty()) {
snapshot = headMap.remove(headMap.firstKey());
logger.info("Discard snapshot: {}.", snapshot.getPath());
snapshot.close();
snapshot.clear();
}
} else {
logger.warn("Compact journal failed! Cause no snapshot at index: {}.", index);
}
} catch (Throwable e) {
logger.warn("Compact journal exception!", e);
}
}
private JsonRpcSuccessResponse extractStorageAt(
final JsonRpcRequestContext requestContext,
final Address accountAddress,
final Hash startKey,
final int limit,
final MutableWorldState worldState) {
final Account account = worldState.get(accountAddress);
final NavigableMap<Bytes32, AccountStorageEntry> entries =
account.storageEntriesFrom(startKey, limit + 1);
Bytes32 nextKey = null;
if (entries.size() == limit + 1) {
nextKey = entries.lastKey();
entries.remove(nextKey);
}
return new JsonRpcSuccessResponse(
requestContext.getRequest().getId(),
new DebugStorageRangeAtResult(entries, nextKey, shortValues));
}
@Override
public NavigableMap<Bytes32, AccountStorageEntry> storageEntriesFrom(
final Bytes32 startKeyHash, final int limit) {
final NavigableMap<Bytes32, AccountStorageEntry> entries;
if (account != null) {
entries = account.storageEntriesFrom(startKeyHash, limit);
} else {
entries = new TreeMap<>();
}
updatedStorage.entrySet().stream()
.map(entry -> AccountStorageEntry.forKeyAndValue(entry.getKey(), entry.getValue()))
.filter(entry -> entry.getKeyHash().compareTo(startKeyHash) >= 0)
.forEach(entry -> entries.put(entry.getKeyHash(), entry));
while (entries.size() > limit) {
entries.remove(entries.lastKey());
}
return entries;
}
/**
* pollLastEntry returns entries in order
*/
public void testDescendingPollLastEntry() {
NavigableMap 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);
}
/**
* pollLastEntry returns entries in order
*/
public void testPollLastEntry() {
NavigableMap map = map5();
Map.Entry e = map.pollLastEntry();
assertEquals(five, e.getKey());
assertEquals("E", e.getValue());
e = map.pollLastEntry();
assertEquals(four, e.getKey());
map.put(five, "E");
e = map.pollLastEntry();
assertEquals(five, e.getKey());
assertEquals("E", e.getValue());
e = map.pollLastEntry();
assertEquals(three, e.getKey());
map.remove(two);
e = map.pollLastEntry();
assertEquals(one, e.getKey());
try {
e.setValue("E");
shouldThrow();
} catch (UnsupportedOperationException success) {}
e = map.pollLastEntry();
assertNull(e);
}
@Override // from Unit
public IUnit add(IUnit unit) {
NavigableMap<String, IExpr> map = new TreeMap<>(navigableMap);
for (Entry<String, IExpr> entry : unit.map().entrySet()) {
String key = entry.getKey();
IExpr value = entry.getValue();
if (map.containsKey(key)) {
// TODO this may not always use the defined UnitHelper.EvalEngine
IExpr sum = F.Plus.of(UnitHelper.ENGINE, map.get(key), value);
if (sum.isZero())
map.remove(key); // exponents cancel out
else
map.put(key, sum); // exponent is updated
} else
map.put(key, value); // unit is introduced
}
return new UnitImpl(map);
}
/**
* pollLastEntry returns entries in order
*/
public void testPollLastEntry() {
NavigableMap map = map5();
Map.Entry e = map.pollLastEntry();
assertEquals(five, e.getKey());
assertEquals("E", e.getValue());
e = map.pollLastEntry();
assertEquals(four, e.getKey());
map.put(five, "E");
e = map.pollLastEntry();
assertEquals(five, e.getKey());
assertEquals("E", e.getValue());
e = map.pollLastEntry();
assertEquals(three, e.getKey());
map.remove(two);
e = map.pollLastEntry();
assertEquals(one, e.getKey());
try {
e.setValue("E");
shouldThrow();
} catch (UnsupportedOperationException success) {}
e = map.pollLastEntry();
assertNull(e);
}
public boolean setCommit(Comparable[] key, long version, long commit) {
NavigableMap<Long, VersionedValue> rowData = cache.getOrDefault(key, new ConcurrentSkipListMap<>());
VersionedValue value = rowData.get(version);
if (value == null) {
return false;
}
if (commit == INVALID_TX) {
rowData.remove(version);
} else {
rowData.put(version, new VersionedValue(version, commit, value.isDeleted(), value.getValue()));
}
garbageCollect(rowData);
cache.put(key, rowData);
return true;
}
private void decrementBitmapOfSize(Integer size, Bitmap.Config config) {
NavigableMap<Integer, Integer> sizes = getSizesForConfig(config);
Integer current = sizes.get(size);
if (current == 1) {
sizes.remove(size);
} else {
sizes.put(size, current - 1);
}
}
private void decrementBitmapOfSize(Integer size, Bitmap.Config config) {
NavigableMap<Integer, Integer> sizes = getSizesForConfig(config);
Integer current = sizes.get(size);
if (current == 1) {
sizes.remove(size);
} else {
sizes.put(size, current - 1);
}
}
private void performDutyForSlot(
final NavigableMap<UnsignedLong, ? extends Duty> duties, final UnsignedLong slot) {
discardDutiesBeforeSlot(duties, slot);
final Duty duty = duties.remove(slot);
if (duty == null) {
return;
}
duty.performDuty()
.finish(
result -> result.report(duty.getProducedType(), slot, VALIDATOR_LOGGER),
error -> VALIDATOR_LOGGER.dutyFailed(duty.getProducedType(), slot, error));
}
/**
* remove(null) throws NPE
*/
public void testRemove1_NullPointerException() {
NavigableMap c = map5();
try {
c.remove(null);
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* remove removes the correct key-value pair from the map
*/
public void testRemove() {
NavigableMap map = map5();
map.remove(five);
assertEquals(4, map.size());
assertFalse(map.containsKey(five));
}
/**
* pollFirstEntry returns entries in order
*/
public void testPollFirstEntry() {
NavigableMap map = map5();
Map.Entry e = map.pollFirstEntry();
assertEquals(one, e.getKey());
assertEquals("A", e.getValue());
e = map.pollFirstEntry();
assertEquals(two, e.getKey());
map.put(one, "A");
e = map.pollFirstEntry();
assertEquals(one, e.getKey());
assertEquals("A", e.getValue());
e = map.pollFirstEntry();
assertEquals(three, e.getKey());
map.remove(four);
e = map.pollFirstEntry();
assertEquals(five, e.getKey());
try {
e.setValue("A");
shouldThrow();
} catch (UnsupportedOperationException success) {}
assertTrue(map.isEmpty());
Map.Entry f = map.firstEntry();
assertNull(f);
e = map.pollFirstEntry();
assertNull(e);
}
/**
* remove removes the correct key-value pair from the map
*/
public void testDescendingRemove() {
NavigableMap map = dmap5();
map.remove(m5);
assertEquals(4, map.size());
assertFalse(map.containsKey(m5));
}
/**
* remove removes the correct key-value pair from the map
*/
public void testRemove() {
NavigableMap map = map5();
map.remove(five);
assertEquals(4, map.size());
assertFalse(map.containsKey(five));
}
void installSnapshot(long offset, long lastIncludedIndex, int lastIncludedTerm, byte[] data, boolean isDone) throws IOException, TimeoutException {
synchronized (partialSnapshot) {
logger.info("Install snapshot, offset: {}, lastIncludedIndex: {}, lastIncludedTerm: {}, data length: {}, isDone: {}... " +
"journal minIndex: {}, maxIndex: {}, commitIndex: {}...",
ThreadSafeFormat.formatWithComma(offset),
ThreadSafeFormat.formatWithComma(lastIncludedIndex),
lastIncludedTerm,
data.length,
isDone,
ThreadSafeFormat.formatWithComma(journal.minIndex()),
ThreadSafeFormat.formatWithComma(journal.maxIndex()),
ThreadSafeFormat.formatWithComma(journal.commitIndex())
);
Snapshot snapshot;
long lastApplied = lastIncludedIndex + 1;
Path snapshotPath = snapshotsPath().resolve(String.valueOf(lastApplied));
partialSnapshot.installTrunk(offset, data, snapshotPath);
if (isDone) {
logger.info("All snapshot files received, discard any existing snapshot with a same or smaller index...");
// discard any existing snapshot with a same or smaller index
NavigableMap<Long, Snapshot> headMap = snapshots.headMap(lastApplied, true);
while (!headMap.isEmpty()) {
snapshot = headMap.remove(headMap.firstKey());
logger.info("Discard snapshot: {}.", snapshot.getPath());
snapshot.close();
snapshot.clear();
}
logger.info("add the installed snapshot to snapshots: {}...", snapshotPath);
partialSnapshot.finish();
// add the installed snapshot to snapshots.
snapshot = new Snapshot(stateFactory, metadataPersistence);
snapshot.recover(snapshotPath, properties);
snapshots.put(lastApplied, snapshot);
logger.info("New installed snapshot: {}.", snapshot.getJournalSnapshot());
// If existing log entry has same index and term as snapshot’s
// last included entry, retain log entries following it.
// Discard the entire log
logger.info("Compact journal entries, journal: {}...", journal);
threads.stopThread(threadName(ThreadNames.FLUSH_JOURNAL_THREAD));
try {
if (journal.minIndex() >= lastIncludedIndex &&
lastIncludedIndex < journal.maxIndex() &&
journal.getTerm(lastIncludedIndex) == lastIncludedTerm) {
journal.compact(snapshot.getJournalSnapshot());
} else {
journal.clear(snapshot.getJournalSnapshot());
}
} finally {
threads.startThread(threadName(ThreadNames.FLUSH_JOURNAL_THREAD));
}
logger.info("Compact journal finished, journal: {}.", journal);
// Reset state machine using snapshot contents (and load
// snapshot’s cluster configuration)
logger.info("Use the new installed snapshot as server's state...");
stopAndWaitScheduledFeature(flushStateFuture, 1000L);
threads.stopThread(threadName(ThreadNames.STATE_MACHINE_THREAD));
try {
state.close();
state.clear();
snapshot.dump(statePath());
state.recover(statePath(), properties);
} finally {
threads.startThread(threadName(ThreadNames.STATE_MACHINE_THREAD));
flushStateFuture = scheduledExecutor.scheduleAtFixedRate(this::flushState,
ThreadLocalRandom.current().nextLong(10L, 50L),
config.getFlushIntervalMs(), TimeUnit.MILLISECONDS);
}
logger.info("Install snapshot successfully!");
}
}
}
void remove(NavigableMap<Integer, Integer> map, int key) {
if (map.remove(key) != null)
bs.clear(key);
}
void remove(NavigableMap<Integer, Integer> map, int key) {
if (map.remove(key) != null)
bs.clear(key);
}
@SuppressWarnings("Duplicates")
@Override
public void ack(Object msgId) {
long start = System.nanoTime();
ConsumerRecordMessageId consumerRecordMessageId = (ConsumerRecordMessageId) msgId;
TopicPartition topicPartition = consumerRecordMessageId.getTopicPartition();
if (!assignedPartitions.contains(topicPartition)) {
LOG.info("ignore {} because it's been revoked", consumerRecordMessageId);
return;
}
long offset = consumerRecordMessageId.getOffset();
ackRegistry.putIfAbsent(topicPartition, new TreeMap<>());
NavigableMap<Long, Long> navigableMap = ackRegistry.get(topicPartition);
Map.Entry<Long, Long> floorRange = navigableMap.floorEntry(offset);
Map.Entry<Long, Long> ceilingRange = navigableMap.ceilingEntry(offset);
long floorBottom = floorRange != null ? floorRange.getKey() : Long.MIN_VALUE;
long floorTop = floorRange != null ? floorRange.getValue() : Long.MIN_VALUE;
long ceilingBottom = ceilingRange != null ? ceilingRange.getKey() : Long.MAX_VALUE;
long ceilingTop = ceilingRange != null ? ceilingRange.getValue() : Long.MAX_VALUE;
//the ack is for a message that has already been acknowledged.
//This happens when a failed tuple has caused
//Kafka consumer to seek back to earlier position, and some messages are replayed.
if ((offset >= floorBottom && offset <= floorTop)
|| (offset >= ceilingBottom && offset <= ceilingTop)) {
return;
}
if (ceilingBottom - floorTop == 2) {
//the ack connects the two adjacent range
navigableMap.put(floorBottom, ceilingTop);
navigableMap.remove(ceilingBottom);
} else if (offset == floorTop + 1) {
//the acknowledged offset is the immediate neighbour
// of the upper bound of the floor range
navigableMap.put(floorBottom, offset);
} else if (offset == ceilingBottom - 1) {
//the acknowledged offset is the immediate neighbour
// of the lower bound of the ceiling range
navigableMap.remove(ceilingBottom);
navigableMap.put(offset, ceilingTop);
} else {
//it is a new born range
navigableMap.put(offset, offset);
}
LOG.debug("ack {} in {} ns", msgId, System.nanoTime() - start);
LOG.debug("{}", ackRegistry.get(consumerRecordMessageId.getTopicPartition()));
}