Java Code Examples for java.util.TreeMap#tailMap()

The following examples show how to use java.util.TreeMap#tailMap() . 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: ExecutorRouteConsistentHash.java    From datax-web with MIT License 6 votes vote down vote up
public String hashJob(int jobId, List<String> addressList) {

        // ------A1------A2-------A3------
        // -----------J1------------------
        TreeMap<Long, String> addressRing = new TreeMap<Long, String>();
        for (String address: addressList) {
            for (int i = 0; i < VIRTUAL_NODE_NUM; i++) {
                long addressHash = hash("SHARD-" + address + "-NODE-" + i);
                addressRing.put(addressHash, address);
            }
        }

        long jobHash = hash(String.valueOf(jobId));
        SortedMap<Long, String> lastRing = addressRing.tailMap(jobHash);
        if (!lastRing.isEmpty()) {
            return lastRing.get(lastRing.firstKey());
        }
        return addressRing.firstEntry().getValue();
    }
 
Example 2
Source File: ExecutorRouteConsistentHash.java    From open-capacity-platform with Apache License 2.0 6 votes vote down vote up
public String route(int jobId, ArrayList<String> addressList) {

        // ------A1------A2-------A3------
        // -----------J1------------------
        TreeMap<Long, String> addressRing = new TreeMap<Long, String>();
        for (String address: addressList) {
            for (int i = 0; i < VIRTUAL_NODE_NUM; i++) {
                long addressHash = hash("SHARD-" + address + "-NODE-" + i);
                addressRing.put(addressHash, address);
            }
        }

        long jobHash = hash(String.valueOf(jobId));
        SortedMap<Long, String> lastRing = addressRing.tailMap(jobHash);
        if (!lastRing.isEmpty()) {
            return lastRing.get(lastRing.firstKey());
        }
        return addressRing.firstEntry().getValue();
    }
 
Example 3
Source File: ExecutorRouteConsistentHash.java    From microservices-platform with Apache License 2.0 6 votes vote down vote up
public String hashJob(int jobId, List<String> addressList) {

        // ------A1------A2-------A3------
        // -----------J1------------------
        TreeMap<Long, String> addressRing = new TreeMap<Long, String>();
        for (String address: addressList) {
            for (int i = 0; i < VIRTUAL_NODE_NUM; i++) {
                long addressHash = hash("SHARD-" + address + "-NODE-" + i);
                addressRing.put(addressHash, address);
            }
        }

        long jobHash = hash(String.valueOf(jobId));
        SortedMap<Long, String> lastRing = addressRing.tailMap(jobHash);
        if (!lastRing.isEmpty()) {
            return lastRing.get(lastRing.firstKey());
        }
        return addressRing.firstEntry().getValue();
    }
 
Example 4
Source File: ExecutorRouteConsistentHash.java    From zuihou-admin-boot with Apache License 2.0 6 votes vote down vote up
public String hashJob(int jobId, List<String> addressList) {

        // ------A1------A2-------A3------
        // -----------J1------------------
        TreeMap<Long, String> addressRing = new TreeMap<Long, String>();
        for (String address : addressList) {
            for (int i = 0; i < VIRTUAL_NODE_NUM; i++) {
                long addressHash = hash("SHARD-" + address + "-NODE-" + i);
                addressRing.put(addressHash, address);
            }
        }

        long jobHash = hash(String.valueOf(jobId));
        SortedMap<Long, String> lastRing = addressRing.tailMap(jobHash);
        if (!lastRing.isEmpty()) {
            return lastRing.get(lastRing.firstKey());
        }
        return addressRing.firstEntry().getValue();
    }
 
Example 5
Source File: TimelineMetricsCache.java    From ambari-metrics with Apache License 2.0 6 votes vote down vote up
public synchronized void putMetric(TimelineMetric metric) {
  TreeMap<Long, Double> metricValues = this.timelineMetric.getMetricValues();
  if (metricValues.size() > maxRecsPerName) {
    // remove values for eldest maxEvictionTimeInMillis
    long newEldestTimestamp = oldestTimestamp + maxEvictionTimeInMillis;
    TreeMap<Long, Double> metricsSubSet =
      new TreeMap<>(metricValues.tailMap(newEldestTimestamp));
    if (metricsSubSet.isEmpty()) {
      oldestTimestamp = metric.getStartTime();
      this.timelineMetric.setStartTime(metric.getStartTime());
    } else {
      Long newStartTime = metricsSubSet.firstKey();
      oldestTimestamp = newStartTime;
      this.timelineMetric.setStartTime(newStartTime);
    }
    this.timelineMetric.setMetricValues(metricsSubSet);
    LOG.warn("Metrics cache overflow. Values for metric " +
      metric.getMetricName() + " older than " + newEldestTimestamp +
      " were removed to clean up the cache.");
  }
  this.timelineMetric.addMetricValues(metric.getMetricValues());
  updateTimeDiff(metric.getStartTime());
}
 
