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(client-cpp): add basic TableModel settings & insertRelationalTablet interface #14097

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
100 changes: 100 additions & 0 deletions example/client-cpp-example/src/SessionTableModelExample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include "Session.h"

using namespace std;

Session *session;

void insertRelationalTablet() {
vector<pair<string, TSDataType::TSDataType>> schemaList;
schemaList.push_back(make_pair("id1", TSDataType::TEXT));
schemaList.push_back(make_pair("attr1", TSDataType::TEXT));
schemaList.push_back(make_pair("m1", TSDataType::DOUBLE));
vector<ColumnCategory> columnTypes = {ColumnCategory::ID, ColumnCategory::ATTRIBUTE, ColumnCategory::MEASUREMENT};

int64_t timestamp = 0;
Tablet tablet("table1", schemaList, columnTypes, 15);

for (int row = 0; row < 15; row++) {
int rowIndex = tablet.rowSize++;
tablet.timestamps[rowIndex] = timestamp + row;
string data = "id:"; data += to_string(row);
tablet.addValue(0, rowIndex, &data);
data = "attr:"; data += to_string(row);
tablet.addValue(1, rowIndex, &data);
double value = row * 1.1;
tablet.addValue(2, rowIndex, &value);
if (tablet.rowSize == tablet.maxRowNumber) {
session->insertRelationalTablet(tablet, true);
tablet.reset();
}
}

if (tablet.rowSize != 0) {
session->insertRelationalTablet(tablet, true);
tablet.reset();
}
}

int main() {

session = new Session("127.0.0.1", 6667, "root", "root");
session->setSqlDialect("table");
session->open(false);

cout << "Create Database db1" << endl;
try {
session->createDatabase("db1");
} catch (IoTDBException &e) {
cout << e.what() << endl;
}

cout << "Use db1 as database" << endl;
session->executeNonQueryStatement("USE db1");

cout << "Create Table table1" << endl;
try {
session->executeNonQueryStatement("CREATE TABLE table1 ("
"id1 string id,"
"attr1 string attribute,"
"m1 double measurement)");
} catch (IoTDBException &e) {
cout << e.what() << endl;
}

cout << "InsertRelationalTablet" << endl;
insertRelationalTablet();

cout << "Drop Database db1" << endl;
try {
session->deleteDatabase("db1");
} catch (IoTDBException &e) {
cout << e.what() << endl;
}

cout << "session close\n" << endl;
session->close();

delete session;

cout << "finished!\n" << endl;
return 0;
}
53 changes: 51 additions & 2 deletions iotdb-client/client-cpp/src/main/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ static const int64_t QUERY_TIMEOUT_MS = -1;
LogLevelType LOG_LEVEL = LEVEL_DEBUG;

TSDataType::TSDataType getTSDataTypeFromString(const string &str) {
// BOOLEAN, INT32, INT64, FLOAT, DOUBLE, TEXT, NULLTYPE
// BOOLEAN, INT32, INT64, FLOAT, DOUBLE, TEXT, TIMESTAMP, NULLTYPE
if (str == "BOOLEAN") return TSDataType::BOOLEAN;
else if (str == "INT32") return TSDataType::INT32;
else if (str == "INT64") return TSDataType::INT64;
else if (str == "FLOAT") return TSDataType::FLOAT;
else if (str == "DOUBLE") return TSDataType::DOUBLE;
else if (str == "TEXT") return TSDataType::TEXT;
else if (str == "TIMESTAMP") return TSDataType::INT64;
else if (str == "NULLTYPE") return TSDataType::NULLTYPE;
return TSDataType::TEXT;
}
Expand Down Expand Up @@ -876,6 +877,10 @@ void Session::open(bool enableRPCCompression, int connectionTimeoutInMs) {

std::map<std::string, std::string> configuration;
configuration["version"] = getVersionString(version);
configuration["sql_dialect"] = sqlDialect;
if (database != "") {
configuration["db"] = database;
}

TSOpenSessionReq openReq;
openReq.__set_username(username);
Expand Down Expand Up @@ -1386,6 +1391,35 @@ void Session::insertTablet(Tablet &tablet, bool sorted) {
insertTablet(request);
}

void Session::insertRelationalTablet(Tablet &tablet, bool sorted) {
TSInsertTabletReq request;
buildInsertTabletReq(request, sessionId, tablet, sorted);
request.__set_writeToTable(true);
std::vector<int8_t> columnCategories;
for (auto &category: tablet.columnTypes) {
columnCategories.push_back(static_cast<int8_t>(category));
}
request.__set_columnCategories(columnCategories);
try {
TSStatus respStatus;
client->insertTablet(respStatus, request);
RpcUtils::verifySuccess(respStatus);
} catch (const TTransportException &e) {
log_debug(e.what());
throw IoTDBConnectionException(e.what());
} catch (const IoTDBException &e) {
log_debug(e.what());
throw;
} catch (const exception &e) {
log_debug(e.what());
throw IoTDBException(e.what());
}
}

void Session::insertRelationalTablet(Tablet &tablet) {
insertRelationalTablet(tablet, false);
}

void Session::insertAlignedTablet(Tablet &tablet) {
insertAlignedTablet(tablet, false);
}
Expand Down Expand Up @@ -1653,6 +1687,18 @@ void Session::deleteStorageGroups(const vector<string> &storageGroups) {
}
}

