All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.elasticsearch.cluster.metadata.MappingMetadata Maven / Gradle / Ivy

There is a newer version: 8.15.1
Show newest version
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */

package org.elasticsearch.cluster.metadata;

import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.TransportVersions;
import org.elasticsearch.cluster.Diff;
import org.elasticsearch.cluster.SimpleDiffable;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.MapperService;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Map;
import java.util.Objects;

import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeBooleanValue;

/**
 * Mapping configuration for a type.
 */
public class MappingMetadata implements SimpleDiffable {

    public static final MappingMetadata EMPTY_MAPPINGS = new MappingMetadata(
        MapperService.SINGLE_MAPPING_NAME,
        Map.of(MapperService.SINGLE_MAPPING_NAME, Map.of())
    );

    private final String type;

    private final CompressedXContent source;

    private final boolean routingRequired;

    public MappingMetadata(DocumentMapper docMapper) {
        this.type = docMapper.type();
        this.source = docMapper.mappingSource();
        this.routingRequired = docMapper.routingFieldMapper().required();
    }

    @SuppressWarnings({ "this-escape", "unchecked" })
    public MappingMetadata(CompressedXContent mapping) {
        this.source = mapping;
        Map mappingMap = XContentHelper.convertToMap(mapping.compressedReference(), true).v2();
        if (mappingMap.size() != 1) {
            throw new IllegalStateException("Can't derive type from mapping, no root type: " + mapping.string());
        }
        this.type = mappingMap.keySet().iterator().next();
        this.routingRequired = routingRequired((Map) mappingMap.get(this.type));
    }

    @SuppressWarnings({ "this-escape", "unchecked" })
    public MappingMetadata(String type, Map mapping) {
        this.type = type;
        try {
            this.source = new CompressedXContent(mapping);
        } catch (IOException e) {
            throw new UncheckedIOException(e);  // XContent exception, should never happen
        }
        Map withoutType = mapping;
        if (mapping.size() == 1 && mapping.containsKey(type)) {
            withoutType = (Map) mapping.get(type);
        }
        this.routingRequired = routingRequired(withoutType);
    }

    public static void writeMappingMetadata(StreamOutput out, Map mappings) throws IOException {
        out.writeMap(mappings, out.getTransportVersion().before(TransportVersions.V_8_0_0) ? (o, v) -> {
            o.writeVInt(v == EMPTY_MAPPINGS ? 0 : 1);
            if (v != EMPTY_MAPPINGS) {
                o.writeString(MapperService.SINGLE_MAPPING_NAME);
                v.writeTo(o);
            }
        } : (o, v) -> {
            o.writeBoolean(v != EMPTY_MAPPINGS);
            if (v != EMPTY_MAPPINGS) {
                v.writeTo(o);
            }
        });
    }

    @SuppressWarnings("unchecked")
    private boolean routingRequired(Map withoutType) {
        boolean required = false;
        if (withoutType.containsKey("_routing")) {
            Map routingNode = (Map) withoutType.get("_routing");
            for (Map.Entry entry : routingNode.entrySet()) {
                String fieldName = entry.getKey();
                Object fieldNode = entry.getValue();
                if (fieldName.equals("required")) {
                    try {
                        required = nodeBooleanValue(fieldNode);
                    } catch (IllegalArgumentException ex) {
                        throw new IllegalArgumentException(
                            "Failed to create mapping for type [" + this.type() + "]. " + "Illegal value in field [_routing.required].",
                            ex
                        );
                    }
                }
            }
        }
        return required;
    }

    public String type() {
        return this.type;
    }

    public CompressedXContent source() {
        return this.source;
    }

    /**
     * Converts the serialized compressed form of the mappings into a parsed map.
     */
    @SuppressWarnings("unchecked")
    public Map sourceAsMap() throws ElasticsearchParseException {
        Map mapping = XContentHelper.convertToMap(source.compressedReference(), true).v2();
        if (mapping.size() == 1 && mapping.containsKey(type())) {
            // the type name is the root value, reduce it
            mapping = (Map) mapping.get(type());
        }
        return mapping;
    }

    /**
     * Converts the serialized compressed form of the mappings into a parsed map.
     */
    public Map getSourceAsMap() throws ElasticsearchParseException {
        return sourceAsMap();
    }

    /**
     * Converts the serialized compressed form of the mappings into a parsed map.
     * In contrast to {@link #sourceAsMap()}, this does not remove the type
     */
    @SuppressWarnings("unchecked")
    public Map rawSourceAsMap() throws ElasticsearchParseException {
        Map mapping = XContentHelper.convertToMap(source.compressedReference(), true).v2();
        return mapping;
    }

    public boolean routingRequired() {
        return this.routingRequired;
    }

    public String getSha256() {
        return source.getSha256();
    }

    @Override
    public void writeTo(StreamOutput out) throws IOException {
        out.writeString(type());
        source().writeTo(out);
        // routing
        out.writeBoolean(routingRequired);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        MappingMetadata that = (MappingMetadata) o;

        if (Objects.equals(this.routingRequired, that.routingRequired) == false) return false;
        if (source.equals(that.source) == false) return false;
        if (type.equals(that.type) == false) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return Objects.hash(type, source, routingRequired);
    }

    public MappingMetadata(StreamInput in) throws IOException {
        type = in.readString();
        source = CompressedXContent.readCompressedString(in);
        routingRequired = in.readBoolean();
    }

    public static Diff readDiffFrom(StreamInput in) throws IOException {
        return SimpleDiffable.readDiffFrom(MappingMetadata::new, in);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy