下面列出了java.util.SortedSet#tailSet ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public int complete(final String buf, final int cursor, final List<CharSequence> candidates) {
// buffer could be null
checkNotNull(candidates);
SortedSet<String> strings = sort(_commands.keySet());
if (buf == null) {
candidates.addAll(strings);
} else {
String buffer = buf.substring(0, cursor);
List<String> partialTableNames = isFirstArgPartialTableName(buffer);
if (partialTableNames != null && !partialTableNames.isEmpty()) {
candidates.addAll(partialTableNames);
} else {
for (String match : strings.tailSet(buffer)) {
if (!match.startsWith(buffer)) {
break;
}
candidates.add(match);
}
}
}
if (candidates.size() == 1) {
candidates.set(0, candidates.get(0) + " ");
}
return candidates.isEmpty() ? -1 : 0;
}
private static boolean contains(SortedSet<IconInfo> set, int hashCode) {
IconInfo fake = new IconInfo("", "", hashCode);
Set<IconInfo> greaterOrEqual = set.tailSet(fake);
if (greaterOrEqual.isEmpty()) {
return false;
}
IconInfo first = greaterOrEqual.iterator().next();
return hashCode == first.hash;
}
/**
* This method is not idempotent, we will save some info to judge VERSION_MASKED.
* @param cell - current cell to check if deleted by a previously seen delete
* @return We don't distinguish DeleteColumn and DeleteFamily. We only return code for column.
*/
@Override
public DeleteResult isDeleted(Cell cell) {
long duplicateMvcc = prepare(cell);
for (Map.Entry<Long, DeleteVersionsNode> e : delColMap.tailMap(cell.getSequenceId())
.entrySet()) {
DeleteVersionsNode node = e.getValue();
long deleteMvcc = Long.MAX_VALUE;
SortedSet<Long> deleteVersionMvccs = node.deletesMap.get(cell.getTimestamp());
if (deleteVersionMvccs != null) {
SortedSet<Long> tail = deleteVersionMvccs.tailSet(cell.getSequenceId());
if (!tail.isEmpty()) {
deleteMvcc = tail.first();
}
}
SortedMap<Long, SortedSet<Long>> subMap =
node.mvccCountingMap
.subMap(cell.getSequenceId(), true, Math.min(duplicateMvcc, deleteMvcc), true);
for (Map.Entry<Long, SortedSet<Long>> seg : subMap.entrySet()) {
if (seg.getValue().size() >= maxVersions) {
return DeleteResult.VERSION_MASKED;
}
seg.getValue().add(cell.getSequenceId());
}
if (deleteMvcc < Long.MAX_VALUE) {
return DeleteResult.VERSION_DELETED;
}
if (cell.getTimestamp() <= node.ts) {
return DeleteResult.COLUMN_DELETED;
}
}
if (duplicateMvcc < Long.MAX_VALUE) {
return DeleteResult.VERSION_MASKED;
}
return DeleteResult.NOT_DELETED;
}
/**
* tailSet returns set with keys in requested range
*/
public void testTailSetContents() {
TreeSet set = set5();
SortedSet sm = set.tailSet(two);
assertFalse(sm.contains(one));
assertTrue(sm.contains(two));
assertTrue(sm.contains(three));
assertTrue(sm.contains(four));
assertTrue(sm.contains(five));
Iterator i = sm.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());
SortedSet ssm = sm.tailSet(four);
assertEquals(four, ssm.first());
assertEquals(five, ssm.last());
assertTrue(ssm.remove(four));
assertEquals(1, ssm.size());
assertEquals(3, sm.size());
assertEquals(4, set.size());
}
/**
* tailSet returns set with keys in requested range
*/
public void testTailSetContents() {
NavigableSet set = set5();
SortedSet sm = set.tailSet(two);
assertFalse(sm.contains(one));
assertTrue(sm.contains(two));
assertTrue(sm.contains(three));
assertTrue(sm.contains(four));
assertTrue(sm.contains(five));
Iterator i = sm.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());
SortedSet ssm = sm.tailSet(four);
assertEquals(four, ssm.first());
assertEquals(five, ssm.last());
assertTrue(ssm.remove(four));
assertEquals(1, ssm.size());
assertEquals(3, sm.size());
assertEquals(4, set.size());
}
/**
* tailSet returns set with keys in requested range
*/
public void testTailSetContents() {
NavigableSet set = set5();
SortedSet sm = set.tailSet(two);
assertFalse(sm.contains(one));
assertTrue(sm.contains(two));
assertTrue(sm.contains(three));
assertTrue(sm.contains(four));
assertTrue(sm.contains(five));
Iterator i = sm.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());
SortedSet ssm = sm.tailSet(four);
assertEquals(four, ssm.first());
assertEquals(five, ssm.last());
assertTrue(ssm.remove(four));
assertEquals(1, ssm.size());
assertEquals(3, sm.size());
assertEquals(4, set.size());
}
/**
* tailSet returns set with keys in requested range
*/
public void testDescendingTailSetContents() {
NavigableSet set = dset5();
SortedSet sm = set.tailSet(m2);
assertFalse(sm.contains(m1));
assertTrue(sm.contains(m2));
assertTrue(sm.contains(m3));
assertTrue(sm.contains(m4));
assertTrue(sm.contains(m5));
Iterator i = sm.iterator();
Object k;
k = (Integer)(i.next());
assertEquals(m2, k);
k = (Integer)(i.next());
assertEquals(m3, k);
k = (Integer)(i.next());
assertEquals(m4, k);
k = (Integer)(i.next());
assertEquals(m5, k);
assertFalse(i.hasNext());
SortedSet ssm = sm.tailSet(m4);
assertEquals(m4, ssm.first());
assertEquals(m5, ssm.last());
assertTrue(ssm.remove(m4));
assertEquals(1, ssm.size());
assertEquals(3, sm.size());
assertEquals(4, set.size());
}
private static <T> T NavigableSetCeiling(SortedSet<T> sortedSet, T e) {
SortedSet<T> tailSet = sortedSet.tailSet(e);
if (tailSet.isEmpty()) {
return null;
}
return tailSet.first();
}
/**
* tailSet returns set with keys in requested range
*/
public void testTailSetContents() {
NavigableSet set = set5();
SortedSet sm = set.tailSet(two);
assertFalse(sm.contains(one));
assertTrue(sm.contains(two));
assertTrue(sm.contains(three));
assertTrue(sm.contains(four));
assertTrue(sm.contains(five));
Iterator i = sm.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());
SortedSet ssm = sm.tailSet(four);
assertEquals(four, ssm.first());
assertEquals(five, ssm.last());
assertTrue(ssm.remove(four));
assertEquals(1, ssm.size());
assertEquals(3, sm.size());
assertEquals(4, set.size());
}
/**
* tailSet returns set with keys in requested range
*/
public void testTailSetContents() {
ConcurrentSkipListSet set = set5();
SortedSet sm = set.tailSet(two);
assertFalse(sm.contains(one));
assertTrue(sm.contains(two));
assertTrue(sm.contains(three));
assertTrue(sm.contains(four));
assertTrue(sm.contains(five));
Iterator i = sm.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());
SortedSet ssm = sm.tailSet(four);
assertEquals(four, ssm.first());
assertEquals(five, ssm.last());
assertTrue(ssm.remove(four));
assertEquals(1, ssm.size());
assertEquals(3, sm.size());
assertEquals(4, set.size());
}
/**
* tailSet returns set with keys in requested range
*/
public void testTailSetContents() {
NavigableSet set = set5();
SortedSet sm = set.tailSet(two);
assertFalse(sm.contains(one));
assertTrue(sm.contains(two));
assertTrue(sm.contains(three));
assertTrue(sm.contains(four));
assertTrue(sm.contains(five));
Iterator i = sm.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());
SortedSet ssm = sm.tailSet(four);
assertEquals(four, ssm.first());
assertEquals(five, ssm.last());
assertTrue(ssm.remove(four));
assertEquals(1, ssm.size());
assertEquals(3, sm.size());
assertEquals(4, set.size());
}
/**
* tailSet returns set with keys in requested range
*/
public void testTailSetContents() {
TreeSet set = set5();
SortedSet sm = set.tailSet(two);
assertFalse(sm.contains(one));
assertTrue(sm.contains(two));
assertTrue(sm.contains(three));
assertTrue(sm.contains(four));
assertTrue(sm.contains(five));
Iterator i = sm.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());
SortedSet ssm = sm.tailSet(four);
assertEquals(four, ssm.first());
assertEquals(five, ssm.last());
assertTrue(ssm.remove(four));
assertEquals(1, ssm.size());
assertEquals(3, sm.size());
assertEquals(4, set.size());
}
/**
* tailSet returns set with keys in requested range
*/
public void testDescendingTailSetContents() {
NavigableSet set = dset5();
SortedSet sm = set.tailSet(m2);
assertFalse(sm.contains(m1));
assertTrue(sm.contains(m2));
assertTrue(sm.contains(m3));
assertTrue(sm.contains(m4));
assertTrue(sm.contains(m5));
Iterator i = sm.iterator();
Object k;
k = (Integer)(i.next());
assertEquals(m2, k);
k = (Integer)(i.next());
assertEquals(m3, k);
k = (Integer)(i.next());
assertEquals(m4, k);
k = (Integer)(i.next());
assertEquals(m5, k);
assertFalse(i.hasNext());
SortedSet ssm = sm.tailSet(m4);
assertEquals(m4, ssm.first());
assertEquals(m5, ssm.last());
assertTrue(ssm.remove(m4));
assertEquals(1, ssm.size());
assertEquals(3, sm.size());
assertEquals(4, set.size());
}
public YearMonthDay getNextPossibleLessonInstanceDate() {
SortedSet<YearMonthDay> allLessonDates = getAllLessonDates();
LessonInstance lastLessonInstance = getLastLessonInstance();
if (lastLessonInstance != null) {
YearMonthDay day = lastLessonInstance.getDay();
SortedSet<YearMonthDay> nextLessonDates = allLessonDates.tailSet(day);
nextLessonDates.remove(day);
return nextLessonDates.isEmpty() ? null : nextLessonDates.first();
}
return allLessonDates.isEmpty() ? null : allLessonDates.first();
}
/**
* tailSet returns set with keys in requested range
*/
public void testDescendingTailSetContents() {
NavigableSet set = dset5();
SortedSet sm = set.tailSet(m2);
assertFalse(sm.contains(m1));
assertTrue(sm.contains(m2));
assertTrue(sm.contains(m3));
assertTrue(sm.contains(m4));
assertTrue(sm.contains(m5));
Iterator i = sm.iterator();
Object k;
k = (Integer)(i.next());
assertEquals(m2, k);
k = (Integer)(i.next());
assertEquals(m3, k);
k = (Integer)(i.next());
assertEquals(m4, k);
k = (Integer)(i.next());
assertEquals(m5, k);
assertFalse(i.hasNext());
SortedSet ssm = sm.tailSet(m4);
assertEquals(m4, ssm.first());
assertEquals(m5, ssm.last());
assertTrue(ssm.remove(m4));
assertEquals(1, ssm.size());
assertEquals(3, sm.size());
assertEquals(4, set.size());
}
/**
* 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;
}
@Override public UnmodSortedSet<E> tailSet(E fromElement) {
SortedSet<E> next = dup(inner);
return new TestSortSet<>(next.tailSet(fromElement), inner.comparator());
}
/**
* Return a manifest of what finalized edit logs are available. All available
* edit logs are returned starting from the transaction id passed. If
* 'fromTxId' falls in the middle of a log, that log is returned as well.
*
* @param fromTxId Starting transaction id to read the logs.
* @return RemoteEditLogManifest object.
*/
public synchronized RemoteEditLogManifest getEditLogManifest(long fromTxId) {
// Collect RemoteEditLogs available from each FileJournalManager
List<RemoteEditLog> allLogs = Lists.newArrayList();
for (JournalAndStream j : journals) {
if (j.getManager() instanceof FileJournalManager) {
FileJournalManager fjm = (FileJournalManager)j.getManager();
try {
allLogs.addAll(fjm.getRemoteEditLogs(fromTxId, false));
} catch (Throwable t) {
LOG.warn("Cannot list edit logs in " + fjm, t);
}
}
}
// Group logs by their starting txid
ImmutableListMultimap<Long, RemoteEditLog> logsByStartTxId =
Multimaps.index(allLogs, RemoteEditLog.GET_START_TXID);
long curStartTxId = fromTxId;
List<RemoteEditLog> logs = Lists.newArrayList();
while (true) {
ImmutableList<RemoteEditLog> logGroup = logsByStartTxId.get(curStartTxId);
if (logGroup.isEmpty()) {
// we have a gap in logs - for example because we recovered some old
// storage directory with ancient logs. Clear out any logs we've
// accumulated so far, and then skip to the next segment of logs
// after the gap.
SortedSet<Long> startTxIds = Sets.newTreeSet(logsByStartTxId.keySet());
startTxIds = startTxIds.tailSet(curStartTxId);
if (startTxIds.isEmpty()) {
break;
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Found gap in logs at " + curStartTxId + ": " +
"not returning previous logs in manifest.");
}
logs.clear();
curStartTxId = startTxIds.first();
continue;
}
}
// Find the one that extends the farthest forward
RemoteEditLog bestLog = Collections.max(logGroup);
logs.add(bestLog);
// And then start looking from after that point
curStartTxId = bestLog.getEndTxId() + 1;
}
RemoteEditLogManifest ret = new RemoteEditLogManifest(logs);
if (LOG.isDebugEnabled()) {
LOG.debug("Generated manifest for logs since " + fromTxId + ":"
+ ret);
}
return ret;
}
private static <T> SortedSet<T> NavigableSetTailSet(SortedSet<T> sortedSet, T fromElement, boolean inclusive) {
Assert.isTrue(inclusive);
return sortedSet.tailSet(fromElement);
}
/**
* Return a manifest of what finalized edit logs are available. All available
* edit logs are returned starting from the transaction id passed. If
* 'fromTxId' falls in the middle of a log, that log is returned as well.
*
* @param fromTxId Starting transaction id to read the logs.
* @return RemoteEditLogManifest object.
*/
public synchronized RemoteEditLogManifest getEditLogManifest(long fromTxId) {
// Collect RemoteEditLogs available from each FileJournalManager
List<RemoteEditLog> allLogs = Lists.newArrayList();
for (JournalAndStream j : journals) {
if (j.getManager() instanceof FileJournalManager) {
FileJournalManager fjm = (FileJournalManager)j.getManager();
try {
allLogs.addAll(fjm.getRemoteEditLogs(fromTxId, false));
} catch (Throwable t) {
LOG.warn("Cannot list edit logs in " + fjm, t);
}
}
}
// Group logs by their starting txid
ImmutableListMultimap<Long, RemoteEditLog> logsByStartTxId =
Multimaps.index(allLogs, RemoteEditLog.GET_START_TXID);
long curStartTxId = fromTxId;
List<RemoteEditLog> logs = Lists.newArrayList();
while (true) {
ImmutableList<RemoteEditLog> logGroup = logsByStartTxId.get(curStartTxId);
if (logGroup.isEmpty()) {
// we have a gap in logs - for example because we recovered some old
// storage directory with ancient logs. Clear out any logs we've
// accumulated so far, and then skip to the next segment of logs
// after the gap.
SortedSet<Long> startTxIds = Sets.newTreeSet(logsByStartTxId.keySet());
startTxIds = startTxIds.tailSet(curStartTxId);
if (startTxIds.isEmpty()) {
break;
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Found gap in logs at " + curStartTxId + ": " +
"not returning previous logs in manifest.");
}
logs.clear();
curStartTxId = startTxIds.first();
continue;
}
}
// Find the one that extends the farthest forward
RemoteEditLog bestLog = Collections.max(logGroup);
logs.add(bestLog);
// And then start looking from after that point
curStartTxId = bestLog.getEndTxId() + 1;
}
RemoteEditLogManifest ret = new RemoteEditLogManifest(logs);
if (LOG.isDebugEnabled()) {
LOG.debug("Generated manifest for logs since " + fromTxId + ":"
+ ret);
}
return ret;
}