Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vagetablechicken committed Jun 26, 2023
1 parent 443d0fe commit 7308e99
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 49 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion demo/cxx_quickstart/Makefile
Original file line number Diff line number Diff line change
@@ -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
9 changes: 6 additions & 3 deletions docs/zh/quickstart/sdk/cxx_sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`静态库目录。

Expand Down Expand Up @@ -135,7 +135,10 @@ t2.join();
## 编译与运行
可参考[编译Makefile](https://github.com/4paradigm/OpenMLDB/blob/main/demo/cxx_quickstart/Makefile)或直接使用以下命令,编译并运行示例代码。
```bash
gcc <user_code>.cxx -o <bin_name> -lstdc++ -std=c++17 -I<install_path>/include -L<install_path>/lib -lopenmldbsdk -lpthread
gcc <user_code>.cxx -o <bin_name> -lstdc++ -std=c++17 -I<install_path>/include -L<install_path>/lib -lopenmldbsdk -lpthread -lm -ldl -lstdc++fs
./<bin_name>
```
79 changes: 43 additions & 36 deletions src/sdk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
)

14 changes: 9 additions & 5 deletions src/sdk/openmldb_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<openmldb::sdk::SQLClusterRouter>(cluster);

Check warning on line 32 in src/sdk/openmldb_api.cc

View check run for this annotation

Codecov / codecov/patch

src/sdk/openmldb_api.cc#L28-L32

Added lines #L28 - L32 were not covered by tests
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<openmldb::sdk::SQLClusterRouter>(standalone);

Check warning on line 41 in src/sdk/openmldb_api.cc

View check run for this annotation

Codecov / codecov/patch

src/sdk/openmldb_api.cc#L37-L41

Added lines #L37 - L41 were not covered by tests
router_->Init();
}

OpenmldbHandler::~OpenmldbHandler() {
delete status_;
if (router_ != nullptr) delete router_;
OpenmldbHandler::OpenmldbHandler(std::shared_ptr<openmldb::sdk::SQLClusterRouter> router) {
status_ = new hybridse::sdk::Status();
router_ = router;

Check warning on line 47 in src/sdk/openmldb_api.cc

View check run for this annotation

Codecov / codecov/patch

src/sdk/openmldb_api.cc#L45-L47

Added lines #L45 - L47 were not covered by tests
}

OpenmldbHandler::~OpenmldbHandler() { delete status_; }

Check warning on line 50 in src/sdk/openmldb_api.cc

View check run for this annotation

Codecov / codecov/patch

src/sdk/openmldb_api.cc#L50

Added line #L50 was not covered by tests

ParameterRow::ParameterRow(const OpenmldbHandler* handler) : handler_(handler) {
parameter_types_ = std::make_shared<hybridse::sdk::ColumnTypes>();
}
Expand Down
9 changes: 5 additions & 4 deletions src/sdk/openmldb_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<openmldb::sdk::SQLClusterRouter> router);
~OpenmldbHandler();
openmldb::sdk::SQLClusterRouter* get_router() const { return router_; }
std::shared_ptr<openmldb::sdk::SQLClusterRouter> get_router() const { return router_; }

Check warning on line 170 in src/sdk/openmldb_api.h

View check run for this annotation

Codecov / codecov/patch

src/sdk/openmldb_api.h#L170

Added line #L170 was not covered by tests

// execute() is used to execute SQL statements without parameters
// first parameter : OpenmldbHandler
Expand Down Expand Up @@ -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<openmldb::sdk::SQLClusterRouter> router_ = nullptr;
std::shared_ptr<hybridse::sdk::ResultSet> resultset_last_;
};

Expand Down

0 comments on commit 7308e99

Please sign in to comment.