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

The following examples show how to use java.util.NavigableMap#lastEntry() . 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: FileLogReader.java    From rcrs-server with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void removeStaleKeyFrames() {
    Logger.trace("Removing stale key frames");
    int size = keyFrames.size();
    if (size < KEY_FRAME_BUFFER_MAX_SIZE) {
        Logger.trace("Key frame buffer is not full: " + size + (size == 1 ? " entry" : " entries"));
        return;
    }
    // Try to balance the number of key frames.
    int window = maxTime / KEY_FRAME_BUFFER_MAX_SIZE;
    for (int i = 0; i < maxTime; i += window) {
        NavigableMap<Integer, WorldModel<? extends Entity>> next = keyFrames.subMap(i, false, i + window, true);
        Logger.trace("Window " + i + " -> " + (i + window) + " has " + next.size() + " entries");
        if (next.size() > 1) {
            // Remove all but the last entry in this window
            Map.Entry<Integer, WorldModel<? extends Entity>> last = next.lastEntry();
            next.clear();
            next.put(last.getKey(), last.getValue());
            Logger.trace("Retained entry " + last);
        }
    }
    Logger.trace("New key frame set: " + keyFrames);
}
 
Example 2
Source File: HBaseStore.java    From datacollector with Apache License 2.0 6 votes vote down vote up
private String getValue(HBaseColumn hBaseColumn, Result result) {
  String value = null;
  if (result.isEmpty()) {
    return value;
  }
  if (!hBaseColumn.getCf().isPresent() || !hBaseColumn.getQualifier().isPresent()) {
    Map<String, String> columnMap = new HashMap<>();
    // parse column family, column, timestamp, and value
    for (Map.Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> entry : result.getMap().entrySet()) {
      String columnFamily = Bytes.toString(entry.getKey());
      for (Map.Entry<byte[], NavigableMap<Long, byte[]>> cells : entry.getValue().entrySet()) {
        String column = Bytes.toString(cells.getKey());
        NavigableMap<Long, byte[]> cell = cells.getValue();
        Map.Entry<Long, byte[]> v = cell.lastEntry();
        String columnValue = Bytes.toString(v.getValue());
        columnMap.put(columnFamily + ":" + column, columnValue);
      }
    }
    JSONObject json = new JSONObject(columnMap);
    value = json.toString();
  } else {
    value = Bytes.toString(result.getValue(hBaseColumn.getCf().get(), hBaseColumn.getQualifier().get()));
  }
  return value;
}
 
Example 3
Source File: StatsBasedCompactionPolicy.java    From ambry with Apache License 2.0 6 votes vote down vote up
/**
 * Finds the best candidate to compact by finding the candidate with the best cost benefit ratio
 * @param validDataPerLogSegments the valid data size for each log segment in the form of a {@link NavigableMap} of segment names to
 * valid data sizes.
 * @param segmentCapacity Segment capacity of one {@link LogSegment}
 * @param segmentHeaderSize Segment header size of a {@link LogSegment}
 * @param maxBlobSize max blob size to factor in when calculating the cost benefit ratio
 * @return the {@link CostBenefitInfo} for the best candidate to compact. {@code null} if there isn't any.
 */
private CostBenefitInfo getBestCandidateToCompact(NavigableMap<String, Long> validDataPerLogSegments,
    long segmentCapacity, long segmentHeaderSize, long maxBlobSize) {
  Map.Entry<String, Long> firstEntry = validDataPerLogSegments.firstEntry();
  Map.Entry<String, Long> lastEntry = validDataPerLogSegments.lastEntry();
  CostBenefitInfo bestCandidateToCompact = null;
  while (firstEntry != null) {
    Map.Entry<String, Long> endEntry = lastEntry;
    while (endEntry != null && LogSegmentNameHelper.COMPARATOR.compare(firstEntry.getKey(), endEntry.getKey()) <= 0) {
      CostBenefitInfo costBenefitInfo =
          getCostBenefitInfo(firstEntry.getKey(), endEntry.getKey(), validDataPerLogSegments, segmentCapacity,
              segmentHeaderSize, maxBlobSize);
      if (costBenefitInfo.getBenefit() >= storeConfig.storeMinLogSegmentCountToReclaimToTriggerCompaction && (
          bestCandidateToCompact == null
              || costBenefitInfo.getCostBenefitRatio().compareTo(bestCandidateToCompact.getCostBenefitRatio()) < 0)) {
        bestCandidateToCompact = costBenefitInfo;
        logger.trace("Updating best candidate to compact to {} ", bestCandidateToCompact);
      }
      endEntry = validDataPerLogSegments.lowerEntry(endEntry.getKey());
    }
    firstEntry = validDataPerLogSegments.higherEntry(firstEntry.getKey());
  }
  return bestCandidateToCompact;
}
 
