2929 * A helper class to write data into global row buffer using `UnsafeRow` format.
3030 *
3131 * It will remember the offset of row buffer which it starts to write, and move the cursor of row
32- * buffer while writing. If a new record comes, the cursor of row buffer will be reset, so we need
33- * to also call `reset` of this class before writing, to update the `startingOffset` and clear out
34- * null bits. Note that if we use it to write data into the result unsafe row, which means we will
35- * always write from the very beginning of the global row buffer, we don't need to update
36- * `startingOffset` and can just call `zeroOutNullBytes` before writing new record.
32+ * buffer while writing. If new data(can be the input record if this is the outermost writer, or
33+ * nested struct if this is an inner writer) comes, the starting cursor of row buffer may be
34+ * changed, so we need to call `UnsafeRowWriter.reset` before writing, to update the
35+ * `startingOffset` and clear out null bits.
36+ *
37+ * Note that if this is the outermost writer, which means we will always write from the very
38+ * beginning of the global row buffer, we don't need to update `startingOffset` and can just call
39+ * `zeroOutNullBytes` before writing new data.
3740 */
3841public class UnsafeRowWriter {
3942
@@ -43,6 +46,17 @@ public class UnsafeRowWriter {
4346 private final int nullBitsSize ;
4447 private final int fixedSize ;
4548
49+ public UnsafeRowWriter (BufferHolder holder , int numFields ) {
50+ this .holder = holder ;
51+ this .nullBitsSize = UnsafeRow .calculateBitSetWidthInBytes (numFields );
52+ this .fixedSize = nullBitsSize + 8 * numFields ;
53+ this .startingOffset = holder .cursor ;
54+ }
55+
56+ /**
57+ * Resets the `startingOffset` according to the current cursor of row buffer, and clear out null
58+ * bits. This should be called before we write a new nested struct to the row buffer.
59+ */
4660 public void reset () {
4761 this .startingOffset = holder .cursor ;
4862
@@ -53,19 +67,15 @@ public void reset() {
5367 zeroOutNullBytes ();
5468 }
5569
70+ /**
71+ * Clears out null bits. This should be called before we write a new row to row buffer.
72+ */
5673 public void zeroOutNullBytes () {
5774 for (int i = 0 ; i < nullBitsSize ; i += 8 ) {
5875 Platform .putLong (holder .buffer , startingOffset + i , 0L );
5976 }
6077 }
6178
62- public UnsafeRowWriter (BufferHolder holder , int numFields ) {
63- this .holder = holder ;
64- this .nullBitsSize = UnsafeRow .calculateBitSetWidthInBytes (numFields );
65- this .fixedSize = nullBitsSize + 8 * numFields ;
66- this .startingOffset = holder .cursor ;
67- }
68-
6979 private void zeroOutPaddingBytes (int numBytes ) {
7080 if ((numBytes & 0x07 ) > 0 ) {
7181 Platform .putLong (holder .buffer , holder .cursor + ((numBytes >> 3 ) << 3 ), 0L );
0 commit comments