8
8
package org .opensearch .index .compositeindex .datacube .startree .fileformats .node ;
9
9
10
10
import org .apache .lucene .store .RandomAccessInput ;
11
+ import org .opensearch .index .compositeindex .datacube .DimensionDataType ;
11
12
import org .opensearch .index .compositeindex .datacube .startree .node .StarTreeNode ;
12
13
import org .opensearch .index .compositeindex .datacube .startree .node .StarTreeNodeType ;
13
14
import org .opensearch .search .startree .StarTreeNodeCollector ;
15
+ import org .opensearch .search .startree .filter .provider .DimensionFilterMapper ;
14
16
15
17
import java .io .IOException ;
16
18
import java .io .UncheckedIOException ;
19
+ import java .util .Comparator ;
17
20
import java .util .Iterator ;
18
21
19
22
/**
@@ -193,15 +196,23 @@ public StarTreeNode getChildStarNode() throws IOException {
193
196
}
194
197
195
198
@ Override
196
- public StarTreeNode getChildForDimensionValue (Long dimensionValue , StarTreeNode lastMatchedChild ) throws IOException {
199
+ public StarTreeNode getChildForDimensionValue (
200
+ Long dimensionValue ,
201
+ StarTreeNode lastMatchedChild ,
202
+ DimensionFilterMapper dimensionFilterMapper
203
+ ) throws IOException {
197
204
// there will be no children for leaf nodes
198
205
if (isLeaf ()) {
199
206
return null ;
200
207
}
201
208
202
209
StarTreeNode resultStarTreeNode = null ;
203
210
if (null != dimensionValue ) {
204
- resultStarTreeNode = binarySearchChild (dimensionValue , lastMatchedChild );
211
+ resultStarTreeNode = binarySearchChild (
212
+ dimensionValue ,
213
+ lastMatchedChild ,
214
+ dimensionFilterMapper == null ? DimensionDataType .LONG ::compare : dimensionFilterMapper .comparator ()
215
+ );
205
216
}
206
217
return resultStarTreeNode ;
207
218
}
@@ -238,11 +249,13 @@ private static FixedLengthStarTreeNode matchStarTreeNodeTypeOrNull(FixedLengthSt
238
249
* Performs a binary search to find a child node with the given dimension value.
239
250
*
240
251
* @param dimensionValue The dimension value to search for
252
+ * @param lastMatchedNode : If not null, we begin the binary search from the node after this.
253
+ * @param comparator : Comparator (LONG or UNSIGNED_LONG) to compare the dimension values
241
254
* @return The child node if found, null otherwise
242
255
* @throws IOException If there's an error reading from the input
243
256
*/
244
- private FixedLengthStarTreeNode binarySearchChild (long dimensionValue , StarTreeNode lastMatchedNode ) throws IOException {
245
-
257
+ private FixedLengthStarTreeNode binarySearchChild (long dimensionValue , StarTreeNode lastMatchedNode , Comparator < Long > comparator )
258
+ throws IOException {
246
259
int low = firstChildId ;
247
260
248
261
int high = getInt (LAST_CHILD_ID_OFFSET );
@@ -268,10 +281,10 @@ private FixedLengthStarTreeNode binarySearchChild(long dimensionValue, StarTreeN
268
281
int mid = low + (high - low ) / 2 ;
269
282
FixedLengthStarTreeNode midNode = new FixedLengthStarTreeNode (in , mid );
270
283
long midDimensionValue = midNode .getDimensionValue ();
271
-
272
- if (midDimensionValue == dimensionValue ) {
284
+ int compare = comparator . compare ( midDimensionValue , dimensionValue );
285
+ if (compare == 0 ) {
273
286
return midNode ;
274
- } else if (midDimensionValue < dimensionValue ) {
287
+ } else if (compare < 0 ) {
275
288
low = mid + 1 ;
276
289
} else {
277
290
high = mid - 1 ;
@@ -281,16 +294,19 @@ private FixedLengthStarTreeNode binarySearchChild(long dimensionValue, StarTreeN
281
294
}
282
295
283
296
@ Override
284
- public void collectChildrenInRange (long low , long high , StarTreeNodeCollector collector ) throws IOException {
285
- if (low <= high ) {
286
- FixedLengthStarTreeNode lowStarTreeNode = binarySearchChild (low , true , null );
297
+ public void collectChildrenInRange (long low , long high , StarTreeNodeCollector collector , DimensionFilterMapper dimensionFilterMapper )
298
+ throws IOException {
299
+ Comparator <Long > comparator = dimensionFilterMapper .comparator ();
300
+ if (comparator .compare (low , high ) <= 0 ) {
301
+ FixedLengthStarTreeNode lowStarTreeNode = binarySearchChild (low , true , null , comparator );
287
302
if (lowStarTreeNode != null ) {
288
- FixedLengthStarTreeNode highStarTreeNode = binarySearchChild (high , false , lowStarTreeNode );
303
+ FixedLengthStarTreeNode highStarTreeNode = binarySearchChild (high , false , lowStarTreeNode , comparator );
289
304
if (highStarTreeNode != null ) {
290
305
for (int lowNodeId = lowStarTreeNode .nodeId (); lowNodeId <= highStarTreeNode .nodeId (); ++lowNodeId ) {
291
306
collector .collectStarTreeNode (new FixedLengthStarTreeNode (in , lowNodeId ));
292
307
}
293
- } else if (lowStarTreeNode .getDimensionValue () <= high ) { // Low StarTreeNode is the last default node for that dimension.
308
+ } else if (comparator .compare (lowStarTreeNode .getDimensionValue (), high ) <= 0 ) { // Low StarTreeNode is the last default//
309
+ // node for that dimension.
294
310
collector .collectStarTreeNode (lowStarTreeNode );
295
311
}
296
312
}
@@ -302,11 +318,16 @@ public void collectChildrenInRange(long low, long high, StarTreeNodeCollector co
302
318
* @param dimensionValue : The dimension to match.
303
319
* @param matchNextHighest : If true then we try to return @dimensionValue or the next Highest. Else, we return @dimensionValue or the next Lowest.
304
320
* @param lastMatchedNode : If not null, we begin the binary search from the node after this.
321
+ * @param comparator : Comparator (LONG or UNSIGNED_LONG) to compare the dimension values
305
322
* @return : Matched node or null.
306
323
* @throws IOException :
307
324
*/
308
- private FixedLengthStarTreeNode binarySearchChild (long dimensionValue , boolean matchNextHighest , StarTreeNode lastMatchedNode )
309
- throws IOException {
325
+ private FixedLengthStarTreeNode binarySearchChild (
326
+ long dimensionValue ,
327
+ boolean matchNextHighest ,
328
+ StarTreeNode lastMatchedNode ,
329
+ Comparator <Long > comparator
330
+ ) throws IOException {
310
331
311
332
int low = firstChildId ;
312
333
int tempLow = low ;
@@ -342,17 +363,18 @@ private FixedLengthStarTreeNode binarySearchChild(long dimensionValue, boolean m
342
363
FixedLengthStarTreeNode midNode = new FixedLengthStarTreeNode (in , mid );
343
364
long midDimensionValue = midNode .getDimensionValue ();
344
365
345
- if (midDimensionValue == dimensionValue ) {
366
+ int compare = comparator .compare (midDimensionValue , dimensionValue );
367
+ if (compare == 0 ) {
346
368
return midNode ;
347
369
} else {
348
- if (midDimensionValue < dimensionValue ) { // Going to the right from mid to search next
370
+ if (compare < 0 ) { // Going to the right from mid to search next
349
371
tempLow = mid + 1 ;
350
372
// We are going out of bounds for this dimension on the right side.
351
373
if (tempLow > high || tempLow == nullNodeId ) {
352
374
return matchNextHighest ? null : midNode ;
353
375
} else {
354
376
FixedLengthStarTreeNode nodeGreaterThanMid = new FixedLengthStarTreeNode (in , tempLow );
355
- if (nodeGreaterThanMid .getDimensionValue () > dimensionValue ) {
377
+ if (comparator . compare ( nodeGreaterThanMid .getDimensionValue (), dimensionValue ) > 0 ) {
356
378
return matchNextHighest ? nodeGreaterThanMid : midNode ;
357
379
}
358
380
}
@@ -363,7 +385,7 @@ private FixedLengthStarTreeNode binarySearchChild(long dimensionValue, boolean m
363
385
return matchNextHighest ? midNode : null ;
364
386
} else {
365
387
FixedLengthStarTreeNode nodeLessThanMid = new FixedLengthStarTreeNode (in , tempHigh );
366
- if (nodeLessThanMid .getDimensionValue () < dimensionValue ) {
388
+ if (comparator . compare ( nodeLessThanMid .getDimensionValue (), dimensionValue ) < 0 ) {
367
389
return matchNextHighest ? midNode : nodeLessThanMid ;
368
390
}
369
391
}
0 commit comments