|
48 | 48 | import java.nio.charset.StandardCharsets;
|
49 | 49 | import java.nio.file.Files;
|
50 | 50 | import java.nio.file.Path;
|
| 51 | +import java.util.Collection; |
51 | 52 | import java.util.Collections;
|
52 | 53 | import java.util.HashMap;
|
| 54 | +import java.util.HashSet; |
53 | 55 | import java.util.List;
|
54 | 56 | import java.util.Map;
|
55 | 57 | import java.util.Map.Entry;
|
@@ -94,10 +96,38 @@ class FieldWFSTCollection implements Closeable {
|
94 | 96 |
|
95 | 97 | private final ReadWriteLock lock = new ReentrantReadWriteLock();
|
96 | 98 |
|
97 |
| - FieldWFSTCollection(final Directory indexDir, final Path suggesterDir, final boolean allowMostPopular) { |
| 99 | + private Set<String> fields; |
| 100 | + |
| 101 | + FieldWFSTCollection( |
| 102 | + final Directory indexDir, |
| 103 | + final Path suggesterDir, |
| 104 | + final boolean allowMostPopular, |
| 105 | + final Set<String> fields |
| 106 | + ) throws IOException { |
98 | 107 | this.indexDir = indexDir;
|
99 | 108 | this.suggesterDir = suggesterDir;
|
100 | 109 | this.allowMostPopular = allowMostPopular;
|
| 110 | + |
| 111 | + initFields(fields); |
| 112 | + } |
| 113 | + |
| 114 | + private void initFields(final Set<String> fields) throws IOException { |
| 115 | + try (IndexReader indexReader = DirectoryReader.open(indexDir)) { |
| 116 | + Collection<String> indexedFields = MultiFields.getIndexedFields(indexReader); |
| 117 | + if (fields == null) { |
| 118 | + this.fields = new HashSet<>(indexedFields); |
| 119 | + } else if (!indexedFields.containsAll(fields)) { |
| 120 | + Set<String> copy = new HashSet<>(fields); |
| 121 | + copy.removeAll(indexedFields); |
| 122 | + logger.log(Level.WARNING, "Fields {0} will be ignored because they are not indexed", copy); |
| 123 | + |
| 124 | + copy = new HashSet<>(fields); |
| 125 | + copy.retainAll(indexedFields); |
| 126 | + this.fields = copy; |
| 127 | + } else { |
| 128 | + this.fields = new HashSet<>(fields); |
| 129 | + } |
| 130 | + } |
101 | 131 | }
|
102 | 132 |
|
103 | 133 | /**
|
@@ -147,14 +177,14 @@ private boolean hasStoredData() {
|
147 | 177 |
|
148 | 178 | private void loadStoredWFSTs() throws IOException {
|
149 | 179 | try (IndexReader indexReader = DirectoryReader.open(indexDir)) {
|
150 |
| - for (String field : MultiFields.getIndexedFields(indexReader)) { |
| 180 | + for (String field : fields) { |
151 | 181 |
|
152 | 182 | File WFSTfile = getWFSTFile(field);
|
153 | 183 | if (WFSTfile.exists()) {
|
154 | 184 | WFSTCompletionLookup WFST = loadStoredWFST(WFSTfile);
|
155 | 185 | lookups.put(field, WFST);
|
156 | 186 | } else {
|
157 |
| - logger.log(Level.INFO, "Missing FieldWFSTCollection file for {0} field in {1}, creating a new one", |
| 187 | + logger.log(Level.INFO, "Missing WFST file for {0} field in {1}, creating a new one", |
158 | 188 | new Object[] {field, suggesterDir});
|
159 | 189 |
|
160 | 190 | WFSTCompletionLookup lookup = build(indexReader, field);
|
@@ -207,7 +237,7 @@ public void rebuild() throws IOException {
|
207 | 237 |
|
208 | 238 | private void build() throws IOException {
|
209 | 239 | try (IndexReader indexReader = DirectoryReader.open(indexDir)) {
|
210 |
| - for (String field : MultiFields.getIndexedFields(indexReader)) { |
| 240 | + for (String field : fields) { |
211 | 241 | WFSTCompletionLookup lookup = build(indexReader, field);
|
212 | 242 | store(lookup, field);
|
213 | 243 |
|
@@ -246,45 +276,44 @@ private void createSuggesterDir() throws IOException {
|
246 | 276 |
|
247 | 277 | private void initSearchCountMap() throws IOException {
|
248 | 278 | searchCountMaps.clear();
|
249 |
| - try (IndexReader indexReader = DirectoryReader.open(indexDir)) { |
250 |
| - for (String field : MultiFields.getIndexedFields(indexReader)) { |
251 |
| - ChronicleMapConfiguration conf = ChronicleMapConfiguration.load(suggesterDir, field); |
252 |
| - if (conf == null) { // it was not yet initialized |
253 |
| - conf = new ChronicleMapConfiguration((int) lookups.get(field).getCount(), getAverageLength(field)); |
254 |
| - conf.save(suggesterDir, field); |
255 |
| - } |
256 | 279 |
|
257 |
| - File f = getChronicleMapFile(field); |
| 280 | + for (String field : fields) { |
| 281 | + ChronicleMapConfiguration conf = ChronicleMapConfiguration.load(suggesterDir, field); |
| 282 | + if (conf == null) { // it was not yet initialized |
| 283 | + conf = new ChronicleMapConfiguration((int) lookups.get(field).getCount(), getAverageLength(field)); |
| 284 | + conf.save(suggesterDir, field); |
| 285 | + } |
258 | 286 |
|
259 |
| - ChronicleMapAdapter m; |
260 |
| - try { |
261 |
| - m = new ChronicleMapAdapter(field, conf.getAverageKeySize(), conf.getEntries(), f); |
262 |
| - } catch (Exception e) { |
263 |
| - logger.log(Level.SEVERE, |
264 |
| - "Could not create ChronicleMap, most popular completion disabled, if you are using " |
265 |
| - + "JDK9+ make sure to specify: " |
266 |
| - + "--add-exports java.base/jdk.internal.ref=ALL-UNNAMED " |
267 |
| - + "--add-exports java.base/jdk.internal.misc=ALL-UNNAMED " |
268 |
| - + "--add-exports java.base/sun.nio.ch=ALL-UNNAMED", e); |
269 |
| - return; |
270 |
| - } |
| 287 | + File f = getChronicleMapFile(field); |
271 | 288 |
|
272 |
| - if (getCommitVersion() != getDataVersion()) { |
273 |
| - removeOldTerms(m, lookups.get(field)); |
| 289 | + ChronicleMapAdapter m; |
| 290 | + try { |
| 291 | + m = new ChronicleMapAdapter(field, conf.getAverageKeySize(), conf.getEntries(), f); |
| 292 | + } catch (Exception e) { |
| 293 | + logger.log(Level.SEVERE, |
| 294 | + "Could not create ChronicleMap, most popular completion disabled, if you are using " |
| 295 | + + "JDK9+ make sure to specify: " |
| 296 | + + "--add-exports java.base/jdk.internal.ref=ALL-UNNAMED " |
| 297 | + + "--add-exports java.base/jdk.internal.misc=ALL-UNNAMED " |
| 298 | + + "--add-exports java.base/sun.nio.ch=ALL-UNNAMED", e); |
| 299 | + return; |
| 300 | + } |
274 | 301 |
|
275 |
| - if (conf.getEntries() < lookups.get(field).getCount()) { |
276 |
| - int newEntriesCount = (int) lookups.get(field).getCount(); |
277 |
| - double newKeyAvgLength = getAverageLength(field); |
| 302 | + if (getCommitVersion() != getDataVersion()) { |
| 303 | + removeOldTerms(m, lookups.get(field)); |
278 | 304 |
|
279 |
| - conf.setEntries(newEntriesCount); |
280 |
| - conf.setAverageKeySize(newKeyAvgLength); |
281 |
| - conf.save(suggesterDir, field); |
| 305 | + if (conf.getEntries() < lookups.get(field).getCount()) { |
| 306 | + int newEntriesCount = (int) lookups.get(field).getCount(); |
| 307 | + double newKeyAvgLength = getAverageLength(field); |
| 308 | + |
| 309 | + conf.setEntries(newEntriesCount); |
| 310 | + conf.setAverageKeySize(newKeyAvgLength); |
| 311 | + conf.save(suggesterDir, field); |
282 | 312 |
|
283 |
| - m.resize(newEntriesCount, newKeyAvgLength); |
284 |
| - } |
| 313 | + m.resize(newEntriesCount, newKeyAvgLength); |
285 | 314 | }
|
286 |
| - searchCountMaps.put(field, m); |
287 | 315 | }
|
| 316 | + searchCountMaps.put(field, m); |
288 | 317 | }
|
289 | 318 | }
|
290 | 319 |
|
|
0 commit comments