Example 6
Source File: ExecutorRouteConsistentHash.java    From xmfcn-spring-cloud with Apache License 2.0 6 votes vote down vote up
public String hashJob(int jobId, List<String> addressList) {

        // ------A1------A2-------A3------
        // -----------J1------------------
        TreeMap<Long, String> addressRing = new TreeMap<Long, String>();
        for (String address: addressList) {
            for (int i = 0; i < VIRTUAL_NODE_NUM; i++) {
                long addressHash = hash("SHARD-" + address + "-NODE-" + i);
                addressRing.put(addressHash, address);
            }
        }

        long jobHash = hash(String.valueOf(jobId));
        SortedMap<Long, String> lastRing = addressRing.tailMap(jobHash);
        if (!lastRing.isEmpty()) {
            return lastRing.get(lastRing.firstKey());
        }
        return addressRing.firstEntry().getValue();
    }
 
Example 7
Source File: ExecutorRouteConsistentHash.java    From zuihou-admin-cloud with Apache License 2.0 6 votes vote down vote up
public String hashJob(int jobId, List<String> addressList) {

        // ------A1------A2-------A3------
        // -----------J1------------------
        TreeMap<Long, String> addressRing = new TreeMap<Long, String>();
        for (String address : addressList) {
            for (int i = 0; i < VIRTUAL_NODE_NUM; i++) {
                long addressHash = hash("SHARD-" + address + "-NODE-" + i);
                addressRing.put(addressHash, address);
            }
        }

        long jobHash = hash(String.valueOf(jobId));
        SortedMap<Long, String> lastRing = addressRing.tailMap(jobHash);
        if (!lastRing.isEmpty()) {
            return lastRing.get(lastRing.firstKey());
        }
        return addressRing.firstEntry().getValue();
    }
 
Example 8
Source File: RouteByConsistentHash.java    From timer with Apache License 2.0 6 votes vote down vote up
@Override
public String routeStrategy(Integer jobId, List<String> addressList) {
    TreeMap<Long, String> addressRing = new TreeMap<Long, String>();
    for (String address: addressList) {
        for (int i = 0; i < VIRTUAL_NODE_NUM; i++) {
            long addressHash = hash("SHARD-" + address + "-NODE-" + i);
            addressRing.put(addressHash, address);
        }
    }

    long jobHash = hash(String.valueOf(jobId));
    SortedMap<Long, String> lastRing = addressRing.tailMap(jobHash);
    if (!lastRing.isEmpty()) {
        return lastRing.get(lastRing.firstKey());
    }
    return addressRing.firstEntry().getValue();
}
 
Example 9
Source File: XxlRpcLoadBalanceConsistentHashStrategy.java    From datax-web with MIT License 6 votes vote down vote up
public String doRoute(String serviceKey, TreeSet<String> addressSet) {

        // ------A1------A2-------A3------
        // -----------J1------------------
        TreeMap<Long, String> addressRing = new TreeMap<Long, String>();
        for (String address: addressSet) {
            for (int i = 0; i < VIRTUAL_NODE_NUM; i++) {
                long addressHash = hash("SHARD-" + address + "-NODE-" + i);
                addressRing.put(addressHash, address);
            }
        }

        long jobHash = hash(serviceKey);
        SortedMap<Long, String> lastRing = addressRing.tailMap(jobHash);
        if (!lastRing.isEmpty()) {
            return lastRing.get(lastRing.firstKey());
        }
        return addressRing.firstEntry().getValue();
    }
 
Example 10
Source File: ExecutorRouteConsistentHash.java    From xxl-job with GNU General Public License v3.0 6 votes vote down vote up
public String hashJob(int jobId, List<String> addressList) {

        // ------A1------A2-------A3------
        // -----------J1------------------
        TreeMap<Long, String> addressRing = new TreeMap<Long, String>();
        for (String address: addressList) {
            for (int i = 0; i < VIRTUAL_NODE_NUM; i++) {
                long addressHash = hash("SHARD-" + address + "-NODE-" + i);
                addressRing.put(addressHash, address);
            }
        }

        long jobHash = hash(String.valueOf(jobId));
        SortedMap<Long, String> lastRing = addressRing.tailMap(jobHash);
        if (!lastRing.isEmpty()) {
            return lastRing.get(lastRing.firstKey());
        }
        return addressRing.firstEntry().getValue();
    }
 
