Java Code Examples for java.util.SortedSet#headSet()

The following examples show how to use java.util.SortedSet#headSet() . 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
void intersect(SortedSet<String> intersection, SortedSet<String> arg) {
	Collection<StringPair> replaces = new ArrayList<StringPair>();
	for (String s : intersection) {
		String sb = s + Character.MAX_VALUE;
		if (!arg.subSet(s, sb).isEmpty()) continue;
		SortedSet<String> head = arg.headSet(s);
		if (!head.isEmpty() && s.startsWith(head.last())) {
			replaces.add(new StringPair(s, head.last()));
		} else {
			replaces.add(new StringPair(s, null));
		}
	}
	for (StringPair p : replaces) {
		intersection.remove(p.value0);
		if (p.value1 != null) {
			intersection.add(p.value1);
		}
	}
}
 
Example 2
@Override
public Collection<Map.Entry<Window.SessionWindow<K>, V>> getSessionEntries(K key, long timestamp, long gap)
{
  List<Map.Entry<Window.SessionWindow<K>, V>> results = new ArrayList<>();
  SortedSet<Window.SessionWindow<K>> sessionWindows = keyToWindows.get(key);
  if (sessionWindows != null) {
    Window.SessionWindow<K> refWindow = new Window.SessionWindow<>(key, timestamp, gap);
    SortedSet<Window.SessionWindow<K>> headSet = sessionWindows.headSet(refWindow);
    if (!headSet.isEmpty()) {
      Window.SessionWindow<K> lower = headSet.last();
      if (lower.getBeginTimestamp() + lower.getDurationMillis() > timestamp) {
        results.add(new AbstractMap.SimpleEntry<>(lower, map.get(lower).get(key)));
      }
    }
    SortedSet<Window.SessionWindow<K>> tailSet = sessionWindows.tailSet(refWindow);
    if (!tailSet.isEmpty()) {
      Window.SessionWindow<K> higher = tailSet.first();
      if (higher.getBeginTimestamp() - gap <= timestamp) {
        results.add(new AbstractMap.SimpleEntry<>(higher, map.get(higher).get(key)));
      }
    }
  }
  return results;
}
 
Example 3
Source Project: geowave   File: FileSystemUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static SortedSet<Pair<FileSystemKey, Path>> getSortedSet(
    final Path subDirectory,
    final byte[] startKeyInclusive,
    final byte[] endKeyExclusive,
    final Function<String, FileSystemKey> fileNameToKey) {
  try {
    final Supplier<SortedSet<Pair<FileSystemKey, Path>>> sortedSetFactory = () -> new TreeSet<>();
    SortedSet<Pair<FileSystemKey, Path>> sortedSet =
        Files.walk(subDirectory).filter(Files::isRegularFile).map(
            path -> Pair.of(fileNameToKey.apply(path.getFileName().toString()), path)).collect(
                Collectors.toCollection(sortedSetFactory));
    if (startKeyInclusive != null) {
      sortedSet = sortedSet.tailSet(Pair.of(new BasicFileSystemKey(startKeyInclusive), null));
    }
    if (endKeyExclusive != null) {
      sortedSet = sortedSet.headSet(Pair.of(new BasicFileSystemKey(endKeyExclusive), null));
    }
    return sortedSet;
  } catch (final IOException e) {
    LOGGER.warn("Unable to iterate through file system", e);
  }
  return new TreeSet<>();
}
 
Example 4
Source Project: codebuff   File: Sets.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public E last() {
  SortedSet<E> sortedUnfiltered = (SortedSet<E>) unfiltered;
  while (true) {
    E element = sortedUnfiltered.last();
    if (predicate.apply(element)) {
      return element;
    }
    sortedUnfiltered = sortedUnfiltered.headSet(element);
  }
}
 
Example 5
Source Project: codebuff   File: Sets.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public E last() {
  SortedSet<E> sortedUnfiltered = (SortedSet<E>) unfiltered;
  while (true) {
    E element = sortedUnfiltered.last();
    if (predicate.apply(element)) {
      return element;
    }
    sortedUnfiltered = sortedUnfiltered.headSet(element);
  }
}
 
