Java Code Examples for java.util.NavigableMap#remove()

The following examples show how to use java.util.NavigableMap#remove() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractServer.java    From journalkeeper with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example 2
Source File: DebugStorageRangeAt.java    From besu with Apache License 2.0 6 votes vote down vote up
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));
}
 
Example 3
Source File: AbstractWorldUpdater.java    From besu with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 4
Source File: TreeSubMapTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 5
Source File: UnitImpl.java    From symja_android_library with GNU General Public License v3.0 6 votes vote down vote up
@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);
}
 
Example 6
Source File: TreeSubMapTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 7
Source File: TreeSubMapTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 8
Source File: TreeSubMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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));
}
 
Example 9
Source File: TreeSubMapTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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));
}
 
Example 10
Source File: TreeSubMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 11
Source File: TreeSubMapTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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));
}
 
Example 12
Source File: TreeSubMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * remove(null) throws NPE
 */
public void testRemove1_NullPointerException() {
    NavigableMap c = map5();
    try {
        c.remove(null);
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 13
Source File: ScheduledDuties.java    From teku with Apache License 2.0 5 votes vote down vote up
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));
}
 
Example 14
Source File: SizeConfigStrategy.java    From giffun with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 15
Source File: SizeConfigStrategy.java    From sketch with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 16
Source File: VersionedCache.java    From kareldb with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 17
Source File: TreeMapTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
void remove(NavigableMap<Integer, Integer> map, int key) {
    if (map.remove(key) != null)
        bs.clear(key);
}
 
Example 18
Source File: KafkaSpout.java    From incubator-heron with Apache License 2.0 4 votes vote down vote up
@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()));
}
 
Example 19
Source File: ConcurrentSkipListMapTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
void remove(NavigableMap<Integer, Integer> map, int key) {
    if (map.remove(key) != null)
        bs.clear(key);
}
 
Example 20
Source File: AbstractServer.java    From journalkeeper with Apache License 2.0 4 votes vote down vote up
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!");
        }
    }
}