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

The following examples show how to use java.util.NavigableMap#ceilingEntry() . 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: TreeSubMapTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ceilingEntry returns next entry.
 */
public void testCeilingEntry() {
    NavigableMap 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);
}
 
Example 2
Source File: TreeSubMapTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ceilingEntry returns next entry.
 */
public void testDescendingCeilingEntry() {
    NavigableMap map = dmap5();
    Map.Entry e1 = map.ceilingEntry(m3);
    assertEquals(m3, e1.getKey());

    Map.Entry e2 = map.ceilingEntry(zero);
    assertEquals(m1, e2.getKey());

    Map.Entry e3 = map.ceilingEntry(m5);
    assertEquals(m5, e3.getKey());

    Map.Entry e4 = map.ceilingEntry(m6);
    assertNull(e4);
}
 
Example 3
Source File: SpanCrossSentenceBehavior.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Override
public void onRender(TypeAdapter aAdapter, VDocument aResponse,
        Map<AnnotationFS, VSpan> annoToSpanIdx, int aPageBegin, int aPageEnd)
{
    if (aAdapter.getLayer().isCrossSentence() || annoToSpanIdx.isEmpty()) {
        return;
    }
    
    CAS cas = annoToSpanIdx.entrySet().iterator().next().getKey().getCAS();
    
    // Build indexes to allow quickly looking up the sentence by its begin/end offsets. Since
    // The indexes are navigable, we can also find the sentences starting/ending closes to a
    // particular offset, even if it is not the start/end offset of a sentence.
    NavigableMap<Integer, AnnotationFS> sentBeginIdx = new TreeMap<>();
    NavigableMap<Integer, AnnotationFS> sentEndIdx = new TreeMap<>();
    for (AnnotationFS sent : selectOverlapping(cas, getType(cas, Sentence.class), aPageBegin,
            aPageEnd)) {
        sentBeginIdx.put(sent.getBegin(), sent);
        sentEndIdx.put(sent.getEnd(), sent);
    }
    
    for (AnnotationFS fs : annoToSpanIdx.keySet()) {
        Entry<Integer, AnnotationFS> s1 = sentBeginIdx.floorEntry(fs.getBegin());
        Entry<Integer, AnnotationFS> s2 = sentEndIdx.ceilingEntry(fs.getEnd());
        
        if (s1 == null || s2 == null) {
            // Unable to determine any sentences overlapping with the annotation
            continue;
        }
        
        if (!WebAnnoCasUtil.isSame(s1.getValue(), s2.getValue())) {
            aResponse.add(new VComment(new VID(fs), ERROR,
                    "Crossing sentence boundaries is not permitted."));
        }
    }
}
 
Example 4
Source File: DeltaStoreBasedWaveletState.java    From swellrt with Apache License 2.0 5 votes vote down vote up
/**
 * @return An entry keyed by a hashed version with the given version number,
 *         if any, otherwise null.
 */
private static <T> Map.Entry<HashedVersion, T> lookupCached(NavigableMap<HashedVersion, T> map,
    long version) {
  // Smallest key with version number >= version.
  HashedVersion key = HashedVersion.unsigned(version);
  Map.Entry<HashedVersion, T> entry = map.ceilingEntry(key);
  return (entry != null && entry.getKey().getVersion() == version) ? entry : null;
}
 
Example 5
Source File: TreeSubMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * ceilingEntry returns next entry.
 */
public void testCeilingEntry() {
    NavigableMap 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);
}
 
Example 6
Source File: TreeSubMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * ceilingEntry returns next entry.
 */
public void testDescendingCeilingEntry() {
    NavigableMap map = dmap5();
    Map.Entry e1 = map.ceilingEntry(m3);
    assertEquals(m3, e1.getKey());

    Map.Entry e2 = map.ceilingEntry(zero);
    assertEquals(m1, e2.getKey());

    Map.Entry e3 = map.ceilingEntry(m5);
    assertEquals(m5, e3.getKey());

    Map.Entry e4 = map.ceilingEntry(m6);
    assertNull(e4);
}
 
Example 7
Source File: DeltaStoreBasedWaveletState.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
/**
 * @return An entry keyed by a hashed version with the given version number,
 *         if any, otherwise null.
 */
private static <T> Map.Entry<HashedVersion, T> lookupCached(NavigableMap<HashedVersion, T> map,
    long version) {
  // Smallest key with version number >= version.
  HashedVersion key = HashedVersion.unsigned(version);
  Map.Entry<HashedVersion, T> entry = map.ceilingEntry(key);
  return (entry != null && entry.getKey().getVersion() == version) ? entry : null;
}
 
Example 8
Source File: InMemoryDBTables.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
private Iterator<BackendEntry> betweenQuery(Id indexLabelId,
                                            Object keyMax,
                                            boolean keyMaxEq,
                                            Object keyMin,
                                            boolean keyMinEq,
                                            HugeType type) {
    NavigableMap<Id, BackendEntry> rs = this.store();

    E.checkArgument(keyMin != null || keyMax != null,
                    "Please specify at least one condition");
    if (keyMin == null) {
        // Field value < keyMax
        keyMin = NumericUtil.minValueOf(keyMax.getClass());
    }
    Id min = HugeIndex.formatIndexId(type, indexLabelId, keyMin);

    if (keyMax == null) {
        // Field value > keyMin
        keyMaxEq = false;
        indexLabelId = IdGenerator.of(indexLabelId.asLong() + 1L);
        keyMax = NumericUtil.minValueOf(keyMin.getClass());
    }
    Id max = HugeIndex.formatIndexId(type, indexLabelId, keyMax);

    max = keyMaxEq ? rs.floorKey(max) : rs.lowerKey(max);
    if (max == null) {
        return QueryResults.emptyIterator();
    }

    Map<Id, BackendEntry> results = InsertionOrderUtil.newMap();
    Map.Entry<Id, BackendEntry> entry = keyMinEq ?
                                        rs.ceilingEntry(min) :
                                        rs.higherEntry(min);
    while (entry != null) {
        if (entry.getKey().compareTo(max) > 0) {
            break;
        }
        results.put(entry.getKey(), entry.getValue());
        entry = rs.higherEntry(entry.getKey());
    }
    return results.values().iterator();
}
 
