Search in sources :

Example 26 with Path

use of java.nio.file.Path in project crate by crate.

the class TransportSQLActionClassLifecycleTest method testCopyToDirectoryOnPartitionedTableWithoutPartitionClause.

@Test
// COPY has no rowcount
@UseJdbc(0)
public void testCopyToDirectoryOnPartitionedTableWithoutPartitionClause() throws Exception {
    String uriTemplate = Paths.get(folder.getRoot().toURI()).toUri().toString();
    SQLResponse response = execute("copy parted to DIRECTORY ?", $(uriTemplate));
    assertThat(response.rowCount(), is(4L));
    List<String> lines = new ArrayList<>(4);
    DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(folder.getRoot().toURI()), "*.json");
    for (Path entry : stream) {
        lines.addAll(Files.readAllLines(entry, StandardCharsets.UTF_8));
    }
    assertThat(lines.size(), is(4));
    for (String line : lines) {
        // date column included in output
        if (!line.contains("1388534400000")) {
            assertTrue(line.contains("1391212800000"));
        }
        assertThat(line, startsWith("{"));
        assertThat(line, endsWith("}"));
    }
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) SQLResponse(io.crate.testing.SQLResponse) UseJdbc(io.crate.testing.UseJdbc)

Example 27 with Path

use of java.nio.file.Path in project crate by crate.

the class BlobContainer method createSubDirectories.

/**
     * All files are saved into a sub-folder
     * that is named after the first two characters of the file's sha1 hash
     * pre create these folders so that a .exists() check can be saved on each put request.
     *
     * @param parentDir
     */
private void createSubDirectories(Path parentDir) throws IOException {
    for (int i = 0; i < SUB_DIRS.length; i++) {
        Path subDir = parentDir.resolve(SUB_DIRS[i]);
        subDirs[i] = subDir.toFile();
        Files.createDirectories(subDir);
    }
}
Also used : Path(java.nio.file.Path)

Example 28 with Path

use of java.nio.file.Path in project crate by crate.

the class PluginLoader method findImplementations.

private Collection<Class<? extends Plugin>> findImplementations() {
    if (!isAccessibleDirectory(pluginsPath, logger)) {
        return Collections.emptyList();
    }
    File[] plugins = pluginsPath.toFile().listFiles();
    if (plugins == null) {
        return Collections.emptyList();
    }
    Collection<Class<? extends Plugin>> allImplementations = new ArrayList<>();
    for (File plugin : plugins) {
        if (!plugin.canRead()) {
            logger.debug("[{}] is not readable.", plugin.getAbsolutePath());
            continue;
        }
        // check if its an elasticsearch plugin
        Path esDescriptorFile = plugin.toPath().resolve(PluginInfo.ES_PLUGIN_PROPERTIES);
        try {
            if (esDescriptorFile.toFile().exists()) {
                continue;
            }
        } catch (Exception e) {
        // ignore
        }
        List<URL> pluginUrls = new ArrayList<>();
        logger.trace("--- adding plugin [{}]", plugin.getAbsolutePath());
        try {
            URL pluginURL = plugin.toURI().toURL();
            // jar-hell check the plugin against the parent classloader
            try {
                checkJarHell(pluginURL);
            } catch (Exception e) {
                String msg = String.format(Locale.ENGLISH, "failed to load plugin %s due to jar hell", pluginURL);
                logger.error(msg, e);
                throw new RuntimeException(msg, e);
            }
            pluginUrls.add(pluginURL);
            if (!plugin.isFile()) {
                // gather files to add
                List<File> libFiles = Lists.newArrayList();
                File[] pluginFiles = plugin.listFiles();
                if (pluginFiles != null) {
                    libFiles.addAll(Arrays.asList(pluginFiles));
                }
                File libLocation = new File(plugin, "lib");
                if (libLocation.exists() && libLocation.isDirectory()) {
                    File[] pluginLibFiles = libLocation.listFiles();
                    if (pluginLibFiles != null) {
                        libFiles.addAll(Arrays.asList(pluginLibFiles));
                    }
                }
                // if there are jars in it, add it as well
                for (File libFile : libFiles) {
                    if (!(libFile.getName().endsWith(".jar") || libFile.getName().endsWith(".zip"))) {
                        continue;
                    }
                    URL libURL = libFile.toURI().toURL();
                    // jar-hell check the plugin lib against the parent classloader
                    try {
                        checkJarHell(libURL);
                        pluginUrls.add(libURL);
                    } catch (Exception e) {
                        String msg = String.format(Locale.ENGLISH, "Library %s of plugin %s already loaded", libURL, pluginURL);
                        logger.error(msg, e);
                        throw new RuntimeException(msg, e);
                    }
                }
            }
        } catch (MalformedURLException e) {
            String msg = String.format(Locale.ENGLISH, "failed to add plugin [%s]", plugin);
            logger.error(msg, e);
            throw new RuntimeException(msg, e);
        }
        Collection<Class<? extends Plugin>> implementations = findImplementations(pluginUrls);
        if (implementations == null || implementations.isEmpty()) {
            String msg = String.format(Locale.ENGLISH, "Path [%s] does not contain a valid Crate or Elasticsearch plugin", plugin.getAbsolutePath());
            RuntimeException e = new RuntimeException(msg);
            logger.error(msg, e);
            throw e;
        }
        jarsToLoad.addAll(pluginUrls);
        allImplementations.addAll(implementations);
    }
    return allImplementations;
}
Also used : Path(java.nio.file.Path) MalformedURLException(java.net.MalformedURLException) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) File(java.io.File) Plugin(io.crate.Plugin)

