Implementation:Risingwavelabs Risingwave StreamChunkIterator
| Property | Value |
|---|---|
| Component | Java Binding |
| Language | Java |
| Source File | java/java-binding/src/main/java/com/risingwave/java/binding/StreamChunkIterator.java
|
| Lines | 43 |
| Package | com.risingwave.java.binding
|
| License | Apache 2.0 |
Overview
StreamChunkIterator is a Java iterator wrapper for traversing rows within a StreamChunk via JNI calls to the native Rust iterator. It enables row-by-row iteration over StreamChunk data in Java sink connectors and integration tests.
The class creates a native stream chunk iterator from a StreamChunk pointer via Binding.iteratorNewStreamChunk. On each call to next(), it advances the native iterator and returns a StreamChunkRow instance (which extends BaseRow) representing the current row, or null when all rows have been consumed. The class implements AutoCloseable to ensure the native iterator is released via Binding.iteratorClose.
Code Reference
Source Location
java/java-binding/src/main/java/com/risingwave/java/binding/StreamChunkIterator.java
Signature
public class StreamChunkIterator implements AutoCloseable {
private final long pointer;
private boolean isClosed;
public StreamChunkIterator(StreamChunk chunk);
public StreamChunkRow next();
@Override
public void close();
}
Import
import com.risingwave.java.binding.StreamChunkIterator;
// Required for construction:
import com.risingwave.java.binding.StreamChunk;
I/O Contract
Inputs
- chunk (StreamChunk): A
StreamChunkinstance whose native pointer is used to create the underlying native iterator viaBinding.iteratorNewStreamChunk(chunk.getPointer()).
Outputs
next()returnsStreamChunkRow: The next row in the stream chunk, ornullwhen no more rows remain.StreamChunkRowextendsBaseRowand additionally provides access to the operation type (insert, delete, update_insert, update_delete) via itsgetOp()method.
Side Effects
- Native resource allocation: The constructor calls
Binding.iteratorNewStreamChunkto create a native Rust iterator over the stream chunk data. - Native resource release:
close()callsBinding.iteratorClose(pointer)to free the native iterator. TheisClosedflag prevents double-close. - Iterator advancement: Each call to
next()advances the native iterator's cursor position viaBinding.iteratorNext.
Usage Examples
Iterating over a StreamChunk
try (StreamChunk chunk = StreamChunk.fromPayload(payload);
StreamChunkIterator iter = new StreamChunkIterator(chunk)) {
StreamChunkRow row;
while ((row = iter.next()) != null) {
int op = row.getOp(); // Operation type
int id = row.getInt(0); // Column 0 as int
String name = row.getString(1); // Column 1 as string
// Process the row based on operation type...
}
}
Using with a pretty-printed test chunk
// Create a test chunk: two integer columns, two rows (one insert, one delete)
try (StreamChunk chunk = StreamChunk.fromPretty("I I\n + 1 10\n - 2 20");
StreamChunkIterator iter = new StreamChunkIterator(chunk)) {
StreamChunkRow insertRow = iter.next();
assert insertRow != null;
assert insertRow.getInt(0) == 1;
assert insertRow.getInt(1) == 10;
StreamChunkRow deleteRow = iter.next();
assert deleteRow != null;
assert deleteRow.getInt(0) == 2;
assert deleteRow.getInt(1) == 20;
assert iter.next() == null; // No more rows
}
Related Pages
- StreamChunk - The stream chunk data structure that this iterator traverses
- BaseRow - Base class for row data access; StreamChunkRow extends BaseRow
- Binding - Declares
iteratorNewStreamChunk,iteratorNext, anditeratorClosenative methods - HummockIterator - Analogous iterator for Hummock storage data (produces KeyedRow instead of StreamChunkRow)