From 8109d13733011dd35db619d7b2ae415586cb2a56 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Mon, 4 Feb 2013 12:46:30 +0100 Subject: [PATCH] Use CacheRecycler when resolving parent docs in TopChildrenQuery. --- .../elasticsearch/common/CacheRecycler.java | 29 +++++++++++++++++++ .../index/search/child/TopChildrenQuery.java | 12 +++++--- .../search/child/ChildSearchBenchmark.java | 21 ++++++++++++++ 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/elasticsearch/common/CacheRecycler.java b/src/main/java/org/elasticsearch/common/CacheRecycler.java index a1deef2c340..ba97cc0fcce 100644 --- a/src/main/java/org/elasticsearch/common/CacheRecycler.java +++ b/src/main/java/org/elasticsearch/common/CacheRecycler.java @@ -44,6 +44,7 @@ public class CacheRecycler { shortIntHashMap.clear(); longIntHashMap.clear(); objectIntHashMap.clear(); + intObjectHashMap.clear(); objectFloatHashMap.clear(); objectArray.clear(); intArray.clear(); @@ -396,6 +397,34 @@ public class CacheRecycler { ref.add(map); } + // ------ TIntObjectHashMap ----- + + private static SoftWrapper> intObjectHashMap = new SoftWrapper>(); + + + @SuppressWarnings({"unchecked"}) + public static TIntObjectHashMap popIntObjectMap() { + Queue ref = intObjectHashMap.get(); + if (ref == null) { + return new TIntObjectHashMap(); + } + TIntObjectHashMap map = ref.poll(); + if (map == null) { + return new TIntObjectHashMap(); + } + return map; + } + + public static void pushIntObjectMap(TIntObjectHashMap map) { + Queue ref = intObjectHashMap.get(); + if (ref == null) { + ref = ConcurrentCollections.newQueue(); + intObjectHashMap.set(ref); + } + map.clear(); + ref.add(map); + } + // ------ TObjectFloatHashMap ----- private static SoftWrapper> objectFloatHashMap = new SoftWrapper>(); diff --git a/src/main/java/org/elasticsearch/index/search/child/TopChildrenQuery.java b/src/main/java/org/elasticsearch/index/search/child/TopChildrenQuery.java index 5445d9008d9..74390668d94 100644 --- a/src/main/java/org/elasticsearch/index/search/child/TopChildrenQuery.java +++ b/src/main/java/org/elasticsearch/index/search/child/TopChildrenQuery.java @@ -32,7 +32,10 @@ import org.elasticsearch.common.trove.ExtTHashMap; import org.elasticsearch.search.internal.SearchContext; import java.io.IOException; -import java.util.*; +import java.util.Arrays; +import java.util.Comparator; +import java.util.Map; +import java.util.Set; /** * A query that evaluates the top matching child documents (based on the score) in order to determine what @@ -124,7 +127,7 @@ public class TopChildrenQuery extends Query implements SearchContext.Rewrite { int resolveParentDocuments(TopDocs topDocs, SearchContext context) { int parentHitsResolved = 0; - Map> parentDocsPerReader = new HashMap>(); + ExtTHashMap> parentDocsPerReader = CacheRecycler.popHashMap(); for (ScoreDoc scoreDoc : topDocs.scoreDocs) { int readerIndex = ReaderUtil.subIndex(scoreDoc.doc, context.searcher().getIndexReader().leaves()); AtomicReaderContext subContext = context.searcher().getIndexReader().leaves().get(readerIndex); @@ -146,7 +149,7 @@ public class TopChildrenQuery extends Query implements SearchContext.Rewrite { TIntObjectHashMap readerParentDocs = parentDocsPerReader.get(indexReader.getCoreCacheKey()); if (readerParentDocs == null) { - readerParentDocs = new TIntObjectHashMap(); + readerParentDocs = CacheRecycler.popIntObjectMap(); parentDocsPerReader.put(indexReader.getCoreCacheKey(), readerParentDocs); } @@ -174,8 +177,9 @@ public class TopChildrenQuery extends Query implements SearchContext.Rewrite { ParentDoc[] values = entry.getValue().values(new ParentDoc[entry.getValue().size()]); Arrays.sort(values, PARENT_DOC_COMP); parentDocs.put(entry.getKey(), values); + CacheRecycler.pushIntObjectMap(entry.getValue()); } - + CacheRecycler.pushHashMap(parentDocsPerReader); return parentHitsResolved; } diff --git a/src/test/java/org/elasticsearch/benchmark/search/child/ChildSearchBenchmark.java b/src/test/java/org/elasticsearch/benchmark/search/child/ChildSearchBenchmark.java index 0427dcf9c6e..b128ac058c1 100644 --- a/src/test/java/org/elasticsearch/benchmark/search/child/ChildSearchBenchmark.java +++ b/src/test/java/org/elasticsearch/benchmark/search/child/ChildSearchBenchmark.java @@ -279,6 +279,27 @@ public class ChildSearchBenchmark { } System.out.println("--> top_children Query Avg: " + (totalQueryTime / QUERY_COUNT) + "ms"); + System.out.println("--> Running top_children query, with match_all as child query"); + // run parent child score query + for (int j = 0; j < QUERY_WARMUP; j++) { + SearchResponse searchResponse = client.prepareSearch(indexName).setQuery(topChildrenQuery("child", matchAllQuery())).execute().actionGet(); + // we expect to have mismatch on hits here +// if (searchResponse.hits().totalHits() != COUNT) { +// System.err.println("mismatch on hits"); +// } + } + + totalQueryTime = 0; + for (int j = 0; j < QUERY_COUNT; j++) { + SearchResponse searchResponse = client.prepareSearch(indexName).setQuery(topChildrenQuery("child", matchAllQuery())).execute().actionGet(); + // we expect to have mismatch on hits here +// if (searchResponse.hits().totalHits() != COUNT) { +// System.err.println("mismatch on hits"); +// } + totalQueryTime += searchResponse.tookInMillis(); + } + System.out.println("--> top_children, with match_all Query Avg: " + (totalQueryTime / QUERY_COUNT) + "ms"); + statsResponse = client.admin().cluster().prepareNodesStats() .setJvm(true).setIndices(true).execute().actionGet();