/* * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. */ /* * Licensed to Elasticsearch under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch licenses this file to you 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. */ /* * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. */ package org.opensearch.action.ingest; import org.opensearch.core.ParseField; import org.opensearch.core.common.io.stream.StreamInput; import org.opensearch.core.common.io.stream.StreamOutput; import org.opensearch.core.common.io.stream.Writeable; import org.opensearch.core.xcontent.ConstructingObjectParser; import org.opensearch.core.xcontent.ToXContentFragment; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.core.xcontent.XContentParser; import org.opensearch.ingest.IngestDocument; import org.opensearch.ingest.IngestDocument.Metadata; import java.io.IOException; import java.time.ZonedDateTime; import java.util.HashMap; import java.util.Map; import java.util.Objects; import static org.opensearch.core.xcontent.ConstructingObjectParser.constructorArg; import static org.opensearch.core.xcontent.ConstructingObjectParser.optionalConstructorArg; /** * An ingest document that implements writeable * * @opensearch.internal */ final class WriteableIngestDocument implements Writeable, ToXContentFragment { static final String SOURCE_FIELD = "_source"; static final String INGEST_FIELD = "_ingest"; static final String DOC_FIELD = "doc"; private final IngestDocument ingestDocument; @SuppressWarnings("unchecked") public static final ConstructingObjectParser INGEST_DOC_PARSER = new ConstructingObjectParser<>( "ingest_document", true, a -> { HashMap sourceAndMetadata = new HashMap<>(); sourceAndMetadata.put(Metadata.INDEX.getFieldName(), a[0]); sourceAndMetadata.put(Metadata.ID.getFieldName(), a[1]); if (a[2] != null) { sourceAndMetadata.put(Metadata.ROUTING.getFieldName(), a[2]); } if (a[3] != null) { sourceAndMetadata.put(Metadata.VERSION.getFieldName(), a[3]); } if (a[4] != null) { sourceAndMetadata.put(Metadata.VERSION_TYPE.getFieldName(), a[4]); } sourceAndMetadata.putAll((Map) a[5]); return new WriteableIngestDocument(new IngestDocument(sourceAndMetadata, (Map) a[6])); } ); static { INGEST_DOC_PARSER.declareString(constructorArg(), new ParseField(Metadata.INDEX.getFieldName())); INGEST_DOC_PARSER.declareString(constructorArg(), new ParseField(Metadata.ID.getFieldName())); INGEST_DOC_PARSER.declareString(optionalConstructorArg(), new ParseField(Metadata.ROUTING.getFieldName())); INGEST_DOC_PARSER.declareLong(optionalConstructorArg(), new ParseField(Metadata.VERSION.getFieldName())); INGEST_DOC_PARSER.declareString(optionalConstructorArg(), new ParseField(Metadata.VERSION_TYPE.getFieldName())); INGEST_DOC_PARSER.declareObject(constructorArg(), (p, c) -> p.map(), new ParseField(SOURCE_FIELD)); INGEST_DOC_PARSER.declareObject(constructorArg(), (p, c) -> { Map ingestMap = p.map(); ingestMap.computeIfPresent("timestamp", (k, o) -> ZonedDateTime.parse((String) o)); return ingestMap; }, new ParseField(INGEST_FIELD)); } public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "writeable_ingest_document", true, a -> (WriteableIngestDocument) a[0] ); static { PARSER.declareObject(constructorArg(), INGEST_DOC_PARSER, new ParseField(DOC_FIELD)); } WriteableIngestDocument(IngestDocument ingestDocument) { assert ingestDocument != null; this.ingestDocument = ingestDocument; } WriteableIngestDocument(StreamInput in) throws IOException { Map sourceAndMetadata = in.readMap(); Map ingestMetadata = in.readMap(); this.ingestDocument = new IngestDocument(sourceAndMetadata, ingestMetadata); } @Override public void writeTo(StreamOutput out) throws IOException { out.writeMap(ingestDocument.getSourceAndMetadata()); out.writeMap(ingestDocument.getIngestMetadata()); } IngestDocument getIngestDocument() { return ingestDocument; } @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(DOC_FIELD); Map metadataMap = ingestDocument.getMetadata(); for (Map.Entry metadata : metadataMap.entrySet()) { if (metadata.getValue() != null) { builder.field(metadata.getKey().getFieldName(), metadata.getValue().toString()); } } Map source = IngestDocument.deepCopyMap(ingestDocument.getSourceAndMetadata()); metadataMap.keySet().forEach(mD -> source.remove(mD.getFieldName())); builder.field(SOURCE_FIELD, source); builder.field(INGEST_FIELD, ingestDocument.getIngestMetadata()); builder.endObject(); return builder; } public static WriteableIngestDocument fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } WriteableIngestDocument that = (WriteableIngestDocument) o; return Objects.equals(ingestDocument, that.ingestDocument); } @Override public int hashCode() { return Objects.hash(ingestDocument); } @Override public String toString() { return ingestDocument.toString(); } }