Skip to content

Commit

Permalink
Create utility dir arrow/jniutil (apache#27)
Browse files Browse the repository at this point in the history
* Create utility dir arrow/jniutil

* fix
  • Loading branch information
zhztheplayer authored Jul 15, 2021
1 parent b8bb16a commit 816d602
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 42 deletions.
2 changes: 2 additions & 0 deletions cpp/cmake_modules/DefineOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion cpp/src/arrow/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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})

Expand Down Expand Up @@ -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()
Expand Down
31 changes: 31 additions & 0 deletions cpp/src/arrow/jniutil/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -27,12 +27,10 @@
#include <flatbuffers/flatbuffers.h>

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<ReservationListener> listener,
Expand Down Expand Up @@ -427,6 +425,5 @@ Result<std::shared_ptr<RecordBatch>> DeserializeUnsafeFromJava(
return RecordBatch::Make(schema, length, columns_array_data);
}

} // namespace jni
} // namespace dataset
} // namespace jniutil
} // namespace arrow
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
#include <jni.h>

namespace arrow {
namespace dataset {
namespace jni {
namespace jniutil {

Status CheckException(JNIEnv* env);

Expand Down Expand Up @@ -140,6 +139,5 @@ class ReservationListenableMemoryPool : public MemoryPool {
std::unique_ptr<Impl> impl_;
};

} // namespace jni
} // namespace dataset
} // namespace jniutil
} // namespace arrow
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -129,6 +128,5 @@ TEST(ReservationListenableMemoryPool, BlockSize2) {
ASSERT_EQ(1, listener->release_count());
}

} // namespace jni
} // namespace dataset
} // namespace jniutil
} // namespace arrow
9 changes: 1 addition & 8 deletions cpp/src/jni/dataset/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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})
36 changes: 18 additions & 18 deletions cpp/src/jni/dataset/jni_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -89,7 +89,7 @@ arrow::Result<std::shared_ptr<arrow::dataset::FileFormat>> 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) {}
Expand All @@ -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();
}

Expand All @@ -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();
}

Expand Down Expand Up @@ -322,7 +322,7 @@ arrow::Result<std::shared_ptr<arrow::RecordBatch>> FromBytes(
JNIEnv* env, std::shared_ptr<arrow::Schema> schema, jbyteArray bytes) {
ARROW_ASSIGN_OR_RAISE(
std::shared_ptr<arrow::RecordBatch> batch,
arrow::dataset::jni::DeserializeUnsafeFromJava(env, schema, bytes))
arrow::jniutil::DeserializeUnsafeFromJava(env, schema, bytes))
return batch;
}

Expand All @@ -347,7 +347,7 @@ arrow::Result<std::shared_ptr<arrow::dataset::Scanner>> 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;
});
Expand All @@ -363,18 +363,18 @@ arrow::Result<std::shared_ptr<arrow::dataset::Scanner>> 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
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 816d602

Please sign in to comment.