Java Code Examples for com.google.common.collect.Iterators#toArray()

The following examples show how to use com.google.common.collect.Iterators#toArray() . 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
Source File: TestCopyCommandCluster.java    From kite with Apache License 2.0 6 votes vote down vote up
public void testCopyWithoutCompaction(int expectedFiles) throws Exception {
  command.repoURI = repoUri;
  command.noCompaction = true;
  command.datasets = Lists.newArrayList(source, dest);

  int rc = command.run();
  Assert.assertEquals("Should return success", 0, rc);

  DatasetRepository repo = DatasetRepositories.repositoryFor("repo:" + repoUri);
  FileSystemDataset<GenericData.Record> ds =
      (FileSystemDataset<GenericData.Record>) repo.<GenericData.Record>
          load("default", dest);
  int size = Iterators.size(ds.newReader());
  Assert.assertEquals("Should contain copied records", 6, size);

  Path[] paths = Iterators.toArray(ds.pathIterator(), Path.class);
  Assert.assertEquals("Should produce " + expectedFiles + " files: " + Arrays.toString(paths),
      expectedFiles, Iterators.size(ds.pathIterator()));

  verify(console).info("Added {} records to \"{}\"", 6l, dest);
  verifyNoMoreInteractions(console);
}
 
Example 2
Source File: RamAccountingPageIteratorTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoCircuitBreaking() {
    PagingIterator<Integer, Row> pagingIterator = PagingIterator.create(
        2,
        true,
        null,
        () -> new RowAccountingWithEstimators(ImmutableList.of(DataTypes.STRING, DataTypes.STRING, DataTypes.STRING),
                                              RamAccounting.NO_ACCOUNTING));
    assertThat(pagingIterator, instanceOf(RamAccountingPageIterator.class));
    assertThat(((RamAccountingPageIterator) pagingIterator).delegatePagingIterator,
               instanceOf(PassThroughPagingIterator.class));

    pagingIterator.merge(Arrays.asList(
        new KeyIterable<>(0, Collections.singletonList(TEST_ROWS[0])),
        new KeyIterable<>(1, Collections.singletonList(TEST_ROWS[1]))));
    pagingIterator.finish();

    Row[] rows = Iterators.toArray(pagingIterator, Row.class);
    assertThat(rows[0], TestingHelpers.isRow("a", "b", "c"));
    assertThat(rows[1], TestingHelpers.isRow("d", "e", "f"));
}
 
Example 3
Source File: Properties2HoconConverterTest.java    From wisdom with Apache License 2.0 6 votes vote down vote up
@Test
public void testOnWikipediaSample() throws IOException {
    File props = new File(root, "/wiki.properties");
    File hocon = Properties2HoconConverter.convert(props, true);

    PropertiesConfiguration properties = loadPropertiesWithApacheConfiguration(props);
    assertThat(properties).isNotNull();
    Config config = load(hocon);
    assertThat(properties.isEmpty()).isFalse();
    assertThat(config.isEmpty()).isFalse();

    Iterator<String> iterator = properties.getKeys();
    String[] names = Iterators.toArray(iterator, String.class);
    for (String name : names) {
        if (!name.isEmpty()) {
            // 'cheeses' is not supported by commons-config.
            String o = properties.getString(name);
            String v = config.getString(name);
            assertThat(o).isEqualTo(v);
        }
    }

    assertThat(config.getString("cheeses")).isEmpty();

}
 
Example 4
Source File: TezClientUtils.java    From tez with Apache License 2.0 6 votes vote down vote up
/**
 * Populate {@link Credentials} for the URI's to access them from their {@link FileSystem}s
 * @param uris URIs that need to be accessed
 * @param credentials Credentials object into which to add the credentials
 * @param conf Configuration to access the FileSystem
 * @throws IOException
 */
