MapperUtils.java 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Licensed to Elasticsearch under one or more contributor
  3. * license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright
  5. * ownership. Elasticsearch licenses this file to you under
  6. * the Apache License, Version 2.0 (the "License"); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.elasticsearch.index.mapper;
  20. import org.elasticsearch.common.Strings;
  21. import org.elasticsearch.index.mapper.object.ObjectMapper;
  22. import org.elasticsearch.index.mapper.object.RootObjectMapper;
  23. import java.io.IOException;
  24. import java.util.Collection;
  25. public enum MapperUtils {
  26. ;
  27. private static MergeResult newStrictMergeContext() {
  28. return new MergeResult(false) {
  29. @Override
  30. public boolean hasConflicts() {
  31. return false;
  32. }
  33. @Override
  34. public String[] buildConflicts() {
  35. return Strings.EMPTY_ARRAY;
  36. }
  37. @Override
  38. public void addObjectMappers(Collection<ObjectMapper> objectMappers) {
  39. // no-op
  40. }
  41. @Override
  42. public void addFieldMappers(Collection<FieldMapper<?>> fieldMappers) {
  43. // no-op
  44. }
  45. @Override
  46. public void addConflict(String mergeFailure) {
  47. throw new MapperParsingException("Merging dynamic updates triggered a conflict: " + mergeFailure);
  48. }
  49. };
  50. }
  51. /**
  52. * Merge {@code mergeWith} into {@code mergeTo}. Note: this method only
  53. * merges mappings, not lookup structures. Conflicts are returned as exceptions.
  54. */
  55. public static void merge(Mapper mergeInto, Mapper mergeWith) {
  56. mergeInto.merge(mergeWith, newStrictMergeContext());
  57. }
  58. /**
  59. * Merge {@code mergeWith} into {@code mergeTo}. Note: this method only
  60. * merges mappings, not lookup structures. Conflicts are returned as exceptions.
  61. */
  62. public static void merge(Mapping mergeInto, Mapping mergeWith) {
  63. mergeInto.merge(mergeWith, newStrictMergeContext());
  64. }
  65. /** Split mapper and its descendants into object and field mappers. */
  66. public static void collect(Mapper mapper, Collection<ObjectMapper> objectMappers, Collection<FieldMapper<?>> fieldMappers) {
  67. if (mapper instanceof RootObjectMapper) {
  68. // root mapper isn't really an object mapper
  69. } else if (mapper instanceof ObjectMapper) {
  70. objectMappers.add((ObjectMapper)mapper);
  71. } else if (mapper instanceof FieldMapper<?>) {
  72. fieldMappers.add((FieldMapper<?>)mapper);
  73. }
  74. for (Mapper child : mapper) {
  75. collect(child, objectMappers, fieldMappers);
  76. }
  77. }
  78. }