|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.index.engine; |
| 7 | + |
| 8 | +import org.apache.lucene.index.BinaryDocValues; |
| 9 | +import org.apache.lucene.index.DirectoryReader; |
| 10 | +import org.apache.lucene.index.FieldInfo; |
| 11 | +import org.apache.lucene.index.FieldInfos; |
| 12 | +import org.apache.lucene.index.Fields; |
| 13 | +import org.apache.lucene.index.IndexCommit; |
| 14 | +import org.apache.lucene.index.IndexWriter; |
| 15 | +import org.apache.lucene.index.LeafMetaData; |
| 16 | +import org.apache.lucene.index.LeafReader; |
| 17 | +import org.apache.lucene.index.LeafReaderContext; |
| 18 | +import org.apache.lucene.index.NumericDocValues; |
| 19 | +import org.apache.lucene.index.PointValues; |
| 20 | +import org.apache.lucene.index.SortedDocValues; |
| 21 | +import org.apache.lucene.index.SortedNumericDocValues; |
| 22 | +import org.apache.lucene.index.SortedSetDocValues; |
| 23 | +import org.apache.lucene.index.StoredFieldVisitor; |
| 24 | +import org.apache.lucene.index.Terms; |
| 25 | +import org.apache.lucene.store.Directory; |
| 26 | +import org.apache.lucene.util.Bits; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | + |
| 33 | +/** |
| 34 | + * This special DirectoryReader is used to handle can_match requests against frozen indices. |
| 35 | + * It' caches all relevant point value data for every point value field ie. min/max packed values etc. |
| 36 | + * to hold enough information to rewrite a date range query and make a decisions if an index can match or not. |
| 37 | + * This allows frozen indices to be searched with wildcards in a very efficient way without opening a reader on them. |
| 38 | + */ |
| 39 | +final class RewriteCachingDirectoryReader extends DirectoryReader { |
| 40 | + |
| 41 | + RewriteCachingDirectoryReader(Directory directory, List<LeafReaderContext> segmentReaders) throws |
| 42 | + IOException { |
| 43 | + super(directory, wrap(segmentReaders)); |
| 44 | + } |
| 45 | + |
| 46 | + private static LeafReader[] wrap(List<LeafReaderContext> readers) throws IOException { |
| 47 | + LeafReader[] wrapped = new LeafReader[readers.size()]; |
| 48 | + int i = 0; |
| 49 | + for (LeafReaderContext ctx : readers) { |
| 50 | + LeafReader wrap = new RewriteCachingLeafReader(ctx.reader()); |
| 51 | + wrapped[i++] = wrap; |
| 52 | + } |
| 53 | + return wrapped; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected DirectoryReader doOpenIfChanged() { |
| 58 | + throw new UnsupportedOperationException(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + protected DirectoryReader doOpenIfChanged(IndexCommit commit) { |
| 63 | + throw new UnsupportedOperationException(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected DirectoryReader doOpenIfChanged(IndexWriter writer, boolean applyAllDeletes) { |
| 68 | + throw new UnsupportedOperationException(); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public long getVersion() { |
| 73 | + throw new UnsupportedOperationException(); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean isCurrent() { |
| 78 | + throw new UnsupportedOperationException(); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public IndexCommit getIndexCommit() { |
| 83 | + throw new UnsupportedOperationException(); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + protected void doClose() { |
| 88 | + throw new UnsupportedOperationException(); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public CacheHelper getReaderCacheHelper() { |
| 93 | + throw new UnsupportedOperationException(); |
| 94 | + } |
| 95 | + |
| 96 | + // except of a couple of selected methods everything else will |
| 97 | + // throw a UOE which causes a can_match phase to just move to the actual phase |
| 98 | + // later such that we never false exclude a shard if something else is used to rewrite. |
| 99 | + private static final class RewriteCachingLeafReader extends LeafReader { |
| 100 | + |
| 101 | + private final int maxDoc; |
| 102 | + private final int numDocs; |
| 103 | + private final Map<String, PointValues> pointValuesMap; |
| 104 | + private final FieldInfos fieldInfos; |
| 105 | + |
| 106 | + private RewriteCachingLeafReader(LeafReader original) throws IOException { |
| 107 | + this.maxDoc = original.maxDoc(); |
| 108 | + this.numDocs = original.numDocs(); |
| 109 | + fieldInfos = original.getFieldInfos(); |
| 110 | + Map<String, PointValues> valuesMap = new HashMap<>(); |
| 111 | + for (FieldInfo info : fieldInfos) { |
| 112 | + if (info.getPointIndexDimensionCount() != 0) { |
| 113 | + PointValues pointValues = original.getPointValues(info.name); |
| 114 | + if (pointValues != null) { // might not be in this reader |
| 115 | + byte[] minPackedValue = pointValues.getMinPackedValue(); |
| 116 | + byte[] maxPackedValue = pointValues.getMaxPackedValue(); |
| 117 | + int numDimensions = pointValues.getNumIndexDimensions(); |
| 118 | + int bytesPerDimension = pointValues.getBytesPerDimension(); |
| 119 | + int numDataDimensions = pointValues.getNumDataDimensions(); |
| 120 | + long size = pointValues.size(); |
| 121 | + int docCount = pointValues.getDocCount(); |
| 122 | + valuesMap.put(info.name, new PointValues() { |
| 123 | + @Override |
| 124 | + public void intersect(IntersectVisitor visitor) { |
| 125 | + throw new UnsupportedOperationException(); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public long estimatePointCount(IntersectVisitor visitor) { |
| 130 | + throw new UnsupportedOperationException(); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public byte[] getMinPackedValue() { |
| 135 | + return minPackedValue; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public byte[] getMaxPackedValue() { |
| 140 | + return maxPackedValue; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public int getNumDataDimensions() { |
| 145 | + return numDataDimensions; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public int getNumIndexDimensions() { |
| 150 | + return numDimensions; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public int getBytesPerDimension() { |
| 155 | + return bytesPerDimension; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public long size() { |
| 160 | + return size; |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public int getDocCount() { |
| 165 | + return docCount; |
| 166 | + } |
| 167 | + }); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + pointValuesMap = valuesMap; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public CacheHelper getCoreCacheHelper() { |
| 176 | + throw new UnsupportedOperationException(); |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public Terms terms(String field) { |
| 181 | + throw new UnsupportedOperationException(); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public NumericDocValues getNumericDocValues(String field) { |
| 186 | + throw new UnsupportedOperationException(); |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public BinaryDocValues getBinaryDocValues(String field) { |
| 191 | + throw new UnsupportedOperationException(); |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public SortedDocValues getSortedDocValues(String field) { |
| 196 | + throw new UnsupportedOperationException(); |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public SortedNumericDocValues getSortedNumericDocValues(String field) { |
| 201 | + throw new UnsupportedOperationException(); |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public SortedSetDocValues getSortedSetDocValues(String field) { |
| 206 | + throw new UnsupportedOperationException(); |
| 207 | + } |
| 208 | + |
| 209 | + @Override |
| 210 | + public NumericDocValues getNormValues(String field) { |
| 211 | + throw new UnsupportedOperationException(); |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + public FieldInfos getFieldInfos() { |
| 216 | + return fieldInfos; |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + public Bits getLiveDocs() { |
| 221 | + throw new UnsupportedOperationException(); |
| 222 | + } |
| 223 | + |
| 224 | + @Override |
| 225 | + public PointValues getPointValues(String field) { |
| 226 | + return pointValuesMap.get(field); |
| 227 | + } |
| 228 | + |
| 229 | + @Override |
| 230 | + public void checkIntegrity() { |
| 231 | + } |
| 232 | + |
| 233 | + @Override |
| 234 | + public LeafMetaData getMetaData() { |
| 235 | + throw new UnsupportedOperationException(); |
| 236 | + } |
| 237 | + |
| 238 | + @Override |
| 239 | + public Fields getTermVectors(int docID) { |
| 240 | + throw new UnsupportedOperationException(); |
| 241 | + } |
| 242 | + |
| 243 | + @Override |
| 244 | + public int numDocs() { |
| 245 | + return numDocs; |
| 246 | + } |
| 247 | + |
| 248 | + @Override |
| 249 | + public int maxDoc() { |
| 250 | + return maxDoc; |
| 251 | + } |
| 252 | + |
| 253 | + @Override |
| 254 | + public void document(int docID, StoredFieldVisitor visitor) { |
| 255 | + throw new UnsupportedOperationException(); |
| 256 | + } |
| 257 | + |
| 258 | + @Override |
| 259 | + protected void doClose() { |
| 260 | + } |
| 261 | + |
| 262 | + @Override |
| 263 | + public CacheHelper getReaderCacheHelper() { |
| 264 | + return null; |
| 265 | + } |
| 266 | + } |
| 267 | +} |
0 commit comments