Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(java/driver/jni): add JNI bindings to native driver manager #2401

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

lidavidm
Copy link
Member

Fixes #2027.

@lidavidm lidavidm force-pushed the gh-2027 branch 4 times, most recently from 1abdf50 to 7df6826 Compare January 3, 2025 04:19
@lidavidm lidavidm marked this pull request as ready for review January 3, 2025 04:38
@github-actions github-actions bot added this to the ADBC Libraries 16 milestone Jan 3, 2025
@lidavidm lidavidm removed this from the ADBC Libraries 16 milestone Jan 6, 2025
import org.apache.arrow.adbc.core.AdbcException;
import org.apache.arrow.util.Preconditions;

public enum JniLoader {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(design) it's not a common pattern to use an enum class directly for creating a singleton, and not sure if there are benefits outside of it vs using a regular class.

I'm not even sure INSTANCE is needed since things are delegated to NativeAdbc anyway. It looks to me that NativeAdbc could simply have a static initializer invoking some JniLoader#checkInitialized() for example which would cause the library to be loaded or NativeAdbc class loading to fail otherwise

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern is actually recommended by Effective Java. But I can remove the singleton, right.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I should check my on copy then :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, the problem is then the "nice" wrapper methods, if merged into NativeAdbc, can't use non-standard-library classes anymore, because of the circular dependency issue...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now I'm going to keep this as-is (note that Arrow itself uses the singleton pattern to gate access to JNI methods) and we can revisit if needed

}
}

public NativeQueryResult statementExecuteQuery(NativeHandle statement) throws AdbcException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It says AdbcException is being thrown but not sure it's true in practice...

* Wrapper for a {@code struct AdbcException} that doesn't depend on other Java code.
*
* <p>This breaks a dependency chain in compilation. If we directly used AdbcException, then we
* would need to compile the Java code before compiling JNI code, but we need the compiled JNI code
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is surprising? I thought javah would use the source code, and that javac has no way to check native code? Is the circular dependency actually because of tests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CMake code has to run javac to generate the JNI headers. And CMake doesn't resolve Maven dependencies. So it's easiest if these files can be compiled standalone.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can try to configure Maven to do this instead? It'd make the build a bit more complicated (run maven, then cmake, then maven) but maybe that's OK.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would invoking Maven from CMake to generate JNI headers address this?

Maybe using native-maven-plugin:javah goal?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could also do that. I sorta dislike invoking one build system from another as it makes it a bit more annoying to get in between them to debug or customize things but just generating headers should be OK.

@lidavidm lidavidm removed this from the ADBC Libraries 17 milestone Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

java: Add JNI bindings to adbc.h
2 participants