-
Notifications
You must be signed in to change notification settings - Fork 957
/
FieldInfo.java
659 lines (606 loc) · 20.8 KB
/
FieldInfo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.lucene.index;
import java.util.Map;
import java.util.Objects;
/**
* Access to the Field Info file that describes document fields and whether or not they are indexed.
* Each segment has a separate Field Info file. Objects of this class are thread-safe for multiple
* readers, but only one thread can be adding documents at a time, with no other reader or writer
* threads accessing this object.
*/
public final class FieldInfo {
/** Field's name */
public final String name;
/** Internal field number */
public final int number;
private DocValuesType docValuesType = DocValuesType.NONE;
// True if any document indexed term vectors
private boolean storeTermVector;
private boolean omitNorms; // omit norms associated with indexed fields
private final IndexOptions indexOptions;
private boolean storePayloads; // whether this field stores payloads together with term positions
private final Map<String, String> attributes;
private long dvGen;
/**
* If both of these are positive it means this field indexed points (see {@link
* org.apache.lucene.codecs.PointsFormat}).
*/
private int pointDimensionCount;
private int pointIndexDimensionCount;
private int pointNumBytes;
// if it is a positive value, it means this field indexes vectors
private final int vectorDimension;
private final VectorEncoding vectorEncoding;
private final VectorSimilarityFunction vectorSimilarityFunction;
// whether this field is used as the soft-deletes field
private final boolean softDeletesField;
/**
* Sole constructor.
*
* @lucene.experimental
*/
public FieldInfo(
String name,
int number,
boolean storeTermVector,
boolean omitNorms,
boolean storePayloads,
IndexOptions indexOptions,
DocValuesType docValues,
long dvGen,
Map<String, String> attributes,
int pointDimensionCount,
int pointIndexDimensionCount,
int pointNumBytes,
int vectorDimension,
VectorEncoding vectorEncoding,
VectorSimilarityFunction vectorSimilarityFunction,
boolean softDeletesField) {
this.name = Objects.requireNonNull(name);
this.number = number;
this.docValuesType =
Objects.requireNonNull(
docValues, "DocValuesType must not be null (field: \"" + name + "\")");
this.indexOptions =
Objects.requireNonNull(
indexOptions, "IndexOptions must not be null (field: \"" + name + "\")");
if (indexOptions != IndexOptions.NONE) {
this.storeTermVector = storeTermVector;
this.storePayloads = storePayloads;
this.omitNorms = omitNorms;
} else { // for non-indexed fields, leave defaults
this.storeTermVector = false;
this.storePayloads = false;
this.omitNorms = false;
}
this.dvGen = dvGen;
this.attributes = Objects.requireNonNull(attributes);
this.pointDimensionCount = pointDimensionCount;
this.pointIndexDimensionCount = pointIndexDimensionCount;
this.pointNumBytes = pointNumBytes;
this.vectorDimension = vectorDimension;
this.vectorEncoding = vectorEncoding;
this.vectorSimilarityFunction = vectorSimilarityFunction;
this.softDeletesField = softDeletesField;
this.checkConsistency();
}
/**
* Check correctness of the FieldInfo options
*
* @throws IllegalArgumentException if some options are incorrect
*/
public void checkConsistency() {
if (indexOptions == null) {
throw new IllegalArgumentException("IndexOptions must not be null (field: '" + name + "')");
}
if (indexOptions != IndexOptions.NONE) {
// Cannot store payloads unless positions are indexed:
if (indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0 && storePayloads) {
throw new IllegalArgumentException(
"indexed field '" + name + "' cannot have payloads without positions");
}
} else {
if (storeTermVector) {
throw new IllegalArgumentException(
"non-indexed field '" + name + "' cannot store term vectors");
}
if (storePayloads) {
throw new IllegalArgumentException(
"non-indexed field '" + name + "' cannot store payloads");
}
if (omitNorms) {
throw new IllegalArgumentException("non-indexed field '" + name + "' cannot omit norms");
}
}
if (docValuesType == null) {
throw new IllegalArgumentException("DocValuesType must not be null (field: '" + name + "')");
}
if (dvGen != -1 && docValuesType == DocValuesType.NONE) {
throw new IllegalArgumentException(
"field '"
+ name
+ "' cannot have a docvalues update generation without having docvalues");
}
if (pointDimensionCount < 0) {
throw new IllegalArgumentException(
"pointDimensionCount must be >= 0; got "
+ pointDimensionCount
+ " (field: '"
+ name
+ "')");
}
if (pointIndexDimensionCount < 0) {
throw new IllegalArgumentException(
"pointIndexDimensionCount must be >= 0; got "
+ pointIndexDimensionCount
+ " (field: '"
+ name
+ "')");
}
if (pointNumBytes < 0) {
throw new IllegalArgumentException(
"pointNumBytes must be >= 0; got " + pointNumBytes + " (field: '" + name + "')");
}
if (pointDimensionCount != 0 && pointNumBytes == 0) {
throw new IllegalArgumentException(
"pointNumBytes must be > 0 when pointDimensionCount="
+ pointDimensionCount
+ " (field: '"
+ name
+ "')");
}
if (pointIndexDimensionCount != 0 && pointDimensionCount == 0) {
throw new IllegalArgumentException(
"pointIndexDimensionCount must be 0 when pointDimensionCount=0"
+ " (field: '"
+ name
+ "')");
}
if (pointNumBytes != 0 && pointDimensionCount == 0) {
throw new IllegalArgumentException(
"pointDimensionCount must be > 0 when pointNumBytes="
+ pointNumBytes
+ " (field: '"
+ name
+ "')");
}
if (vectorSimilarityFunction == null) {
throw new IllegalArgumentException(
"Vector similarity function must not be null (field: '" + name + "')");
}
if (vectorDimension < 0) {
throw new IllegalArgumentException(
"vectorDimension must be >=0; got " + vectorDimension + " (field: '" + name + "')");
}
}
/**
* Verify that the provided FieldInfo has the same schema as this FieldInfo
*
* @param o – other FieldInfo whose schema is verified against this FieldInfo's schema
* @throws IllegalArgumentException if the field schemas are not the same
*/
void verifySameSchema(FieldInfo o, boolean strictlyConsistent) {
String fieldName = this.name;
verifySameIndexOptions(fieldName, this.indexOptions, o.getIndexOptions(), strictlyConsistent);
if (this.indexOptions != IndexOptions.NONE) {
verifySameOmitNorms(fieldName, this.omitNorms, o.omitNorms, strictlyConsistent);
verifySameStoreTermVectors(
fieldName, this.storeTermVector, o.storeTermVector, strictlyConsistent);
}
verifySameDocValuesType(fieldName, this.docValuesType, o.docValuesType, strictlyConsistent);
verifySamePointsOptions(
fieldName,
this.pointDimensionCount,
this.pointIndexDimensionCount,
this.pointNumBytes,
o.pointDimensionCount,
o.pointIndexDimensionCount,
o.pointNumBytes,
strictlyConsistent);
verifySameVectorOptions(
fieldName,
this.vectorDimension,
this.vectorEncoding,
this.vectorSimilarityFunction,
o.vectorDimension,
o.vectorEncoding,
o.vectorSimilarityFunction);
}
/**
* Verify that the provided index options are the same
*
* @throws IllegalArgumentException if they are not the same
*/
static void verifySameIndexOptions(
String fieldName,
IndexOptions indexOptions1,
IndexOptions indexOptions2,
boolean strictlyConsistent) {
if (strictlyConsistent == false
&& (indexOptions1 == IndexOptions.NONE || indexOptions2 == IndexOptions.NONE)) {
return;
}
if (indexOptions1 != indexOptions2) {
throw new IllegalArgumentException(
"cannot change field \""
+ fieldName
+ "\" from index options="
+ indexOptions1
+ " to inconsistent index options="
+ indexOptions2);
}
}
/**
* Verify that the provided docValues type are the same
*
* @throws IllegalArgumentException if they are not the same
*/
static void verifySameDocValuesType(
String fieldName,
DocValuesType docValuesType1,
DocValuesType docValuesType2,
boolean strictlyConsistent) {
if (strictlyConsistent == false
&& (docValuesType1 == DocValuesType.NONE || docValuesType2 == DocValuesType.NONE)) {
return;
}
if (docValuesType1 != docValuesType2) {
throw new IllegalArgumentException(
"cannot change field \""
+ fieldName
+ "\" from doc values type="
+ docValuesType1
+ " to inconsistent doc values type="
+ docValuesType2);
}
}
/**
* Verify that the provided store term vectors options are the same
*
* @throws IllegalArgumentException if they are not the same
*/
static void verifySameStoreTermVectors(
String fieldName,
boolean storeTermVector1,
boolean storeTermVector2,
boolean strictlyConsistent) {
if (strictlyConsistent && storeTermVector1 != storeTermVector2) {
throw new IllegalArgumentException(
"cannot change field \""
+ fieldName
+ "\" from storeTermVector="
+ storeTermVector1
+ " to inconsistent storeTermVector="
+ storeTermVector2);
}
}
/**
* Verify that the provided omitNorms are the same
*
* @throws IllegalArgumentException if they are not the same
*/
static void verifySameOmitNorms(
String fieldName, boolean omitNorms1, boolean omitNorms2, boolean strictlyConsistent) {
if (strictlyConsistent && omitNorms1 != omitNorms2) {
throw new IllegalArgumentException(
"cannot change field \""
+ fieldName
+ "\" from omitNorms="
+ omitNorms1
+ " to inconsistent omitNorms="
+ omitNorms2);
}
}
/**
* Verify that the provided points indexing options are the same
*
* @throws IllegalArgumentException if they are not the same
*/
static void verifySamePointsOptions(
String fieldName,
int pointDimensionCount1,
int indexDimensionCount1,
int numBytes1,
int pointDimensionCount2,
int indexDimensionCount2,
int numBytes2,
boolean strictlyConsistent) {
if (strictlyConsistent == false && (pointDimensionCount1 == 0 || pointDimensionCount2 == 0)) {
return;
}
if (pointDimensionCount1 != pointDimensionCount2
|| indexDimensionCount1 != indexDimensionCount2
|| numBytes1 != numBytes2) {
throw new IllegalArgumentException(
"cannot change field \""
+ fieldName
+ "\" from points dimensionCount="
+ pointDimensionCount1
+ ", indexDimensionCount="
+ indexDimensionCount1
+ ", numBytes="
+ numBytes1
+ " to inconsistent dimensionCount="
+ pointDimensionCount2
+ ", indexDimensionCount="
+ indexDimensionCount2
+ ", numBytes="
+ numBytes2);
}
}
/**
* Verify that the provided vector indexing options are the same
*
* @throws IllegalArgumentException if they are not the same
*/
static void verifySameVectorOptions(
String fieldName,
int vd1,
VectorEncoding ve1,
VectorSimilarityFunction vsf1,
int vd2,
VectorEncoding ve2,
VectorSimilarityFunction vsf2) {
if (vd1 != vd2 || vsf1 != vsf2 || ve1 != ve2) {
throw new IllegalArgumentException(
"cannot change field \""
+ fieldName
+ "\" from vector dimension="
+ vd1
+ ", vector encoding="
+ ve1
+ ", vector similarity function="
+ vsf1
+ " to inconsistent vector dimension="
+ vd2
+ ", vector encoding="
+ ve2
+ ", vector similarity function="
+ vsf2);
}
}
/**
* Record that this field is indexed with points, with the specified number of dimensions and
* bytes per dimension.
*/
public void setPointDimensions(int dimensionCount, int indexDimensionCount, int numBytes) {
if (dimensionCount <= 0) {
throw new IllegalArgumentException(
"point dimension count must be >= 0; got "
+ dimensionCount
+ " for field=\""
+ name
+ "\"");
}
if (indexDimensionCount > PointValues.MAX_INDEX_DIMENSIONS) {
throw new IllegalArgumentException(
"point index dimension count must be < PointValues.MAX_INDEX_DIMENSIONS (= "
+ PointValues.MAX_INDEX_DIMENSIONS
+ "); got "
+ indexDimensionCount
+ " for field=\""
+ name
+ "\"");
}
if (indexDimensionCount > dimensionCount) {
throw new IllegalArgumentException(
"point index dimension count must be <= point dimension count (= "
+ dimensionCount
+ "); got "
+ indexDimensionCount
+ " for field=\""
+ name
+ "\"");
}
if (numBytes <= 0) {
throw new IllegalArgumentException(
"point numBytes must be >= 0; got " + numBytes + " for field=\"" + name + "\"");
}
if (numBytes > PointValues.MAX_NUM_BYTES) {
throw new IllegalArgumentException(
"point numBytes must be <= PointValues.MAX_NUM_BYTES (= "
+ PointValues.MAX_NUM_BYTES
+ "); got "
+ numBytes
+ " for field=\""
+ name
+ "\"");
}
if (pointDimensionCount != 0 && pointDimensionCount != dimensionCount) {
throw new IllegalArgumentException(
"cannot change point dimension count from "
+ pointDimensionCount
+ " to "
+ dimensionCount
+ " for field=\""
+ name
+ "\"");
}
if (pointIndexDimensionCount != 0 && pointIndexDimensionCount != indexDimensionCount) {
throw new IllegalArgumentException(
"cannot change point index dimension count from "
+ pointIndexDimensionCount
+ " to "
+ indexDimensionCount
+ " for field=\""
+ name
+ "\"");
}
if (pointNumBytes != 0 && pointNumBytes != numBytes) {
throw new IllegalArgumentException(
"cannot change point numBytes from "
+ pointNumBytes
+ " to "
+ numBytes
+ " for field=\""
+ name
+ "\"");
}
pointDimensionCount = dimensionCount;
pointIndexDimensionCount = indexDimensionCount;
pointNumBytes = numBytes;
this.checkConsistency();
}
/** Return point data dimension count */
public int getPointDimensionCount() {
return pointDimensionCount;
}
/** Return point data dimension count */
public int getPointIndexDimensionCount() {
return pointIndexDimensionCount;
}
/** Return number of bytes per dimension */
public int getPointNumBytes() {
return pointNumBytes;
}
/** Returns the number of dimensions of the vector value */
public int getVectorDimension() {
return vectorDimension;
}
/** Returns the number of dimensions of the vector value */
public VectorEncoding getVectorEncoding() {
return vectorEncoding;
}
/** Returns {@link VectorSimilarityFunction} for the field */
public VectorSimilarityFunction getVectorSimilarityFunction() {
return vectorSimilarityFunction;
}
/** Record that this field is indexed with docvalues, with the specified type */
public void setDocValuesType(DocValuesType type) {
if (type == null) {
throw new NullPointerException("DocValuesType must not be null (field: \"" + name + "\")");
}
if (docValuesType != DocValuesType.NONE
&& type != DocValuesType.NONE
&& docValuesType != type) {
throw new IllegalArgumentException(
"cannot change DocValues type from "
+ docValuesType
+ " to "
+ type
+ " for field \""
+ name
+ "\"");
}
docValuesType = type;
this.checkConsistency();
}
/** Returns IndexOptions for the field, or IndexOptions.NONE if the field is not indexed */
public IndexOptions getIndexOptions() {
return indexOptions;
}
/**
* Returns name of this field
*
* @return name
*/
public String getName() {
return name;
}
/**
* Returns the field number
*
* @return field number
*/
public int getFieldNumber() {
return number;
}
/**
* Returns {@link DocValuesType} of the docValues; this is {@code DocValuesType.NONE} if the field
* has no docvalues.
*/
public DocValuesType getDocValuesType() {
return docValuesType;
}
/** Sets the docValues generation of this field. */
void setDocValuesGen(long dvGen) {
this.dvGen = dvGen;
this.checkConsistency();
}
/** Returns the docValues generation of this field, or -1 if no docValues updates exist for it. */
public long getDocValuesGen() {
return dvGen;
}
void setStoreTermVectors() {
storeTermVector = true;
this.checkConsistency();
}
void setStorePayloads() {
if (indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0) {
storePayloads = true;
}
this.checkConsistency();
}
/** Returns true if norms are explicitly omitted for this field */
public boolean omitsNorms() {
return omitNorms;
}
/** Omit norms for this field. */
public void setOmitsNorms() {
if (indexOptions == IndexOptions.NONE) {
throw new IllegalStateException("cannot omit norms: this field is not indexed");
}
omitNorms = true;
this.checkConsistency();
}
/** Returns true if this field actually has any norms. */
public boolean hasNorms() {
return indexOptions != IndexOptions.NONE && omitNorms == false;
}
/** Returns true if any payloads exist for this field. */
public boolean hasPayloads() {
return storePayloads;
}
/** Returns true if any term vectors exist for this field. */
public boolean hasVectors() {
return storeTermVector;
}
/** Returns whether any (numeric) vector values exist for this field */
public boolean hasVectorValues() {
return vectorDimension > 0;
}
/** Get a codec attribute value, or null if it does not exist */
public String getAttribute(String key) {
return attributes.get(key);
}
/**
* Puts a codec attribute value.
*
* <p>This is a key-value mapping for the field that the codec can use to store additional
* metadata, and will be available to the codec when reading the segment via {@link
* #getAttribute(String)}
*
* <p>If a value already exists for the key in the field, it will be replaced with the new value.
* If the value of the attributes for a same field is changed between the documents, the behaviour
* after merge is undefined.
*/
public String putAttribute(String key, String value) {
return attributes.put(key, value);
}
/** Returns internal codec attributes map. */
public Map<String, String> attributes() {
return attributes;
}
/**
* Returns true if this field is configured and used as the soft-deletes field. See {@link
* IndexWriterConfig#softDeletesField}
*/
public boolean isSoftDeletesField() {
return softDeletesField;
}
}