2727 * <p>Call {@link #close()} to release this handle and its cached file resources promptly. Scans and partitions already
2828 * created from it remain valid and retain the resources they still need. {@link VortexCleaner} releases an unclosed
2929 * handle when it becomes unreachable.
30+ *
31+ * <p>Native calls are synchronized with {@code close()} to prevent another thread from freeing the handle during an
32+ * operation.
3033 */
3134public final class DataSource implements AutoCloseable {
3235 private final Session session ;
@@ -129,8 +132,6 @@ public synchronized Schema arrowSchema(BufferAllocator allocator) {
129132 try (ArrowSchema schema = ArrowSchema .allocateNew (allocator )) {
130133 NativeDataSource .arrowSchema (session .nativePointer (), pointer , schema .memoryAddress ());
131134 return Data .importSchema (allocator , schema , null );
132- } finally {
133- Reference .reachabilityFence (this );
134135 }
135136 }
136137
@@ -141,17 +142,13 @@ public synchronized Schema arrowSchema(BufferAllocator allocator) {
141142 */
142143 public synchronized RowCount rowCount () {
143144 Preconditions .checkState (!closed , "data source is closed" );
144- try {
145- long [] out = new long [2 ];
146- NativeDataSource .rowCount (pointer , out );
147- return switch ((int ) out [1 ]) {
148- case 1 -> new RowCount .Estimate (out [0 ]);
149- case 2 -> new RowCount .Exact (out [0 ]);
150- default -> RowCount .Unknown .INSTANCE ;
151- };
152- } finally {
153- Reference .reachabilityFence (this );
154- }
145+ long [] out = new long [2 ];
146+ NativeDataSource .rowCount (pointer , out );
147+ return switch ((int ) out [1 ]) {
148+ case 1 -> new RowCount .Estimate (out [0 ]);
149+ case 2 -> new RowCount .Exact (out [0 ]);
150+ default -> RowCount .Unknown .INSTANCE ;
151+ };
155152 }
156153
157154 /** Precision-aware row count. See {@link #rowCount()}. */
@@ -197,17 +194,13 @@ public OptionalLong asOptional() {
197194 */
198195 public synchronized ByteSize byteSize () {
199196 Preconditions .checkState (!closed , "data source is closed" );
200- try {
201- long [] out = new long [2 ];
202- NativeDataSource .byteSize (pointer , out );
203- return switch ((int ) out [1 ]) {
204- case 1 -> new ByteSize .Estimate (out [0 ]);
205- case 2 -> new ByteSize .Exact (out [0 ]);
206- default -> ByteSize .Unknown .INSTANCE ;
207- };
208- } finally {
209- Reference .reachabilityFence (this );
210- }
197+ long [] out = new long [2 ];
198+ NativeDataSource .byteSize (pointer , out );
199+ return switch ((int ) out [1 ]) {
200+ case 1 -> new ByteSize .Estimate (out [0 ]);
201+ case 2 -> new ByteSize .Exact (out [0 ]);
202+ default -> ByteSize .Unknown .INSTANCE ;
203+ };
211204 }
212205
213206 /** Precision-aware byte size. See {@link #byteSize()}. */
@@ -276,7 +269,7 @@ public synchronized Scan scan(ScanOptions options) {
276269 splitConcurrency );
277270 return Scan .fromPointer (session , scanPtr );
278271 } finally {
279- Reference .reachabilityFence (this );
272+ Reference .reachabilityFence (options );
280273 }
281274 }
282275
0 commit comments