<commit_before>/*
 * Copyright 2016-2017 Crown Copyright
 *
 * 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 uk.gov.gchq.gaffer.store.schema;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.google.common.collect.Lists;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.gov.gchq.gaffer.commonutil.CommonConstants;
import uk.gov.gchq.gaffer.commonutil.iterable.ChainedIterable;
import uk.gov.gchq.gaffer.data.element.Edge;
import uk.gov.gchq.gaffer.data.element.Element;
import uk.gov.gchq.gaffer.data.element.Entity;
import uk.gov.gchq.gaffer.data.elementdefinition.ElementDefinitions;
import uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException;
import uk.gov.gchq.gaffer.serialisation.Serialiser;
import uk.gov.gchq.koryphe.ValidationResult;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Function;

/**
 * Contains the full list of {@link uk.gov.gchq.gaffer.data.element.Element} types to be stored in the graph.
 * <p>
 * Each type of element should have the identifier type(s) listed and a map of property names and their corresponding types.
 * Each type can either be a full java class name or a custom type. Using custom types then allows you to specify
 * validation and aggregation for the element components.
 * <p>
 * This class must be JSON serialisable.
 * A schema should normally be written in JSON and then it will be automatically deserialised at runtime.
 * An example of a JSON schemas can be found in the Example module.
 *
 * @see Schema.Builder
 * @see ElementDefinitions
 */
@JsonDeserialize(builder = Schema.Builder.class)
public class Schema extends ElementDefinitions<SchemaEntityDefinition, SchemaEdgeDefinition> implements Cloneable {
    private static final Logger LOGGER = LoggerFactory.getLogger(ElementDefinitions.class);
    private final TypeDefinition unknownType = new TypeDefinition();

    /**
     * The {@link Serialiser} for all vertices.
     */
    private Serialiser vertexSerialiser;

    /**
     * A map of custom type name to {@link TypeDefinition}.
     *
     * @see TypeDefinition
     */
    private Map<String, TypeDefinition> types;

    private String visibilityProperty;

    private String timestampProperty;

    public Schema() {
        this(new LinkedHashMap<>());
    }

    protected Schema(final Map<String, TypeDefinition> types) {
        this.types = types;
    }

    public static Schema fromJson(final InputStream... inputStreams) throws SchemaException {
        return new Schema.Builder().json(inputStreams).build();
    }

    public static Schema fromJson(final Path... filePaths) throws SchemaException {
        return new Schema.Builder().json(filePaths).build();
    }

    public static Schema fromJson(final byte[]... jsonBytes) throws SchemaException {
        return new Schema.Builder().json(jsonBytes).build();
    }

    @SuppressWarnings("CloneDoesntCallSuperClone")
    @SuppressFBWarnings(value = "CN_IDIOM_NO_SUPER_CALL", justification = "Uses toJson instead.")
    @Override
    public Schema clone() {
        return fromJson(toJson(false));
    }

    /**
     * Checks the schema has aggregators.
     *
     * @return {@code true} if the schema contains aggregators, otherwise {@code false}
     */
    @JsonIgnore
    public boolean isAggregationEnabled() {
        boolean isEnabled = false;

        for (final Entry<String, ? extends SchemaElementDefinition> entry : getElementDefinitions()) {
            if (null != entry.getValue() && entry.getValue().isAggregate()) {
                isEnabled = true;
                break;
            }
        }

        return isEnabled;
    }

