Skip to content

Commit

Permalink
Make some Mappers Singletons (#77067)
Browse files Browse the repository at this point in the history
Just some obvious singletons we can use to save a little memory/cache
that I found during other experiments.
  • Loading branch information
original-brownbear committed Aug 31, 2021
1 parent 9e9a6d5 commit 37516da
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public class DocCountFieldMapper extends MetadataFieldMapper {
public static final String NAME = "_doc_count";
public static final String CONTENT_TYPE = "_doc_count";

public static final TypeParser PARSER = new FixedTypeParser(c -> new DocCountFieldMapper());
private static final DocCountFieldMapper INSTANCE = new DocCountFieldMapper();

public static final TypeParser PARSER = new FixedTypeParser(c -> INSTANCE);

public static final class DocCountFieldType extends MappedFieldType {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,10 @@ public final Map<String, NamedAnalyzer> indexAnalyzers() {

public static class MultiFields implements Iterable<FieldMapper>, ToXContent {

private static final MultiFields EMPTY = new MultiFields(Collections.emptyMap());

public static MultiFields empty() {
return new MultiFields(Collections.emptyMap());
return EMPTY;
}

public static class Builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,28 @@ public FieldNamesFieldMapper build() {
"field_names_enabled_parameter", ENABLED_DEPRECATION_MESSAGE);
}
}
FieldNamesFieldType fieldNamesFieldType = new FieldNamesFieldType(enabled.getValue().value());
return new FieldNamesFieldMapper(enabled.getValue(), indexVersionCreated, fieldNamesFieldType);
return new FieldNamesFieldMapper(enabled.getValue(), indexVersionCreated);
}
}

public static final TypeParser PARSER = new ConfigurableTypeParser(
c -> new FieldNamesFieldMapper(Defaults.ENABLED, c.indexVersionCreated(), new FieldNamesFieldType(Defaults.ENABLED.value())),
c -> new FieldNamesFieldMapper(Defaults.ENABLED, c.indexVersionCreated()),
c -> new Builder(c.indexVersionCreated())
);

public static final class FieldNamesFieldType extends TermBasedFieldType {

private static final FieldNamesFieldType ENABLED = new FieldNamesFieldType(true);

private static final FieldNamesFieldType DISABLED = new FieldNamesFieldType(false);

private final boolean enabled;

public FieldNamesFieldType(boolean enabled) {
public static FieldNamesFieldType get(boolean enabled) {
return enabled ? ENABLED : DISABLED;
}

private FieldNamesFieldType(boolean enabled) {
super(Defaults.NAME, true, false, false, TextSearchInfo.SIMPLE_MATCH_ONLY, Collections.emptyMap());
this.enabled = enabled;
}
Expand Down Expand Up @@ -145,8 +152,8 @@ public Query termQuery(Object value, SearchExecutionContext context) {
private final Explicit<Boolean> enabled;
private final Version indexVersionCreated;

private FieldNamesFieldMapper(Explicit<Boolean> enabled, Version indexVersionCreated, FieldNamesFieldType mappedFieldType) {
super(mappedFieldType);
private FieldNamesFieldMapper(Explicit<Boolean> enabled, Version indexVersionCreated) {
super(FieldNamesFieldType.get(enabled.value()));
this.enabled = enabled;
this.indexVersionCreated = indexVersionCreated;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public static class Defaults {
}
}

public static final TypeParser PARSER = new FixedTypeParser(c -> new IgnoredFieldMapper());
private static final IgnoredFieldMapper INSTANCE = new IgnoredFieldMapper();

public static final TypeParser PARSER = new FixedTypeParser(c -> INSTANCE);

public static final class IgnoredFieldType extends StringFieldType {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public class IndexFieldMapper extends MetadataFieldMapper {

public static final String CONTENT_TYPE = "_index";

public static final TypeParser PARSER = new FixedTypeParser(c -> new IndexFieldMapper());
private static final IndexFieldMapper INSTANCE = new IndexFieldMapper();

public static final TypeParser PARSER = new FixedTypeParser(c -> INSTANCE);

static final class IndexFieldType extends ConstantFieldType {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,8 @@ public boolean isDimension() {

private final IndexAnalyzers indexAnalyzers;

protected KeywordFieldMapper(String simpleName, FieldType fieldType, KeywordFieldType mappedFieldType,
MultiFields multiFields, CopyTo copyTo, Builder builder) {
private KeywordFieldMapper(String simpleName, FieldType fieldType, KeywordFieldType mappedFieldType,
MultiFields multiFields, CopyTo copyTo, Builder builder) {
super(simpleName, mappedFieldType, mappedFieldType.normalizer, multiFields, copyTo,
builder.script.get() != null, builder.onScriptError.getValue());
assert fieldType.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) <= 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
public class NestedPathFieldMapper extends MetadataFieldMapper {

public static final String NAME_PRE_V8 = "_type";

public static final String NAME = "_nested_path";

private static final NestedPathFieldMapper INSTANCE = new NestedPathFieldMapper(NAME);
private static final NestedPathFieldMapper INSTANCE_PRE_V8 = new NestedPathFieldMapper(NAME_PRE_V8);

public static String name(Version version) {
if (version.before(Version.V_8_0_0)) {
return NAME_PRE_V8;
Expand Down Expand Up @@ -53,12 +57,13 @@ public static class Defaults {
}
}

public static final TypeParser PARSER = new FixedTypeParser(c -> new NestedPathFieldMapper(c.indexVersionCreated()));
public static final TypeParser PARSER =
new FixedTypeParser(c -> c.indexVersionCreated().before(Version.V_8_0_0) ? INSTANCE_PRE_V8 : INSTANCE);

public static final class NestedPathFieldType extends StringFieldType {

private NestedPathFieldType(Version version) {
super(NestedPathFieldMapper.name(version), true, false, false, TextSearchInfo.SIMPLE_MATCH_ONLY, Collections.emptyMap());
private NestedPathFieldType(String name) {
super(name, true, false, false, TextSearchInfo.SIMPLE_MATCH_ONLY, Collections.emptyMap());
}

@Override
Expand All @@ -77,8 +82,8 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format)
}
}

private NestedPathFieldMapper(Version version) {
super(new NestedPathFieldType(version));
private NestedPathFieldMapper(String name) {
super(new NestedPathFieldType(name));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ protected List<Parameter<?>> getParameters() {

@Override
public RoutingFieldMapper build() {
return new RoutingFieldMapper(required.getValue());
return RoutingFieldMapper.get(required.getValue());
}
}

public static final TypeParser PARSER = new ConfigurableTypeParser(
c -> new RoutingFieldMapper(Defaults.REQUIRED),
c -> RoutingFieldMapper.get(Defaults.REQUIRED),
c -> new Builder()
);

Expand All @@ -90,6 +90,13 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format)

private final boolean required;

private static final RoutingFieldMapper REQUIRED = new RoutingFieldMapper(true);
private static final RoutingFieldMapper NOT_REQUIRED = new RoutingFieldMapper(false);

public static RoutingFieldMapper get(boolean required) {
return required ? REQUIRED : NOT_REQUIRED;
}

private RoutingFieldMapper(boolean required) {
super(RoutingFieldType.INSTANCE, Lucene.KEYWORD_ANALYZER);
this.required = required;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, S
}
}

public SeqNoFieldMapper() {
private SeqNoFieldMapper() {
super(SeqNoFieldType.INSTANCE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class SourceFieldMapper extends MetadataFieldMapper {
public static final String CONTENT_TYPE = "_source";
private final Function<Map<String, ?>, Map<String, Object>> filter;

private static final SourceFieldMapper DEFAULT = new SourceFieldMapper(Defaults.ENABLED, Strings.EMPTY_ARRAY, Strings.EMPTY_ARRAY);

public static class Defaults {
public static final String NAME = SourceFieldMapper.NAME;
public static final boolean ENABLED = true;
Expand Down Expand Up @@ -80,13 +82,16 @@ protected List<Parameter<?>> getParameters() {

@Override
public SourceFieldMapper build() {
if (enabled.getValue() == Defaults.ENABLED && includes.getValue().isEmpty() && excludes.getValue().isEmpty()) {
return DEFAULT;
}
return new SourceFieldMapper(enabled.getValue(),
includes.getValue().toArray(String[]::new),
excludes.getValue().toArray(String[]::new));
}
}

public static final TypeParser PARSER = new ConfigurableTypeParser(c -> new SourceFieldMapper(), c -> new Builder());
public static final TypeParser PARSER = new ConfigurableTypeParser(c -> DEFAULT, c -> new Builder());

static final class SourceFieldType extends MappedFieldType {

Expand Down Expand Up @@ -122,10 +127,6 @@ public Query termQuery(Object value, SearchExecutionContext context) {
private final String[] includes;
private final String[] excludes;

private SourceFieldMapper() {
this(Defaults.ENABLED, Strings.EMPTY_ARRAY, Strings.EMPTY_ARRAY);
}

private SourceFieldMapper(boolean enabled, String[] includes, String[] excludes) {
super(new SourceFieldType(enabled));
this.enabled = enabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
public class FieldNamesFieldTypeTests extends ESTestCase {

public void testTermQuery() {
FieldNamesFieldMapper.FieldNamesFieldType fieldNamesFieldType = new FieldNamesFieldMapper.FieldNamesFieldType(true);
FieldNamesFieldMapper.FieldNamesFieldType fieldNamesFieldType = FieldNamesFieldMapper.FieldNamesFieldType.get(true);
KeywordFieldMapper.KeywordFieldType fieldType = new KeywordFieldMapper.KeywordFieldType("field_name");

Settings settings = settings(Version.CURRENT).build();
Expand All @@ -42,7 +42,7 @@ public void testTermQuery() {
assertEquals(new TermQuery(new Term(FieldNamesFieldMapper.CONTENT_TYPE, "field_name")), termQuery);
assertWarnings("terms query on the _field_names field is deprecated and will be removed, use exists query instead");

FieldNamesFieldMapper.FieldNamesFieldType unsearchable = new FieldNamesFieldMapper.FieldNamesFieldType(false);
FieldNamesFieldMapper.FieldNamesFieldType unsearchable = FieldNamesFieldMapper.FieldNamesFieldType.get(false);
IllegalStateException e = expectThrows(IllegalStateException.class, () -> unsearchable.termQuery("field_name", null));
assertEquals("Cannot run [exists] queries if the [_field_names] field is disabled", e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ private void docValuesFieldExistsTestCase(
) throws IOException {
AggregationBuilder builder = new FiltersAggregationBuilder("test", new KeyedFilter("q1", exists));
// Exists queries convert to MatchNone if this isn't defined
FieldNamesFieldMapper.FieldNamesFieldType fnft = new FieldNamesFieldMapper.FieldNamesFieldType(true);
FieldNamesFieldMapper.FieldNamesFieldType fnft = FieldNamesFieldMapper.FieldNamesFieldType.get(true);
debugTestCase(builder, new MatchAllDocsQuery(), iw -> {
for (int i = 0; i < 10; i++) {
iw.addDocument(buildDocWithField.apply(i));
Expand All @@ -1127,7 +1127,7 @@ private void docValuesFieldExistsNoDataTestCase(MappedFieldType fieldType) throw
}
};
// Exists queries convert to MatchNone if this isn't defined
FieldNamesFieldMapper.FieldNamesFieldType fnft = new FieldNamesFieldMapper.FieldNamesFieldType(true);
FieldNamesFieldMapper.FieldNamesFieldType fnft = FieldNamesFieldMapper.FieldNamesFieldType.get(true);
withAggregator(builder, new MatchAllDocsQuery(), buildIndex, (searcher, aggregator) -> {
assertThat(aggregator, instanceOf(FilterByFilterAggregator.class));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ public void testOneBucketOptimized() throws IOException {
};
DateFieldMapper.DateFieldType ft = new DateFieldMapper.DateFieldType("f");
// Exists queries convert to MatchNone if this isn't defined
FieldNamesFieldMapper.FieldNamesFieldType fnft = new FieldNamesFieldMapper.FieldNamesFieldType(true);
FieldNamesFieldMapper.FieldNamesFieldType fnft = FieldNamesFieldMapper.FieldNamesFieldType.get(true);
debugTestCase(
builder,
new MatchAllDocsQuery(),
Expand Down

0 comments on commit 37516da

Please sign in to comment.