Example 29 with Path

use of java.nio.file.Path in project crate by crate.

the class CrateSettingsPreparer method prepareEnvironment.

/**
     * ES_COPY_OF: core/src/main/java/org/elasticsearch/node/internal/InternalSettingsPreparer.java
     * This is a copy of {@link InternalSettingsPreparer#prepareEnvironment(Settings, Terminal)}
     * <p>
     * with the addition of the "applyCrateDefaults" call.
     */
public static Environment prepareEnvironment(Settings input, Terminal terminal) {
    // just create enough settings to build the environment, to get the config dir
    Settings.Builder output = settingsBuilder();
    InternalSettingsPreparer.initializeSettings(output, input, true);
    Environment environment = new Environment(output.build());
    Path path = environment.configFile().resolve("crate.yml");
    if (Files.exists(path)) {
        output.loadFromPath(path);
    }
    // re-initialize settings now that the config file has been loaded
    // TODO: only re-initialize if a config file was actually loaded
    InternalSettingsPreparer.initializeSettings(output, input, false);
    InternalSettingsPreparer.finalizeSettings(output, terminal, environment.configFile());
    validateKnownSettings(output);
    applyCrateDefaults(output);
    environment = new Environment(output.build());
    // we put back the path.logs so we can use it in the logging configuration file
    output.put("path.logs", cleanPath(environment.logsFile().toAbsolutePath().toString()));
    return new Environment(output.build());
}
Also used : Path(java.nio.file.Path) Strings.cleanPath(org.elasticsearch.common.Strings.cleanPath) Environment(org.elasticsearch.env.Environment) CrateSettings(io.crate.metadata.settings.CrateSettings) Settings(org.elasticsearch.common.settings.Settings)

Example 30 with Path

use of java.nio.file.Path in project crate by crate.

the class BlobPathITest method testDataIsStoredInGlobalBlobPath.

@Test
public void testDataIsStoredInGlobalBlobPath() throws Exception {
    launchNodeAndInitClient(configureGlobalBlobPath());
    Settings indexSettings = oneShardAndZeroReplicas();
    blobAdminClient.createBlobTable("test", indexSettings).get();
    client.put("test", "abcdefg");
    String digest = "2fb5e13419fc89246865e7a324f476ec624e8740";
    try (Stream<Path> files = Files.walk(globalBlobPath)) {
        assertThat(files.anyMatch(i -> digest.equals(i.getFileName().toString())), is(true));
    }
}
Also used : Path(java.nio.file.Path) HttpServerTransport(org.elasticsearch.http.HttpServerTransport) Files(java.nio.file.Files) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) SETTING_NUMBER_OF_SHARDS(org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) IOException(java.io.IOException) BlobIndicesService(io.crate.blob.v2.BlobIndicesService) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) BlobAdminClient(io.crate.blob.v2.BlobAdminClient) SETTING_NUMBER_OF_REPLICAS(org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS) List(java.util.List) Settings(org.elasticsearch.common.settings.Settings) Stream(java.util.stream.Stream) ESIntegTestCase(org.elasticsearch.test.ESIntegTestCase) Matchers.lessThan(org.hamcrest.Matchers.lessThan) Is.is(org.hamcrest.core.Is.is) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Path(java.nio.file.Path) Settings(org.elasticsearch.common.settings.Settings) Test(org.junit.Test)

Aggregations

Path (java.nio.file.Path)4893 Test (org.junit.Test)1960 IOException (java.io.IOException)829 File (java.io.File)445 SourcePath (com.facebook.buck.rules.SourcePath)389 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)334 BuildTarget (com.facebook.buck.model.BuildTarget)320 ArrayList (java.util.ArrayList)313 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)250 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)231 PathSourcePath (com.facebook.buck.rules.PathSourcePath)226 InputStream (java.io.InputStream)210 ImmutableList (com.google.common.collect.ImmutableList)175 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)166 HashMap (java.util.HashMap)159 ImmutableMap (com.google.common.collect.ImmutableMap)157 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)154 Matchers.containsString (org.hamcrest.Matchers.containsString)148 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)147 Map (java.util.Map)146