<commit_before>/**
 * Copyright 2011-2016 Asakusa Framework Team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.asakusafw.lang.compiler.extension.testdriver;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.Objects;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.asakusafw.lang.compiler.internalio.InternalExporterDescription;
import com.asakusafw.runtime.io.ModelOutput;
import com.asakusafw.runtime.stage.temporary.TemporaryStorage;
import com.asakusafw.runtime.util.VariableTable;
import com.asakusafw.testdriver.core.BaseExporterRetriever;
import com.asakusafw.testdriver.core.DataModelDefinition;
import com.asakusafw.testdriver.core.DataModelSource;
import com.asakusafw.testdriver.core.ExporterRetriever;
import com.asakusafw.testdriver.core.TestContext;
import com.asakusafw.testdriver.hadoop.ConfigurationFactory;

/**
 * Implementation of {@link ExporterRetriever} for {@link InternalExporterDescription}s.
 */
public class InternalExporterRetriever extends BaseExporterRetriever<InternalExporterDescription> {

    static final Logger LOG = LoggerFactory.getLogger(InternalExporterRetriever.class);

    private final ConfigurationFactory configurations;

    /**
     * Creates a new instance with default configurations.
     */
    public InternalExporterRetriever() {
        this(ConfigurationFactory.getDefault());
    }

    /**
     * Creates a new instance.
     * @param configurations the configuration factory
     * @throws IllegalArgumentException if some parameters were {@code null}
     */
    public InternalExporterRetriever(ConfigurationFactory configurations) {
        this.configurations = Objects.requireNonNull(configurations, "configurations must not be null"); //$NON-NLS-1$
    }

    @Override
    public void truncate(
            InternalExporterDescription description,
            TestContext context) throws IOException {
        LOG.debug("deleting output directory: {}", description); //$NON-NLS-1$
        VariableTable variables = createVariables(context);
        Configuration config = configurations.newInstance();
        FileSystem fs = FileSystem.get(config);
        String resolved = variables.parse(description.getPathPrefix(), false);
        Path path = new Path(resolved);
        Path output = path.getParent();
        Path target;
        if (output == null) {
            LOG.warn(MessageFormat.format(
                    "skipped deleting output directory because it is a base directory: {0}",
                    path));
            target = fs.makeQualified(path);
        } else {
            LOG.debug("output directory will be deleted: {}", output); //$NON-NLS-1$
            target = fs.makeQualified(output);
        }
        LOG.debug("deleting output target: {}", target); //$NON-NLS-1$
        boolean succeed = fs.delete(target, true);
        LOG.debug("deleted output target (succeed={}): {}", succeed, target); //$NON-NLS-1$
    }

    @Override
    public <V> ModelOutput<V> createOutput(
            DataModelDefinition<V> definition,
            InternalExporterDescription description,
            TestContext context) throws IOException {
        LOG.debug("preparing initial output: {}", description); //$NON-NLS-1$<commit_msg>Fix `InternalExporterRetriever.truncate` for a corner case.<commit_after>/**
 * Copyright 2011-2016 Asakusa Framework Team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.asakusafw.lang.compiler.extension.testdriver;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.Objects;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.asakusafw.lang.compiler.internalio.InternalExporterDescription;
import com.asakusafw.runtime.io.ModelOutput;
import com.asakusafw.runtime.stage.temporary.TemporaryStorage;
import com.asakusafw.runtime.util.VariableTable;
import com.asakusafw.testdriver.core.BaseExporterRetriever;
import com.asakusafw.testdriver.core.DataModelDefinition;
import com.asakusafw.testdriver.core.DataModelSource;
import com.asakusafw.testdriver.core.ExporterRetriever;
import com.asakusafw.testdriver.core.TestContext;
import com.asakusafw.testdriver.hadoop.ConfigurationFactory;

/**
 * Implementation of {@link ExporterRetriever} for {@link InternalExporterDescription}s.
 */
public class InternalExporterRetriever extends BaseExporterRetriever<InternalExporterDescription> {

    static final Logger LOG = LoggerFactory.getLogger(InternalExporterRetriever.class);

    private final ConfigurationFactory configurations;

    /**
     * Creates a new instance with default configurations.
     */
    public InternalExporterRetriever() {
        this(ConfigurationFactory.getDefault());
    }

    /**
     * Creates a new instance.
     * @param configurations the configuration factory
     * @throws IllegalArgumentException if some parameters were {@code null}
     */
    public InternalExporterRetriever(ConfigurationFactory configurations) {
        this.configurations = Objects.requireNonNull(configurations, "configurations must not be null"); //$NON-NLS-1$
    }

    @Override
    public void truncate(
            InternalExporterDescription description,
            TestContext context) throws IOException {
        LOG.debug("deleting output directory: {}", description); //$NON-NLS-1$
        VariableTable variables = createVariables(context);
        Configuration config = configurations.newInstance();
        FileSystem fs = FileSystem.get(config);
        String resolved = variables.parse(description.getPathPrefix(), false);
        Path path = new Path(resolved);
        Path output = path.getParent();
        Path target;
        if (output == null) {
            LOG.warn(MessageFormat.format(
                    "skipped deleting output directory because it is a base directory: {0}",
                    path));
            target = fs.makeQualified(path);
        } else {
            LOG.debug("output directory will be deleted: {}", output); //$NON-NLS-1$
            target = fs.makeQualified(output);
        }
        LOG.debug("deleting output target: {}", target); //$NON-NLS-1$
        try {
            FileStatus[] stats = fs.globStatus(path);
            for (FileStatus s : stats) {
                Path f = s.getPath();
                boolean deleted = fs.delete(f, true);
                LOG.debug("deleted output target (succeed={}): {}", deleted, f); //$NON-NLS-1$
            }
        } catch (IOException e) {
            LOG.debug("exception in truncate", e);
        }
    }

    @Override
    public <V> ModelOutput<V> createOutput(
            DataModelDefinition<V> definition,
            InternalExporterDescription description,
            TestContext context) throws IOException {
        LOG.debug("preparing initial output: {}", description); //$NON-NLS-1$
{'fraction_non_alphanumeric': 0.09821428571428571, 'fraction_numerical': 0.005136986301369863, 'mean_word_length': 3.344845908607864, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1701389', 'n_tokens_mistral': 2198, 'n_tokens_neox': 2163, 'n_words': 584}