Skip to content

Commit 813113c

Browse files
committed
[format] Refactor blob element writer
1 parent 82b5e2b commit 813113c

1 file changed

Lines changed: 41 additions & 23 deletions

File tree

paimon-format/src/main/java/org/apache/paimon/format/blob/BlobFormatWriter.java

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.apache.paimon.fs.PositionOutputStream;
3333
import org.apache.paimon.fs.SeekableInputStream;
3434
import org.apache.paimon.rest.HttpClientUtils;
35-
import org.apache.paimon.types.DataType;
3635
import org.apache.paimon.types.DataTypeRoot;
3736
import org.apache.paimon.types.RowType;
3837
import org.apache.paimon.utils.DeltaVarintCompressor;
@@ -70,7 +69,7 @@ public class BlobFormatWriter implements FileAwareFormatWriter {
7069
private final String blobFieldName;
7170
private final boolean writeNullOnMissingFile;
7271
private final boolean writeNullOnFetchFailure;
73-
private final DataType blobFieldType;
72+
private final BlobElementWriter elementWriter;
7473
private final CRC32 crc32;
7574
private final byte[] tmpBuffer;
7675
private final LongArrayList lengths;
@@ -102,7 +101,10 @@ public BlobFormatWriter(
102101
this.writeNullOnFetchFailure = writeNullOnFetchFailure;
103102
checkArgument(type.getFieldCount() == 1, "BlobFormatWriter only support one field.");
104103
this.blobFieldName = type.getFieldNames().get(0);
105-
this.blobFieldType = type.getTypeAt(0);
104+
this.elementWriter =
105+
type.getTypeAt(0).getTypeRoot() == DataTypeRoot.ARRAY
106+
? new ArrayBlobElementWriter()
107+
: new RawBlobElementWriter();
106108
this.crc32 = new CRC32();
107109
this.tmpBuffer = new byte[4096];
108110
this.lengths = new LongArrayList(16);
@@ -126,33 +128,49 @@ public void addElement(InternalRow element) throws IOException {
126128
return;
127129
}
128130

129-
if (blobFieldType.getTypeRoot() == DataTypeRoot.ARRAY) {
130-
InternalArray array = element.getArray(0);
131-
if (array == BlobArrayPlaceholder.INSTANCE) {
131+
elementWriter.addElement(element);
132+
}
133+
134+
private interface BlobElementWriter {
135+
void addElement(InternalRow element) throws IOException;
136+
}
137+
138+
private class RawBlobElementWriter implements BlobElementWriter {
139+
140+
@Override
141+
public void addElement(InternalRow element) throws IOException {
142+
Blob blob;
143+
try {
144+
blob = element.getBlob(0);
145+
} catch (RuntimeException e) {
146+
if (shouldWriteNullOnFetchFailure(e)) {
147+
logWriteNullOnFetchFailure(e, null);
148+
writeNullElement();
149+
return;
150+
}
151+
throw e;
152+
}
153+
if (blob == BlobPlaceholder.INSTANCE) {
132154
lengths.add(PLACE_HOLDER_LENGTH);
133-
} else {
134-
addBlobArray(array);
155+
return;
135156
}
136-
return;
157+
158+
addBlob(blob);
137159
}
160+
}
138161

139-
Blob blob;
140-
try {
141-
blob = element.getBlob(0);
142-
} catch (RuntimeException e) {
143-
if (shouldWriteNullOnFetchFailure(e)) {
144-
logWriteNullOnFetchFailure(e, null);
145-
writeNullElement();
162+
private class ArrayBlobElementWriter implements BlobElementWriter {
163+
164+
@Override
165+
public void addElement(InternalRow element) throws IOException {
166+
InternalArray array = element.getArray(0);
167+
if (array == BlobArrayPlaceholder.INSTANCE) {
168+
lengths.add(PLACE_HOLDER_LENGTH);
146169
return;
147170
}
148-
throw e;
149-
}
150-
if (blob == BlobPlaceholder.INSTANCE) {
151-
lengths.add(PLACE_HOLDER_LENGTH);
152-
return;
153-
}
154171

155-
addBlob(blob);
172+
addBlobArray(array);
173+
}
156174
}
157175

158176
private void addBlob(Blob blob) throws IOException {

0 commit comments

Comments
 (0)