Skip to content

Commit

Permalink
[Feat][Java] Fill two incompatible gaps between C++ and Java (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thespica authored Nov 14, 2023
1 parent 77f547a commit 5330e93
Show file tree
Hide file tree
Showing 19 changed files with 152 additions and 583 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/java.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ on:
branches:
- main
paths:
- 'cpp/include/**'
- 'java/**'
- '.github/workflows/java.yml'
pull_request:
Expand Down Expand Up @@ -37,10 +36,10 @@ jobs:
sudo apt-get install -y /tmp/apache-arrow-apt-source-latest-"$(lsb_release --codename --short)".deb
sudo apt-get update -y
# TODO: ISSUE-241
sudo apt install -y libarrow-dev=13.0.0-1 \
libarrow-dataset-dev=13.0.0-1 \
libarrow-acero-dev=13.0.0-1 \
libparquet-dev=13.0.0-1
sudo apt install -y libarrow-dev=14.0.1-1 \
libarrow-dataset-dev=14.0.1-1 \
libarrow-acero-dev=14.0.1-1 \
libparquet-dev=14.0.1-1
sudo apt-get install libcurl4-openssl-dev -y
sudo apt-get install llvm-11 clang-11 lld-11 libclang-11-dev libz-dev -y
Expand All @@ -53,7 +52,8 @@ jobs:
- name: Run test
run: |
export JAVA_HOME=${JAVA_HOME_11_X64}
# Temporarily using Java 8, related issue: https://github.com/alibaba/GraphAr/issues/277
export JAVA_HOME=${JAVA_HOME_8_X64}
export LLVM11_HOME=/usr/lib/llvm-11
pushd java
mvn test -Dspotless.check.skip=true
Expand Down
2 changes: 1 addition & 1 deletion cpp/include/gar/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ ConstructVerticesCollection(const GraphInfo& graph_info,
* @param edge_label The edge label.
* @param dst_label The destination vertex label.
* @param adj_list_type The adjList type.
* @param vertex_chunk_begin The index of the begin vertex chunk.
* @param vertex_chunk_begin The index of the beginning vertex chunk.
* @param vertex_chunk_end The index of the end vertex chunk (not included).
* @return The constructed collection or error.
*/
Expand Down
30 changes: 18 additions & 12 deletions java/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
cmake_minimum_required(VERSION 3.6)
project(gar-java)

option(BUILD_GAR_CPP "Build GraphAr C++ library with specific version" ON)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -g -std=c++17 -Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -g -std=c++17 -Wall")

# set auto-generated JNI code and handwriting JNI code as source files
file(GLOB SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/target/generated-sources/annotations/*.cc" "${CMAKE_CURRENT_SOURCE_DIR}/target/generated-test-sources/test-annotations/*.cc"
file(GLOB SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/target/generated-sources/annotations/*.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/target/generated-test-sources/test-annotations/*.cc"
"${CMAKE_CURRENT_SOURCE_DIR}/src/main/cpp/ffi/*.cc")
# remove auto-generated JNI code for specific method cause we have handwriting JNI code for it
list(REMOVE_ITEM SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/target/generated-sources/annotations/jni_com_alibaba_graphar_arrow_ArrowTable_Static_cxx_0x58c7409.cc")
Expand All @@ -19,19 +21,23 @@ find_package(JNI REQUIRED)
include_directories(SYSTEM ${JAVA_INCLUDE_PATH})
include_directories(SYSTEM ${JAVA_INCLUDE_PATH2})

# some JNI code depends on arrow
# some JNI code depends on arrow directly
find_package(Arrow REQUIRED)
# build graphar-cpp in specific version
include(graphar-cpp)
build_graphar_cpp()

# build the bridge JNI library
add_library(${LIBNAME} SHARED ${SOURCES})
# include graphar-cpp headers
target_include_directories(${LIBNAME} SYSTEM BEFORE PRIVATE ${GAR_INCLUDE_DIR})
# link graphar-cpp and arrow
target_link_libraries(${LIBNAME} ${CMAKE_JNI_LINKER_FLAGS} gar_shared)
target_link_libraries(${LIBNAME} ${CMAKE_JNI_LINKER_FLAGS} Arrow::arrow_static)
# build GraphAr C++ library or link system installed GraphAr C++ library
if(BUILD_GAR_CPP)
include(graphar-cpp)
build_graphar_cpp()
target_link_libraries(${LIBNAME} ${CMAKE_JNI_LINKER_FLAGS} gar_shared)
# include graphar-cpp headers
target_include_directories(${LIBNAME} SYSTEM BEFORE PRIVATE ${GAR_INCLUDE_DIR})
else()
find_package(gar REQUIRED)
target_link_libraries(${LIBNAME} ${CMAKE_JNI_LINKER_FLAGS} gar)
endif()
target_link_libraries(${LIBNAME} ${CMAKE_JNI_LINKER_FLAGS} Arrow::arrow_shared)

set_target_properties(${LIBNAME} PROPERTIES LINKER_LANGUAGE CXX)

Expand All @@ -43,4 +49,4 @@ add_custom_command(TARGET ${LIBNAME}
add_custom_command(TARGET ${LIBNAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_SOURCE_DIR}/target/native/bitcode
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_OBJECTS:${LIBNAME}> ${CMAKE_CURRENT_SOURCE_DIR}/target/native/bitcode COMMAND_EXPAND_LISTS)
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_OBJECTS:${LIBNAME}> ${CMAKE_CURRENT_SOURCE_DIR}/target/native/bitcode COMMAND_EXPAND_LISTS)
2 changes: 1 addition & 1 deletion java/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<echo message="Native Library Name: ${native.library.name}" level="info"/>

<exec executable="cmake" dir="${project.build.directory}/native" failonerror="true">
<arg line="-DCMAKE_AR=&quot;${env.LLVM11_HOME}/bin/llvm-ar&quot; -DCMAKE_RANLIB=&quot;${env.LLVM11_HOME}/bin/llvm-ranlib&quot; -DCMAKE_C_COMPILER=&quot;${env.LLVM11_HOME}/bin/clang&quot; -DCMAKE_CXX_COMPILER=&quot;${env.LLVM11_HOME}/bin/clang++&quot; -DCMAKE_CXX_FLAGS=&quot;-flto -fforce-emit-vtables&quot; ${basedir}"/>
<arg line="-DBUILD_GAR_CPP=${build_gar_cpp}; -DCMAKE_AR=&quot;${env.LLVM11_HOME}/bin/llvm-ar&quot; -DCMAKE_RANLIB=&quot;${env.LLVM11_HOME}/bin/llvm-ranlib&quot; -DCMAKE_C_COMPILER=&quot;${env.LLVM11_HOME}/bin/clang&quot; -DCMAKE_CXX_COMPILER=&quot;${env.LLVM11_HOME}/bin/clang++&quot; -DCMAKE_CXX_FLAGS=&quot;-flto -fforce-emit-vtables&quot; ${basedir}"/>
</exec>

<exec executable="make" dir="${project.build.directory}/native" failonerror="true">
Expand Down
2 changes: 1 addition & 1 deletion java/cmake/graphar-cpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function(build_graphar_cpp)
set(GAR_INCLUDE_DIR "${GAR_PREFIX}/include" CACHE INTERNAL "graphar cpp include directory")
set(GAR_BUILD_BYPRODUCTS "${GAR_SHARED_LIB}")

set(GAR_VERSION_TO_BUILD "v0.9.0")
set(GAR_VERSION_TO_BUILD "v0.10.0")

include(ExternalProject)
ExternalProject_Add(graphar_ep
Expand Down
2 changes: 1 addition & 1 deletion java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
<configuration>
<target>
<property name="compile_classpath" refid="maven.compile.classpath"/>

<property name="build_gar_cpp" value="${buildGarCPP}"/>
<ant antfile="${basedir}/build.xml">
<target name="run-llvm4jni" />
</ant>
Expand Down
99 changes: 17 additions & 82 deletions java/src/main/java/com/alibaba/graphar/edges/EdgesCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,96 +14,29 @@

package com.alibaba.graphar.edges;

import static com.alibaba.graphar.util.CppClassName.GAR_EDGES_COLLECTION;
import static com.alibaba.graphar.util.CppHeaderName.GAR_GRAPH_H;

import com.alibaba.fastffi.CXXHead;
import com.alibaba.fastffi.CXXPointer;
import com.alibaba.fastffi.CXXReference;
import com.alibaba.fastffi.CXXValue;
import com.alibaba.fastffi.FFIGen;
import com.alibaba.fastffi.FFINameAlias;
import com.alibaba.graphar.graphinfo.EdgeInfo;
import com.alibaba.graphar.graphinfo.GraphInfo;
import com.alibaba.graphar.stdcxx.StdString;
import com.alibaba.graphar.types.AdjListType;
import com.alibaba.graphar.util.Result;
import com.alibaba.fastffi.FFITypeAlias;
import java.util.Iterator;

/** EdgesCollection is designed for reading a collection of edges. */
@FFIGen
@FFITypeAlias(GAR_EDGES_COLLECTION)
@CXXHead(GAR_GRAPH_H)
public interface EdgesCollection extends CXXPointer, Iterable<Edge> {
/**
* Construct the collection for a range of edges.
*
* @param graphInfo The GraphInfo for the graph.
* @param srcLabel The source vertex label.
* @param edgeLabel The edge label.
* @param dstLabel The destination vertex label.
* @param adjListType The adjList type.
* @param vertexChunkBegin The index of the beginning vertex chunk.
* @param vertexChunkEnd The index of the end vertex chunk (not included).
* @return The constructed collection or error.
*/
static EdgesCollection create(
final GraphInfo graphInfo,
String srcLabel,
String edgeLabel,
String dstLabel,
AdjListType adjListType,
long vertexChunkBegin,
long vertexChunkEnd) {
Result<EdgeInfo> maybeEdgeInfo =
graphInfo.getEdgeInfo(
StdString.create(srcLabel),
StdString.create(edgeLabel),
StdString.create(dstLabel));
if (maybeEdgeInfo.hasError()) {
throw new RuntimeException(
"graphInfo to create EdgesCollection has error: "
+ maybeEdgeInfo.status().message().toJavaString());
}
EdgeInfo edgeInfo = maybeEdgeInfo.value();
if (!edgeInfo.containAdjList(adjListType)) {
throw new RuntimeException(
"The edge "
+ edgeLabel
+ " of adj list type "
+ adjListType
+ " doesn't exist.");
}
switch (adjListType) {
case ordered_by_source:
return EdgesCollectionOrderedBySource.factory.create(
edgeInfo, graphInfo.getPrefix(), vertexChunkBegin, vertexChunkEnd);
case ordered_by_dest:
return EdgesCollectionOrderedByDest.factory.create(
edgeInfo, graphInfo.getPrefix(), vertexChunkBegin, vertexChunkEnd);
case unordered_by_source:
return EdgesCollectionUnorderedBySource.factory.create(
edgeInfo, graphInfo.getPrefix(), vertexChunkBegin, vertexChunkEnd);
case unordered_by_dest:
return EdgesCollectionUnorderedByDest.factory.create(
edgeInfo, graphInfo.getPrefix(), vertexChunkBegin, vertexChunkEnd);
}
throw new RuntimeException("Unknown adj list type: " + adjListType);
}

/**
* Construct the collection for a range of edges.
*
* @param graphInfo The GraphInfo for the graph.
* @param srcLabel The source vertex label.
* @param edgeLabel The edge label.
* @param dstLabel The destination vertex label.
* @param adjListType The adjList type.
* @return The constructed collection or error.
*/
static EdgesCollection create(
final GraphInfo graphInfo,
String srcLabel,
String edgeLabel,
String dstLabel,
AdjListType adjListType) {
return create(graphInfo, srcLabel, edgeLabel, dstLabel, adjListType, 0, Long.MAX_VALUE);
}

/** The iterator pointing to the first edge. */
@CXXValue
EdgeIter begin();

/** The iterator pointing to the past-the-end element. */
@CXXValue
EdgeIter end();

/**
Expand All @@ -115,7 +48,8 @@ static EdgesCollection create(
* @return The new constructed iterator.
*/
@FFINameAlias("find_src")
EdgeIter findSrc(long id, EdgeIter from);
@CXXValue
EdgeIter findSrc(long id, @CXXReference EdgeIter from);

/**
* Construct and return the iterator pointing to the first incoming edge of the vertex with
Expand All @@ -126,7 +60,8 @@ static EdgesCollection create(
* @return The new constructed iterator.
*/
@FFINameAlias("find_dst")
EdgeIter findDst(long id, EdgeIter from);
@CXXValue
EdgeIter findDst(long id, @CXXReference EdgeIter from);

/** Get the number of edges in the collection. */
long size();
Expand Down

This file was deleted.

Loading

0 comments on commit 5330e93

Please sign in to comment.