zihanx commented on code in PR #15803:
URL: https://github.com/apache/lucene/pull/15803#discussion_r2955630772
##########
lucene/core/src/java/org/apache/lucene/index/ReaderUtil.java:
##########
@@ -89,4 +93,82 @@ public static int subIndex(int n, List<LeafReaderContext>
leaves) {
}
return hi;
}
+
+ /**
+ * Partitions global doc IDs from ScoreDoc array by leaf. Extracts doc IDs,
sorts them, and
+ * partitions across leaves.
+ *
+ * @param hits the ScoreDoc array (typically from TopDocs.scoreDocs)
+ * @param leaves the index reader's leaves
+ * @return array indexed by leaf ord, containing global doc IDs for that
leaf (empty if no hits)
+ */
+ public static int[][] partitionByLeaf(ScoreDoc[] hits,
List<LeafReaderContext> leaves) {
+ int[] docIds = new int[hits.length];
+ for (int i = 0; i < hits.length; i++) {
+ docIds[i] = hits[i].doc;
+ }
+ Arrays.sort(docIds);
+ return partitionByLeaf(docIds, leaves);
+ }
+
+ /**
+ * Partitions sorted global doc IDs by leaf.
+ *
+ * @param sortedDocIds global doc IDs, must be sorted ascending
+ * @param leaves the index reader's leaves
+ * @return array indexed by leaf ord, containing global doc IDs for that
leaf (empty if no hits)
+ */
+ public static int[][] partitionByLeaf(int[] sortedDocIds,
List<LeafReaderContext> leaves) {
+ assert isSorted(sortedDocIds) : "docIds must be sorted";
+ int numLeaves = leaves.size();
+ int[][] result = new int[numLeaves][];
+ if (sortedDocIds.length == 0) {
+ for (int i = 0; i < numLeaves; i++) {
+ result[i] = EMPTY_INT_ARRAY;
+ }
+ return result;
+ }
+ int leafStart = 0;
+ int leafIdx = 0;
+ LeafReaderContext leaf = leaves.get(0);
+ int leafEnd = leaf.docBase + leaf.reader().maxDoc();
+ for (int i = 0; i < sortedDocIds.length; i++) {
+ int docId = sortedDocIds[i];
+ while (docId >= leafEnd) {
+ int count = i - leafStart;
+ if (count == 0) {
+ result[leafIdx] = EMPTY_INT_ARRAY;
+ } else {
+ result[leafIdx] = new int[count];
+ System.arraycopy(sortedDocIds, leafStart, result[leafIdx], 0, count);
+ }
+ leafStart = i;
+ leafIdx++;
+ leaf = leaves.get(leafIdx);
+ leafEnd = leaf.docBase + leaf.reader().maxDoc();
+ }
+ }
+ // Handle last leaf
Review Comment:
Thanks for the suggestion, will change it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]