Example 6
Source Project: jstorm   File: BatchCache.java    License: Apache License 2.0 5 votes vote down vote up
public void removeExpiredBatches(long lastSuccessfulBatch) {
    SortedSet<Long> pendingBatchIds = new TreeSet<>(pendingBatches.keySet());
    SortedSet<Long> expiredBatchIds = pendingBatchIds.headSet(lastSuccessfulBatch + 1);
  
    if (!expiredBatchIds.isEmpty())
        LOG.info("Following expired batches will be removed: {}", expiredBatchIds);
    for (Long expiredBatchId : expiredBatchIds) {
        PendingBatch expiredBatch = pendingBatches.remove(expiredBatchId);
        expiredBatch.removeTuples();
    } 
}
 
Example 7
Source Project: codebuff   File: Sets.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public E last() {
  SortedSet<E> sortedUnfiltered = (SortedSet<E>) unfiltered;
  while (true) {
    E element = sortedUnfiltered.last();
    if (predicate.apply(element)) {
      return element;
    }
    sortedUnfiltered = sortedUnfiltered.headSet(element);
  }
}
 
Example 8
Source Project: codebuff   File: Sets.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public E last() {
  SortedSet<E> sortedUnfiltered = (SortedSet<E>) unfiltered;
  while (true) {
    E element = sortedUnfiltered.last();
    if (predicate.apply(element)) {
      return element;
    }
    sortedUnfiltered = sortedUnfiltered.headSet(element);
  }
}
 
Example 9
private void testHeadSet(SortedSet<Integer> groundTruth, SortedSet<Integer> testSet, Random rand, int depth)
{
    if(groundTruth.isEmpty() || groundTruth.last() - groundTruth.first() <= 0 || groundTruth.last() <= 0)//avoid bad tests
        return;
    int toElement = rand.nextInt(groundTruth.last());
    
    SortedSet<Integer> g_s = groundTruth.headSet(toElement);
    SortedSet<Integer> t_s = testSet.headSet(toElement);
    
    assertSameContent(g_s, t_s);
    for(int i = 0; i < 5; i++)
    {
        int new_val;
        if(toElement <= 0)
            new_val = Math.min(toElement-1, -rand.nextInt(1000));
        else
            new_val = rand.nextInt(toElement);
        g_s.add(new_val);
        t_s.add(new_val);
    }
    assertSameContent(g_s, t_s);
    assertSameContent(groundTruth, testSet);
    
    if(depth-- > 0)
        testHeadSet(g_s, t_s, rand, depth);
    assertSameContent(groundTruth, testSet);
}
 
Example 10
Source Project: kylin   File: QueryContextFacade.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @param runningTime in milliseconds
 * @return running queries that have run more than specified time
 */
public static TreeSet<QueryContext> getLongRunningQueries(long runningTime) {
    SortedSet<QueryContext> allRunningQueries = getAllRunningQueries();
    QueryContext tmpCtx = new QueryContext(KylinConfig.getInstanceFromEnv().getHBaseMaxConnectionThreadsPerQuery(),
            runningTime + 1L); // plus 1 to include those contexts in same accumulatedMills but different uuid
    return (TreeSet<QueryContext>) allRunningQueries.headSet(tmpCtx);
}
 
Example 11
/** Workaround for NavigatableSet interface which is @since java 1.6
 */

private static <T> T NavigableSetFloor(SortedSet<T> sortedSet, T e) {
	if (sortedSet.contains(e)) {
		return e;
	}
	SortedSet<T> headSet = sortedSet.headSet(e);
	if (headSet.isEmpty()) {
		return null;
	}
	return headSet.last();
}
 
