Skip to content

Commit

Permalink
OAK-8978: Cache facet results
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/jackrabbit/oak/trunk@1875940 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
tihom88 committed Mar 31, 2020
1 parent f3ea311 commit f7c1076
Show file tree
Hide file tree
Showing 2 changed files with 352 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Deque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -196,8 +197,12 @@ public class LucenePropertyIndex extends FulltextIndex {
private static boolean NON_LAZY = Boolean.getBoolean("oak.lucene.nonLazyIndex");
public final static String OLD_FACET_PROVIDER_CONFIG_NAME = "oak.lucene.oldFacetProvider";
private final static boolean OLD_FACET_PROVIDER = Boolean.getBoolean(OLD_FACET_PROVIDER_CONFIG_NAME);
public final static String CACHE_FACET_RESULTS_NAME = "oak.lucene.cacheFacetResults";
private final boolean CACHE_FACET_RESULTS =
Boolean.parseBoolean(System.getProperty(CACHE_FACET_RESULTS_NAME, "true"));

private static double MIN_COST = 2.1;
private static boolean FLAG_CACHE_FACET_RESULTS_CHANGE = true;

private static final Logger LOG = LoggerFactory
.getLogger(LucenePropertyIndex.class);
Expand Down Expand Up @@ -232,6 +237,15 @@ public LucenePropertyIndex(IndexTracker tracker, ScorerProviderFactory factory,
this.tracker = tracker;
this.scorerProviderFactory = factory;
this.augmentorFactory = augmentorFactory;
logConfigsOnce();
}

private void logConfigsOnce() {
if (FLAG_CACHE_FACET_RESULTS_CHANGE) {
LOG.info(OLD_FACET_PROVIDER_CONFIG_NAME + " = " + OLD_FACET_PROVIDER);
LOG.info(CACHE_FACET_RESULTS_NAME + " = " + CACHE_FACET_RESULTS);
FLAG_CACHE_FACET_RESULTS_CHANGE = false;
}
}

@Override
Expand Down Expand Up @@ -730,7 +744,7 @@ protected LuceneIndexNode acquireIndexNode(IndexPlan plan) {
protected String getType() {
return TYPE_LUCENE;
}

@Override
protected boolean filterReplacedIndexes() {
return tracker.getMountInfoProvider().hasNonDefaultMounts();
Expand Down Expand Up @@ -1574,11 +1588,12 @@ private static Iterator<FulltextResultRow> mergePropertyIndexResult(IndexPlan pl
return Iterators.concat(propIndex.iterator(), itr);
}

static class DelayedLuceneFacetProvider implements FacetProvider {
class DelayedLuceneFacetProvider implements FacetProvider {
private final LucenePropertyIndex index;
private final Query query;
private final IndexPlan plan;
private final SecureFacetConfiguration config;
private final Map<String, List<Facet>> cachedResults = new HashMap<>();

DelayedLuceneFacetProvider(LucenePropertyIndex index, Query query, IndexPlan plan, SecureFacetConfiguration config) {
this.index = index;
Expand All @@ -1589,6 +1604,22 @@ static class DelayedLuceneFacetProvider implements FacetProvider {

@Override
public List<Facet> getFacets(int numberOfFacets, String columnName) throws IOException {
if (!CACHE_FACET_RESULTS) {
LOG.trace(CACHE_FACET_RESULTS_NAME + " = " + CACHE_FACET_RESULTS + " getting uncached results for columnName = " + columnName);
return getFacetsUncached(numberOfFacets, columnName);
}
String cacheKey = columnName + "/" + numberOfFacets;
if (cachedResults.containsKey(cacheKey)) {
LOG.trace("columnName = " + columnName + " returning Facet Data from cache.");
return cachedResults.get(cacheKey);
}
LOG.trace("columnName = " + columnName + " facet Data not present in cache...");
List<Facet> result = getFacetsUncached(numberOfFacets, columnName);
cachedResults.put(cacheKey, result);
return result;
}

private List<Facet> getFacetsUncached(int numberOfFacets, String columnName) throws IOException {
LuceneIndexNode indexNode = index.acquireIndexNode(plan);
try {
IndexSearcher searcher = indexNode.getSearcher();
Expand All @@ -1600,7 +1631,7 @@ public List<Facet> getFacets(int numberOfFacets, String columnName) throws IOExc
if (topChildren != null) {
for (LabelAndValue lav : topChildren.labelValues) {
res.add(new Facet(
lav.label, lav.value.intValue()
lav.label, lav.value.intValue()
));
}
return res.build();
Expand Down
Loading

0 comments on commit f7c1076

Please sign in to comment.