Appearance
C++ 接口使用介绍
基于 C ABI(Application Binary Interface)规范的动态库实现。
创建连接
cpp
const char* ip = "192.168.2.50";
int port = 19504;
int timeout = 30;
const char* user = "root";
const char* password = "root";
const char* database = "db"; //default database
LightningDB ldb = init(ip, port, timeout, user, password, database);
if (!ldb) {
fprintf(stderr, "init error");
return 0;
}
使用 memCache 碎片化写入数据
适用于数据批量较小、频率较高的场景,如:数据采集、数据上报等
cpp
// use database
use_database(ldb, "magus_abc");
// new row table
LdbTable rowTable = new_table("dd_tt", TtTABLE);
add_column(rowTable, "IsArray", vtDOUBLE);
add_column(rowTable, "Quality", vtDOUBLE);
add_column(rowTable, "TagValue", vtDOUBLE);
add_column(rowTable, "UaDataType", vtDOUBLE);
add_column(rowTable, "TagStringValue", vtSTRING);
add_column(rowTable, "HostNameTag", vtSTRING);
add_column(rowTable, "e_date", vtDATETIME);
int64_t now = time(NULL) * 1e9;
char buf[128] = { 0 };
for (int i = 1; i <= 20; i++) {
sprintf(buf, "ud %d\n", i);
bind_double(rowTable, 0, i * 1.22);
bind_double(rowTable, 1, i * 3.22);
bind_double(rowTable, 2, i * 4.22);
bind_double(rowTable, 3, i * 5.22);
bind_string(rowTable, 4, buf);
bind_string(rowTable, 5, buf);
bind_datetime(rowTable, 6, now);
bind_row(rowTable);
}
uint32_t timeout = 2000; //ms
LdbDataset res = insert_realtime(ldb, rowTable, timeout);
if (!get_errno(res)) {
printf("insert success\n");
}
else {
printf("insert faild,%s\n", get_error(res));
}
free_table(rowTable);
free_response(res);
使用事务 TX 批量写入数据
适用于大批量数据写入场景,如:数据迁移、数据初始化等
cpp
// use database
use_database(ldb, "magus_abc");
// new sheet table
LdbTable sheetTable = new_table("dd_tt", TtSHEET);
add_column(sheetTable, "IsArray", vtDOUBLE);
add_column(sheetTable, "Quality", vtDOUBLE);
add_column(sheetTable, "TagValue", vtDOUBLE);
add_column(sheetTable, "UaDataType", vtDOUBLE);
add_column(sheetTable, "TagStringValue", vtSTRING);
add_column(sheetTable, "HostNameTag", vtSTRING);
add_column(sheetTable, "e_date", vtDATETIME);
// start transaction
LdbResultSet res = start_transaction(ldb);
if (!res) {
printf("start_transaction error\n");
free_table(sheetTable);
return;
}
int errno_ = get_errno(res);
if (errno_ != 0) {
const char* err = get_error(res);
printf("start_transaction error msg:%s\n", err);
free_response(res);
return;
}
const char* tx_id = get_tx_id(res);
free_response(res);
// bind sheet sheetTable
int total = 20;
double* isArray = (double*)malloc(total * sizeof(double));
double* quality = (double*)malloc(total * sizeof(double));
double* tagValue = (double*)malloc(total * sizeof(double));
double* uaDataType = (double*)malloc(total * sizeof(double));
char** tagStringValue = (char**)malloc(total * sizeof(char*));
char** hostNameTag = (char**)malloc(total * sizeof(char*));
int64_t* eDate = (int64_t*)malloc(total * sizeof(int64_t));
char buf[256] = { 0 };
uint64_t now = GetCurrentTimerMS() * 1e6; // nanosecond
for (int i = 0; i < total; i++) {
isArray[i] = i * 1.22;
quality[i] = i * 2.22;
tagValue[i] = i * 3.22;
uaDataType[i] = i * 4.22;
sprintf(buf, "ud %d\n", i);
tagStringValue[i] = strdup(buf);
hostNameTag[i] = strdup(buf);
eDate[i] = now;
}
set_values_double(sheetTable, 0, isArray, total, NULL, 0);
set_values_double(sheetTable, 1, quality, total, NULL, 0);
set_values_double(sheetTable, 2, tagValue, total, NULL, 0);
set_values_double(sheetTable, 3, uaDataType, total, NULL, 0);
set_values_string(sheetTable, 4, tagStringValue, total, NULL, 0);
set_values_string(sheetTable, 5, hostNameTag, total, NULL, 0);
set_values_datetime(sheetTable, 6, eDate, total, NULL, 0);
free(isArray);
free(quality);
free(tagValue);
free(uaDataType);
for (int i = 0; i < total; i++) {
free(tagStringValue[i]);
free(hostNameTag[i]);
}
free(tagStringValue);
free(hostNameTag);
free(eDate);
// put data
res = put_data(ldb, sheetTable);
if (!res) {
printf("put_data error\n");
free_table(sheetTable);
return;
}
errno_ = get_errno(res);
if (errno_ != 0) {
const char* err = get_error(res);
printf("put_data error msg:%s\n", err);
free_table(sheetTable);
free_response(res);
return;
}
free_table(sheetTable);
free_response(res);
// commit
res = commit(ldb, tx_id);
if (!res) {
printf("commit error\n");
return;
}
errno_ = get_errno(res);
if (errno_ != 0) {
const char* err = get_error(res);
printf("commit error msg:%s\n", err);
free_response(res);
return;
}
free_response(res);
// commit error, recommit or rollback
if (errno_ != 0) {
res = rollback(ldb);
if (!res) {
printf("rollback error\n");
return;
}
int errno_ = get_errno(res);
if (errno_ != 0) {
const char* err = get_error(res);
printf("rollback error msg:%s\n", err);
free_response(res);
return;
}
free_response(res);
}
执行 SQL 语句
sql 遵循标准的 SQL 语法规范(MySQL),支持查询、更新、删除等操作
sql 示例
- 建库 sql 示例sql
CREATE DATABASE IF NOT EXISTS magus_abc
- 建表 sql 示例sql
CREATE TABLE dd_tt ( IsArray VtDouble not null default 0.0, Quality VtDouble not null default 0.0, TagValue VtDouble not null default 0.0, UaDataType VtDouble not null default 0.0, TagStringValue VtString not null default '', HostNameTag VtString not null default '' keyCol, e_date VtDateTime not null DEFAULT '2000-01-01 00:00:00' timeCol )
- 更新数据 sql 示例sql
update dd_tt set Quality =1 where HostNameTag ="zs"
- 删除数据 sql 示例sql
delete from dd_tt where e_date < '2024-02-01'
- 查询数据 sql 示例sql
select * from db.dd_tt
代码示例
cpp
const char* sql = "select * from db.dd_tt";
int timeout = 30; // seconds
LdbResultSet res = exec(ldb, sql, timeout);
if (!res) {
fprintf(stderr, "exec error");
return;
}
LdbDataset dataset = get_dataset(res);
if (!dataset) {
fprintf(stderr, "exec get_dataset error");
return;
}
LdbTable table = get_table(dataset);
int col_count = column_count(table);
for (int i = 0; i < col_count; i++) {
int typ = column_type(table, i);
const char* col_name = column_name(table, i);
printf("col name:%s,type:%d\t", col_name, typ);
}
printf("\n");
int64_t i_val = 0,
int row_count = 0;
double d_val = 0;
const char* s_val = "";
const unsigned char* b_val;
int binary_len = -1;
int is_null = 0;
string rv = "";
while (next(dataset))
{
for (int i = 0; i < col_count; i++) {
int typ = column_type(table, i);
switch (typ)
{
case vtBOOL:
case vtINT8:
case vtINT16:
case vtINT32:
case vtINT64:
case vtDATETIME:
i_val = column_int(dataset, i, &is_null);
if (is_null) {
rv += "null,";
}
else
{
rv += to_string(i_val) + ",";
}
break;
case vtFLOAT:
case vtDOUBLE:
d_val = column_double(dataset, i, &is_null);
if (is_null) {
rv += "null,";
}
else
{
rv += to_string(d_val) + ",";
}
break;
case vtSTRING:
s_val = column_string(dataset, i);
rv += string(s_val) + ",";
break;
case vtBINARY:
b_val = column_binary(dataset, i, &binary_len);
if (b_val && binary_len > 0) {
int len = 2 + (binary_len << 1) + 1; // 0xAAAA\0
char* dst = new char[len];
dst[0] = '0';
dst[1] = 'x';
octet2hex(dst + 2, (unsigned char*)b_val, binary_len);
printf("%s\n", dst);
delete dst;
}
rv += string(s_val) + ",";
break;
default:
printf("not support type:%d\n", typ);
break;
}
}
if (row_count % 1000 == 0) {
printf("%s\n", rv.c_str());
}
rv = "";
row_count++;
}
printf("row count:%d\n", row_count);
free_table(table);
free_dataset(dataset);
free_response(res);