    /**
     * Creates a Function that takes and element as input and outputs a set of objects including the
     * Group-by values, the Identifiers and the group. These act as a key and can be used in a
     * Collector. This function is dependent on the hashcode of the key components so make sure the
     * Identifiers and Group-By objects all have a valid hashcode method.
     *
     * @return a Set which makes up a unique Idenfifier. The set is composed of the input Element's<commit_msg>gh-822 fixed spelling mistake<commit_after>/*
 * Copyright 2016-2017 Crown Copyright
 *
 * 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 uk.gov.gchq.gaffer.store.schema;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.google.common.collect.Lists;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.gov.gchq.gaffer.commonutil.CommonConstants;
import uk.gov.gchq.gaffer.commonutil.iterable.ChainedIterable;
import uk.gov.gchq.gaffer.data.element.Edge;
import uk.gov.gchq.gaffer.data.element.Element;
import uk.gov.gchq.gaffer.data.element.Entity;
import uk.gov.gchq.gaffer.data.elementdefinition.ElementDefinitions;
import uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException;
import uk.gov.gchq.gaffer.serialisation.Serialiser;
import uk.gov.gchq.koryphe.ValidationResult;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Function;

/**
 * Contains the full list of {@link uk.gov.gchq.gaffer.data.element.Element} types to be stored in the graph.
 * <p>
 * Each type of element should have the identifier type(s) listed and a map of property names and their corresponding types.
 * Each type can either be a full java class name or a custom type. Using custom types then allows you to specify
 * validation and aggregation for the element components.
 * <p>
 * This class must be JSON serialisable.
 * A schema should normally be written in JSON and then it will be automatically deserialised at runtime.
 * An example of a JSON schemas can be found in the Example module.
 *
 * @see Schema.Builder
 * @see ElementDefinitions
 */
@JsonDeserialize(builder = Schema.Builder.class)
public class Schema extends ElementDefinitions<SchemaEntityDefinition, SchemaEdgeDefinition> implements Cloneable {
    private static final Logger LOGGER = LoggerFactory.getLogger(ElementDefinitions.class);
    private final TypeDefinition unknownType = new TypeDefinition();

    /**
     * The {@link Serialiser} for all vertices.
     */
    private Serialiser vertexSerialiser;

    /**
     * A map of custom type name to {@link TypeDefinition}.
     *
     * @see TypeDefinition
     */
    private Map<String, TypeDefinition> types;

    private String visibilityProperty;

    private String timestampProperty;

    public Schema() {
        this(new LinkedHashMap<>());
    }

    protected Schema(final Map<String, TypeDefinition> types) {
        this.types = types;
    }

    public static Schema fromJson(final InputStream... inputStreams) throws SchemaException {
        return new Schema.Builder().json(inputStreams).build();
    }

    public static Schema fromJson(final Path... filePaths) throws SchemaException {
        return new Schema.Builder().json(filePaths).build();
    }

    public static Schema fromJson(final byte[]... jsonBytes) throws SchemaException {
        return new Schema.Builder().json(jsonBytes).build();
    }

    @SuppressWarnings("CloneDoesntCallSuperClone")
    @SuppressFBWarnings(value = "CN_IDIOM_NO_SUPER_CALL", justification = "Uses toJson instead.")
    @Override
    public Schema clone() {
        return fromJson(toJson(false));
    }

    /**
     * Checks the schema has aggregators.
     *
     * @return {@code true} if the schema contains aggregators, otherwise {@code false}
     */
    @JsonIgnore
    public boolean isAggregationEnabled() {
        boolean isEnabled = false;

        for (final Entry<String, ? extends SchemaElementDefinition> entry : getElementDefinitions()) {
            if (null != entry.getValue() && entry.getValue().isAggregate()) {
                isEnabled = true;
                break;
            }
        }

        return isEnabled;
    }

    /**
     * Creates a Function that takes and element as input and outputs a set of objects including the
     * Group-by values, the Identifiers and the group. These act as a key and can be used in a
     * Collector. This function is dependent on the hashcode of the key components so make sure the
     * Identifiers and Group-By objects all have a valid hashcode method.
     *
     * @return a Set which makes up a unique Identifier. The set is composed of the input Element's
{'fraction_non_alphanumeric': 0.08783913796421013, 'fraction_numerical': 0.0031749086011160286, 'mean_word_length': 3.63855421686747, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, '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': '5564091', 'n_tokens_mistral': 2881, 'n_tokens_neox': 2788, 'n_words': 950}