Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cxx sdk): support multi-thread, fix package logic and quickstart #3334

Merged
merged 19 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/cicd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ jobs:

- name: build
run: |
# even gnu binutils support ar -M script, get error `ar: BFD (GNU Binutils) 2.40 assertion fail archive.c:1813`
# brew install binutils
# export PATH="/usr/local/opt/binutils/bin:$PATH"
make build
# GitHub runner disk space is limited
# delete thirdparty build directory($ROOT/.deps/) to save disk space
Expand Down
27 changes: 9 additions & 18 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 Expand Up @@ -195,7 +196,7 @@ set(LLVM_DIR "${CMAKE_PREFIX_PATH}/lib/cmake/llvm")
find_package(LLVM REQUIRED CONFIG)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
llvm_map_components_to_libnames(LLVM_LIBS support core orcjit nativecodegen)
llvm_map_components_to_libnames(LLVM_LIBS support core irreader orcjit nativecodegen)
message(STATUS "Using LLVM components: ${LLVM_LIBS}")
add_definitions(${LLVM_DEFINITIONS})

Expand All @@ -222,6 +223,7 @@ list(
absl::random_random
absl::strings
absl::strings_internal
absl::str_format
absl::synchronization
absl::time
absl::status
Expand All @@ -238,28 +240,17 @@ if (NOT ICU_FOUND)
endif ()
message(STATUS "Found ICU Libraries: ${ICU_LIBRARIES}")

list(APPEND file_based_test_driver_LIBS
alternations
test_case_options
test_case_outputs
test_case_mode
logging
path
status
unified_diff_oss
ret_check
)
find_library(zetasql_LIB zetasql)
find_library(re2_LIB re2)
set(ZETASQL_LIBS
zetasql
${file_based_test_driver_LIBS}
${zetasql_LIB}
${ABSL_LIBS}
re2
${re2_LIB}
${ICU_LIBRARIES}
date_proto
timeofday_proto
latlng_proto
)

find_library(zookeeper_mt_LIB zookeeper_mt)

if (TCMALLOC_ENABLE)
set(CMAKE_EXE_LINKER_FLAGS "-fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free ${CMAKE_EXE_LINKER_FLAGS}")
endif()
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
7 changes: 7 additions & 0 deletions demo/cxx_quickstart/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
all: demo

demo: quickstart.cxx
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
113 changes: 113 additions & 0 deletions demo/cxx_quickstart/quickstart.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <ctime>
#include <iostream>
#include <string>
#include <thread>

#include "openmldb_api.h"

Check warning on line 6 in demo/cxx_quickstart/quickstart.cxx

View workflow job for this annotation

GitHub Actions / cpplint

[cpplint] reported by reviewdog 🐶 Include the directory when naming header files [build/include_subdir] [4] Raw Output: demo/cxx_quickstart/quickstart.cxx:6: Include the directory when naming header files [build/include_subdir] [4]
#include "sdk/result_set.h"

int main() {
// 创建并初始化 OpenmldbHandler 对象
// 单机版:参数(ip, port),如:OpenmldbHandler handler("127.0.0.1", 6527);
// 集群版:参数(ip:port, path),如:OpenmldbHandler handler("127.0.0.1:2181", "/openmldb");
// 在此以单机版为示例。
OpenmldbHandler handler("127.0.0.1:2181", "/openmldb");
if (!handler.is_connected()) {
std::cout << "connect failed" << std::endl;
return -1;
}
// 定义数据库名
std::time_t t = std::time(0);
std::string db = "test_db" + std::to_string(t);

// 创建 SQL 语句,创建数据库
std::string sql = "create database " + db + ";";
// 执行 SQL 语句,execute() 函数返回 bool 值,值为 true 表示正确执行
std::cout << handler.execute(sql);

// 创建 SQL 语句,使用数据库
sql = "use " + db + ";";
std::cout << handler.execute(sql);

// 创建 SQL 语句,创建表
sql =
"create table test_table ("
"col1 string, col2 bigint,"
"index(key=col1, ts=col2));";
std::cout << handler.execute(sql);

// 创建 SQL 语句,向表中插入行
sql = "insert test_table values(\"hello\", 1)";
std::cout << handler.execute(sql);
sql = "insert test_table values(\"Hi~\", 2)";
std::cout << handler.execute(sql);

// 普通模式
sql = "select * from test_table;";
std::cout << handler.execute(sql);

// 获得最近一次 SQL 的执行结果
auto res = handler.get_resultset();
// 输出 SQL 的执行结果
print_resultset(res);
// 本示例中输出应该为:
// +-------+--------+
// | col1 | col2 |
// +-------+--------+
// | hello | 1 |
// | Hi~ | 2 |
// +-------+---------+

// 带参模式
// SQL 语句中待填参数的位置用 ? 来表示
sql = "select * from test_table where col1 = ? ;";
// 创建 ParameterRow 对象,用于填充参数
ParameterRow para(&handler);
// 填入参数
para << "Hi~";
// 执行 SQL 语句,execute_parameterized() 函数返回 bool 值,值为 true 表示正确执行
handler.execute_parameterized(db, sql, para);
res = handler.get_resultset();
print_resultset(res);
// 本示例中输出应该为:
// +------+--------+
// | col1 | col2 |
// +------+-------+
// | Hi~ | 2 |
// +------+--------+

// 请求模式
sql =
"select col1, sum(col2) over w as w_col2_sum from test_table "
"window w as (partition by test_table.col1 order by test_table.col2 "
"rows between 2 preceding and current row);";
RequestRow req(&handler, db, sql);
req << "Hi~" << 3l;
handler.execute_request(req);
res = handler.get_resultset();
print_resultset(res);
// 本示例中输出应该为:
// +------+--------------------+
// | col1 | w_col2_sum |
// +------+--------------------+
// | Hi~ | 5 |
// +------+--------------------+

// multi thread example

OpenmldbHandler h1("127.0.0.1:2181", "/openmldb");
OpenmldbHandler h2(h1.get_router());

std::thread t1([&]() {
h1.execute("show components;");
print_resultset(h1.get_resultset());
});

std::thread t2([&]() {
h2.execute("show table status;");
print_resultset(h2.get_resultset());
});

t1.join();
t2.join();
}
Loading
Loading