@Override public Iterator<? extends T> iterator(long first, long count) { Collection<T> data = dataModel.getObject(); if (data == null || data.size() == 0) return Iterators.emptyIterator(); Iterator<T> it; final SortParam<S> sortParam = getSort(); if (sortParam != null && sortParam.getProperty() != null) { Ordering<T> ordering = Ordering.natural() .nullsFirst() .onResultOf( new Function<T, Comparable<?>>() { @Override public Comparable<?> apply(T input) { return comparableValue(input, sortParam.getProperty()); } }); if (!sortParam.isAscending()) ordering = ordering.reverse(); it = ordering.sortedCopy(data).iterator(); } else { it = data.iterator(); } if (first > 0) Iterators.advance(it, (int) first); return count >= 0 ? Iterators.limit(it, (int) count) : it; }
private Iterator<E> limit(Iterator<E> iter) { if (limit >= 0) { return Iterators.limit(iter, limit); } else { return iter; } }
public Rows toRows(Page _page, String expr) { Parser<SelectProjection> parser = ExpressionParser.selectProjection(); SelectProjection sp = TerminalParser.parse(parser, expr); Page page = (_page == Page.ALL) ? Page.create(10000, 1) : _page; // limit IteratorList<ReadNode> iterator = this.iterator(); Iterators.skip(iterator, page.getSkipOnScreen()); Iterator<ReadNode> limitIter = Iterators.limit(iterator, page.getOffsetOnScreen()); List<ReadNode> screenList = ListUtil.newList(); while (limitIter.hasNext()) { screenList.add(limitIter.next()); } int count = screenList.size(); Page pageOnScreen = Page.create( page.getListNum(), page.getPageNo() % page.getScreenCount(), page.getScreenCount()); return AdNodeRows.create( session, pageOnScreen.subList(screenList).iterator(), sp, count, "cnt"); }
/** * Ensures that at most <code>n</code> are up and running. If less nodes that <code>n</code> are * running this method will not start any additional nodes. */ public synchronized void ensureAtMostNumNodes(int n) { if (nodes.size() <= n) { return; } // prevent killing the master if possible final Iterator<NodeAndClient> values = n == 0 ? nodes.values().iterator() : Iterators.filter( nodes.values().iterator(), Predicates.not(new MasterNodePredicate(getMasterName()))); final Iterator<NodeAndClient> limit = Iterators.limit(values, nodes.size() - n); logger.info("reducing cluster size from {} to {}", nodes.size() - n, n); Set<NodeAndClient> nodesToRemove = new HashSet<NodeAndClient>(); while (limit.hasNext()) { NodeAndClient next = limit.next(); nodesToRemove.add(next); next.close(); } for (NodeAndClient toRemove : nodesToRemove) { nodes.remove(toRemove.name); } }
@Override @SuppressWarnings("PMD.PrematureDeclaration") int runCommandWithOptionsInternal(BuildCommandOptions options) throws IOException { // Set the logger level based on the verbosity option. Verbosity verbosity = console.getVerbosity(); Logging.setLoggingLevelForVerbosity(verbosity); // Create artifact cache to initialize Cassandra connection, if appropriate. ArtifactCache artifactCache = getArtifactCache(); try { buildTargets = getBuildTargets(options.getArgumentsFormattedAsBuildTargets()); } catch (NoSuchBuildTargetException e) { console.printBuildFailureWithoutStacktrace(e); return 1; } if (buildTargets.isEmpty()) { console.printBuildFailure("Must specify at least one build target."); // If there are aliases defined in .buckconfig, suggest that the user // build one of them. We show the user only the first 10 aliases. ImmutableSet<String> aliases = options.getBuckConfig().getAliases(); if (!aliases.isEmpty()) { console .getStdErr() .println( String.format( "Try building one of the following targets:\n%s", Joiner.on(' ').join(Iterators.limit(aliases.iterator(), 10)))); } return 1; } getBuckEventBus().post(BuildEvent.started(buildTargets)); // Parse the build files to create a DependencyGraph. DependencyGraph dependencyGraph; try { dependencyGraph = getParser() .parseBuildFilesForTargets( buildTargets, options.getDefaultIncludes(), getBuckEventBus()); } catch (BuildTargetException | BuildFileParseException e) { console.printBuildFailureWithoutStacktrace(e); return 1; } // Create and execute the build. build = options.createBuild( options.getBuckConfig(), dependencyGraph, getProjectFilesystem(), getAndroidDirectoryResolver(), artifactCache, console, getBuckEventBus(), Optional.<TargetDevice>absent(), getCommandRunnerParams().getPlatform()); int exitCode = 0; try { exitCode = executeBuildAndPrintAnyFailuresToConsole(build, console); } finally { // Shutdown the Executor Service once the build completes. // Note: we need to use shutdown() instead of shutdownNow() to ensure that tasks submitted to // the Execution Service are completed. build.getStepRunner().getListeningExecutorService().shutdown(); } getBuckEventBus().post(BuildEvent.finished(buildTargets, exitCode)); if (exitCode != 0) { return exitCode; } return 0; }
@Override public SweepResults run(String tableName, int batchSize, @Nullable byte[] startRow) { Preconditions.checkNotNull(tableName); Preconditions.checkState(!AtlasDbConstants.hiddenTables.contains(tableName)); if (tableName.startsWith(AtlasDbConstants.NAMESPACE_PREFIX)) { // this happens sometimes; I think it's because some places in the code can // start this sweeper without doing the full normally ordered KVSModule startup. // I did check and sweep.stats did contain the FQ table name for all of the tables, // so it is at least broken in some way that still allows namespaced tables to eventually be // swept. log.warn("The sweeper should not be run on tables passed through namespace mapping."); return SweepResults.EMPTY_SWEEP; } // Earliest start timestamp of any currently open transaction, with two caveats: // (1) unreadableTimestamps are calculated via wall-clock time, and so may not be correct // under pathological clock conditions // (2) immutableTimestamps do not account for locks have timed out after checking their locks; // such a transaction may have a start timestamp less than the immutableTimestamp, and it // could still get successfully committed (its commit timestamp may or may not be less than // the immutableTimestamp // Note that this is fine, because we'll either // (1) force old readers to abort (if they read a garbage collection sentinel), or // (2) force old writers to retry (note that we must roll back any uncommitted transactions that // we encounter SweepStrategy sweepStrategy = sweepStrategyManager.get().get(tableName); if (sweepStrategy == null) { sweepStrategy = SweepStrategy.CONSERVATIVE; } else if (sweepStrategy == SweepStrategy.NOTHING) { return SweepResults.EMPTY_SWEEP; } if (startRow == null) { startRow = new byte[0]; } RangeRequest rangeRequest = RangeRequest.builder().startRowInclusive(startRow).batchHint(batchSize).build(); long sweepTimestamp = getSweepTimestamp(sweepStrategy); ClosableIterator<RowResult<Value>> valueResults; if (sweepStrategy == SweepStrategy.CONSERVATIVE) { valueResults = ClosableIterators.wrap(ImmutableList.<RowResult<Value>>of().iterator()); } else { valueResults = keyValueService.getRange(tableName, rangeRequest, sweepTimestamp); } ClosableIterator<RowResult<Set<Long>>> rowResults = keyValueService.getRangeOfTimestamps(tableName, rangeRequest, sweepTimestamp); try { List<RowResult<Set<Long>>> rowResultTimestamps = ImmutableList.copyOf(Iterators.limit(rowResults, batchSize)); PeekingIterator<RowResult<Value>> peekingValues = Iterators.peekingIterator(valueResults); Set<Cell> sentinelsToAdd = Sets.newHashSet(); Multimap<Cell, Long> rowTimestamps = getTimestampsFromRowResults(rowResultTimestamps, sweepStrategy); Multimap<Cell, Long> cellTsPairsToSweep = getCellTsPairsToSweep( rowTimestamps, peekingValues, sweepTimestamp, sweepStrategy, sentinelsToAdd); sweepCells(tableName, cellTsPairsToSweep, sentinelsToAdd); byte[] nextRow = rowResultTimestamps.size() < batchSize ? null : RangeRequests.getNextStartRow( false, Iterables.getLast(rowResultTimestamps).getRowName()); return new SweepResults(nextRow, rowResultTimestamps.size(), cellTsPairsToSweep.size()); } finally { rowResults.close(); valueResults.close(); } }
@Override public Iterator<Link> iterator() { return Iterators.concat( Iterators.limit(originalPath.iterator(), diversionLinkIdx), newSubPath.iterator()); }
private synchronized Set<String> nRandomNodes(int numNodes) { assert size() >= numNodes; return Sets.newHashSet(Iterators.limit(this.nodes.keySet().iterator(), numNodes)); }