diff --git a/cpp/cmake_modules/DefineOptions.cmake b/cpp/cmake_modules/DefineOptions.cmake index bfc27e46d308b..2befafff28669 100644 --- a/cpp/cmake_modules/DefineOptions.cmake +++ b/cpp/cmake_modules/DefineOptions.cmake @@ -238,6 +238,8 @@ if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}") define_option(ARROW_JNI "Build the Arrow JNI lib" OFF) + define_option(ARROW_JNIUTIL "Build Arrow JNI utilities" ON) + define_option(ARROW_JSON "Build Arrow with JSON support (requires RapidJSON)" OFF) define_option(ARROW_MIMALLOC "Build the Arrow mimalloc-based allocator" OFF) diff --git a/cpp/src/arrow/CMakeLists.txt b/cpp/src/arrow/CMakeLists.txt index 20ff1da67554c..9ef7c1a6dd4a5 100644 --- a/cpp/src/arrow/CMakeLists.txt +++ b/cpp/src/arrow/CMakeLists.txt @@ -450,6 +450,14 @@ if(ARROW_FILESYSTEM) list(APPEND ARROW_TESTING_SRCS filesystem/test_util.cc) endif() +if(ARROW_JNIUTIL) + find_package(JNI REQUIRED) + list(APPEND ARROW_SRCS + jniutil/jni_util.cc) + + set(ARROW_PRIVATE_INCLUDES ${ARROW_PRIVATE_INCLUDES} ${JNI_INCLUDE_DIRS}) +endif() + if(ARROW_IPC) list(APPEND ARROW_SRCS ipc/dictionary.cc @@ -516,7 +524,9 @@ add_arrow_lib(arrow SHARED_INSTALL_INTERFACE_LIBS ${ARROW_SHARED_INSTALL_INTERFACE_LIBS} STATIC_INSTALL_INTERFACE_LIBS - ${ARROW_STATIC_INSTALL_INTERFACE_LIBS}) + ${ARROW_STATIC_INSTALL_INTERFACE_LIBS} + PRIVATE_INCLUDES + ${JNI_INCLUDE_DIRS}) add_dependencies(arrow ${ARROW_LIBRARIES}) @@ -700,6 +710,10 @@ if(ARROW_IPC) add_subdirectory(ipc) endif() +if(ARROW_JNIUTIL) + add_subdirectory(jniutil) +endif() + if(ARROW_JSON) add_subdirectory(json) endif() diff --git a/cpp/src/arrow/jniutil/CMakeLists.txt b/cpp/src/arrow/jniutil/CMakeLists.txt new file mode 100644 index 0000000000000..d1a11fdf18777 --- /dev/null +++ b/cpp/src/arrow/jniutil/CMakeLists.txt @@ -0,0 +1,31 @@ +# 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 limitationsn +# under the License. + +# +# arrow_dataset_jni +# + +arrow_install_all_headers("arrow/jniutil") + +find_package(JNI REQUIRED) + +add_arrow_test(arrow_jniutil_test + SOURCES + jni_util_test.cc + jni_util.cc + EXTRA_INCLUDES + ${JNI_INCLUDE_DIRS}) diff --git a/cpp/src/jni/dataset/jni_util.cc b/cpp/src/arrow/jniutil/jni_util.cc similarity index 99% rename from cpp/src/jni/dataset/jni_util.cc rename to cpp/src/arrow/jniutil/jni_util.cc index 50613088ecbff..acc11bb236098 100644 --- a/cpp/src/jni/dataset/jni_util.cc +++ b/cpp/src/arrow/jniutil/jni_util.cc @@ -15,8 +15,8 @@ // specific language governing permissions and limitations // under the License. -#include "jni/dataset/jni_util.h" #include "arrow/ipc/metadata_internal.h" +#include "arrow/jniutil/jni_util.h" #include "arrow/util/base64.h" #include "arrow/util/key_value_metadata.h" #include "arrow/util/logging.h" @@ -27,12 +27,10 @@ #include namespace arrow { +namespace jniutil { namespace flatbuf = org::apache::arrow::flatbuf; -namespace dataset { -namespace jni { - class ReservationListenableMemoryPool::Impl { public: explicit Impl(MemoryPool* pool, std::shared_ptr listener, @@ -427,6 +425,5 @@ Result> DeserializeUnsafeFromJava( return RecordBatch::Make(schema, length, columns_array_data); } -} // namespace jni -} // namespace dataset +} // namespace jniutil } // namespace arrow diff --git a/cpp/src/jni/dataset/jni_util.h b/cpp/src/arrow/jniutil/jni_util.h similarity index 98% rename from cpp/src/jni/dataset/jni_util.h rename to cpp/src/arrow/jniutil/jni_util.h index 8c83d5e17a143..5af84038a9a93 100644 --- a/cpp/src/jni/dataset/jni_util.h +++ b/cpp/src/arrow/jniutil/jni_util.h @@ -27,8 +27,7 @@ #include namespace arrow { -namespace dataset { -namespace jni { +namespace jniutil { Status CheckException(JNIEnv* env); @@ -140,6 +139,5 @@ class ReservationListenableMemoryPool : public MemoryPool { std::unique_ptr impl_; }; -} // namespace jni -} // namespace dataset +} // namespace jniutil } // namespace arrow diff --git a/cpp/src/jni/dataset/jni_util_test.cc b/cpp/src/arrow/jniutil/jni_util_test.cc similarity index 96% rename from cpp/src/jni/dataset/jni_util_test.cc rename to cpp/src/arrow/jniutil/jni_util_test.cc index 589f00b1cc750..893c28125c0c5 100644 --- a/cpp/src/jni/dataset/jni_util_test.cc +++ b/cpp/src/arrow/jniutil/jni_util_test.cc @@ -19,11 +19,10 @@ #include "arrow/memory_pool.h" #include "arrow/testing/gtest_util.h" -#include "jni/dataset/jni_util.h" +#include "arrow/jniutil/jni_util.h" namespace arrow { -namespace dataset { -namespace jni { +namespace jniutil { class MyListener : public ReservationListener { public: @@ -129,6 +128,5 @@ TEST(ReservationListenableMemoryPool, BlockSize2) { ASSERT_EQ(1, listener->release_count()); } -} // namespace jni -} // namespace dataset +} // namespace jniutil } // namespace arrow diff --git a/cpp/src/jni/dataset/CMakeLists.txt b/cpp/src/jni/dataset/CMakeLists.txt index ea218710d6e4e..fc27fe727fc04 100644 --- a/cpp/src/jni/dataset/CMakeLists.txt +++ b/cpp/src/jni/dataset/CMakeLists.txt @@ -59,7 +59,7 @@ add_custom_target(arrow_dataset_jni_proto ALL DEPENDS ${PROTO_OUTPUT_FILES}) set(PROTO_SRCS "${PROTO_OUTPUT_DIR}/DTypes.pb.cc") set(PROTO_HDRS "${PROTO_OUTPUT_DIR}/DTypes.pb.h") -set(ARROW_DATASET_JNI_SOURCES jni_wrapper.cc jni_util.cc ${PROTO_SRCS}) +set(ARROW_DATASET_JNI_SOURCES jni_wrapper.cc ${PROTO_SRCS}) add_arrow_lib(arrow_dataset_jni BUILD_SHARED @@ -81,10 +81,3 @@ add_arrow_lib(arrow_dataset_jni arrow_dataset_jni_proto) add_dependencies(arrow_dataset_jni ${ARROW_DATASET_JNI_LIBRARIES}) - -add_arrow_test(dataset_jni_test - SOURCES - jni_util_test.cc - jni_util.cc - EXTRA_INCLUDES - ${JNI_INCLUDE_DIRS}) diff --git a/cpp/src/jni/dataset/jni_wrapper.cc b/cpp/src/jni/dataset/jni_wrapper.cc index 6c2f26fe714c9..d9d02cd392d50 100644 --- a/cpp/src/jni/dataset/jni_wrapper.cc +++ b/cpp/src/jni/dataset/jni_wrapper.cc @@ -23,10 +23,10 @@ #include "arrow/dataset/file_base.h" #include "arrow/filesystem/localfs.h" #include "arrow/ipc/api.h" +#include "arrow/jniutil/jni_util.h" #include "arrow/util/iterator.h" #include "jni/dataset/DTypes.pb.h" -#include "jni/dataset/jni_util.h" #include "org_apache_arrow_dataset_file_JniWrapper.h" #include "org_apache_arrow_dataset_jni_JniWrapper.h" @@ -89,7 +89,7 @@ arrow::Result> GetFileFormat( } } -class ReserveFromJava : public arrow::dataset::jni::ReservationListener { +class ReserveFromJava : public arrow::jniutil::ReservationListener { public: ReserveFromJava(JavaVM* vm, jobject java_reservation_listener) : vm_(vm), java_reservation_listener_(java_reservation_listener) {} @@ -100,7 +100,7 @@ class ReserveFromJava : public arrow::dataset::jni::ReservationListener { return arrow::Status::Invalid("JNIEnv was not attached to current thread"); } env->CallObjectMethod(java_reservation_listener_, reserve_memory_method, size); - RETURN_NOT_OK(arrow::dataset::jni::CheckException(env)); + RETURN_NOT_OK(arrow::jniutil::CheckException(env)); return arrow::Status::OK(); } @@ -110,7 +110,7 @@ class ReserveFromJava : public arrow::dataset::jni::ReservationListener { return arrow::Status::Invalid("JNIEnv was not attached to current thread"); } env->CallObjectMethod(java_reservation_listener_, unreserve_memory_method, size); - RETURN_NOT_OK(arrow::dataset::jni::CheckException(env)); + RETURN_NOT_OK(arrow::jniutil::CheckException(env)); return arrow::Status::OK(); } @@ -322,7 +322,7 @@ arrow::Result> FromBytes( JNIEnv* env, std::shared_ptr schema, jbyteArray bytes) { ARROW_ASSIGN_OR_RAISE( std::shared_ptr batch, - arrow::dataset::jni::DeserializeUnsafeFromJava(env, schema, bytes)) + arrow::jniutil::DeserializeUnsafeFromJava(env, schema, bytes)) return batch; } @@ -347,7 +347,7 @@ arrow::Result> MakeJavaDatasetScanner( } auto bytes = (jbyteArray)env->CallObjectMethod( java_serialized_record_batch_iterator, serialized_record_batch_iterator_next); - RETURN_NOT_OK(arrow::dataset::jni::CheckException(env)); + RETURN_NOT_OK(arrow::jniutil::CheckException(env)); ARROW_ASSIGN_OR_RAISE(auto batch, FromBytes(env, schema, bytes)); return batch; }); @@ -363,18 +363,18 @@ arrow::Result> MakeJavaDatasetScanner( } } // namespace -using arrow::dataset::jni::CreateGlobalClassReference; -using arrow::dataset::jni::CreateNativeRef; -using arrow::dataset::jni::FromSchemaByteArray; -using arrow::dataset::jni::GetMethodID; -using arrow::dataset::jni::JStringToCString; -using arrow::dataset::jni::ReleaseNativeRef; -using arrow::dataset::jni::RetrieveNativeInstance; -using arrow::dataset::jni::ToSchemaByteArray; -using arrow::dataset::jni::ToStringVector; +using arrow::jniutil::CreateGlobalClassReference; +using arrow::jniutil::CreateNativeRef; +using arrow::jniutil::FromSchemaByteArray; +using arrow::jniutil::GetMethodID; +using arrow::jniutil::JStringToCString; +using arrow::jniutil::ReleaseNativeRef; +using arrow::jniutil::RetrieveNativeInstance; +using arrow::jniutil::ToSchemaByteArray; +using arrow::jniutil::ToStringVector; -using arrow::dataset::jni::ReservationListenableMemoryPool; -using arrow::dataset::jni::ReservationListener; +using arrow::jniutil::ReservationListenableMemoryPool; +using arrow::jniutil::ReservationListener; #define JNI_METHOD_START try { // macro ended @@ -659,7 +659,7 @@ JNIEXPORT jbyteArray JNICALL Java_org_apache_arrow_dataset_jni_JniWrapper_nextRe if (record_batch == nullptr) { return nullptr; // stream ended } - return JniGetOrThrow(arrow::dataset::jni::SerializeUnsafeFromNative(env, record_batch)); + return JniGetOrThrow(arrow::jniutil::SerializeUnsafeFromNative(env, record_batch)); JNI_METHOD_END(nullptr) }