@Override Cluster.Action generate(Cluster cluster) { if (this.masterServices == null) { int thisServer = pickRandomServer(cluster); // Pick the other server int otherServer = pickOtherRandomServer(cluster, thisServer); return pickRandomRegions(cluster, thisServer, otherServer); } // Randomly iterate through regions until you find one that is not on ideal host for (int region : getRandomIterationOrder(cluster.numRegions)) { int currentServer = cluster.regionIndexToServerIndex[region]; if (currentServer != cluster.getOrComputeRegionsToMostLocalEntities(LocalityType.SERVER)[region]) { Optional<Action> potential = tryMoveOrSwap( cluster, currentServer, region, cluster.getOrComputeRegionsToMostLocalEntities(LocalityType.SERVER)[region] ); if (potential.isPresent()) { return potential.get(); } } } return Cluster.NullAction; }
private static <T> void acceptIfPresent(Consumer<? super T> action, Optional<T> node) { if (node.isPresent()) { action.accept(node.get()); } } }
private static <T> void pushIfPresent(Deque<T> stack, Optional<T> node) { if (node.isPresent()) { stack.addLast(node.get()); } }
/** * If a value is present in {@code optional}, returns a stream containing only that element, * otherwise returns an empty stream. */ public static <T> Stream<T> stream(org.apache.hbase.thirdparty.com.google.common.base.Optional<T> optional) { return optional.isPresent() ? Stream.of(optional.get()) : Stream.of(); }
@Override public Optional<Long> sizeIfKnown() { Optional<Long> optionalUnslicedSize = ByteSource.this.sizeIfKnown(); if (optionalUnslicedSize.isPresent()) { long unslicedSize = optionalUnslicedSize.get(); long off = Math.min(offset, unslicedSize); return Optional.of(Math.min(length, unslicedSize - off)); } return Optional.absent(); }
@Override public Optional<Long> sizeIfKnown() { long result = 0L; for (ByteSource source : sources) { Optional<Long> sizeIfKnown = source.sizeIfKnown(); if (!sizeIfKnown.isPresent()) { return Optional.absent(); } result += sizeIfKnown.get(); } return Optional.of(result); }
@Override public Optional<Long> lengthIfKnown() { long result = 0L; for (CharSource source : sources) { Optional<Long> lengthIfKnown = source.lengthIfKnown(); if (!lengthIfKnown.isPresent()) { return Optional.absent(); } result += lengthIfKnown.get(); } return Optional.of(result); }
/** * Returns whether the source has zero chars. The default implementation returns true if * {@link #lengthIfKnown} returns zero, falling back to opening a stream and checking for EOF if * the length is not known. * * <p>Note that, in cases where {@code lengthIfKnown} returns zero, it is <i>possible</i> that * chars are actually available for reading. This means that a source may return {@code true} from * {@code isEmpty()} despite having readable content. * * @throws IOException if an I/O error occurs * @since 15.0 */ public boolean isEmpty() throws IOException { Optional<Long> lengthIfKnown = lengthIfKnown(); if (lengthIfKnown.isPresent() && lengthIfKnown.get() == 0L) { return true; } Closer closer = Closer.create(); try { Reader reader = closer.register(openStream()); return reader.read() == -1; } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
/** * Returns whether the source has zero bytes. The default implementation returns true if * {@link #sizeIfKnown} returns zero, falling back to opening a stream and checking for EOF if the * size is not known. * * <p>Note that, in cases where {@code sizeIfKnown} returns zero, it is <i>possible</i> that bytes * are actually available for reading. (For example, some special files may return a size of 0 * despite actually having content when read.) This means that a source may return {@code true} * from {@code isEmpty()} despite having readable content. * * @throws IOException if an I/O error occurs * @since 15.0 */ public boolean isEmpty() throws IOException { Optional<Long> sizeIfKnown = sizeIfKnown(); if (sizeIfKnown.isPresent() && sizeIfKnown.get() == 0L) { return true; } Closer closer = Closer.create(); try { InputStream in = closer.register(openStream()); return in.read() == -1; } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
Optional<Long> lengthIfKnown = lengthIfKnown(); if (lengthIfKnown.isPresent()) { return lengthIfKnown.get();
Optional<Long> sizeIfKnown = sizeIfKnown(); if (sizeIfKnown.isPresent()) { return sizeIfKnown.get();