void Session::createDatabase(const string &database) {
this->setStorageGroup(database);
}

void Session::deleteDatabase(const string &database) {
this->deleteStorageGroups(vector<string>{database});
}

void Session::deleteDatabases(const vector<string> &databases) {
this->deleteStorageGroups(databases);
}

void Session::createTimeseries(const string &path,
TSDataType::TSDataType dataType,
TSEncoding::TSEncoding encoding,
Expand Down Expand Up @@ -1917,7 +1963,10 @@ void Session::executeNonQueryStatement(const string &sql) {
req.__set_timeout(0); //0 means no timeout. This value keep consistent to JAVA SDK.
TSExecuteStatementResp resp;
try {
client->executeUpdateStatement(resp, req);
client->executeUpdateStatementV2(resp, req);
if (resp.database != "") {
database = resp.database;
}
RpcUtils::verifySuccess(resp.status);
} catch (const TTransportException &e) {
log_debug(e.what());
Expand Down
48 changes: 47 additions & 1 deletion iotdb-client/client-cpp/src/main/Session.h
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,12 @@ class Field {
Field() = default;
};

enum class ColumnCategory {
ID,
MEASUREMENT,
ATTRIBUTE
};

/*
* A tablet data of one device, the tablet contains multiple measurements of this device that share
* the same time column.
Expand All @@ -560,6 +566,7 @@ class Tablet {
public:
std::string deviceId; // deviceId of this tablet
std::vector<std::pair<std::string, TSDataType::TSDataType>> schemas; // the list of measurement schemas for creating the tablet
std::vector<ColumnCategory> columnTypes; // the list of column types (used in table model)
std::vector<int64_t> timestamps; // timestamps in this tablet
std::vector<void*> values; // each object is a primitive type array, which represents values of one measurement
std::vector<BitMap> bitMaps; // each bitmap represents the existence of each value in the current column
Expand All @@ -580,6 +587,11 @@ class Tablet {
const std::vector<std::pair<std::string, TSDataType::TSDataType>> &timeseries)
: Tablet(deviceId, timeseries, DEFAULT_ROW_SIZE) {}

Tablet(const std::string &deviceId,
const std::vector<std::pair<std::string, TSDataType::TSDataType>> &timeseries,
const std::vector<ColumnCategory> &columnTypes)
: Tablet(deviceId, timeseries, columnTypes, DEFAULT_ROW_SIZE) {}

/**
* Return a tablet with the specified number of rows (maxBatchSize). Only
* call this constructor directly for testing purposes. Tablet should normally
Expand All @@ -588,10 +600,16 @@ class Tablet {
* @param deviceId the name of the device specified to be written in
* @param schemas the list of measurement schemas for creating the row
* batch
* @param columnTypes the list of column types (used in table model)
* @param maxRowNumber the maximum number of rows for this tablet
*/
Tablet(const std::string &deviceId,
const std::vector<std::pair<std::string, TSDataType::TSDataType>> &schemas,
int maxRowNumber)
: Tablet(deviceId, schemas, std::vector<ColumnCategory>(schemas.size(), ColumnCategory::MEASUREMENT), maxRowNumber) {}
Tablet(const std::string &deviceId, const std::vector<std::pair<std::string, TSDataType::TSDataType>> &schemas,
size_t maxRowNumber, bool _isAligned = false) : deviceId(deviceId), schemas(schemas),
const std::vector<ColumnCategory> columnTypes,
size_t maxRowNumber, bool _isAligned = false) : deviceId(deviceId), schemas(schemas), columnTypes(columnTypes),
maxRowNumber(maxRowNumber), isAligned(_isAligned) {
// create timestamp column
timestamps.resize(maxRowNumber);
Expand Down Expand Up @@ -973,6 +991,8 @@ class Session {
const static int DEFAULT_FETCH_SIZE = 10000;
const static int DEFAULT_TIMEOUT_MS = 0;
Version::Version version;
std::string sqlDialect = "tree"; // default sql dialect
std::string database;

private:
static bool checkSorted(const Tablet &tablet);
Expand Down Expand Up @@ -1046,6 +1066,22 @@ class Session {

~Session();

void setSqlDialect(const std::string &dialect){
this->sqlDialect = dialect;
}

void setDatabase(const std::string &database) {
this->database = database;
}

string getDatabase() {
return database;
}

void changeDatabase(string database) {
this->database = database;
}

int64_t getSessionId();

void open();
Expand Down Expand Up @@ -1124,6 +1160,10 @@ class Session {

void insertTablet(Tablet &tablet, bool sorted);

void insertRelationalTablet(Tablet &tablet);

void insertRelationalTablet(Tablet &tablet, bool sorted);

static void buildInsertTabletReq(TSInsertTabletReq &request, int64_t sessionId, Tablet &tablet, bool sorted);

void insertTablet(const TSInsertTabletReq &request);
Expand Down Expand Up @@ -1165,6 +1205,12 @@ class Session {

void deleteStorageGroups(const std::vector<std::string> &storageGroups);

void createDatabase(const std::string &database);

void deleteDatabase(const std::string &database);

void deleteDatabases(const std::vector<std::string> &databases);

void createTimeseries(const std::string &path, TSDataType::TSDataType dataType, TSEncoding::TSEncoding encoding,
CompressionType::CompressionType compressor);

Expand Down
9 changes: 9 additions & 0 deletions iotdb-client/client-cpp/src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ INCLUDE( CTest )
SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
SET(TARGET_NAME session_tests)
SET(TARGET_NAME_RELATIONAL session_relational_tests)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -g -O2")
ENABLE_TESTING()

Expand All @@ -47,18 +48,26 @@ ELSE()
ENDIF()

ADD_EXECUTABLE(${TARGET_NAME} main.cpp cpp/sessionIT.cpp)
ADD_EXECUTABLE(${TARGET_NAME_RELATIONAL} main_Relational.cpp cpp/sessionRelationalIT.cpp)

# Link with shared library iotdb_session and pthread
IF(MSVC)
TARGET_LINK_LIBRARIES(${TARGET_NAME} iotdb_session ${THRIFT_STATIC_LIB})
TARGET_LINK_LIBRARIES(${TARGET_NAME_RELATIONAL} iotdb_session ${THRIFT_STATIC_LIB})
ELSE()
TARGET_LINK_LIBRARIES(${TARGET_NAME} iotdb_session pthread)
TARGET_LINK_LIBRARIES(${TARGET_NAME_RELATIONAL} iotdb_session pthread)
ENDIF()

# Add Catch2 include directory
TARGET_INCLUDE_DIRECTORIES(${TARGET_NAME} PUBLIC ./catch2/)
TARGET_INCLUDE_DIRECTORIES(${TARGET_NAME_RELATIONAL} PUBLIC ./catch2/)

# Add 'sessionIT' to the project to be run by ctest
IF(MSVC)
ADD_TEST(NAME sessionIT CONFIGURATIONS Release COMMAND ${TARGET_NAME})
ADD_TEST(NAME sessionRelationalIT CONFIGURATIONS Release COMMAND ${TARGET_NAME_RELATIONAL})
ELSE()
ADD_TEST(NAME sessionIT COMMAND ${TARGET_NAME})
ADD_TEST(NAME sessionRelationalIT COMMAND ${TARGET_NAME_RELATIONAL})
ENDIF()
Loading
Loading