Search in sources :

Example 86 with ArrayDeque

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));
}
Also used : Path(org.apache.hadoop.fs.Path) HdfsEnvironment(com.facebook.presto.hive.HdfsEnvironment) SchemaAlreadyExistsException(com.facebook.presto.hive.SchemaAlreadyExistsException) MetastoreUtil.makePartName(com.facebook.presto.hive.metastore.MetastoreUtil.makePartName) FileSystem(org.apache.hadoop.fs.FileSystem) EXTERNAL_TABLE(org.apache.hadoop.hive.metastore.TableType.EXTERNAL_TABLE) PrincipalType(com.facebook.presto.hive.metastore.PrincipalType) FileStatus(org.apache.hadoop.fs.FileStatus) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) SchemaTableName(com.facebook.presto.spi.SchemaTableName) HIVE_METASTORE_ERROR(com.facebook.presto.hive.HiveErrorCode.HIVE_METASTORE_ERROR) ColumnNotFoundException(com.facebook.presto.spi.ColumnNotFoundException) ExtendedHiveMetastore(com.facebook.presto.hive.metastore.ExtendedHiveMetastore) SchemaNotFoundException(com.facebook.presto.spi.SchemaNotFoundException) Locale(java.util.Locale) Map(java.util.Map) Path(org.apache.hadoop.fs.Path) TableAlreadyExistsException(com.facebook.presto.hive.TableAlreadyExistsException) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) EnumSet(java.util.EnumSet) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) PrincipalPrivileges(com.facebook.presto.hive.metastore.PrincipalPrivileges) Collection(java.util.Collection) Set(java.util.Set) ThreadSafe(javax.annotation.concurrent.ThreadSafe) DEFAULT_DATABASE_NAME(com.facebook.presto.hive.metastore.Database.DEFAULT_DATABASE_NAME) MANAGED_TABLE(org.apache.hadoop.hive.metastore.TableType.MANAGED_TABLE) List(java.util.List) NOT_SUPPORTED(com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED) ByteStreams(com.google.common.io.ByteStreams) Entry(java.util.Map.Entry) USER(com.facebook.presto.hive.metastore.PrincipalType.USER) Optional(java.util.Optional) HivePrivilegeInfo(com.facebook.presto.hive.metastore.HivePrivilegeInfo) JsonCodec(io.airlift.json.JsonCodec) Table(com.facebook.presto.hive.metastore.Table) Column(com.facebook.presto.hive.metastore.Column) Database(com.facebook.presto.hive.metastore.Database) HiveType(com.facebook.presto.hive.HiveType) PrestoException(com.facebook.presto.spi.PrestoException) Function(java.util.function.Function) Partition(com.facebook.presto.hive.metastore.Partition) ArrayList(java.util.ArrayList) OWNERSHIP(com.facebook.presto.hive.metastore.HivePrivilegeInfo.HivePrivilege.OWNERSHIP) Inject(javax.inject.Inject) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) ImmutableList(com.google.common.collect.ImmutableList) ALREADY_EXISTS(com.facebook.presto.spi.StandardErrorCode.ALREADY_EXISTS) Objects.requireNonNull(java.util.Objects.requireNonNull) HiveUtil.toPartitionValues(com.facebook.presto.hive.HiveUtil.toPartitionValues) VIRTUAL_VIEW(org.apache.hadoop.hive.metastore.TableType.VIRTUAL_VIEW) LinkedHashSet(java.util.LinkedHashSet) OutputStream(java.io.OutputStream) ROLE(com.facebook.presto.hive.metastore.PrincipalType.ROLE) IOException(java.io.IOException) Collectors.toList(java.util.stream.Collectors.toList) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) TableType(org.apache.hadoop.hive.metastore.TableType) ArrayDeque(java.util.ArrayDeque) Table(com.facebook.presto.hive.metastore.Table) ArrayDeque(java.util.ArrayDeque)

Example 87 with ArrayDeque

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);
    }
}
Also used : FileStatus(org.apache.hadoop.fs.FileStatus) ArrayList(java.util.ArrayList) PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) ArrayDeque(java.util.ArrayDeque)

Example 88 with ArrayDeque

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);
        }
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) ArrayDeque(java.util.ArrayDeque)

Example 89 with ArrayDeque

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.");
    }
}
Also used : Path(java.nio.file.Path) Stopwatch(com.google.common.base.Stopwatch) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) MessageDigest(java.security.MessageDigest) URI(java.net.URI) ArrayDeque(java.util.ArrayDeque)

Example 90 with ArrayDeque

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;
}
Also used : ArrayDeque(java.util.ArrayDeque) Pair(iurii.job.interview.utils.pair.Pair)

Aggregations

ArrayDeque (java.util.ArrayDeque)217 ArrayList (java.util.ArrayList)36 Test (org.junit.Test)36 IOException (java.io.IOException)27 HashMap (java.util.HashMap)23 List (java.util.List)20 HashSet (java.util.HashSet)19 Map (java.util.Map)17 Deque (java.util.Deque)11 Iterator (java.util.Iterator)10 NoSuchElementException (java.util.NoSuchElementException)8 AtomicLong (java.util.concurrent.atomic.AtomicLong)8 File (java.io.File)7 Path (java.nio.file.Path)7 Random (java.util.Random)7 ByteBuffer (java.nio.ByteBuffer)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 HttpFields (org.eclipse.jetty.http.HttpFields)5 Name (com.github.anba.es6draft.ast.scope.Name)4 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)4