|
| 1 | +package com.linkedin.metadata.search.client; |
| 2 | + |
| 3 | +import static com.linkedin.metadata.search.client.CachingEntitySearchService.*; |
| 4 | + |
| 5 | +import com.linkedin.common.urn.Urn; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.HashSet; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Set; |
| 10 | +import org.javatuples.Octet; |
| 11 | +import org.javatuples.Septet; |
| 12 | + |
| 13 | +class UrnCacheKeyMatcher implements CacheKeyMatcher { |
| 14 | + private final List<Urn> urns; |
| 15 | + private final Set<String> entityTypes; |
| 16 | + |
| 17 | + final List<String> SUPPORTED_CACHE_NAMES = |
| 18 | + Arrays.asList( |
| 19 | + ENTITY_SEARCH_SERVICE_SEARCH_CACHE_NAME, ENTITY_SEARCH_SERVICE_SCROLL_CACHE_NAME); |
| 20 | + |
| 21 | + UrnCacheKeyMatcher(List<Urn> urns) { |
| 22 | + this.urns = urns; |
| 23 | + this.entityTypes = new HashSet<>(); |
| 24 | + urns.forEach( |
| 25 | + urn -> { |
| 26 | + this.entityTypes.add(urn.getEntityType()); |
| 27 | + }); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public boolean supportsCache(String cacheName) { |
| 32 | + return SUPPORTED_CACHE_NAMES.contains(cacheName); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public boolean match(String cacheName, Object key) { |
| 37 | + switch (cacheName) { |
| 38 | + case ENTITY_SEARCH_SERVICE_SEARCH_CACHE_NAME: |
| 39 | + return matchSearchServiceCacheKey(key); |
| 40 | + case ENTITY_SEARCH_SERVICE_SCROLL_CACHE_NAME: |
| 41 | + return matchSearchServiceScrollCacheKey(key); |
| 42 | + } |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + private boolean matchSearchServiceScrollCacheKey(Object key) { |
| 47 | + Octet<?, List<String>, String, String, ?, ?, List<String>, ?> cacheKey = |
| 48 | + (Octet<?, List<String>, String, String, ?, ?, List<String>, ?>) key; |
| 49 | + // For reference - cache key contents |
| 50 | + // @Nonnull OperationContext opContext, |
| 51 | + // @Nonnull List<String> entities, |
| 52 | + // @Nonnull String query, |
| 53 | + // @Nullable Filter filters, |
| 54 | + // List<SortCriterion> sortCriteria, |
| 55 | + // @Nullable String scrollId, |
| 56 | + // @Nonnull List<String> facets |
| 57 | + // int size, |
| 58 | + List<String> entitiesInCacheKey = (List<String>) cacheKey.getValue(1); |
| 59 | + String filter = (String) cacheKey.getValue(3); |
| 60 | + String query = (String) cacheKey.getValue(2); |
| 61 | + List<String> facets = (List<String>) cacheKey.getValue(6); |
| 62 | + |
| 63 | + if (filter == null) { |
| 64 | + filter = ""; |
| 65 | + } |
| 66 | + filter += " " + String.join(" ", facets); |
| 67 | + // Facets may contain urns. Since the check for urns in filters is similar, can append it to the |
| 68 | + // filter. |
| 69 | + return isKeyImpactedByEntity(entitiesInCacheKey, query, filter); |
| 70 | + } |
| 71 | + |
| 72 | + private boolean matchSearchServiceCacheKey(Object key) { |
| 73 | + Septet<?, List<String>, ?, String, ?, ?, ?> cacheKey = |
| 74 | + (Septet<?, List<String>, ?, String, ?, ?, ?>) key; |
| 75 | + // For reference |
| 76 | + // @Nonnull OperationContext opContext, |
| 77 | + // @Nonnull List<String> entityNames, |
| 78 | + // @Nonnull String query, |
| 79 | + // @Nullable Filter filters, |
| 80 | + // List<SortCriterion> sortCriteria, |
| 81 | + // @Nonnull List<String> facets |
| 82 | + // querySize |
| 83 | + |
| 84 | + List<String> entitiesInCacheKey = (List<String>) cacheKey.getValue(1); |
| 85 | + String filter = (String) cacheKey.getValue(3); |
| 86 | + String query = (String) cacheKey.getValue(2); |
| 87 | + List<String> facets = (List<String>) cacheKey.getValue(5); |
| 88 | + |
| 89 | + // Facets may contain urns. Since the check for urns in filters is similar, can append it to the |
| 90 | + // filter. |
| 91 | + if (filter == null) { |
| 92 | + filter = ""; |
| 93 | + } |
| 94 | + filter += " " + String.join(" ", facets); |
| 95 | + |
| 96 | + return isKeyImpactedByEntity(entitiesInCacheKey, query, filter); |
| 97 | + } |
| 98 | + |
| 99 | + boolean isKeyImpactedByEntity(List<String> entitiesInCacheKey, String query, String filter) { |
| 100 | + boolean entityMatch = entitiesInCacheKey.stream().anyMatch(entityTypes::contains); |
| 101 | + if (!entityMatch) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + // Ignoring query for now. A query could make this cache entry more targeted, but till there is |
| 106 | + // a quick way to evaluate if the entities that were updated are affected by this query, |
| 107 | + // ignoring it may mean some cache entries are invalidated even if they may not be a match, |
| 108 | + // and an uncached query result will still be fetched. |
| 109 | + |
| 110 | + boolean containsUrn = filter.contains("urn:li"); |
| 111 | + if (!containsUrn) { |
| 112 | + return true; // Entity match, has a filter, but not on urn. this may be a suboptimal |
| 113 | + } |
| 114 | + |
| 115 | + return urns.stream() |
| 116 | + .anyMatch( |
| 117 | + urn -> |
| 118 | + filter.contains( |
| 119 | + urn.toString())); // If we found an exact URN match, this is to be evicted. If |
| 120 | + |
| 121 | + // this entry was for some other urn, do not evict. |
| 122 | + } |
| 123 | +} |
0 commit comments