public static void addFileSystemCredentialsFromURIs(Collection<URI> uris, Credentials credentials,
    Configuration conf) throws IOException {
  // Obtain Credentials for any paths that the user may have configured.
  if (uris != null && !uris.isEmpty()) {
    Iterator<Path> pathIter = Iterators.transform(uris.iterator(), new Function<URI, Path>() {
      @Override
      public Path apply(URI input) {
        return new Path(input);
      }
    });

    Path[] paths = Iterators.toArray(pathIter, Path.class);
    TokenCache.obtainTokensForFileSystems(credentials, paths, conf);
  }
}
 
Example 5
Source File: InternalAdapterStoreImpl.java    From geowave with Apache License 2.0 6 votes vote down vote up
@Override
public String[] getTypeNames() {
  final MetadataReader reader = getReader(false);
  if (reader == null) {
    return new String[0];
  }
  final CloseableIterator<GeoWaveMetadata> results =
      reader.query(new MetadataQuery(null, INTERNAL_TO_EXTERNAL_ID));
  try (CloseableIterator<String> it =
      new CloseableIteratorWrapper<>(
          results,
          Iterators.transform(
              results,
              input -> StringUtils.stringFromBinary(input.getValue())))) {
    return Iterators.toArray(it, String.class);
  }
}
 
Example 6
Source File: RocksDBReader.java    From geowave with Apache License 2.0 5 votes vote down vote up
private CloseableIterator<T> createIterator(
    final RocksDBClient client,
    final RangeReaderParams<T> readerParams,
    final GeoWaveRowIteratorTransformer<T> rowTransformer,
    final Collection<SinglePartitionQueryRanges> ranges,
    final Set<String> authorizations,
    final boolean async) {
  final Iterator<CloseableIterator> it =
      Arrays.stream(ArrayUtils.toObject(readerParams.getAdapterIds())).map(
          adapterId -> new RocksDBQueryExecution(
              client,
              RocksDBUtils.getTablePrefix(
                  readerParams.getInternalAdapterStore().getTypeName(adapterId),
                  readerParams.getIndex().getName()),
              adapterId,
              rowTransformer,
              ranges,
              new ClientVisibilityFilter(authorizations),
              DataStoreUtils.isMergingIteratorRequired(
                  readerParams,
                  client.isVisibilityEnabled()),
              async,
              RocksDBUtils.isGroupByRowAndIsSortByTime(readerParams, adapterId),
              RocksDBUtils.isSortByKeyRequired(readerParams)).results()).iterator();
  final CloseableIterator<T>[] itArray = Iterators.toArray(it, CloseableIterator.class);
  return new CloseableIteratorWrapper<>(new Closeable() {
    AtomicBoolean closed = new AtomicBoolean(false);

    @Override
    public void close() throws IOException {
      if (!closed.getAndSet(true)) {
        Arrays.stream(itArray).forEach(it -> it.close());
      }
    }
  }, Iterators.concat(itArray));
}
 
Example 7
Source File: RedisReader.java    From geowave with Apache License 2.0 5 votes vote down vote up
private CloseableIterator<T> createIterator(
    final RedissonClient client,
    final Compression compression,
    final RangeReaderParams<T> readerParams,
    final GeoWaveRowIteratorTransformer<T> rowTransformer,
    final String namespace,
    final Collection<SinglePartitionQueryRanges> ranges,
    final Set<String> authorizations,
    final boolean visibilityEnabled,
    final boolean async) {
  final Iterator<CloseableIterator> it =
      Arrays.stream(ArrayUtils.toObject(readerParams.getAdapterIds())).map(
          adapterId -> new BatchedRangeRead(
              client,
              compression,
              RedisUtils.getRowSetPrefix(
                  namespace,
                  readerParams.getInternalAdapterStore().getTypeName(adapterId),
                  readerParams.getIndex().getName()),
              adapterId,
              ranges,
              rowTransformer,
              new ClientVisibilityFilter(authorizations),
              DataStoreUtils.isMergingIteratorRequired(readerParams, visibilityEnabled),
              async,
              RedisUtils.isGroupByRowAndIsSortByTime(readerParams, adapterId),
              RedisUtils.isSortByKeyRequired(readerParams),
              visibilityEnabled).results()).iterator();
  final CloseableIterator<T>[] itArray = Iterators.toArray(it, CloseableIterator.class);
  return new CloseableIteratorWrapper<>(new Closeable() {
    AtomicBoolean closed = new AtomicBoolean(false);

    @Override
    public void close() throws IOException {
      if (!closed.getAndSet(true)) {
        Arrays.stream(itArray).forEach(it -> it.close());
      }
    }
  }, Iterators.concat(itArray));
}
 
