From 7308e994fb59a7df9946594e73f39d5f5d030e8a Mon Sep 17 00:00:00 2001 From: Huang Wei Date: Mon, 26 Jun 2023 17:50:55 +0800 Subject: [PATCH] fix --- CMakeLists.txt | 1 + Makefile | 3 ++ demo/cxx_quickstart/Makefile | 2 +- docs/zh/quickstart/sdk/cxx_sdk.md | 9 ++-- src/sdk/CMakeLists.txt | 79 +++++++++++++++++-------------- src/sdk/openmldb_api.cc | 14 ++++-- src/sdk/openmldb_api.h | 9 ++-- 7 files changed, 68 insertions(+), 49 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 364f5cfbfe9..d03a42d00fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -90,6 +90,7 @@ option(TESTING_ENABLE "Enable test" OFF) option(TCMALLOC_ENABLE "Enable TCMALLOC" ON) option(SQL_PYSDK_ENABLE "Enable sql pysdk" OFF) option(SQL_JAVASDK_ENABLE "Enable sql javasdk" OFF) +option(INSTALL_CXXSDK "Enable sql cxxsdk install" OFF) option(MAC_TABLET_ENABLE "Enable Table on Mac OS" ON) option(COVERAGE_ENABLE "Enable Coverage" OFF) option(COVERAGE_NO_DEPS "Coverage without test deps, should ensure test built" OFF) diff --git a/Makefile b/Makefile index b5bf51a8d96..74274755b4b 100644 --- a/Makefile +++ b/Makefile @@ -34,6 +34,9 @@ endif ifdef SQL_JAVASDK_ENABLE OPENMLDB_CMAKE_FLAGS += -DSQL_JAVASDK_ENABLE=$(SQL_JAVASDK_ENABLE) endif +ifdef INSTALL_CXXSDK + OPENMLDB_CMAKE_FLAGS += -DINSTALL_CXXSDK=$(INSTALL_CXXSDK) +endif ifdef TESTING_ENABLE OPENMLDB_CMAKE_FLAGS += -DTESTING_ENABLE=$(TESTING_ENABLE) endif diff --git a/demo/cxx_quickstart/Makefile b/demo/cxx_quickstart/Makefile index 736f7e5c37f..8925876f53a 100644 --- a/demo/cxx_quickstart/Makefile +++ b/demo/cxx_quickstart/Makefile @@ -1,7 +1,7 @@ all: demo demo: quickstart.cxx - g++ -o demo quickstart.cxx -lstdc++ -std=c++17 -I../../openmldb/include -L. -lopenmldbsdk -lpthread -lm -ldl -lstdc++fs + g++ -o demo quickstart.cxx -lstdc++ -std=c++17 -I/work/openmldb/include -L/work/openmldb/lib -lopenmldbsdk -lpthread -lm -ldl -lstdc++fs clean: rm -f demo diff --git a/docs/zh/quickstart/sdk/cxx_sdk.md b/docs/zh/quickstart/sdk/cxx_sdk.md index 56ee0fd881b..3477be369ed 100644 --- a/docs/zh/quickstart/sdk/cxx_sdk.md +++ b/docs/zh/quickstart/sdk/cxx_sdk.md @@ -7,14 +7,14 @@ C++ SDK 目前功能支持上并不完善,不支持多线程,目前仅用于 ## C++ SDK 包编译安装 ```{warning} -C++ SDK 静态库的编译目前存在问题,如果需要使用 C++ SDK 库或需要临时编译方法,请联系我们提供。 +C++ SDK 静态库仅支持Linux系统,且不在标准发布中。如果需要使用 C++ SDK 库,请源码编译,并打开编译选项`INSTALL_CXXSDK=ON`。 ``` 编译需要满足 [硬件要求](../../deploy/compile.md#硬件要求),安装 [依赖工具](../../deploy/compile.md#依赖工具)。 ```bash git clone git@github.com:4paradigm/OpenMLDB.git cd OpenMLDB -make && make install +make INSTALL_CXXSDK=ON && make install ``` 编译完成后,会在 install 目录下生成`include`头文件目录和`lib`静态库目录。 @@ -135,7 +135,10 @@ t2.join(); ## 编译与运行 +可参考[编译Makefile](https://github.com/4paradigm/OpenMLDB/blob/main/demo/cxx_quickstart/Makefile)或直接使用以下命令,编译并运行示例代码。 + ```bash -gcc .cxx -o -lstdc++ -std=c++17 -I/include -L/lib -lopenmldbsdk -lpthread +gcc .cxx -o -lstdc++ -std=c++17 -I/include -L/lib -lopenmldbsdk -lpthread -lm -ldl -lstdc++fs + ./ ``` diff --git a/src/sdk/CMakeLists.txt b/src/sdk/CMakeLists.txt index 6664b8f50d4..7ae31897d91 100644 --- a/src/sdk/CMakeLists.txt +++ b/src/sdk/CMakeLists.txt @@ -251,6 +251,19 @@ target_link_libraries(openmldb_api openmldb_sdk hybridse_flags ) +if(TESTING_ENABLE) + # load cxx sdk library built below is difficult, cuz we need more libs e.g. mini cluster/stdc++fs to build test + # so we just use lower libs to build test + # P.S. openmldb_sdk contains a lot, but it's not a good idea to use it to include mini cluster ..., we should reduce ibopenmldbsdk.a + add_executable(openmldb_api_test openmldb_api_test.cc) + target_link_libraries(openmldb_api_test ${EXPORTER_LIBS} ${GTEST_LIBRARIES}) +endif() + +if(APPLE) + message(STATUS "skip c++ sdk build") + return() +endif() + # To get the final SDK, we need add built-in libraries and third party libraries # Below, we will find and integrate these required libraries @@ -318,7 +331,7 @@ foreach(X IN LISTS ZETASQL_LIBS) endforeach() # 2. absl llvm libs(some libs needs to be linked explicitly) -set(ABSL_LLVM_LIBS ${ABSL_LIBS} ${LLVM_LIBS}) # TODO LLVM miss some libs, add them here +set(ABSL_LLVM_LIBS ${ABSL_LIBS} ${LLVM_LIBS}) # set(ABSL_LLVM_LIBS ${LLVM_LIBS}) # handle absl libs, need to recursively look up, target may need more deps foreach(X IN LISTS ABSL_LLVM_LIBS) @@ -358,46 +371,40 @@ list(APPEND TOTAL_LIBS ${CXXSDK_LIBS} ${CXXSDK_THIRDPARTY_LIBS}) list(JOIN TOTAL_LIBS "\nADDLIB " ADD_LIBS_PART) string(CONCAT ADD_LIBS "ADDLIB " ${ADD_LIBS_PART}) file(GENERATE OUTPUT cxxsdk.mri CONTENT "CREATE ${CXXSDK_NAME}\n${ADD_LIBS}\nSAVE\nEND") -add_custom_target(gen_mri DEPENDS cxxsdk.mri openmldb_api) # depends openmldb_api can't work? +add_custom_target(gen_mri DEPENDS cxxsdk.mri openmldb_api) +# ~2G, too big lib add_custom_target(pack_cxxsdk ALL COMMAND rm -f ${CXXSDK_NAME} COMMAND ar -TM < cxxsdk.mri DEPENDS gen_mri ) -if(TESTING_ENABLE) - # load cxx sdk library built before is difficult, cuz we need more libs e.g. mini cluster/stdc++fs to build test - # so we just use lower libs to build test - # P.S. openmldb_sdk contains a lot, but it's not a good idea to use it to include mini cluster ..., we should reduce ibopenmldbsdk.a - add_executable(openmldb_api_test openmldb_api_test.cc) - target_link_libraries(openmldb_api_test ${EXPORTER_LIBS} ${GTEST_LIBRARIES}) +if(INSTALL_CXXSDK) + FILE(GLOB USER_HEADER + "${PROJECT_SOURCE_DIR}/src/sdk/openmldb_api.h" + ) + FILE(GLOB USER_HEADER_SDK + "${PROJECT_SOURCE_DIR}/hybridse/include/sdk/result_set.h" + "${PROJECT_SOURCE_DIR}/hybridse/include/sdk/base_schema.h" + ) + FILE(GLOB USER_LIB + "${CMAKE_CURRENT_BINARY_DIR}/${CXXSDK_NAME}" + ) + + install( + FILES ${USER_HEADER} + DESTINATION include/ + PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ + ) + install( + FILES ${USER_HEADER_SDK} + DESTINATION include/sdk/ + PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ + ) + install( + FILES ${USER_LIB} + DESTINATION lib/ + PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ + ) endif() - -FILE(GLOB USER_HEADER - "${PROJECT_SOURCE_DIR}/src/sdk/openmldb_api.h" -) -FILE(GLOB USER_HEADER_SDK - "${PROJECT_SOURCE_DIR}/hybridse/include/sdk/result_set.h" - "${PROJECT_SOURCE_DIR}/hybridse/include/sdk/base_schema.h" -) -FILE(GLOB USER_LIB - "${CMAKE_CURRENT_BINARY_DIR}/${CXXSDK_NAME}" -) - -install( - FILES ${USER_HEADER} - DESTINATION include/ - PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ -) -install( - FILES ${USER_HEADER_SDK} - DESTINATION include/sdk/ - PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ -) -install( - FILES ${USER_LIB} - DESTINATION lib/ - PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ -) - diff --git a/src/sdk/openmldb_api.cc b/src/sdk/openmldb_api.cc index 14479d68083..af1b934f6fe 100644 --- a/src/sdk/openmldb_api.cc +++ b/src/sdk/openmldb_api.cc @@ -25,26 +25,30 @@ #include "sdk/sql_cluster_router.h" OpenmldbHandler::OpenmldbHandler(std::string _zk_cluster, std::string _zk_path) { + status_ = new hybridse::sdk::Status(); openmldb::sdk::SQLRouterOptions cluster; cluster.zk_cluster = _zk_cluster; cluster.zk_path = _zk_path; - router_ = new openmldb::sdk::SQLClusterRouter(cluster); + router_ = std::make_shared(cluster); router_->Init(); } OpenmldbHandler::OpenmldbHandler(std::string _host, uint32_t _port) { + status_ = new hybridse::sdk::Status(); openmldb::sdk::StandaloneOptions standalone; standalone.host = _host; standalone.port = _port; - router_ = new openmldb::sdk::SQLClusterRouter(standalone); + router_ = std::make_shared(standalone); router_->Init(); } -OpenmldbHandler::~OpenmldbHandler() { - delete status_; - if (router_ != nullptr) delete router_; +OpenmldbHandler::OpenmldbHandler(std::shared_ptr router) { + status_ = new hybridse::sdk::Status(); + router_ = router; } +OpenmldbHandler::~OpenmldbHandler() { delete status_; } + ParameterRow::ParameterRow(const OpenmldbHandler* handler) : handler_(handler) { parameter_types_ = std::make_shared(); } diff --git a/src/sdk/openmldb_api.h b/src/sdk/openmldb_api.h index 5ced4b774b6..20e9e3b0b64 100644 --- a/src/sdk/openmldb_api.h +++ b/src/sdk/openmldb_api.h @@ -85,6 +85,7 @@ class OpenmldbNull { OpenmldbNull() {} }; +class OpenmldbHandler; // In the request with parameter and request mode of openmldb, the position of parameters to be filled is indicated by // the symbol "?" to express @@ -164,9 +165,9 @@ class OpenmldbHandler { public: OpenmldbHandler(std::string zk_cluster, std::string zk_path); OpenmldbHandler(std::string host, uint32_t _port); - OpenmldbHandler(openmldb::sdk::SQLClusterRouter* router) { router_ = router; } + OpenmldbHandler(std::shared_ptr router); ~OpenmldbHandler(); - openmldb::sdk::SQLClusterRouter* get_router() const { return router_; } + std::shared_ptr get_router() const { return router_; } // execute() is used to execute SQL statements without parameters // first parameter : OpenmldbHandler @@ -199,8 +200,8 @@ class OpenmldbHandler { OpenmldbHandler& operator=(const OpenmldbHandler&); private: - hybridse::sdk::Status* status_ = new hybridse::sdk::Status(); - openmldb::sdk::SQLClusterRouter* router_ = nullptr; + hybridse::sdk::Status* status_; + std::shared_ptr router_ = nullptr; std::shared_ptr resultset_last_; };