|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.action.index; |
| 11 | + |
| 12 | +import org.elasticsearch.common.bytes.BytesReference; |
| 13 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 14 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 15 | +import org.elasticsearch.common.io.stream.Writeable; |
| 16 | +import org.elasticsearch.common.xcontent.XContentHelper; |
| 17 | +import org.elasticsearch.core.Releasable; |
| 18 | +import org.elasticsearch.xcontent.XContentType; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +public class SourceContext implements Writeable { |
| 24 | + |
| 25 | + private XContentType contentType; |
| 26 | + private BytesReference source; |
| 27 | + private Releasable sourceReleasable; |
| 28 | + |
| 29 | + public SourceContext(XContentType contentType, BytesReference source, Releasable sourceReleasable) { |
| 30 | + this.contentType = contentType; |
| 31 | + this.source = source; |
| 32 | + this.sourceReleasable = sourceReleasable; |
| 33 | + } |
| 34 | + |
| 35 | + public SourceContext(StreamInput in) throws IOException { |
| 36 | + if (in.readBoolean()) { |
| 37 | + // faster than StreamInput::readEnum, do not replace we read a lot of these instances at times |
| 38 | + contentType = XContentType.ofOrdinal(in.readByte()); |
| 39 | + } else { |
| 40 | + contentType = null; |
| 41 | + } |
| 42 | + source = in.readBytesReference(); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void writeTo(StreamOutput out) throws IOException { |
| 47 | + if (contentType != null) { |
| 48 | + out.writeBoolean(true); |
| 49 | + XContentHelper.writeTo(out, contentType); |
| 50 | + } else { |
| 51 | + out.writeBoolean(false); |
| 52 | + } |
| 53 | + out.writeBytesReference(source); |
| 54 | + } |
| 55 | + |
| 56 | + public XContentType getContentType() { |
| 57 | + return contentType; |
| 58 | + } |
| 59 | + |
| 60 | + public BytesReference getSource() { |
| 61 | + return source; |
| 62 | + } |
| 63 | + |
| 64 | + public Map<String, Object> sourceAsMap() { |
| 65 | + return XContentHelper.convertToMap(source, false, contentType).v2(); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | +} |
0 commit comments