Skip to content

Commit 5407d43

Browse files
committed
extract checkOpen and unwrap_context helpers
1 parent 45743e3 commit 5407d43

2 files changed

Lines changed: 24 additions & 20 deletions

File tree

‎core/src/main/java/org/apache/datafusion/SessionContext.java‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -609,9 +609,7 @@ private static byte[] serializeSchemaIpc(Schema schema) {
609609
* @throws IllegalStateException if this context is closed.
610610
*/
611611
public boolean tableExists(String name) {
612-
if (nativeHandle == 0) {
613-
throw new IllegalStateException("SessionContext is closed");
614-
}
612+
checkOpen();
615613
if (name == null) {
616614
throw new IllegalArgumentException("tableExists name must be non-null");
617615
}
@@ -627,15 +625,19 @@ public boolean tableExists(String name) {
627625
* @throws IllegalStateException if this context is closed.
628626
*/
629627
public void deregisterTable(String name) {
630-
if (nativeHandle == 0) {
631-
throw new IllegalStateException("SessionContext is closed");
632-
}
628+
checkOpen();
633629
if (name == null) {
634630
throw new IllegalArgumentException("deregisterTable name must be non-null");
635631
}
636632
deregisterTable(nativeHandle, name);
637633
}
638634

635+
private void checkOpen() {
636+
if (nativeHandle == 0) {
637+
throw new IllegalStateException("SessionContext is closed");
638+
}
639+
}
640+
639641
@Override
640642
public void close() {
641643
if (nativeHandle != 0) {

‎native/src/lib.rs‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,20 @@ pub extern "system" fn Java_org_apache_datafusion_SessionContext_getOptionNative
10421042
)
10431043
}
10441044

1045+
/// Converts a raw JNI handle into a shared reference to a [`SessionContext`].
1046+
///
1047+
/// # Safety for the unsafe usage
1048+
/// The caller must ensure `handle` was produced by `Box::into_raw(Box::new(ctx))`
1049+
/// in `createSessionContext` and has not yet been freed. The Java side zeroes
1050+
/// `nativeHandle` on `close()`, so the `handle == 0` guard in every JNI handler
1051+
/// ensures this invariant holds before `unwrap_context` is called.
1052+
fn unwrap_context(handle: jlong) -> JniResult<&'static SessionContext> {
1053+
if handle == 0 {
1054+
return Err("SessionContext handle is null".into());
1055+
}
1056+
Ok(unsafe { &*(handle as *const SessionContext) })
1057+
}
1058+
10451059
#[no_mangle]
10461060
pub extern "system" fn Java_org_apache_datafusion_SessionContext_tableExists<'local>(
10471061
mut env: JNIEnv<'local>,
@@ -1050,13 +1064,7 @@ pub extern "system" fn Java_org_apache_datafusion_SessionContext_tableExists<'lo
10501064
name: JString<'local>,
10511065
) -> jboolean {
10521066
try_unwrap_or_throw(&mut env, 0, |env| -> JniResult<jboolean> {
1053-
if handle == 0 {
1054-
return Err("SessionContext handle is null".into());
1055-
}
1056-
// SAFETY: handle is a valid Box<SessionContext> allocated by createSessionContext,
1057-
// the null check above guards against zero, and the Java side zeroes nativeHandle
1058-
// on close() so a closed context is caught before reaching here.
1059-
let ctx = unsafe { &*(handle as *const SessionContext) };
1067+
let ctx = unwrap_context(handle)?;
10601068
let name: String = env.get_string(&name)?.into();
10611069
Ok(ctx.table_exist(&name)? as jboolean)
10621070
})
@@ -1070,13 +1078,7 @@ pub extern "system" fn Java_org_apache_datafusion_SessionContext_deregisterTable
10701078
name: JString<'local>,
10711079
) {
10721080
try_unwrap_or_throw(&mut env, (), |env| -> JniResult<()> {
1073-
if handle == 0 {
1074-
return Err("SessionContext handle is null".into());
1075-
}
1076-
// SAFETY: handle is a valid Box<SessionContext> allocated by createSessionContext,
1077-
// the null check above guards against zero, and the Java side zeroes nativeHandle
1078-
// on close() so a closed context is caught before reaching here.
1079-
let ctx = unsafe { &*(handle as *const SessionContext) };
1081+
let ctx = unwrap_context(handle)?;
10801082
let name: String = env.get_string(&name)?.into();
10811083
ctx.deregister_table(&name)?;
10821084
Ok(())

0 commit comments

Comments
 (0)