Example 8
Source File: HBaseOperations.java    From geowave with Apache License 2.0 5 votes vote down vote up
public Iterator<GeoWaveRow> getDataIndexResults(
    final byte[] startRow,
    final byte[] endRow,
    final short adapterId,
    final String... additionalAuthorizations) {
  Result[] results = null;
  final byte[] family = StringUtils.stringToBinary(ByteArrayUtils.shortToString(adapterId));
  final Scan scan = new Scan();
  if (startRow != null) {
    scan.setStartRow(startRow);
  }
  if (endRow != null) {
    scan.setStopRow(HBaseUtils.getInclusiveEndKey(endRow));
  }
  if ((additionalAuthorizations != null) && (additionalAuthorizations.length > 0)) {
    scan.setAuthorizations(new Authorizations(additionalAuthorizations));
  }
  Iterable<Result> s = null;
  try {
    s = getScannedResults(scan, DataIndexUtils.DATA_ID_INDEX.getName());
    results = Iterators.toArray(s.iterator(), Result.class);
  } catch (final IOException e) {
    LOGGER.error("Unable to close HBase table", e);
  } finally {
    if (s instanceof ResultScanner) {
      ((ResultScanner) s).close();
    }
  }
  if (results != null) {
    return Arrays.stream(results).filter(r -> r.containsColumn(family, new byte[0])).map(
        r -> DataIndexUtils.deserializeDataIndexRow(
            r.getRow(),
            adapterId,
            r.getValue(family, new byte[0]),
            false)).iterator();
  }
  return Collections.emptyIterator();
}
 
Example 9
Source File: AndroidResourceNode.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
@Override
protected Node[] createNodes(Node key) {
    return Iterators.toArray(
            Iterators.transform(
                    Iterators.forArray(super.createNodes(key)),
                    new Function<Node, Node>() {
                @Override
                public Node apply(Node input) {
                    return new NotRootResourceFilterNode(input, project);
                }
            }),
            Node.class);
}
 
Example 10
Source File: MizoRDD.java    From mizo with Apache License 2.0 5 votes vote down vote up
@Override
public Partition[] getPartitions() {
    return Iterators.toArray(IntStream
            .range(0, this.regionsPaths.size())
            .mapToObj(i -> (Partition) () -> i)
            .iterator(), Partition.class);
}
 
Example 11
Source File: FileSystemReader.java    From geowave with Apache License 2.0 5 votes vote down vote up
private CloseableIterator<T> createIterator(
    final FileSystemClient client,
    final RangeReaderParams<T> readerParams,
    final GeoWaveRowIteratorTransformer<T> rowTransformer,
    final Collection<SinglePartitionQueryRanges> ranges,
    final Set<String> authorizations,
    final boolean async) {
  final Iterator<CloseableIterator> it =
      Arrays.stream(ArrayUtils.toObject(readerParams.getAdapterIds())).map(
          adapterId -> new FileSystemQueryExecution(
              client,
              adapterId,
              readerParams.getInternalAdapterStore().getTypeName(adapterId),
              readerParams.getIndex().getName(),
              client.getFormat(),
              rowTransformer,
              ranges,
              new ClientVisibilityFilter(authorizations),
              DataStoreUtils.isMergingIteratorRequired(
                  readerParams,
                  client.isVisibilityEnabled()),
              async,
              FileSystemUtils.isGroupByRowAndIsSortByTime(readerParams, adapterId),
              FileSystemUtils.isSortByKeyRequired(readerParams)).results()).iterator();
  final CloseableIterator<T>[] itArray = Iterators.toArray(it, CloseableIterator.class);
  return new CloseableIteratorWrapper<>(new Closeable() {
    AtomicBoolean closed = new AtomicBoolean(false);

    @Override
    public void close() throws IOException {
      if (!closed.getAndSet(true)) {
        Arrays.stream(itArray).forEach(it -> it.close());
      }
    }
  }, Iterators.concat(itArray));
}
 
