pranavshenoy commented on code in PR #4909:
URL: https://github.com/apache/cassandra/pull/4909#discussion_r3488445508


##########
src/java/org/apache/cassandra/index/sai/disk/v1/postings/PostingsWriter.java:
##########
@@ -204,6 +204,67 @@ public long getTotalPostings()
         return totalPostings;
     }
 
+    /**
+     * Writes a V2 posting list with separate exact and prefix sections.
+     * <p>
+     * On-disk layout: {@code [exact FOR blocks][prefix FOR blocks][V2 BLOCK 
SUMMARY]}. Each section is
+     * block-aligned (its final partial block is flushed before the next 
section starts) and the first block
+     * of each section carries its own {@code firstPosting} VLong, so the 
sections can be read independently.
+     * The V2 block summary prepends {@code prefixIndex} (= number of exact 
postings) and {@code suffixIndex}
+     * (= exact + prefix postings) before the standard summary fields.
+     *
+     * @param exactPostings  ascending row IDs for the exact section; may be 
null/empty
+     * @param prefixPostings ascending row IDs for the prefix section; may be 
null/empty
+     * @return file offset to the V2 block summary
+     */
+    public long writeV2(PostingList exactPostings, PostingList prefixPostings) 
throws IOException
+    {
+        resetBlockCounters();
+        blockOffsets.clear();
+        blockMaximumPostings.clear();
+
+        int exactCount = writeSection(exactPostings);
+        int prefixCount = writeSection(prefixPostings);
+        int totalCount = exactCount + prefixCount;
+
+        assert totalCount > 0 : "V2 posting list must have at least one 
posting";
+
+        final long summaryOffset = dataOutput.getFilePointer();
+        // V2 header: prefixIndex (count of exact postings) then suffixIndex 
(exact + prefix), then standard summary.
+        dataOutput.writeVInt(exactCount);
+        dataOutput.writeVInt(totalCount);
+        writeSummary(totalCount);
+        return summaryOffset;
+    }
+
+    /**
+     * Writes one section's postings as a self-contained run of FOR blocks 
(the first block carries its own
+     * {@code firstPosting}). Returns the number of postings written.
+     */
+    private int writeSection(PostingList postings) throws IOException
+    {
+        if (postings == null)
+            return 0;
+
+        // Reset the delta base so this section starts fresh (its first block 
writes firstPosting).
+        lastPosting = Long.MIN_VALUE;
+        resetBlockCounters();
+
+        int count = 0;
+        long posting;
+        while ((posting = postings.nextPosting()) != PostingList.END_OF_STREAM)
+        {
+            writePosting(posting);
+            count++;
+            totalPostings++;
+        }
+
+        if (count > 0)

Review Comment:
   If it writes to the disk, we are technically doing two different IOs (one 
for exact and other for Prefix), this can be avoided



-- 
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]

Reply via email to