Example 12
Source Project: hbase   File: NewVersionBehaviorTracker.java    License: Apache License 2.0 5 votes vote down vote up
public void addVersionDelete(Cell cell) {
  SortedSet<Long> set = deletesMap.get(cell.getTimestamp());
  if (set == null) {
    set = new TreeSet<>();
    deletesMap.put(cell.getTimestamp(), set);
  }
  set.add(cell.getSequenceId());
  // The init set should be the puts whose mvcc is smaller than this Delete. Because
  // there may be some Puts masked by them. The Puts whose mvcc is larger than this Delete can
  // not be copied to this node because we may delete one version and the oldest put may not be
  // masked.
  SortedSet<Long> nextValue = mvccCountingMap.ceilingEntry(cell.getSequenceId()).getValue();
  SortedSet<Long> thisValue = new TreeSet<>(nextValue.headSet(cell.getSequenceId()));
  mvccCountingMap.put(cell.getSequenceId(), thisValue);
}
 
Example 13
Source Project: tutorials   File: WhenUsingTreeSet.java    License: MIT License 5 votes vote down vote up
@Test
public void whenUsingHeadSet_shouldReturnHeadSetElements() {
    SortedSet<Integer> treeSet = new TreeSet<>();
    treeSet.add(1);
    treeSet.add(2);
    treeSet.add(3);
    treeSet.add(4);
    treeSet.add(5);
    treeSet.add(6);

    Set<Integer> subSet = treeSet.headSet(6);
    Assert.assertEquals(subSet, treeSet.subSet(1, 6));
}
 
Example 14
public void watch() throws RegistryException {
  SortedSet<LeaderId> leaderIds = getSortedLockIds() ;
  LeaderId ownerId = leaderIds.first() ;
  if(ownerId.equals(leaderId)) {
    listener.onElected();
    elected = true ;
    return ;
  }
  SortedSet<LeaderId> lessThanMe = leaderIds.headSet(leaderId);
  LeaderId previousLock = lessThanMe.last();
  registry.watchExists(previousLock.getPath(), this);
}
 
Example 15
/**
 * @param runningTime in milliseconds
 * @return running queries that have run more than specified time
 */
public static TreeSet<QueryContext> getLongRunningQueries(long runningTime) {
    SortedSet<QueryContext> allRunningQueries = getAllRunningQueries();
    QueryContext tmpCtx = new QueryContext(runningTime + 1L); // plus 1 to include those contexts in same accumulatedMills but different uuid
    return (TreeSet<QueryContext>) allRunningQueries.headSet(tmpCtx);
}
 
Example 16
/**
 * Optimize the broker if the disk usage of the broker is not within the required range.
 *
 * @param allBrokers a sorted set of all the alive brokers in the cluster.
 * @param toOptimize the broker to optimize
 * @param clusterModel the cluster model
 * @param meanDiskUsage the average disk usage of the cluster
 * @param lowerThreshold the lower limit of the disk usage for a broker
 * @param upperThreshold the upper limit of the disk usage for a broker
 * @param excludedTopics the topics to exclude from movement.
 *
 * @return True if an action has been taken to improve the disk usage of the broker, false when a broker cannot or
 * does not need to be improved further.
 */
