11package org .opendatasoft .elasticsearch .search .aggregations .bucket ;
22
3- import org .apache .lucene .index .LeafReaderContext ;
43import org .apache .lucene .index .SortedNumericDocValues ;
54import org .apache .lucene .util .BytesRef ;
65import org .elasticsearch .ElasticsearchException ;
7- import org .elasticsearch .common .Rounding ;
86import org .elasticsearch .common .io .stream .StreamInput ;
97import org .elasticsearch .common .io .stream .StreamOutput ;
108import org .elasticsearch .common .io .stream .Writeable ;
119import org .elasticsearch .common .util .BytesRefHash ;
10+ import org .elasticsearch .common .util .LongArray ;
11+ import org .elasticsearch .common .util .ObjectArray ;
1212import org .elasticsearch .core .Releasables ;
13+ import org .elasticsearch .search .aggregations .AggregationExecutionContext ;
1314import org .elasticsearch .search .aggregations .Aggregator ;
1415import org .elasticsearch .search .aggregations .AggregatorFactories ;
1516import org .elasticsearch .search .aggregations .BucketOrder ;
2526
2627import java .io .IOException ;
2728import java .util .Arrays ;
28- import java .util .Comparator ;
2929import java .util .Iterator ;
3030import java .util .List ;
3131import java .util .Map ;
@@ -54,7 +54,6 @@ public DateHierarchyAggregator(
5454 this .bucketCountThresholds = bucketCountThresholds ;
5555 order .validate (this );
5656 this .order = order ;
57- this .partiallyBuiltBucketComparator = order == null ? null : order .partiallyBuiltBucketComparator (b -> b .bucketOrd , this );
5857 }
5958
6059 public static class BucketCountThresholds implements Writeable , ToXContentFragment {
@@ -144,7 +143,6 @@ public boolean equals(Object obj) {
144143 private final long minDocCount ;
145144 private final BucketCountThresholds bucketCountThresholds ;
146145 private final List <DateHierarchyAggregationBuilder .PreparedRounding > preparedRoundings ;
147- protected final Comparator <InternalPathHierarchy .InternalBucket > partiallyBuiltBucketComparator ;
148146
149147 /**
150148 * The collector collects the docs, including or not some score (depending of the including of a Scorer) in the
@@ -153,11 +151,11 @@ public boolean equals(Object obj) {
153151 * The LeafBucketCollector is a "Per-leaf bucket collector". It collects docs for the account of buckets.
154152 */
155153 @ Override
156- public LeafBucketCollector getLeafCollector (LeafReaderContext ctx , LeafBucketCollector sub ) throws IOException {
154+ public LeafBucketCollector getLeafCollector (AggregationExecutionContext ctx , LeafBucketCollector sub ) throws IOException {
157155 if (valuesSource == null ) {
158156 return LeafBucketCollector .NO_OP_COLLECTOR ;
159157 }
160- final SortedNumericDocValues values = valuesSource .longValues (ctx );
158+ final SortedNumericDocValues values = valuesSource .longValues (ctx . getLeafReaderContext () );
161159
162160 return new LeafBucketCollectorBase (sub , values ) {
163161
@@ -189,63 +187,76 @@ public void collect(int doc, long bucket) throws IOException {
189187 }
190188
191189 @ Override
192- public InternalAggregation [] buildAggregations (long [] owningBucketOrdinals ) throws IOException {
190+ public InternalAggregation [] buildAggregations (LongArray owningBucketOrdinals ) throws IOException {
193191
194- InternalDateHierarchy .InternalBucket [][] topBucketsPerOrd = new InternalDateHierarchy .InternalBucket [owningBucketOrdinals .length ][];
195- InternalDateHierarchy [] results = new InternalDateHierarchy [owningBucketOrdinals .length ];
192+ // InternalDateHierarchy.InternalBucket[][] topBucketsPerOrd = new
193+ // InternalDateHierarchy.InternalBucket[owningBucketOrdinals.length][];
194+ // InternalDateHierarchy[] results = new InternalDateHierarchy[owningBucketOrdinals.length];
196195
197- for (int ordIdx = 0 ; ordIdx < owningBucketOrdinals .length ; ordIdx ++) {
198- assert owningBucketOrdinals [ordIdx ] == 0 ;
196+ try (
197+ ObjectArray <InternalDateHierarchy .InternalBucket []> topBucketsPerOrd = bigArrays ().newObjectArray (owningBucketOrdinals .size ())
198+ ) {
199199
200- // build buckets and store them sorted
201- final int size = (int ) Math .min (bucketOrds .size (), bucketCountThresholds .getShardSize ());
200+ InternalDateHierarchy [] results = new InternalDateHierarchy [Math .toIntExact (owningBucketOrdinals .size ())];
202201
203- PathSortedTree <String , InternalDateHierarchy .InternalBucket > pathSortedTree = new PathSortedTree <>(order .comparator (), size );
202+ for (int ordIdx = 0 ; ordIdx < owningBucketOrdinals .size (); ordIdx ++) {
203+ // assert owningBucketOrdinals[ordIdx] == 0;
204+ assert owningBucketOrdinals .get (ordIdx ) == 0 ;
204205
205- InternalDateHierarchy .InternalBucket spare ;
206- for (int i = 0 ; i < bucketOrds .size (); i ++) {
207- spare = new InternalDateHierarchy .InternalBucket (0 , null , null , null , 0 , null );
206+ // build buckets and store them sorted
207+ final int size = (int ) Math .min (bucketOrds .size (), bucketCountThresholds .getShardSize ());
208208
209- BytesRef term = new BytesRef ();
210- bucketOrds .get (i , term );
211- String [] paths = term .utf8ToString ().split ("/" , -1 );
209+ PathSortedTree <String , InternalDateHierarchy .InternalBucket > pathSortedTree = new PathSortedTree <>(
210+ order .comparator (),
211+ size
212+ );
212213
213- spare .paths = paths ;
214- spare .key = term ;
215- spare .level = paths .length - 1 ;
216- spare .name = paths [spare .level ];
217- spare .docCount = bucketDocCount (i );
218- spare .bucketOrd = i ;
214+ InternalDateHierarchy .InternalBucket spare ;
215+ for (int i = 0 ; i < bucketOrds .size (); i ++) {
216+ spare = new InternalDateHierarchy .InternalBucket (0 , null , null , null , 0 , null );
219217
220- pathSortedTree .add (spare .paths , spare );
221- }
218+ BytesRef term = new BytesRef ();
219+ bucketOrds .get (i , term );
220+ String [] paths = term .utf8ToString ().split ("/" , -1 );
222221
223- // Get the top buckets
224- topBucketsPerOrd [ordIdx ] = new InternalDateHierarchy .InternalBucket [size ];
225- long otherHierarchyNodes = pathSortedTree .getFullSize ();
226- Iterator <InternalDateHierarchy .InternalBucket > iterator = pathSortedTree .consumer ();
227- for (int i = 0 ; i < size ; i ++) {
228- final InternalDateHierarchy .InternalBucket bucket = iterator .next ();
229- topBucketsPerOrd [ordIdx ][i ] = bucket ;
230- otherHierarchyNodes -= 1 ;
231- }
222+ spare .paths = paths ;
223+ spare .key = term ;
224+ spare .level = paths .length - 1 ;
225+ spare .name = paths [spare .level ];
226+ spare .docCount = bucketDocCount (i );
227+ spare .bucketOrd = i ;
232228
233- results [ordIdx ] = new InternalDateHierarchy (
234- name ,
235- Arrays .asList (topBucketsPerOrd [ordIdx ]),
236- order ,
237- minDocCount ,
238- bucketCountThresholds .getRequiredSize (),
239- bucketCountThresholds .getShardSize (),
240- otherHierarchyNodes ,
241- metadata ()
242- );
243- }
229+ pathSortedTree .add (spare .paths , spare );
230+ }
244231
245- // Build sub-aggregations for pruned buckets
246- buildSubAggsForAllBuckets (topBucketsPerOrd , b -> b .bucketOrd , (b , aggregations ) -> b .aggregations = aggregations );
232+ // Get the top buckets
233+ topBucketsPerOrd .set (ordIdx , new InternalDateHierarchy .InternalBucket [size ]);
234+ // topBucketsPerOrd[ordIdx] = new InternalDateHierarchy.InternalBucket[size];
235+ long otherHierarchyNodes = pathSortedTree .getFullSize ();
236+ Iterator <InternalDateHierarchy .InternalBucket > iterator = pathSortedTree .consumer ();
237+ for (int i = 0 ; i < size ; i ++) {
238+ final InternalDateHierarchy .InternalBucket bucket = iterator .next ();
239+ topBucketsPerOrd .get (ordIdx )[i ] = bucket ;
240+ otherHierarchyNodes -= 1 ;
241+ }
247242
248- return results ;
243+ results [ordIdx ] = new InternalDateHierarchy (
244+ name ,
245+ Arrays .asList (topBucketsPerOrd .get (ordIdx )),
246+ order ,
247+ minDocCount ,
248+ bucketCountThresholds .getRequiredSize (),
249+ bucketCountThresholds .getShardSize (),
250+ otherHierarchyNodes ,
251+ metadata ()
252+ );
253+ }
254+
255+ // Build sub-aggregations for pruned buckets
256+ buildSubAggsForAllBuckets (topBucketsPerOrd , b -> b .bucketOrd , (b , aggregations ) -> b .aggregations = aggregations );
257+
258+ return results ;
259+ }
249260 }
250261
251262 @ Override
0 commit comments