use of java.util.ArrayDeque in project presto by prestodb.
the class FileHiveMetastore method getPartitionNames.
@Override
public synchronized Optional<List<String>> getPartitionNames(String databaseName, String tableName) {
requireNonNull(databaseName, "databaseName is null");
requireNonNull(tableName, "tableName is null");
Optional<Table> tableReference = getTable(databaseName, tableName);
if (!tableReference.isPresent()) {
return Optional.empty();
}
Table table = tableReference.get();
Path tableMetadataDirectory = getTableMetadataDirectory(table);
List<ArrayDeque<String>> partitions = listPartitions(tableMetadataDirectory, table.getPartitionColumns());
List<String> partitionNames = partitions.stream().map(partitionValues -> makePartName(table.getPartitionColumns(), ImmutableList.copyOf(partitionValues))).collect(toList());
return Optional.of(ImmutableList.copyOf(partitionNames));
}
use of java.util.ArrayDeque in project presto by prestodb.
the class FileHiveMetastore method listPartitions.
private List<ArrayDeque<String>> listPartitions(Path director, List<Column> partitionColumns) {
if (partitionColumns.isEmpty()) {
return ImmutableList.of();
}
try {
String directoryPrefix = partitionColumns.get(0).getName() + '=';
List<ArrayDeque<String>> partitionValues = new ArrayList<>();
for (FileStatus fileStatus : metadataFileSystem.listStatus(director)) {
if (!fileStatus.isDirectory()) {
continue;
}
if (!fileStatus.getPath().getName().startsWith(directoryPrefix)) {
continue;
}
List<ArrayDeque<String>> childPartitionValues;
if (partitionColumns.size() == 1) {
childPartitionValues = ImmutableList.of(new ArrayDeque<>());
} else {
childPartitionValues = listPartitions(fileStatus.getPath(), partitionColumns.subList(1, partitionColumns.size()));
}
String value = fileStatus.getPath().getName().substring(directoryPrefix.length());
for (ArrayDeque<String> childPartition : childPartitionValues) {
childPartition.addFirst(value);
partitionValues.add(childPartition);
}
}
return partitionValues;
} catch (IOException e) {
throw new PrestoException(HIVE_METASTORE_ERROR, "Error listing partition directories", e);
}
}
use of java.util.ArrayDeque in project cdap by caskdata.
the class DirUtils method deleteDirectoryContents.
/**
* Wipes out content of a directory starting from a given directory.
*
* @param directory to be cleaned
* @param retain if true, the given directory will be retained.
* @throws IOException
*/
public static void deleteDirectoryContents(File directory, boolean retain) throws IOException {
if (!directory.isDirectory()) {
throw new IOException("Not a directory: " + directory);
}
// avoid using guava's Queues.newArrayDeque() since this is a utility class that can be used in all sorts of
// contexts, some of which may use clashing guava versions... For example, when explore launches a Hive query,
// it includes hive-exec.jar which bundles guava 11 in its jar...
Deque<File> stack = new ArrayDeque<>();
stack.addAll(listFiles(directory));
while (!stack.isEmpty()) {
File file = stack.peekLast();
List<File> files = listFiles(file);
if (files.isEmpty()) {
if (!file.delete()) {
throw new IOException("Failed to delete file " + file);
}
stack.pollLast();
} else {
stack.addAll(files);
}
}
if (!retain) {
if (!directory.delete()) {
throw new IOException("Failed to delete directory " + directory);
}
}
}
use of java.util.ArrayDeque in project google-cloud-java by GoogleCloudPlatform.
the class ParallelCountBytes method countFile.
/**
* Print the length and MD5 of the indicated file.
*
* <p>This uses the normal Java NIO Api, so it can take advantage of any installed
* NIO Filesystem provider without any extra effort.
*/
private static void countFile(String fname) throws Exception {
// large buffers pay off
final int bufSize = 50 * 1024 * 1024;
Queue<Future<WorkUnit>> work = new ArrayDeque<>();
Path path = Paths.get(new URI(fname));
long size = Files.size(path);
System.out.println(fname + ": " + size + " bytes.");
int nThreads = (int) Math.ceil(size / (double) bufSize);
if (nThreads > 4)
nThreads = 4;
System.out.println("Reading the whole file using " + nThreads + " threads...");
Stopwatch sw = Stopwatch.createStarted();
long total = 0;
MessageDigest md = MessageDigest.getInstance("MD5");
ExecutorService exec = Executors.newFixedThreadPool(nThreads);
int blockIndex;
for (blockIndex = 0; blockIndex < nThreads; blockIndex++) {
work.add(exec.submit(new WorkUnit(Files.newByteChannel(path), bufSize, blockIndex)));
}
while (!work.isEmpty()) {
WorkUnit full = work.remove().get();
md.update(full.buf.array(), 0, full.buf.position());
total += full.buf.position();
if (full.buf.hasRemaining()) {
full.close();
} else {
work.add(exec.submit(full.resetForIndex(blockIndex++)));
}
}
exec.shutdown();
long elapsed = sw.elapsed(TimeUnit.SECONDS);
System.out.println("Read all " + total + " bytes in " + elapsed + "s. ");
String hex = String.valueOf(BaseEncoding.base16().encode(md.digest()));
System.out.println("The MD5 is: 0x" + hex);
if (total != size) {
System.out.println("Wait, this doesn't match! We saw " + total + " bytes, " + "yet the file size is listed at " + size + " bytes.");
}
}
use of java.util.ArrayDeque in project algorithms by Iurii-Dziuban.
the class IslandSearch method expandIslandReturnSquareWithoutRecursionBFS.
private static int expandIslandReturnSquareWithoutRecursionBFS(int[][] islandMatrix, int i, int j) {
if (islandMatrix[i][j] == 0) {
return 0;
}
Queue<Pair> queue = new ArrayDeque<Pair>();
queue.add(new Pair(i, j));
int sum = 0;
while (!queue.isEmpty()) {
Pair element = queue.poll();
int curI = element.getFirst();
int curJ = element.getSecond();
if (islandMatrix[curI][curJ] != 0) {
sum += 1;
islandMatrix[curI][curJ] = 0;
// up, down left, right
addToQueueIfIsland(islandMatrix, queue, curI + 1, curJ);
addToQueueIfIsland(islandMatrix, queue, curI, curJ + 1);
addToQueueIfIsland(islandMatrix, queue, curI - 1, curJ);
addToQueueIfIsland(islandMatrix, queue, curI, curJ - 1);
// south-west, south-east, north-west, north-east
addToQueueIfIsland(islandMatrix, queue, curI + 1, curJ + 1);
addToQueueIfIsland(islandMatrix, queue, curI + 1, curJ - 1);
addToQueueIfIsland(islandMatrix, queue, curI - 1, curJ + 1);
addToQueueIfIsland(islandMatrix, queue, curI - 1, curJ - 1);
}
}
return sum;
}
Aggregations