Skip to content

Commit

Permalink
Replaced namespace string with pattern.
Browse files Browse the repository at this point in the history
  • Loading branch information
an1310 committed Jan 23, 2024
1 parent 1a74151 commit 1456431
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
*/
package org.eclipse.ditto.internal.models.signalenrichment;

import org.apache.pekko.japi.Pair;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.internal.utils.cache.config.CacheConfig;
import org.eclipse.ditto.internal.utils.pekko.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.pekko.logging.ThreadSafeDittoLogger;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.things.model.Thing;
Expand All @@ -25,6 +28,7 @@
import java.util.Map;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.regex.Pattern;

import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;

Expand All @@ -34,18 +38,18 @@
*/
public final class SearchIndexingSignalEnrichmentFacade extends DittoCachingSignalEnrichmentFacade {

private final Map<String, JsonFieldSelector> selectedIndexes;
private final List<Pair<Pattern, JsonFieldSelector>> selectedIndexes;

private SearchIndexingSignalEnrichmentFacade(
final Map<String, JsonFieldSelector> selectedIndexes,
final List<Pair<Pattern, JsonFieldSelector>> selectedIndexes,
final SignalEnrichmentFacade cacheLoaderFacade,
final CacheConfig cacheConfig,
final Executor cacheLoaderExecutor,
final String cacheNamePrefix) {

super(cacheLoaderFacade, cacheConfig, cacheLoaderExecutor, cacheNamePrefix);

this.selectedIndexes = Collections.unmodifiableMap(selectedIndexes);
this.selectedIndexes = Collections.unmodifiableList(selectedIndexes);
}

/**
Expand All @@ -59,7 +63,7 @@ private SearchIndexingSignalEnrichmentFacade(
* @throws NullPointerException if any argument is null.
*/
public static SearchIndexingSignalEnrichmentFacade newInstance(
final Map<String, JsonFieldSelector> selectedIndexes,
final List<Pair<Pattern, JsonFieldSelector>> selectedIndexes,
final SignalEnrichmentFacade cacheLoaderFacade,
final CacheConfig cacheConfig,
final Executor cacheLoaderExecutor,
Expand All @@ -75,6 +79,15 @@ public static SearchIndexingSignalEnrichmentFacade newInstance(

@Override
protected JsonFieldSelector determineSelector(String namespace) {
return selectedIndexes.get(namespace);

// We iterate through the list and return the first JsonFieldSelector that matches the namespace pattern.
for (final Pair<Pattern, JsonFieldSelector> pair : selectedIndexes) {

if (pair.first().matcher(namespace).matches()) {
return pair.second();
}
}

return super.determineSelector(namespace);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import com.typesafe.config.ConfigFactory;
import org.apache.pekko.actor.ActorSelection;
import org.apache.pekko.japi.Pair;
import org.apache.pekko.testkit.javadsl.TestKit;
import org.assertj.core.api.JUnitSoftAssertions;
import org.eclipse.ditto.base.model.auth.AuthorizationContext;
Expand All @@ -40,8 +41,10 @@

import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletionStage;
import java.util.regex.Pattern;

/**
* Unit tests for {@link SearchIndexingSignalEnrichmentFacade}.
Expand Down Expand Up @@ -100,7 +103,7 @@ protected SignalEnrichmentFacade createSignalEnrichmentFacadeUnderTest(final Tes
final ByRoundTripSignalEnrichmentFacade cacheLoaderFacade =
ByRoundTripSignalEnrichmentFacade.of(commandHandler, Duration.ofSeconds(10L));
return SearchIndexingSignalEnrichmentFacade.newInstance(
Map.of("org.eclipse.test", SELECTED_INDEXES),
List.of(Pair.create(Pattern.compile("org.eclipse.test"), SELECTED_INDEXES)),
cacheLoaderFacade,
cacheConfig,
kit.getSystem().getDispatcher(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

import org.eclipse.ditto.base.model.common.LikeHelper;
import org.eclipse.ditto.internal.utils.config.ConfigWithFallback;
import org.eclipse.ditto.internal.utils.config.DittoConfigError;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
import org.eclipse.ditto.internal.utils.config.KnownConfigValue;

import javax.annotation.concurrent.Immutable;
import java.time.Duration;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;

/**
* Provides configuration settings of the namespace-scoped search indexes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

import java.util.*;
import java.util.concurrent.Executor;
import java.util.regex.Pattern;

import org.apache.pekko.japi.Pair;
import org.eclipse.ditto.base.model.common.LikeHelper;
import org.eclipse.ditto.internal.models.signalenrichment.CachingSignalEnrichmentFacade;
import org.eclipse.ditto.internal.models.signalenrichment.SearchIndexingSignalEnrichmentFacade;
import org.eclipse.ditto.internal.models.signalenrichment.SignalEnrichmentFacade;
Expand Down Expand Up @@ -65,26 +68,30 @@ public CachingSignalEnrichmentFacade getSignalEnrichmentFacade(
DittoSearchConfig.of(DefaultScopedConfig.dittoScoped(actorSystem.settings().config()));

// Build a map of field selectors for the enrichment facade to use to quickly look up by Thing namespace.
final Map<String, JsonFieldSelector> namespaceToFieldSelector = new HashMap<>();
final List<Pair<Pattern, JsonFieldSelector>> namespaceAndFieldSelector = new ArrayList<>();

for (final NamespaceSearchIndexConfig namespaceConfig : searchConfig.getNamespaceSearchIncludeFields()) {

if (!namespaceConfig.getSearchIncludeFields().isEmpty()) {

// Ensure the list has the required fields needed for the search to work.
// Ensure the constructed JsonFieldSelector has the required fields needed for the search to work.
final Set<String> set = new HashSet<>(namespaceConfig.getSearchIncludeFields());
set.addAll(REQUIRED_INDEXED_FIELDS.stream().map(JsonFieldDefinition::getPointer).map(JsonPointer::toString).toList());

final List<String> searchIncludeFields = new ArrayList<>(set);

JsonFieldSelector indexedFields = JsonFactory.newFieldSelector(searchIncludeFields, JsonParseOptions.newBuilder().build());

namespaceToFieldSelector.put(namespaceConfig.getNamespace(), indexedFields);
// Build a Pattern from the namespace value.
final Pattern namespacePattern = Pattern.compile(
Objects.requireNonNull(LikeHelper.convertToRegexSyntax(namespaceConfig.getNamespace())));

namespaceAndFieldSelector.add(Pair.create(namespacePattern, indexedFields));
}
}

return SearchIndexingSignalEnrichmentFacade.newInstance(
namespaceToFieldSelector,
namespaceAndFieldSelector,
cacheLoaderFacade,
cacheConfig,
cacheLoaderExecutor,
Expand Down

0 comments on commit 1456431

Please sign in to comment.