下面列出了com.google.common.collect.Ordering#max ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Convenience method to compute the replay position for a group of SSTables.
* @param sstables
* @return the most recent (highest) replay position
*/
public static ReplayPosition getReplayPosition(Iterable<? extends SSTableReader> sstables)
{
if (Iterables.isEmpty(sstables))
return NONE;
Function<SSTableReader, ReplayPosition> f = new Function<SSTableReader, ReplayPosition>()
{
public ReplayPosition apply(SSTableReader sstable)
{
return sstable.getReplayPosition();
}
};
Ordering<ReplayPosition> ordering = Ordering.from(ReplayPosition.comparator);
return ordering.max(Iterables.transform(sstables, f));
}
@Override
public void enter(CssNode n) {
SourceCodeLocation loc = n.getSourceCodeLocation();
if (loc == null || loc.isUnknown()) {
return;
}
if (result == null || result.isUnknown()) {
result = loc;
} else {
Ordering<SourceCodePoint> o = Ordering.natural();
SourceCodePoint lowerBound = o.min(result.getBegin(), loc.getBegin());
SourceCodePoint upperBound = o.max(result.getEnd(), loc.getEnd());
result = new SourceCodeLocationBuilder()
.setSourceCode(result.getSourceCode())
.setBeginLocation(lowerBound)
.setEndLocation(upperBound)
.getSourceCodeLocation();
}
}
public CommitLogReplayer()
{
this.keyspacesRecovered = new NonBlockingHashSet<Keyspace>();
this.futures = new ArrayList<Future<?>>();
this.buffer = new byte[4096];
this.invalidMutations = new HashMap<UUID, AtomicInteger>();
// count the number of replayed mutation. We don't really care about atomicity, but we need it to be a reference.
this.replayedCount = new AtomicInteger();
this.checksum = new PureJavaCrc32();
// compute per-CF and global replay positions
cfPositions = new HashMap<UUID, ReplayPosition>();
Ordering<ReplayPosition> replayPositionOrdering = Ordering.from(ReplayPosition.comparator);
for (ColumnFamilyStore cfs : ColumnFamilyStore.all())
{
// it's important to call RP.gRP per-cf, before aggregating all the positions w/ the Ordering.min call
// below: gRP will return NONE if there are no flushed sstables, which is important to have in the
// list (otherwise we'll just start replay from the first flush position that we do have, which is not correct).
ReplayPosition rp = ReplayPosition.getReplayPosition(cfs.getSSTables());
// but, if we've truncted the cf in question, then we need to need to start replay after the truncation
ReplayPosition truncatedAt = SystemKeyspace.getTruncatedPosition(cfs.metadata.cfId);
if (truncatedAt != null)
rp = replayPositionOrdering.max(Arrays.asList(rp, truncatedAt));
cfPositions.put(cfs.metadata.cfId, rp);
}
globalPosition = replayPositionOrdering.min(cfPositions.values());
logger.debug("Global replay position is {} from columnfamilies {}", globalPosition, FBUtilities.toString(cfPositions));
}
/** Returns the maximum element from this list, as determined by maxOrdering. */
private static Object findExtreme(Sequence<?> args, Ordering<Object> maxOrdering)
throws EvalException {
// Args can either be a list of items to compare, or a singleton list whose element is an
// iterable of items to compare. In either case, there must be at least one item to compare.
try {
Iterable<?> items = (args.size() == 1) ? Starlark.toIterable(args.get(0)) : args;
return maxOrdering.max(items);
} catch (NoSuchElementException ex) {
throw new EvalException(null, "expected at least one item", ex);
}
}
static
public <K, V extends Number> Map.Entry<K, Value<V>> getWinner(Type type, Collection<Map.Entry<K, Value<V>>> entries){
Ordering<Map.Entry<K, Value<V>>> ordering = Classification.<K, V>createOrdering(type);
try {
return ordering.max(entries);
} catch(NoSuchElementException nsee){
return null;
}
}
@Test
public void order_by_object_field () {
List<GlassWare> beerGlasses = Lists.newArrayList(
new GlassWare("Flute Glass", "Enhances and showcases..."),
new GlassWare("Pilsner Glass (or Pokal)", "showcases color, ..."),
new GlassWare("Pint Glass", "cheap to make..."),
new GlassWare("Goblet (or Chalice)", "Eye candy..."),
new GlassWare("Mug (or Seidel, Stein)", "Easy to drink..."),
new GlassWare(null, null)
);
Ordering<GlassWare> byGlassWareName = Ordering.natural().nullsFirst()
.onResultOf(new Function<GlassWare, String>() {
public String apply(GlassWare glassWare) {
return glassWare.getName();
}
});
GlassWare firstBeerGlass = byGlassWareName.min(beerGlasses);
// first element will be null
assertNull(firstBeerGlass.getName());
GlassWare lastBeerGlass = byGlassWareName.max(beerGlasses);
assertEquals("Pint Glass", lastBeerGlass.getName());
}