private boolean checkAndOptimize(SortedSet<BrokerAndSortedReplicas> allBrokers,
                                 BrokerAndSortedReplicas toOptimize,
                                 ClusterModel clusterModel,
                                 double meanDiskUsage,
                                 double lowerThreshold,
                                 double upperThreshold,
                                 Set<String> excludedTopics) {
  if (LOG.isTraceEnabled()) {
    LOG.trace("Optimizing broker {}. BrokerDiskUsage = {}, meanDiskUsage = {}",
              toOptimize.broker(), dWrap(diskUsage(toOptimize.broker())), dWrap(meanDiskUsage));
  }
  double brokerDiskUsage = diskUsage(toOptimize.broker());
  boolean improved = false;
  List<BrokerAndSortedReplicas> candidateBrokersToSwapWith;

  if (brokerDiskUsage > upperThreshold) {
    if (LOG.isDebugEnabled()) {
      LOG.debug("Broker {} disk usage {} is above upper threshold of {}",
                toOptimize.broker().id(), dWrap(brokerDiskUsage), dWrap(upperThreshold));
    }
    // Get the brokers whose disk usage is less than the broker to optimize. The list is in ascending order based on
    // broker disk usage.
    candidateBrokersToSwapWith = new ArrayList<>(allBrokers.headSet(toOptimize));

  } else if (brokerDiskUsage < lowerThreshold) {
    if (LOG.isDebugEnabled()) {
      LOG.debug("Broker {} disk usage {} is below lower threshold of {}",
                toOptimize.broker().id(), dWrap(brokerDiskUsage), dWrap(lowerThreshold));
    }
    // Get the brokers whose disk usage is more than the broker to optimize. The list is in descending order based on
    // broker disk usage.
    candidateBrokersToSwapWith = new ArrayList<>(allBrokers.tailSet(toOptimize));
    Collections.reverse(candidateBrokersToSwapWith);
  } else {
    // Nothing to optimize.
    return false;
  }

  for (BrokerAndSortedReplicas toSwapWith : candidateBrokersToSwapWith) {
    if (toSwapWith == toOptimize || Math.abs(diskUsage(toSwapWith) - diskUsage(toOptimize)) < USAGE_EQUALITY_DELTA) {
      continue;
    }
    // Remove the brokers involved in swap from the tree set before swap.
    allBrokers.removeAll(Arrays.asList(toOptimize, toSwapWith));
    try {
      if (swapReplicas(toOptimize, toSwapWith, meanDiskUsage, clusterModel, excludedTopics)) {
        improved = true;
        break;
      }
    } finally {
      // Add the brokers back to the tree set after the swap.
      allBrokers.addAll(Arrays.asList(toOptimize, toSwapWith));
    }
  }
  return improved;
}
 
Example 17
Source Project: MultimediaDesktop   File: WriteLock.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * the command that is run and retried for actually 
 * obtaining the lock
 * @return if the command was successful or not
 */
public boolean execute() throws KeeperException, InterruptedException {
    do {
        if (id == null) {
            long sessionId = zookeeper.getSessionId();
            String prefix = "x-" + sessionId + "-";
            // lets try look up the current ID if we failed 
            // in the middle of creating the znode
            findPrefixInChildren(prefix, zookeeper, dir);
            idName = new ZNodeName(id);
        }
        if (id != null) {
            List<String> names = zookeeper.getChildren(dir, false);
            if (names.isEmpty()) {
                LOG.warn("No children in: " + dir + " when we've just " +
                "created one! Lets recreate it...");
                // lets force the recreation of the id
                id = null;
            } else {
                // lets sort them explicitly (though they do seem to come back in order ususally :)
                SortedSet<ZNodeName> sortedNames = new TreeSet<ZNodeName>();
                for (String name : names) {
                    sortedNames.add(new ZNodeName(dir + "/" + name));
                }
                ownerId = sortedNames.first().getName();
                SortedSet<ZNodeName> lessThanMe = sortedNames.headSet(idName);
                if (!lessThanMe.isEmpty()) {
                    ZNodeName lastChildName = lessThanMe.last();
                    lastChildId = lastChildName.getName();
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("watching less than me node: " + lastChildId);
                    }
                    Stat stat = zookeeper.exists(lastChildId, new LockWatcher());
                    if (stat != null) {
                        return Boolean.FALSE;
                    } else {
                        LOG.warn("Could not find the" +
                        		" stats for less than me: " + lastChildName.getName());
                    }
                } else {
                    if (isOwner()) {
                        if (callback != null) {
                            callback.lockAcquired();
                        }
                        return Boolean.TRUE;
                    }
                }
            }
        }
    }
    while (id == null);
    return Boolean.FALSE;
}
 
Example 18
public void watch(SortedSet<LockId> currentLockIds) throws RegistryException {
  SortedSet<LockId> lessThanMe = currentLockIds.headSet(lockId);
  LockId previousLock = lessThanMe.last();
  registry.watchExists(previousLock.getPath(), this);
}
 
