diff --git a/lucene/facet/src/java/org/apache/lucene/facet/sortedset/FacetCounter.java b/lucene/facet/src/java/org/apache/lucene/facet/sortedset/FacetCounter.java new file mode 100644 index 0000000..d521517 --- /dev/null +++ b/lucene/facet/src/java/org/apache/lucene/facet/sortedset/FacetCounter.java @@ -0,0 +1,16 @@ +package org.apache.lucene.facet.sortedset; + +/** + * Facet counting abstraction + */ +public interface FacetCounter { + /** + * increment term with ord by count + */ + void increment(int ord, int count); + + /** + * get accumulated count for a term ord + */ + int getCount(int ord); +} diff --git a/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesFacetCounts.java b/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesFacetCounts.java index a8fcfc6..5f718cb 100644 --- a/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesFacetCounts.java +++ b/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesFacetCounts.java @@ -62,7 +62,7 @@ public class SortedSetDocValuesFacetCounts extends Facets { final SortedSetDocValuesReaderState state; final SortedSetDocValues dv; final String field; - final int[] counts; + final FacetCounter counter; /** Sparse faceting: returns any dimension that had any * hits, topCount labels per dimension. */ @@ -70,11 +70,31 @@ public class SortedSetDocValuesFacetCounts extends Facets { throws IOException { this.state = state; this.field = state.getField(); - counts = new int[state.getSize()]; + counter = getFacetCounter(state.getSize()); dv = state.getDocValues(); //System.out.println("field=" + field); count(hits.getMatchingDocs()); } + + /** + * default impl for an array-backed {@link FacetCounter} + */ + public FacetCounter getFacetCounter(final int numValues) { + return new FacetCounter() { + int[] counts = new int[numValues]; + + @Override + public void increment(int ord, int count) { + counts[ord] += count; + } + + @Override + public int getCount(int ord) { + return counts[ord]; + } + + }; + } @Override public FacetResult getTopChildren(int topN, String dim, String... path) throws IOException { @@ -104,15 +124,16 @@ public class SortedSetDocValuesFacetCounts extends Facets { //System.out.println("getDim : " + ordRange.start + " - " + ordRange.end); for(int ord=ordRange.start; ord<=ordRange.end; ord++) { //System.out.println(" ord=" + ord + " count=" + counts[ord]); - if (counts[ord] > 0) { - dimCount += counts[ord]; + int count = counter.getCount(ord); + if (count > 0) { + dimCount += count; childCount++; - if (counts[ord] > bottomCount) { + if (count > bottomCount) { if (reuse == null) { reuse = new TopOrdAndIntQueue.OrdAndValue(); } reuse.ord = ord; - reuse.value = counts[ord]; + reuse.value = count; if (q == null) { // Lazy init, so we don't create this for the // sparse case unnecessarily @@ -202,8 +223,8 @@ public class SortedSetDocValuesFacetCounts extends Facets { segValues.setDocument(doc); int term = (int) segValues.nextOrd(); while (term != SortedSetDocValues.NO_MORE_ORDS) { - //System.out.println(" segOrd=" + segOrd + " ord=" + term + " globalOrd=" + ordinalMap.getGlobalOrd(segOrd, term)); - counts[(int) ordinalMap.getGlobalOrd(segOrd, term)]++; + //System.out.println(" segOrd=" + segOrd + " ord=" + term + " globalOrd=" + ordinalMap.getGlobalOrd(segOrd, term)); + counter.increment((int) ordinalMap.getGlobalOrd(segOrd, term), 1); term = (int) segValues.nextOrd(); } ++doc; @@ -230,8 +251,8 @@ public class SortedSetDocValuesFacetCounts extends Facets { for(int ord=0;ord