SegmentReaderUtils.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.common.lucene;
  20. import org.apache.lucene.index.AtomicReader;
  21. import org.apache.lucene.index.FilterAtomicReader;
  22. import org.apache.lucene.index.SegmentReader;
  23. import org.elasticsearch.ElasticsearchIllegalStateException;
  24. import org.elasticsearch.common.Nullable;
  25. public class SegmentReaderUtils {
  26. /**
  27. * Tries to extract a segment reader from the given index reader.
  28. * If no SegmentReader can be extracted an {@link org.elasticsearch.ElasticsearchIllegalStateException} is thrown.
  29. */
  30. @Nullable
  31. public static SegmentReader segmentReader(AtomicReader reader) {
  32. return internalSegmentReader(reader, true);
  33. }
  34. /**
  35. * Tries to extract a segment reader from the given index reader and returns it, otherwise <code>null</code>
  36. * is returned
  37. */
  38. @Nullable
  39. public static SegmentReader segmentReaderOrNull(AtomicReader reader) {
  40. return internalSegmentReader(reader, false);
  41. }
  42. public static boolean registerCoreListener(AtomicReader reader, SegmentReader.CoreClosedListener listener) {
  43. SegmentReader segReader = SegmentReaderUtils.segmentReaderOrNull(reader);
  44. if (segReader != null) {
  45. segReader.addCoreClosedListener(listener);
  46. return true;
  47. }
  48. return false;
  49. }
  50. private static SegmentReader internalSegmentReader(AtomicReader reader, boolean fail) {
  51. if (reader == null) {
  52. return null;
  53. }
  54. if (reader instanceof SegmentReader) {
  55. return (SegmentReader) reader;
  56. } else if (reader instanceof FilterAtomicReader) {
  57. final FilterAtomicReader fReader = (FilterAtomicReader) reader;
  58. return segmentReader(FilterAtomicReader.unwrap(fReader));
  59. }
  60. if (fail) {
  61. // hard fail - we can't get a SegmentReader
  62. throw new ElasticsearchIllegalStateException("Can not extract segment reader from given index reader [" + reader + "]");
  63. }
  64. return null;
  65. }
  66. }