Example 9
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 10
Source File: SpanCrossSentenceBehavior.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Override
public List<Pair<LogMessage, AnnotationFS>> onValidate(TypeAdapter aAdapter, CAS aCas)
{
    // If crossing sentence boundaries is permitted, then there is nothing to validate here
    if (aAdapter.getLayer().isCrossSentence()) {
        return emptyList();
    }
    
    Type type = getType(aCas, aAdapter.getAnnotationTypeName());
    
    // If there are no annotations on this layer, nothing to do
    Collection<AnnotationFS> annotations = select(aCas, type);
    if (annotations.isEmpty()) {
        return emptyList();
    }

    // Prepare feedback messsage list
    List<Pair<LogMessage, AnnotationFS>> messages = new ArrayList<>();

    // Build indexes to allow quickly looking up the sentence by its begin/end offsets. Since
    // The indexes are navigable, we can also find the sentences starting/ending closes to a
    // particular offset, even if it is not the start/end offset of a sentence.
    NavigableMap<Integer, AnnotationFS> sentBeginIdx = new TreeMap<>();
    NavigableMap<Integer, AnnotationFS> sentEndIdx = new TreeMap<>();
    for (AnnotationFS sent : select(aCas, getType(aCas, Sentence.class))) {
        sentBeginIdx.put(sent.getBegin(), sent);
        sentEndIdx.put(sent.getEnd(), sent);
    }
    
    for (AnnotationFS fs : annotations) {
        Entry<Integer, AnnotationFS> s1 = sentBeginIdx.floorEntry(fs.getBegin());
        Entry<Integer, AnnotationFS> s2 = sentEndIdx.ceilingEntry(fs.getEnd());
        
        if (s1 == null || s2 == null) {
            messages.add(Pair.of(LogMessage.error(this,
                    "Unable to determine any sentences overlapping with [%d-%d]", fs.getBegin(),
                    fs.getEnd()), fs));
            continue;
        }
        
        if (!WebAnnoCasUtil.isSame(s1.getValue(), s2.getValue())) {
            messages.add(Pair.of(
                    LogMessage.error(this, "Crossing sentence boundaries is not permitted."),
                    fs));
        }
    }

    return messages;
}
 
Example 11
Source File: RelationCrossSentenceBehavior.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Override
public List<Pair<LogMessage, AnnotationFS>> onValidate(TypeAdapter aAdapter, CAS aCas)
{
    // If crossing sentence boundaries is permitted, then there is nothing to validate here
    if (aAdapter.getLayer().isCrossSentence()) {
        return emptyList();
    }
    
    RelationAdapter adapter = (RelationAdapter) aAdapter;
    Type type = getType(aCas, aAdapter.getAnnotationTypeName());
    Feature targetFeature = type.getFeatureByBaseName(adapter.getTargetFeatureName());
    Feature sourceFeature = type.getFeatureByBaseName(adapter.getSourceFeatureName());
    
    // If there are no annotations on this layer, nothing to do
    Collection<AnnotationFS> annotations = select(aCas, type);
    if (annotations.isEmpty()) {
        return emptyList();
    }

    // Prepare feedback messsage list
    List<Pair<LogMessage, AnnotationFS>> messages = new ArrayList<>();

    // Build indexes to allow quickly looking up the sentence by its begin/end offsets. Since
    // The indexes are navigable, we can also find the sentences starting/ending closes to a
    // particular offset, even if it is not the start/end offset of a sentence.
    NavigableMap<Integer, AnnotationFS> sentBeginIdx = new TreeMap<>();
    NavigableMap<Integer, AnnotationFS> sentEndIdx = new TreeMap<>();
    for (AnnotationFS sent : select(aCas, getType(aCas, Sentence.class))) {
        sentBeginIdx.put(sent.getBegin(), sent);
        sentEndIdx.put(sent.getEnd(), sent);
    }
    
    for (AnnotationFS fs : annotations) {
        AnnotationFS sourceFs = (AnnotationFS) fs.getFeatureValue(sourceFeature);
        AnnotationFS targetFs = (AnnotationFS) fs.getFeatureValue(targetFeature);

        Entry<Integer, AnnotationFS> s1 = sentBeginIdx.floorEntry(sourceFs.getBegin());
        Entry<Integer, AnnotationFS> s2 = sentEndIdx.ceilingEntry(targetFs.getEnd());
        
        if (s1 == null || s2 == null) {
            messages.add(Pair.of(LogMessage.error(this,
                    "Unable to determine any sentences overlapping with [%d-%d]",
                    sourceFs.getBegin(), targetFs.getEnd()), fs));
            continue;
        }
        
        if (!WebAnnoCasUtil.isSame(s1.getValue(), s2.getValue())) {
            messages.add(Pair.of(
                    LogMessage.error(this, "Crossing sentence boundaries is not permitted."),
                    fs));
        }
    }

    return messages;
    
}