This is an automated email from the ASF dual-hosted git repository.

nfsantos pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git


The following commit(s) were added to refs/heads/trunk by this push:
     new b00f7e3d80 OAK-11850 - Use MutableInt to compute facets to avoid 
boxing/unboxing between int and Integer (#2441)
b00f7e3d80 is described below

commit b00f7e3d800b5b7c201da68de5f944616218f002
Author: Nuno Santos <[email protected]>
AuthorDate: Mon Aug 11 20:35:28 2025 +0200

    OAK-11850 - Use MutableInt to compute facets to avoid boxing/unboxing 
between int and Integer (#2441)
---
 .../facets/ElasticSecureFacetAsyncProvider.java    | 31 ++++++++++-------
 .../ElasticStatisticalFacetAsyncProvider.java      | 40 +++++++++++++---------
 2 files changed, 42 insertions(+), 29 deletions(-)

diff --git 
a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/async/facets/ElasticSecureFacetAsyncProvider.java
 
b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/async/facets/ElasticSecureFacetAsyncProvider.java
index 79107050aa..ee0e359128 100644
--- 
a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/async/facets/ElasticSecureFacetAsyncProvider.java
+++ 
b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/async/facets/ElasticSecureFacetAsyncProvider.java
@@ -19,6 +19,7 @@ package 
org.apache.jackrabbit.oak.plugins.index.elastic.query.async.facets;
 import co.elastic.clients.elasticsearch.core.search.Hit;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.commons.lang3.mutable.MutableInt;
 import 
org.apache.jackrabbit.oak.plugins.index.elastic.query.ElasticRequestHandler;
 import 
org.apache.jackrabbit.oak.plugins.index.elastic.query.ElasticResponseHandler;
 import 
org.apache.jackrabbit.oak.plugins.index.elastic.query.async.ElasticResponseListener;
@@ -27,6 +28,7 @@ import 
org.apache.jackrabbit.oak.plugins.index.search.spi.query.FulltextIndex;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -45,7 +47,7 @@ class ElasticSecureFacetAsyncProvider implements 
ElasticFacetProvider, ElasticRe
     private static final Logger LOG = 
LoggerFactory.getLogger(ElasticSecureFacetAsyncProvider.class);
 
     private final Set<String> facetFields;
-    private final Map<String, Map<String, Integer>> accessibleFacetCounts = 
new ConcurrentHashMap<>();
+    private final Map<String, Map<String, MutableInt>> accessibleFacetCounts = 
new ConcurrentHashMap<>();
     private final ElasticResponseHandler elasticResponseHandler;
     private final Predicate<String> isAccessible;
     private final CountDownLatch latch = new CountDownLatch(1);
@@ -82,11 +84,18 @@ class ElasticSecureFacetAsyncProvider implements 
ElasticFacetProvider, ElasticRe
                 if (value != null) {
                     accessibleFacetCounts.compute(field, (column, facetValues) 
-> {
                         if (facetValues == null) {
-                            Map<String, Integer> values = new HashMap<>();
-                            values.put(value.asText(), 1);
+                            Map<String, MutableInt> values = new HashMap<>();
+                            values.put(value.asText(), new MutableInt(1));
                             return values;
                         } else {
-                            facetValues.merge(value.asText(), 1, Integer::sum);
+                            facetValues.compute(value.asText(), (k, v) -> {
+                                if (v == null) {
+                                    return new MutableInt(1);
+                                } else {
+                                    v.increment();
+                                    return v;
+                                }
+                            });
                             return facetValues;
                         }
                     });
@@ -99,19 +108,17 @@ class ElasticSecureFacetAsyncProvider implements 
ElasticFacetProvider, ElasticRe
     @Override
     public void endData() {
         // create Facet objects, order by count (desc) and then by label (asc)
+        Comparator<FulltextIndex.Facet> comparator = Comparator
+                .comparing(FulltextIndex.Facet::getCount).reversed()
+                .thenComparing(FulltextIndex.Facet::getLabel);
+        // create Facet objects, order by count (desc) and then by label (asc)
         facets = accessibleFacetCounts.entrySet()
                 .stream()
                 .collect(Collectors.toMap
                         (Map.Entry::getKey, x -> x.getValue().entrySet()
                                 .stream()
-                                .map(e -> new FulltextIndex.Facet(e.getKey(), 
e.getValue()))
-                                .sorted((f1, f2) -> {
-                                    int f1Count = f1.getCount();
-                                    int f2Count = f2.getCount();
-                                    if (f1Count == f2Count) {
-                                        return 
f1.getLabel().compareTo(f2.getLabel());
-                                    } else return f2Count - f1Count;
-                                })
+                                .map(e -> new FulltextIndex.Facet(e.getKey(), 
e.getValue().intValue()))
+                                .sorted(comparator)
                                 .collect(Collectors.toList())
                         )
                 );
diff --git 
a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/async/facets/ElasticStatisticalFacetAsyncProvider.java
 
b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/async/facets/ElasticStatisticalFacetAsyncProvider.java
index e6be3193ab..df9c671663 100644
--- 
a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/async/facets/ElasticStatisticalFacetAsyncProvider.java
+++ 
b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/async/facets/ElasticStatisticalFacetAsyncProvider.java
@@ -18,7 +18,7 @@ package 
org.apache.jackrabbit.oak.plugins.index.elastic.query.async.facets;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
-
+import org.apache.commons.lang3.mutable.MutableInt;
 import org.apache.jackrabbit.oak.plugins.index.elastic.ElasticConnection;
 import org.apache.jackrabbit.oak.plugins.index.elastic.ElasticIndexDefinition;
 import 
org.apache.jackrabbit.oak.plugins.index.elastic.query.ElasticRequestHandler;
@@ -39,6 +39,7 @@ import co.elastic.clients.elasticsearch.core.search.Hit;
 import co.elastic.clients.elasticsearch.core.search.SourceConfig;
 
 import java.util.ArrayList;
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -63,7 +64,7 @@ public class ElasticStatisticalFacetAsyncProvider implements 
ElasticFacetProvide
     private final Predicate<String> isAccessible;
     private final Set<String> facetFields;
     private final Map<String, List<FulltextIndex.Facet>> allFacets = new 
HashMap<>();
-    private final Map<String, Map<String, Integer>> accessibleFacetCounts = 
new ConcurrentHashMap<>();
+    private final Map<String, Map<String, MutableInt>> accessibleFacetCounts = 
new ConcurrentHashMap<>();
     private Map<String, List<FulltextIndex.Facet>> facets;
     private final SearchRequest searchRequest;
     private final CountDownLatch latch = new CountDownLatch(1);
@@ -145,11 +146,18 @@ public class ElasticStatisticalFacetAsyncProvider 
implements ElasticFacetProvide
                 if (value != null) {
                     accessibleFacetCounts.compute(field, (column, facetValues) 
-> {
                         if (facetValues == null) {
-                            Map<String, Integer> values = new HashMap<>();
-                            values.put(value.asText(), 1);
+                            Map<String, MutableInt> values = new HashMap<>();
+                            values.put(value.asText(), new MutableInt(1));
                             return values;
                         } else {
-                            facetValues.merge(value.asText(), 1, Integer::sum);
+                            facetValues.compute(value.asText(), (k, v) -> {
+                                if (v == null) {
+                                    return new MutableInt(1);
+                                } else {
+                                    v.increment();
+                                    return v;
+                                }
+                            });
                             return facetValues;
                         }
                     });
@@ -171,31 +179,29 @@ public class ElasticStatisticalFacetAsyncProvider 
implements ElasticFacetProvide
     private void computeStatisticalFacets() {
         for (String facetKey : allFacets.keySet()) {
             if (accessibleFacetCounts.containsKey(facetKey)) {
-                Map<String, Integer> accessibleFacet = 
accessibleFacetCounts.get(facetKey);
+                Map<String, MutableInt> accessibleFacet = 
accessibleFacetCounts.get(facetKey);
                 List<FulltextIndex.Facet> uncheckedFacet = 
allFacets.get(facetKey);
                 for (FulltextIndex.Facet facet : uncheckedFacet) {
-                    if (accessibleFacet.containsKey(facet.getLabel())) {
-                        double sampleProportion = (double) 
accessibleFacet.get(facet.getLabel()) / sampled;
+                    MutableInt currCount = 
accessibleFacet.get(facet.getLabel());
+                    if (currCount != null) {
+                        double sampleProportion = 
accessibleFacet.get(facet.getLabel()).doubleValue() / sampled;
                         // returned count is the minimum between the 
accessible count and the count computed from the sample
-                        accessibleFacet.put(facet.getLabel(), 
Math.min(facet.getCount(), (int) (sampleProportion * totalHits)));
+                        currCount.setValue(Math.min(facet.getCount(), (int) 
(sampleProportion * totalHits)));
                     }
                 }
             }
         }
         // create Facet objects, order by count (desc) and then by label (asc)
+        Comparator<FulltextIndex.Facet> comparator = Comparator
+                .comparing(FulltextIndex.Facet::getCount).reversed()
+                .thenComparing(FulltextIndex.Facet::getLabel);
         facets = accessibleFacetCounts.entrySet()
                 .stream()
                 .collect(Collectors.toMap
                         (Map.Entry::getKey, x -> x.getValue().entrySet()
                                 .stream()
-                                .map(e -> new FulltextIndex.Facet(e.getKey(), 
e.getValue()))
-                                .sorted((f1, f2) -> {
-                                    int f1Count = f1.getCount();
-                                    int f2Count = f2.getCount();
-                                    if (f1Count == f2Count) {
-                                        return 
f1.getLabel().compareTo(f2.getLabel());
-                                    } else return f2Count - f1Count;
-                                })
+                                .map(e -> new FulltextIndex.Facet(e.getKey(), 
e.getValue().intValue()))
+                                .sorted(comparator)
                                 .collect(Collectors.toList())
                         )
                 );

Reply via email to