Example 4
Source File: HBaseDao.java    From metron with Apache License 2.0 5 votes vote down vote up
private Document getDocumentFromResult(Result result) throws IOException {
  NavigableMap<byte[], byte[]> columns = result.getFamilyMap( cf);
  if(columns == null || columns.size() == 0) {
    return null;
  }
  Map.Entry<byte[], byte[]> entry= columns.lastEntry();
  Long ts = Bytes.toLong(entry.getKey());
  if(entry.getValue()!= null) {
    Map<String, Object> json = JSONUtils.INSTANCE.load(new String(entry.getValue(),
            StandardCharsets.UTF_8),
        JSONUtils.MAP_SUPPLIER);

    // Make sure comments are in the proper format
    @SuppressWarnings("unchecked")
    List<Map<String, Object>> commentsMap = (List<Map<String, Object>>) json.get(COMMENTS_FIELD);
    try {
      if (commentsMap != null) {
        List<AlertComment> comments = new ArrayList<>();
        for (Map<String, Object> commentMap : commentsMap) {
          comments.add(new AlertComment(commentMap));
        }
        if (comments.size() > 0) {
          json.put(COMMENTS_FIELD,
              comments.stream().map(AlertComment::asMap).collect(Collectors.toList()));
        }
      }
      Key k = Key.fromBytes(result.getRow());
      return new Document(json, k.getGuid(), k.getSensorType(), ts);
    } catch (IOException e) {
      throw new RuntimeException("Unable to convert row key to a document", e);
    }
  }
  else {
    return null;
  }
}
 
Example 5
Source File: KieRepositoryImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
synchronized KieModule load(InternalKieScanner kieScanner, ReleaseId releaseId, VersionRange versionRange) {
    String ga = releaseId.getGroupId() + ":" + releaseId.getArtifactId();

    NavigableMap<ComparableVersion, KieModule> artifactMap = kieModules.get(ga);
    if ( artifactMap == null || artifactMap.isEmpty() ) {
        return null;
    }
    KieModule kieModule = artifactMap.get(new ComparableVersion(releaseId.getVersion()));

    if (versionRange.fixed) {
        if ( kieModule != null && releaseId.isSnapshot() ) {
            String oldSnapshotVersion = ((ReleaseIdImpl)kieModule.getReleaseId()).getSnapshotVersion();
            if ( oldSnapshotVersion != null ) {
                String currentSnapshotVersion = kieScanner.getArtifactVersion(releaseId);
                if (currentSnapshotVersion != null &&
                    new ComparableVersion(currentSnapshotVersion).compareTo(new ComparableVersion(oldSnapshotVersion)) > 0) {
                    // if the snapshot currently available on the maven repo is newer than the cached one
                    // return null to enforce the building of this newer version
                    return null;
                }
            }
        }
        return kieModule;
    }

    Map.Entry<ComparableVersion, KieModule> entry =
            versionRange.upperBound == null ?
            artifactMap.lastEntry() :
            versionRange.upperInclusive ?
                artifactMap.floorEntry(new ComparableVersion(versionRange.upperBound)) :
                artifactMap.lowerEntry(new ComparableVersion(versionRange.upperBound));

    if ( entry == null ) {
        return null;
    }

    if ( versionRange.lowerBound == null ) {
        return entry.getValue();
    }

    int comparison = entry.getKey().compareTo(new ComparableVersion(versionRange.lowerBound));
    return comparison > 0 || (comparison == 0 && versionRange.lowerInclusive) ? entry.getValue() : null;
}