From 559786574bc83acc28e9ededf8640ae285f6361f Mon Sep 17 00:00:00 2001 From: Matheus Lima Date: Fri, 31 Mar 2023 21:30:11 -0300 Subject: [PATCH] refactor(EarlyBirdIndexConfig): create strategy pattern to create configs --- .../factory/EarlybirdIndexConfigUtil.java | 19 ++++++++------- .../EarlybirdIndexConfigContext.java | 19 +++++++++++++++ .../EarlybirdIndexConfigStrategy.java | 21 +++++++++++++++++ .../RealtimeEarlybirdIndexConfigStrategy.java | 23 +++++++++++++++++++ 4 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 src/java/com/twitter/search/earlybird/factory/configStrategy/EarlybirdIndexConfigContext.java create mode 100644 src/java/com/twitter/search/earlybird/factory/configStrategy/EarlybirdIndexConfigStrategy.java create mode 100644 src/java/com/twitter/search/earlybird/factory/configStrategy/RealtimeEarlybirdIndexConfigStrategy.java diff --git a/src/java/com/twitter/search/earlybird/factory/EarlybirdIndexConfigUtil.java b/src/java/com/twitter/search/earlybird/factory/EarlybirdIndexConfigUtil.java index 7fcef3e0b..82ee8905e 100644 --- a/src/java/com/twitter/search/earlybird/factory/EarlybirdIndexConfigUtil.java +++ b/src/java/com/twitter/search/earlybird/factory/EarlybirdIndexConfigUtil.java @@ -3,11 +3,12 @@ package com.twitter.search.earlybird.factory; import com.twitter.decider.Decider; import com.twitter.search.common.schema.earlybird.EarlybirdCluster; import com.twitter.search.earlybird.EarlybirdIndexConfig; -import com.twitter.search.earlybird.RealtimeEarlybirdIndexConfig; -import com.twitter.search.earlybird.archive.ArchiveOnDiskEarlybirdIndexConfig; import com.twitter.search.earlybird.archive.ArchiveSearchPartitionManager; import com.twitter.search.earlybird.common.config.EarlybirdConfig; import com.twitter.search.earlybird.exception.CriticalExceptionHandler; +import com.twitter.search.earlybird.factory.configStrategy.ArchiveOnDiskEarlybirdIndexConfigStrategy; +import com.twitter.search.earlybird.factory.configStrategy.EarlybirdIndexConfigContext; +import com.twitter.search.earlybird.factory.configStrategy.RealtimeEarlybirdIndexConfigStrategy; import com.twitter.search.earlybird.partition.SearchIndexingMetricSet; public final class EarlybirdIndexConfigUtil { @@ -20,19 +21,17 @@ public final class EarlybirdIndexConfigUtil { public static EarlybirdIndexConfig createEarlybirdIndexConfig( Decider decider, SearchIndexingMetricSet searchIndexingMetricSet, CriticalExceptionHandler criticalExceptionHandler) { + EarlybirdIndexConfigContext context = new EarlybirdIndexConfigContext(); if (isArchiveSearch()) { - return new ArchiveOnDiskEarlybirdIndexConfig(decider, searchIndexingMetricSet, - criticalExceptionHandler); + context.setStrategy(new ArchiveOnDiskEarlybirdIndexConfigStrategy()); } else if (isProtectedSearch()) { - return new RealtimeEarlybirdIndexConfig( - EarlybirdCluster.PROTECTED, decider, searchIndexingMetricSet, criticalExceptionHandler); + context.setStrategy(new RealtimeEarlybirdIndexConfigStrategy(EarlybirdCluster.PROTECTED)); } else if (isRealtimeCG()) { - return new RealtimeEarlybirdIndexConfig( - EarlybirdCluster.REALTIME_CG, decider, searchIndexingMetricSet, criticalExceptionHandler); + context.setStrategy(new RealtimeEarlybirdIndexConfigStrategy(EarlybirdCluster.REALTIME_CG)); } else { - return new RealtimeEarlybirdIndexConfig( - EarlybirdCluster.REALTIME, decider, searchIndexingMetricSet, criticalExceptionHandler); + context.setStrategy(new RealtimeEarlybirdIndexConfigStrategy(EarlybirdCluster.REALTIME)); } + return context.createEarlybirdIndexConfig(decider, searchIndexingMetricSet, criticalExceptionHandler); } public static boolean isArchiveSearch() { diff --git a/src/java/com/twitter/search/earlybird/factory/configStrategy/EarlybirdIndexConfigContext.java b/src/java/com/twitter/search/earlybird/factory/configStrategy/EarlybirdIndexConfigContext.java new file mode 100644 index 000000000..9fe6c144b --- /dev/null +++ b/src/java/com/twitter/search/earlybird/factory/configStrategy/EarlybirdIndexConfigContext.java @@ -0,0 +1,19 @@ +package com.twitter.search.earlybird.factory.configStrategy; + +import com.twitter.search.earlybird.EarlybirdIndexConfig; +import com.twitter.search.earlybird.exception.CriticalExceptionHandler; +import com.twitter.search.earlybird.partition.SearchIndexingMetricSet; + +public class EarlybirdIndexConfigContext { + private EarlybirdIndexConfigStrategy strategy; + + public void setStrategy(EarlybirdIndexConfigStrategy strategy) { + this.strategy = strategy; + } + + public EarlybirdIndexConfig createEarlybirdIndexConfig( + Decider decider, SearchIndexingMetricSet searchIndexingMetricSet, + CriticalExceptionHandler criticalExceptionHandler) { + return strategy.createEarlybirdIndexConfig(decider, searchIndexingMetricSet, criticalExceptionHandler); + } +} \ No newline at end of file diff --git a/src/java/com/twitter/search/earlybird/factory/configStrategy/EarlybirdIndexConfigStrategy.java b/src/java/com/twitter/search/earlybird/factory/configStrategy/EarlybirdIndexConfigStrategy.java new file mode 100644 index 000000000..6b62718c4 --- /dev/null +++ b/src/java/com/twitter/search/earlybird/factory/configStrategy/EarlybirdIndexConfigStrategy.java @@ -0,0 +1,21 @@ +package com.twitter.search.earlybird.factory.configStrategy; + +import com.twitter.search.earlybird.EarlybirdIndexConfig; +import com.twitter.search.earlybird.archive.ArchiveOnDiskEarlybirdIndexConfig; +import com.twitter.search.earlybird.exception.CriticalExceptionHandler; +import com.twitter.search.earlybird.partition.SearchIndexingMetricSet; + +public interface EarlybirdIndexConfigStrategy { + EarlybirdIndexConfig createEarlybirdIndexConfig( + Decider decider, SearchIndexingMetricSet searchIndexingMetricSet, + CriticalExceptionHandler criticalExceptionHandler); +} + +public class ArchiveOnDiskEarlybirdIndexConfigStrategy implements EarlybirdIndexConfigStrategy { + @Override + public EarlybirdIndexConfig createEarlybirdIndexConfig( + Decider decider, SearchIndexingMetricSet searchIndexingMetricSet, + CriticalExceptionHandler criticalExceptionHandler) { + return new ArchiveOnDiskEarlybirdIndexConfig(decider, searchIndexingMetricSet, criticalExceptionHandler); + } +} \ No newline at end of file diff --git a/src/java/com/twitter/search/earlybird/factory/configStrategy/RealtimeEarlybirdIndexConfigStrategy.java b/src/java/com/twitter/search/earlybird/factory/configStrategy/RealtimeEarlybirdIndexConfigStrategy.java new file mode 100644 index 000000000..41cf532de --- /dev/null +++ b/src/java/com/twitter/search/earlybird/factory/configStrategy/RealtimeEarlybirdIndexConfigStrategy.java @@ -0,0 +1,23 @@ +package com.twitter.search.earlybird.factory.configStrategy; + +import com.twitter.search.common.schema.earlybird.EarlybirdCluster; +import com.twitter.search.earlybird.EarlybirdIndexConfig; +import com.twitter.search.earlybird.RealtimeEarlybirdIndexConfig; +import com.twitter.search.earlybird.exception.CriticalExceptionHandler; +import com.twitter.search.earlybird.partition.SearchIndexingMetricSet; + +public class RealtimeEarlybirdIndexConfigStrategy implements EarlybirdIndexConfigStrategy { + private EarlybirdCluster earlybirdCluster; + + public RealtimeEarlybirdIndexConfigStrategy(EarlybirdCluster earlybirdCluster) { + this.earlybirdCluster = earlybirdCluster; + } + + @Override + public EarlybirdIndexConfig createEarlybirdIndexConfig( + Decider decider, SearchIndexingMetricSet searchIndexingMetricSet, + CriticalExceptionHandler criticalExceptionHandler) { + return new RealtimeEarlybirdIndexConfig( + earlybirdCluster, decider, searchIndexingMetricSet, criticalExceptionHandler); + } +} \ No newline at end of file