Example 19
Source Project: disconf   File: WriteLock.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * the command that is run and retried for actually
 * obtaining the lock
 *
 * @return if the command was successful or not
 */
public boolean execute() throws KeeperException, InterruptedException {
    do {
        if (id == null) {
            long sessionId = zookeeper.getSessionId();
            String prefix = "x-" + sessionId + "-";
            // lets try look up the current ID if we failed 
            // in the middle of creating the znode
            findPrefixInChildren(prefix, zookeeper, dir);
            idName = new ZNodeName(id);
        }
        if (id != null) {
            List<String> names = zookeeper.getChildren(dir, false);
            if (names.isEmpty()) {
                LOG.warn("No children in: " + dir + " when we've just " +
                             "created one! Lets recreate it...");
                // lets force the recreation of the id
                id = null;
            } else {
                // lets sort them explicitly (though they do seem to come back in order ususally :)
                SortedSet<ZNodeName> sortedNames = new TreeSet<ZNodeName>();
                for (String name : names) {
                    sortedNames.add(new ZNodeName(dir + "/" + name));
                }
                ownerId = sortedNames.first().getName();
                SortedSet<ZNodeName> lessThanMe = sortedNames.headSet(idName);
                if (!lessThanMe.isEmpty()) {
                    ZNodeName lastChildName = lessThanMe.last();
                    lastChildId = lastChildName.getName();
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("watching less than me node: " + lastChildId);
                    }
                    Stat stat = zookeeper.exists(lastChildId, new LockWatcher());
                    if (stat != null) {
                        return Boolean.FALSE;
                    } else {
                        LOG.warn("Could not find the" +
                                     " stats for less than me: " + lastChildName.getName());
                    }
                } else {
                    if (isOwner()) {
                        if (callback != null) {
                            callback.lockAcquired();
                        }
                        return Boolean.TRUE;
                    }
                }
            }
        }
    } while (id == null);
    return Boolean.FALSE;
}
 
Example 20
Source Project: disconf   File: WriteLock.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * the command that is run and retried for actually
 * obtaining the lock
 *
 * @return if the command was successful or not
 */
public boolean execute() throws KeeperException, InterruptedException {
    do {
        if (id == null) {
            long sessionId = zookeeper.getSessionId();
            String prefix = "x-" + sessionId + "-";
            // lets try look up the current ID if we failed 
            // in the middle of creating the znode
            findPrefixInChildren(prefix, zookeeper, dir);
            idName = new ZNodeName(id);
        }
        if (id != null) {
            List<String> names = zookeeper.getChildren(dir, false);
            if (names.isEmpty()) {
                LOG.warn("No children in: " + dir + " when we've just " +
                             "created one! Lets recreate it...");
                // lets force the recreation of the id
                id = null;
            } else {
                // lets sort them explicitly (though they do seem to come back in order ususally :)
                SortedSet<ZNodeName> sortedNames = new TreeSet<ZNodeName>();
                for (String name : names) {
                    sortedNames.add(new ZNodeName(dir + "/" + name));
                }
                ownerId = sortedNames.first().getName();
                SortedSet<ZNodeName> lessThanMe = sortedNames.headSet(idName);
                if (!lessThanMe.isEmpty()) {
                    ZNodeName lastChildName = lessThanMe.last();
                    lastChildId = lastChildName.getName();
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("watching less than me node: " + lastChildId);
                    }
                    Stat stat = zookeeper.exists(lastChildId, new LockWatcher());
                    if (stat != null) {
                        return Boolean.FALSE;
                    } else {
                        LOG.warn("Could not find the" +
                                     " stats for less than me: " + lastChildName.getName());
                    }
                } else {
                    if (isOwner()) {
                        if (callback != null) {
                            callback.lockAcquired();
                        }
                        return Boolean.TRUE;
                    }
                }
            }
        }
    } while (id == null);
    return Boolean.FALSE;
}