1212import org .elasticsearch .Version ;
1313import org .elasticsearch .cluster .AbstractDiffable ;
1414import org .elasticsearch .cluster .Diff ;
15- import org .elasticsearch .common .Strings ;
1615import org .elasticsearch .common .collect .ImmutableOpenMap ;
1716import org .elasticsearch .common .compress .CompressedXContent ;
18- import org .elasticsearch .common .hash .MessageDigests ;
1917import org .elasticsearch .common .io .stream .StreamInput ;
2018import org .elasticsearch .common .io .stream .StreamOutput ;
2119import org .elasticsearch .common .xcontent .XContentHelper ;
2220import org .elasticsearch .index .mapper .DocumentMapper ;
2321import org .elasticsearch .index .mapper .MapperService ;
24- import org .elasticsearch .xcontent .XContentType ;
2522
2623import java .io .IOException ;
2724import java .io .UncheckedIOException ;
28- import java .nio .charset .StandardCharsets ;
2925import java .util .Collections ;
3026import java .util .Map ;
3127import 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 {
0 commit comments