Example 11
Source File: MaildirFolder.java    From james-project with Apache License 2.0 5 votes vote down vote up
/**
 * Sorts the given map and returns a subset which is constricted by a lower and an upper limit.
 * @param map The source map
 * @param from The lower limit
 * @param to The upper limit; <code>-1</code> disables the upper limit.
 * @return The sorted subset
 */
private SortedMap<MessageUid, MaildirMessageName> truncateMap(Map<MessageUid, MaildirMessageName> map, MessageUid from, MessageUid to) {
    TreeMap<MessageUid, MaildirMessageName> sortedMap;
    if (map instanceof TreeMap<?, ?>) {
        sortedMap = (TreeMap<MessageUid, MaildirMessageName>) map;
    } else {
        sortedMap = new TreeMap<>(map);
    }
    if (to != null) {
        return sortedMap.subMap(from, to.next());
    }
    return sortedMap.tailMap(from);
}
 
Example 12
Source File: TreeSubMapTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private static NavigableMap map0() {
    TreeMap map = new TreeMap();
    assertTrue(map.isEmpty());
    return map.tailMap(one, true);
}
 
Example 13
Source File: TreeMapTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * headMap returns map with keys in requested range
 */
public void testTailMapContents() {
    TreeMap map = map5();
    NavigableMap sm = map.tailMap(two, true);
    assertFalse(sm.containsKey(one));
    assertTrue(sm.containsKey(two));
    assertTrue(sm.containsKey(three));
    assertTrue(sm.containsKey(four));
    assertTrue(sm.containsKey(five));
    Iterator i = sm.keySet().iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(two, k);
    k = (Integer)(i.next());
    assertEquals(three, k);
    k = (Integer)(i.next());
    assertEquals(four, k);
    k = (Integer)(i.next());
    assertEquals(five, k);
    assertFalse(i.hasNext());
    Iterator r = sm.descendingKeySet().iterator();
    k = (Integer)(r.next());
    assertEquals(five, k);
    k = (Integer)(r.next());
    assertEquals(four, k);
    k = (Integer)(r.next());
    assertEquals(three, k);
    k = (Integer)(r.next());
    assertEquals(two, k);
    assertFalse(r.hasNext());

    Iterator ei = sm.entrySet().iterator();
    Map.Entry e;
    e = (Map.Entry)(ei.next());
    assertEquals(two, e.getKey());
    assertEquals("B", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(three, e.getKey());
    assertEquals("C", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(four, e.getKey());
    assertEquals("D", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(five, e.getKey());
    assertEquals("E", e.getValue());
    assertFalse(i.hasNext());

    NavigableMap ssm = sm.tailMap(four, true);
    assertEquals(four, ssm.firstKey());
    assertEquals(five, ssm.lastKey());
    assertEquals("D", ssm.remove(four));
    assertEquals(1, ssm.size());
    assertEquals(3, sm.size());
    assertEquals(4, map.size());
}
 
Example 14
Source File: TreeMapTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public void test_empty_subMap() throws Exception {
    TreeMap<Float, List<Integer>> tm = new TreeMap<Float, List<Integer>>();
    SortedMap<Float, List<Integer>> sm = tm.tailMap(1.1f);
    assertTrue(sm.values().size() == 0);
}
 
Example 15
Source File: AMRMClientImpl.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized List<? extends Collection<T>> getMatchingRequests(
                                        Priority priority, 
                                        String resourceName, 
                                        Resource capability) {
  Preconditions.checkArgument(capability != null,
      "The Resource to be requested should not be null ");
  Preconditions.checkArgument(priority != null,
      "The priority at which to request containers should not be null ");
  List<LinkedHashSet<T>> list = new LinkedList<LinkedHashSet<T>>();
  Map<String, TreeMap<Resource, ResourceRequestInfo>> remoteRequests = 
      this.remoteRequestsTable.get(priority);
  if (remoteRequests == null) {
    return list;
  }
  TreeMap<Resource, ResourceRequestInfo> reqMap = remoteRequests
      .get(resourceName);
  if (reqMap == null) {
    return list;
  }

  ResourceRequestInfo resourceRequestInfo = reqMap.get(capability);
  if (resourceRequestInfo != null &&
      !resourceRequestInfo.containerRequests.isEmpty()) {
    list.add(resourceRequestInfo.containerRequests);
    return list;
  }
  
  // no exact match. Container may be larger than what was requested.
  // get all resources <= capability. map is reverse sorted. 
  SortedMap<Resource, ResourceRequestInfo> tailMap = 
                                                reqMap.tailMap(capability);
  for(Map.Entry<Resource, ResourceRequestInfo> entry : tailMap.entrySet()) {
    if (canFit(entry.getKey(), capability) &&
        !entry.getValue().containerRequests.isEmpty()) {
      // match found that fits in the larger resource
      list.add(entry.getValue().containerRequests);
    }
  }
  
  // no match found
  return list;          
}
 
Example 16
Source File: AMRMClientImpl.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized List<? extends Collection<T>> getMatchingRequests(
                                        Priority priority, 
                                        String resourceName, 
                                        Resource capability) {
  Preconditions.checkArgument(capability != null,
      "The Resource to be requested should not be null ");
  Preconditions.checkArgument(priority != null,
      "The priority at which to request containers should not be null ");
  List<LinkedHashSet<T>> list = new LinkedList<LinkedHashSet<T>>();
  Map<String, TreeMap<Resource, ResourceRequestInfo>> remoteRequests = 
      this.remoteRequestsTable.get(priority);
  if (remoteRequests == null) {
    return list;
  }
  TreeMap<Resource, ResourceRequestInfo> reqMap = remoteRequests
      .get(resourceName);
  if (reqMap == null) {
    return list;
  }

  ResourceRequestInfo resourceRequestInfo = reqMap.get(capability);
  if (resourceRequestInfo != null &&
      !resourceRequestInfo.containerRequests.isEmpty()) {
    list.add(resourceRequestInfo.containerRequests);
    return list;
  }
  
  // no exact match. Container may be larger than what was requested.
  // get all resources <= capability. map is reverse sorted. 
  SortedMap<Resource, ResourceRequestInfo> tailMap = 
                                                reqMap.tailMap(capability);
  for(Map.Entry<Resource, ResourceRequestInfo> entry : tailMap.entrySet()) {
    if (canFit(entry.getKey(), capability) &&
        !entry.getValue().containerRequests.isEmpty()) {
      // match found that fits in the larger resource
      list.add(entry.getValue().containerRequests);
    }
  }
  
  // no match found
  return list;          
}
 
Example 17
Source File: TreeSubMapTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static NavigableMap map0() {
    TreeMap map = new TreeMap();
    assertTrue(map.isEmpty());
    return map.tailMap(one, true);
}
 
Example 18
Source File: TreeMapTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * headMap returns map with keys in requested range
 */
public void testTailMapContents() {
    TreeMap map = map5();
    NavigableMap sm = map.tailMap(two, true);
    assertFalse(sm.containsKey(one));
    assertTrue(sm.containsKey(two));
    assertTrue(sm.containsKey(three));
    assertTrue(sm.containsKey(four));
    assertTrue(sm.containsKey(five));
    Iterator i = sm.keySet().iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(two, k);
    k = (Integer)(i.next());
    assertEquals(three, k);
    k = (Integer)(i.next());
    assertEquals(four, k);
    k = (Integer)(i.next());
    assertEquals(five, k);
    assertFalse(i.hasNext());
    Iterator r = sm.descendingKeySet().iterator();
    k = (Integer)(r.next());
    assertEquals(five, k);
    k = (Integer)(r.next());
    assertEquals(four, k);
    k = (Integer)(r.next());
    assertEquals(three, k);
    k = (Integer)(r.next());
    assertEquals(two, k);
    assertFalse(r.hasNext());

    Iterator ei = sm.entrySet().iterator();
    Map.Entry e;
    e = (Map.Entry)(ei.next());
    assertEquals(two, e.getKey());
    assertEquals("B", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(three, e.getKey());
    assertEquals("C", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(four, e.getKey());
    assertEquals("D", e.getValue());
    e = (Map.Entry)(ei.next());
    assertEquals(five, e.getKey());
    assertEquals("E", e.getValue());
    assertFalse(i.hasNext());

    NavigableMap ssm = sm.tailMap(four, true);
    assertEquals(four, ssm.firstKey());
    assertEquals(five, ssm.lastKey());
    assertEquals("D", ssm.remove(four));
    assertEquals(1, ssm.size());
    assertEquals(3, sm.size());
    assertEquals(4, map.size());
}