下面列出了com.google.common.collect.Iterators#toArray ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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();
}
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);
}
@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"));
}
/**
* 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);
}
}
@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);
}
}
@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);
}
@Override
public Partition[] getPartitions() {
return Iterators.toArray(IntStream
.range(0, this.regionsPaths.size())
.mapToObj(i -> (Partition) () -> i)
.iterator(), Partition.class);
}
@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);
}
/**
* 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);
}
}
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));
}
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();
}
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));
}
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));
}
/**
* 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);
}
}
/**
* 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);
}
}
/**
* 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);
}
}
@Override
public Object[] toArray() {
return Iterators.toArray(iterator(), Object.class);
}
public static Typed from( Iterable<? extends Class<?>> iterable )
{
return new Typed( Iterators.toArray( iterable.iterator(), Class.class ) );
}
@Override
public GamaDate[] toArray() {
return Iterators.toArray(iterator(), GamaDate.class);
}
@Override
public String[] getPropertyNames() {
return Iterators.toArray(ConfigurationManager.getConfigInstance().getKeys(), String.class);
}