Skip to content

Commit c770523

Browse files
committed
First step of moving sha256 to CompressedXContent, by letting it get computed
from from the compressed bytes in CompressedXContent.
1 parent 15b05af commit c770523

2 files changed

Lines changed: 26 additions & 23 deletions

File tree

server/src/main/java/org/elasticsearch/cluster/metadata/MappingMetadata.java

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,16 @@
1212
import org.elasticsearch.Version;
1313
import org.elasticsearch.cluster.AbstractDiffable;
1414
import org.elasticsearch.cluster.Diff;
15-
import org.elasticsearch.common.Strings;
1615
import org.elasticsearch.common.collect.ImmutableOpenMap;
1716
import org.elasticsearch.common.compress.CompressedXContent;
18-
import org.elasticsearch.common.hash.MessageDigests;
1917
import org.elasticsearch.common.io.stream.StreamInput;
2018
import org.elasticsearch.common.io.stream.StreamOutput;
2119
import org.elasticsearch.common.xcontent.XContentHelper;
2220
import org.elasticsearch.index.mapper.DocumentMapper;
2321
import org.elasticsearch.index.mapper.MapperService;
24-
import org.elasticsearch.xcontent.XContentType;
2522

2623
import java.io.IOException;
2724
import java.io.UncheckedIOException;
28-
import java.nio.charset.StandardCharsets;
2925
import java.util.Collections;
3026
import java.util.Map;
3127
import java.util.Objects;
@@ -51,7 +47,7 @@ public MappingMetadata(DocumentMapper docMapper) {
5147
this.type = docMapper.type();
5248
this.source = docMapper.mappingSource();
5349
this.routingRequired = docMapper.routingFieldMapper().required();
54-
this.digest = computeDigest(source);
50+
this.digest = source.getSha256();
5551
}
5652

5753
public MappingMetadata(CompressedXContent mapping) {
@@ -62,14 +58,13 @@ public MappingMetadata(CompressedXContent mapping) {
6258
}
6359
this.type = mappingMap.keySet().iterator().next();
6460
this.routingRequired = routingRequired((Map<?, ?>) mappingMap.get(this.type));
65-
this.digest = computeDigest(mapping);
61+
this.digest = source.getSha256();
6662
}
6763

6864
public MappingMetadata(String type, Map<String, Object> mapping) {
6965
this.type = type;
70-
String mappingAsString = Strings.toString((builder, params) -> builder.mapContents(mapping));
7166
try {
72-
this.source = new CompressedXContent(mappingAsString);
67+
this.source = new CompressedXContent((builder, params) -> builder.mapContents(mapping));
7368
} catch (IOException e) {
7469
throw new UncheckedIOException(e); // XContent exception, should never happen
7570
}
@@ -78,20 +73,7 @@ public MappingMetadata(String type, Map<String, Object> mapping) {
7873
withoutType = (Map<?, ?>) mapping.get(type);
7974
}
8075
this.routingRequired = routingRequired(withoutType);
81-
this.digest = computeDigest(mappingAsString);
82-
}
83-
84-
static String computeDigest(CompressedXContent source) {
85-
try {
86-
String mapping = XContentHelper.convertToJson(source.uncompressed(), false, XContentType.JSON);
87-
return computeDigest(mapping);
88-
} catch (IOException e) {
89-
throw new UncheckedIOException(e);
90-
}
91-
}
92-
93-
static String computeDigest(String mapping) {
94-
return MessageDigests.toHexString(MessageDigests.sha256().digest(mapping.getBytes(StandardCharsets.UTF_8)));
76+
this.digest = source.getSha256();
9577
}
9678

9779
public static void writeMappingMetadata(StreamOutput out, ImmutableOpenMap<String, MappingMetadata> mappings) throws IOException {
@@ -201,7 +183,7 @@ public MappingMetadata(StreamInput in) throws IOException {
201183
type = in.readString();
202184
source = CompressedXContent.readCompressedString(in);
203185
routingRequired = in.readBoolean();
204-
digest = in.getVersion().onOrAfter(Version.V_8_1_0) ? in.readString() : computeDigest(source);
186+
digest = in.getVersion().onOrAfter(Version.V_8_1_0) ? in.readString() : source.getSha256();
205187
}
206188

207189
public static Diff<MappingMetadata> readDiffFrom(StreamInput in) throws IOException {

server/src/main/java/org/elasticsearch/common/compress/CompressedXContent.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.elasticsearch.ElasticsearchException;
1212
import org.elasticsearch.common.bytes.BytesArray;
1313
import org.elasticsearch.common.bytes.BytesReference;
14+
import org.elasticsearch.common.hash.MessageDigests;
1415
import org.elasticsearch.common.io.Streams;
1516
import org.elasticsearch.common.io.stream.BytesStreamOutput;
1617
import org.elasticsearch.common.io.stream.StreamInput;
@@ -24,6 +25,7 @@
2425
import java.io.OutputStream;
2526
import java.nio.ByteBuffer;
2627
import java.nio.charset.StandardCharsets;
28+
import java.security.MessageDigest;
2729
import java.util.Arrays;
2830
import java.util.zip.CRC32;
2931
import java.util.zip.CheckedOutputStream;
@@ -160,6 +162,25 @@ public String string() {
160162
return uncompressed().utf8ToString();
161163
}
162164

165+
public String getSha256() {
166+
MessageDigest messageDigest = MessageDigests.sha256();
167+
try (InflaterAndBuffer inflaterAndBuffer = inflater1.get()) {
168+
final Inflater inflater = inflaterAndBuffer.inflater;
169+
final ByteBuffer buffer = inflaterAndBuffer.buffer;
170+
assert assertBufferIsCleared(buffer);
171+
setInflaterInput(compressed(), inflater);
172+
do {
173+
if (inflater.inflate(buffer) > 0) {
174+
messageDigest.update(buffer.flip());
175+
}
176+
buffer.clear();
177+
} while (inflater.finished() == false);
178+
return MessageDigests.toHexString(messageDigest.digest());
179+
} catch (DataFormatException e) {
180+
throw new ElasticsearchException(e);
181+
}
182+
}
183+
163184
public static CompressedXContent readCompressedString(StreamInput in) throws IOException {
164185
int crc32 = in.readInt();
165186
return new CompressedXContent(in.readByteArray(), crc32);

0 commit comments

Comments
 (0)