Example 12
Source File: ExprTimes.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
@Override
protected Number[] get(final Event e) {
	Iterator<? extends Integer> iter = iterator(e);
	if (iter == null) {
		return null;
	}
	return Iterators.toArray(iter, Integer.class);
}
 
Example 13
Source File: ServiceProvidersTestUtil.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an iterator from the callable class via reflection, and checks that all expected
 * classes were loaded.
 *
 * <p>{@code callableClassName} is a {@code Callable<Iterable<Class<?>>} rather than the iterable
 * class name itself so that the iterable class can be non-public.
 *
 * <p>We accept class names as input so that we can test against classes not in the
 * testing class path.
 */
static void testHardcodedClasses(
    String callableClassName,
    ClassLoader cl,
    Set<String> hardcodedClassNames) throws Exception {
  final Set<String> notLoaded = new HashSet<>(hardcodedClassNames);
  cl = new ClassLoader(cl) {
    @Override
    public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
      if (notLoaded.remove(name)) {
        throw new ClassNotFoundException();
      } else {
        return super.loadClass(name, resolve);
      }
    }
  };
  cl = new StaticTestingClassLoader(cl, Pattern.compile("io\\.grpc\\.[^.]*"));
  // Some classes fall back to the context class loader.
  // Ensure that the context class loader is not an accidental backdoor.
  ClassLoader ccl = Thread.currentThread().getContextClassLoader();
  try {
    Thread.currentThread().setContextClassLoader(cl);
    Object[] results = Iterators.toArray(
        invokeIteratorCallable(callableClassName, cl), Object.class);
    assertWithMessage("The Iterable loaded a class that was not in hardcodedClassNames")
        .that(results).isEmpty();
    assertWithMessage(
        "The Iterable did not attempt to load some classes from hardcodedClassNames")
        .that(notLoaded).isEmpty();
  } finally {
    Thread.currentThread().setContextClassLoader(ccl);
  }
}
 
Example 14
Source File: Typed.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static Typed from( Iterable<? extends Class<?>> iterable )
{
    return new Typed( Iterators.toArray( iterable.iterator(), Class.class ) );
}
 
Example 15
Source File: ArchaiusSpringPropertySource.java    From chassis with Apache License 2.0 4 votes vote down vote up
@Override
public String[] getPropertyNames() {
	return Iterators.toArray(ConfigurationManager.getConfigInstance().getKeys(), String.class);
}
 
Example 16
Source File: GamaDateInterval.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
@Override
public GamaDate[] toArray() {
	return Iterators.toArray(iterator(), GamaDate.class);
}
 
Example 17
Source File: SortedSets.java    From buck with Apache License 2.0 4 votes vote down vote up
@Override
public Object[] toArray() {
  return Iterators.toArray(iterator(), Object.class);
}
 
Example 18
Source File: FileSystemUtils.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Returns an array of all files in the given directory matching the glob.
 */
public static Path[] files(Path directory, String glob) throws IOException {
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory, glob)) {
        return Iterators.toArray(stream.iterator(), Path.class);
    }
}
 
Example 19
Source File: FileSystemUtils.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Returns an array of all files in the given directory.
 */
public static Path[] files(Path directory) throws IOException {
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory)) {
        return Iterators.toArray(stream.iterator(), Path.class);
    }
}
 
Example 20
Source File: FileSystemUtils.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Returns an array of all files in the given directory matching.
 */
public static Path[] files(Path from, DirectoryStream.Filter<Path> filter) throws IOException {
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(from, filter)) {
        return Iterators.toArray(stream.iterator(), Path.class);
    }
}