Skip to content

Commit

Permalink
feat(java/driver/jni): add JNI bindings to native driver manager
Browse files Browse the repository at this point in the history
  • Loading branch information
lidavidm committed Dec 30, 2024
1 parent a3d6589 commit ddfc612
Show file tree
Hide file tree
Showing 16 changed files with 660 additions and 3 deletions.
2 changes: 1 addition & 1 deletion c/driver_manager/adbc_driver_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ struct ManagedLibrary {

void* handle = dlopen(library, RTLD_NOW | RTLD_LOCAL);
if (!handle) {
error_message = "[DriverManager] dlopen() failed: ";
error_message = "dlopen() failed: ";
error_message += dlerror();

// If applicable, append the shared library prefix/extension and
Expand Down
61 changes: 61 additions & 0 deletions java/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

cmake_minimum_required(VERSION 3.16)
message(STATUS "Building using CMake version: ${CMAKE_VERSION}")

# find_package() uses <PackageName>_ROOT variables.
# https://cmake.org/cmake/help/latest/policy/CMP0074.html
if(POLICY CMP0074)
cmake_policy(SET CMP0074 NEW)
endif()

project(adbc-java)

if("${CMAKE_CXX_STANDARD}" STREQUAL "")
set(CMAKE_CXX_STANDARD 17)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(GNUInstallDirs)

# Dependencies

find_package(Java REQUIRED)
find_package(JNI REQUIRED)

include(UseJava)

# ADBC_ARCH_DIR is derived from the architecture. The user can override this
# variable if auto-detection fails.
if("${ADBC_ARCH_DIR}" STREQUAL "")
if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64")
set(ADBC_ARCH_DIR "aarch_64")
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i386")
set(ADBC_ARCH_DIR "x86_64")
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm64")
set(ADBC_ARCH_DIR "aarch_64")
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "AMD64")
set(ADBC_ARCH_DIR "x86_64")
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
set(ADBC_ARCH_DIR "x86_64")
else()
message(FATAL_ERROR "Unsupported architecture: ${CMAKE_SYSTEM_PROCESSOR}")
endif()
endif()

add_subdirectory(driver/jni)
3 changes: 1 addition & 2 deletions java/driver/flight-sql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<!-- Latest version still supporting Java 8 -->
<version>2.9.3</version>
<version>3.1.8</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
Expand Down
38 changes: 38 additions & 0 deletions java/driver/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

find_package(AdbcDriverManager)

add_jar(adbc_driver_jni_jar
SOURCES src/main/java/org/apache/arrow/adbc/driver/jni/impl/NativeAdbc.java
src/main/java/org/apache/arrow/adbc/driver/jni/impl/NativeAdbcException.java
src/main/java/org/apache/arrow/adbc/driver/jni/impl/NativeDriver.java
GENERATE_NATIVE_HEADERS
adbc_driver_jni_headers)

add_library(adbc_driver_jni SHARED src/main/cpp/jni_wrapper.cc)
set_property(TARGET adbc_driver_jni PROPERTY OUTPUT_NAME "adbc_drvier_jni")
target_link_libraries(adbc_driver_jni adbc_driver_jni_headers JNI::JNI AdbcDriverManager::adbc_driver_manager_static)

set(ARROW_JAVA_JNI_C_LIBDIR
"${CMAKE_INSTALL_PREFIX}/lib/adbc_driver_jni/${ADBC_ARCH_DIR}")
set(ADBC_DRIVER_JNI_C_BINDIR
"${CMAKE_INSTALL_PREFIX}/bin/adbc_driver_jni/${ADBC_ARCH_DIR}")

install(TARGETS adbc_driver_jni
LIBRARY DESTINATION ${ADBC_DRIVER_JNI_C_LIBDIR}
RUNTIME DESTINATION ${ADBC_DRIVER_JNI_C_BINDIR})
128 changes: 128 additions & 0 deletions java/driver/jni/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.arrow.adbc</groupId>
<artifactId>arrow-adbc-java-root</artifactId>
<version>0.16.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>adbc-driver-jni</artifactId>
<packaging>jar</packaging>
<name>Arrow ADBC Driver Native</name>
<description>An ADBC driver wrapping the native driver manager.</description>

<dependencies>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<!-- Latest version still supporting Java 8 -->
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>4.29.2</version>
</dependency>

<!-- Arrow -->
<dependency>
<groupId>org.apache.arrow</groupId>
<artifactId>arrow-memory-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.arrow</groupId>
<artifactId>arrow-vector</artifactId>
</dependency>
<dependency>
<groupId>org.apache.arrow</groupId>
<artifactId>flight-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.arrow</groupId>
<artifactId>flight-sql</artifactId>
</dependency>

<dependency>
<groupId>org.apache.arrow.adbc</groupId>
<artifactId>adbc-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.arrow.adbc</groupId>
<artifactId>adbc-driver-manager</artifactId>
</dependency>
<dependency>
<groupId>org.apache.arrow.adbc</groupId>
<artifactId>adbc-sql</artifactId>
</dependency>

<!-- Helpers for mapping Arrow types to ANSI SQL types and building test servers -->
<dependency>
<groupId>org.apache.arrow</groupId>
<artifactId>flight-sql-jdbc-core</artifactId>
</dependency>

<!-- Static analysis and linting -->
<dependency>
<groupId>org.checkerframework</groupId>
<artifactId>checker-qual</artifactId>
</dependency>

<!-- Testing -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.arrow</groupId>
<artifactId>flight-sql-jdbc-core</artifactId>
<version>${dep.arrow.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration combine.self="append">
<systemPropertyVariables>
<arrow.test.dataRoot>${project.basedir}/../../../testing/data</arrow.test.dataRoot>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
130 changes: 130 additions & 0 deletions java/driver/jni/src/main/cpp/jni_wrapper.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#include "org_apache_arrow_adbc_driver_jni_impl_NativeAdbc.h"

#include <cassert>
#include <optional>
#include <string>

#include <arrow-adbc/adbc.h>
#include <arrow-adbc/adbc_driver_manager.h>
#include <jni.h>

// We will use exceptions for error handling as that's easier with the JNI
// model.

namespace {

struct JniStringView {
JNIEnv* env;
jstring jni_string;
const char* value;

explicit JniStringView(JNIEnv* env, jstring jni_string)
: env(env), jni_string(jni_string), value(nullptr) {
if (jni_string == nullptr) {
// TODO:
}
value = env->GetStringUTFChars(jni_string, nullptr);
if (value == nullptr) {
// TODO:
}
}

~JniStringView() {
if (jni_string == nullptr) {
return;
}

env->ReleaseStringUTFChars(jni_string, value);
jni_string = nullptr;
}
};

std::string GetJniString(JNIEnv* env, jstring jni_string) {
JniStringView view(env, jni_string);
return std::string(view.value);
}

std::optional<std::string> MaybeGetJniString(JNIEnv* env, jstring jni_string) {
if (jni_string == nullptr) {
return std::nullopt;
}
JniStringView view(env, jni_string);
return std::string(view.value);
}

struct AdbcException {
AdbcStatusCode code;
std::string message;

void ThrowJavaException(JNIEnv* env) const {
jclass exception_klass =
env->FindClass("org/apache/arrow/adbc/driver/jni/impl/NativeAdbcException");

env->ThrowNew(exception_klass, message.c_str());
}
};

void RaiseAdbcException(AdbcStatusCode code, const AdbcError& error) {
assert(code != ADBC_STATUS_OK);
throw AdbcException{
.code = code,
.message = std::string(error.message),
};
}

#define CHECK_ADBC_ERROR(expr, error) \
do { \
AdbcStatusCode status = (expr); \
if (status != ADBC_STATUS_OK) { \
RaiseAdbcException(status, error); \
} \
} while (0)

} // namespace

extern "C" {

JNIEXPORT jobject JNICALL
Java_org_apache_arrow_adbc_driver_jni_impl_NativeAdbc_loadDriver(
JNIEnv* env, [[maybe_unused]] jclass self, jstring driver_name_jni,
jstring entrypoint_jni, jint version) {
try {
std::string driver_name = GetJniString(env, driver_name_jni);
std::optional<std::string> entrypoint = MaybeGetJniString(env, entrypoint_jni);

struct AdbcDriver* driver = new struct AdbcDriver;
struct AdbcError error = ADBC_ERROR_INIT;
CHECK_ADBC_ERROR(
AdbcLoadDriver(driver_name.c_str(), entrypoint ? entrypoint->c_str() : nullptr,
version, driver, &error),
error);

// TODO: error handling
jclass nativeDriverKlass =
env->FindClass("org/apache/arrow/adbc/driver/jni/impl/NativeDriver");
jmethodID nativeDriverCtor = env->GetMethodID(nativeDriverKlass, "<init>", "(J)V");
return env->NewObject(nativeDriverKlass, nativeDriverCtor,
reinterpret_cast<jlong>(driver));
} catch (const AdbcException& e) {
e.ThrowJavaException(env);
return nullptr;
}
}
}
Loading

0 comments on commit ddfc612

Please sign in to comment.