Skip to content

Commit

Permalink
Make static method an instance method
Browse files Browse the repository at this point in the history
  • Loading branch information
andreidan committed Oct 24, 2024
1 parent de96932 commit f9b2913
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format)
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
}

String tierPreference = QueryRewriteContext.getTierPreference(context);
String tierPreference = context.getTierPreference();
return tierPreference == null ? ValueFetcher.EMPTY : ValueFetcher.singleton(tierPreference);
}

Expand All @@ -63,7 +63,7 @@ protected boolean matches(String pattern, boolean caseInsensitive, QueryRewriteC
pattern = Strings.toLowercaseAscii(pattern);
}

String tierPreference = QueryRewriteContext.getTierPreference(context);
String tierPreference = context.getTierPreference();
if (tierPreference == null) {
return false;
}
Expand All @@ -72,7 +72,7 @@ protected boolean matches(String pattern, boolean caseInsensitive, QueryRewriteC

@Override
public Query existsQuery(SearchExecutionContext context) {
String tierPreference = QueryRewriteContext.getTierPreference(context);
String tierPreference = context.getTierPreference();
if (tierPreference == null) {
return new MatchNoDocsQuery();
}
Expand Down Expand Up @@ -158,6 +158,12 @@ public CoordinatorRewriteContext convertToCoordinatorRewriteContext() {
return this;
}

@Override
public String getTierPreference() {
// dominant branch first (tier preference is configured)
return tier.isEmpty() == false ? tier : null;
}

/**
* We're holding on to the index tier in the context as otherwise we'd need
* to re-parse it from the index settings when evaluating the _tier field.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,13 +416,8 @@ public PointInTimeBuilder getPointInTimeBuilder() {
* present, then return null.
*/
@Nullable
public static String getTierPreference(QueryRewriteContext context) {
if (context instanceof CoordinatorRewriteContext) {
String tier = ((CoordinatorRewriteContext) context).tier();
// dominant branch first (tier preference is configured)
return tier.isEmpty() == false ? tier : null;
}
Settings settings = context.getIndexSettings().getSettings();
public String getTierPreference() {
Settings settings = getIndexSettings().getSettings();
String value = DataTier.TIER_PREFERENCE_SETTING.get(settings);

if (Strings.hasText(value) == false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ public class QueryRewriteContextTests extends ESTestCase {
public void testGetTierPreference() {
{
// cold->hot tier preference
IndexMetadata metadata = newIndexMeta(
"index",
IndexMetadata metadata = newIndexMeta("index",
Settings.builder()
.put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current())
.put(DataTier.TIER_PREFERENCE, "data_cold,data_warm,data_hot")
.build()
);
QueryRewriteContext context = new QueryRewriteContext(
parserConfig(),
QueryRewriteContext context = new QueryRewriteContext(parserConfig(),
null,
System::currentTimeMillis,
null,
Expand All @@ -54,17 +52,14 @@ public void testGetTierPreference() {
null
);

assertThat(QueryRewriteContext.getTierPreference(context), is("data_cold"));
assertThat(context.getTierPreference(), is("data_cold"));
}

{
// missing tier preference
IndexMetadata metadata = newIndexMeta(
"index",
Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()).build()
);
QueryRewriteContext context = new QueryRewriteContext(
parserConfig(),
IndexMetadata metadata =
newIndexMeta("index", Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()).build());
QueryRewriteContext context = new QueryRewriteContext(parserConfig(),
null,
System::currentTimeMillis,
null,
Expand All @@ -81,46 +76,42 @@ public void testGetTierPreference() {
null
);

assertThat(QueryRewriteContext.getTierPreference(context), is(nullValue()));
assertThat(context.getTierPreference(), is(nullValue()));
}

{
// coordinator rewrite context
IndexMetadata metadata = newIndexMeta(
"index",
IndexMetadata metadata = newIndexMeta("index",
Settings.builder()
.put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current())
.put(DataTier.TIER_PREFERENCE, "data_cold,data_warm,data_hot")
.build()
);
CoordinatorRewriteContext coordinatorRewriteContext = new CoordinatorRewriteContext(
parserConfig(),
CoordinatorRewriteContext coordinatorRewriteContext = new CoordinatorRewriteContext(parserConfig(),
null,
System::currentTimeMillis,
new DateFieldRangeInfo(null, null, new DateFieldMapper.DateFieldType(IndexMetadata.EVENT_INGESTED_FIELD_NAME), null),
"data_frozen"
);

assertThat(QueryRewriteContext.getTierPreference(coordinatorRewriteContext), is("data_frozen"));
assertThat(coordinatorRewriteContext.getTierPreference(), is("data_frozen"));
}
{
// coordinator rewrite context empty tier
IndexMetadata metadata = newIndexMeta(
"index",
IndexMetadata metadata = newIndexMeta("index",
Settings.builder()
.put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current())
.put(DataTier.TIER_PREFERENCE, "data_cold,data_warm,data_hot")
.build()
);
CoordinatorRewriteContext coordinatorRewriteContext = new CoordinatorRewriteContext(
parserConfig(),
CoordinatorRewriteContext coordinatorRewriteContext = new CoordinatorRewriteContext(parserConfig(),
null,
System::currentTimeMillis,
new DateFieldRangeInfo(null, null, new DateFieldMapper.DateFieldType(IndexMetadata.EVENT_INGESTED_FIELD_NAME), null),
""
);

assertThat(QueryRewriteContext.getTierPreference(coordinatorRewriteContext), is(nullValue()));
assertThat(coordinatorRewriteContext.getTierPreference(), is(nullValue()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected boolean matches(String pattern, boolean caseInsensitive, QueryRewriteC
pattern = Strings.toLowercaseAscii(pattern);
}

String tierPreference = QueryRewriteContext.getTierPreference(context);
String tierPreference = context.getTierPreference();
if (tierPreference == null) {
return false;
}
Expand All @@ -62,7 +62,7 @@ protected boolean matches(String pattern, boolean caseInsensitive, QueryRewriteC

@Override
public Query existsQuery(SearchExecutionContext context) {
String tierPreference = QueryRewriteContext.getTierPreference(context);
String tierPreference = context.getTierPreference();
if (tierPreference == null) {
return new MatchNoDocsQuery();
}
Expand All @@ -75,7 +75,7 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format)
throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats.");
}

String tierPreference = QueryRewriteContext.getTierPreference(context);
String tierPreference = context.getTierPreference();
return tierPreference == null ? ValueFetcher.EMPTY : ValueFetcher.singleton(tierPreference);
}
}
Expand Down

0 comments on commit f9b2913

Please sign in to comment.