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

The following examples show how to use java.util.TreeMap#tailMap() . These examples are extracted from open source projects. 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
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 2
Source Project: datax-web   File: ExecutorRouteConsistentHash.java    License: 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 3
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 4
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
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 6
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 7
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
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 9
Source Project: timer   File: RouteByConsistentHash.java    License: 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 10
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 Project: james-project   File: MaildirFolder.java    License: 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
/**
 * 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 13
private static NavigableMap map0() {
    TreeMap map = new TreeMap();
    assertTrue(map.isEmpty());
    return map.tailMap(one, true);
}
 
Example 14
Source Project: hadoop   File: AMRMClientImpl.java    License: 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 15
Source Project: big-c   File: AMRMClientImpl.java    License: 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 Project: j2objc   File: TreeMapTest.java    License: 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 17
Source Project: j2objc   File: TreeMapTest.java    License: 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 18
Source Project: j2objc   File: TreeSubMapTest.java    License: 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);
}