CodecService.java 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.codec;
  20. import org.apache.logging.log4j.Logger;
  21. import org.apache.lucene.codecs.Codec;
  22. import org.apache.lucene.codecs.lucene50.Lucene50PostingsFormat.FSTLoadMode;
  23. import org.apache.lucene.codecs.lucene50.Lucene50StoredFieldsFormat.Mode;
  24. import org.apache.lucene.codecs.lucene80.Lucene80Codec;
  25. import org.elasticsearch.common.Nullable;
  26. import org.elasticsearch.index.mapper.MapperService;
  27. import java.util.HashMap;
  28. import java.util.Map;
  29. /**
  30. * Since Lucene 4.0 low level index segments are read and written through a
  31. * codec layer that allows to use use-case specific file formats &
  32. * data-structures per field. Elasticsearch exposes the full
  33. * {@link Codec} capabilities through this {@link CodecService}.
  34. */
  35. public class CodecService {
  36. private final Map<String, Codec> codecs;
  37. public static final String DEFAULT_CODEC = "default";
  38. public static final String BEST_COMPRESSION_CODEC = "best_compression";
  39. /** the raw unfiltered lucene default. useful for testing */
  40. public static final String LUCENE_DEFAULT_CODEC = "lucene_default";
  41. public CodecService(@Nullable MapperService mapperService, Logger logger) {
  42. final var codecs = new HashMap<String, Codec>();
  43. if (mapperService == null) {
  44. codecs.put(DEFAULT_CODEC, new Lucene80Codec());
  45. codecs.put(BEST_COMPRESSION_CODEC, new Lucene80Codec(Mode.BEST_COMPRESSION, FSTLoadMode.AUTO));
  46. } else {
  47. codecs.put(DEFAULT_CODEC,
  48. new PerFieldMappingPostingFormatCodec(Mode.BEST_SPEED, mapperService, logger));
  49. codecs.put(BEST_COMPRESSION_CODEC,
  50. new PerFieldMappingPostingFormatCodec(Mode.BEST_COMPRESSION, mapperService, logger));
  51. }
  52. codecs.put(LUCENE_DEFAULT_CODEC, Codec.getDefault());
  53. for (String codec : Codec.availableCodecs()) {
  54. codecs.put(codec, Codec.forName(codec));
  55. }
  56. this.codecs = Map.copyOf(codecs);
  57. }
  58. public Codec codec(String name) {
  59. Codec codec = codecs.get(name);
  60. if (codec == null) {
  61. throw new IllegalArgumentException("failed to find codec [" + name + "]");
  62. }
  63. return codec;
  64. }
  65. /**
  66. * Returns all registered available codec names
  67. */
  68. public String[] availableCodecs() {
  69. return codecs.keySet().toArray(new String[0]);
  70. }
  71. }