Skip to content

Commit 755516f

Browse files
xiangmy21jt2594838
andauthored
refactor(client-cpp): TableModel ID->TAG, MEASUREMENT->FIELD (#14546)
* refactor(client-cpp): TableModel ID->TAG, MEASUREMENT->FIELD * refactor(client-cpp): TableModel ID->TAG, MEASUREMENT->FIELD * fix(example-cpp): fix bugs in TableModelSessionExample.cpp * fix(client-cpp): fix bugs in type convertion. --------- Co-authored-by: Jiang Tian <[email protected]>
1 parent f1199fa commit 755516f

File tree

7 files changed

+92
-79
lines changed

7 files changed

+92
-79
lines changed

example/client-cpp-example/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@
6060
<sourceFile>${project.basedir}/src/AlignedTimeseriesSessionExample.cpp</sourceFile>
6161
<destinationFile>${project.build.directory}/AlignedTimeseriesSessionExample.cpp</destinationFile>
6262
</fileSet>
63+
<fileSet>
64+
<sourceFile>${project.basedir}/src/TableModelSessionExample.cpp</sourceFile>
65+
<destinationFile>${project.build.directory}/TableModelSessionExample.cpp</destinationFile>
66+
</fileSet>
6367
<fileSet>
6468
<sourceFile>${project.basedir}/src/CMakeLists.txt</sourceFile>
6569
<destinationFile>${project.build.directory}/CMakeLists.txt</destinationFile>

example/client-cpp-example/src/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@ LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/client/lib)
3838

3939
ADD_EXECUTABLE(SessionExample SessionExample.cpp)
4040
ADD_EXECUTABLE(AlignedTimeseriesSessionExample AlignedTimeseriesSessionExample.cpp)
41+
ADD_EXECUTABLE(TableModelSessionExample TableModelSessionExample.cpp)
4142

4243
IF(MSVC)
4344
TARGET_LINK_LIBRARIES(SessionExample iotdb_session "${CMAKE_SOURCE_DIR}/thrift/lib/Release/thriftmd.lib")
4445
TARGET_LINK_LIBRARIES(AlignedTimeseriesSessionExample iotdb_session "${CMAKE_SOURCE_DIR}/thrift/lib/Release/thriftmd.lib")
46+
TARGET_LINK_LIBRARIES(TableModelSessionExample iotdb_session "${CMAKE_SOURCE_DIR}/thrift/lib/Release/thriftmd.lib")
4547
ELSE()
4648
TARGET_LINK_LIBRARIES(SessionExample iotdb_session pthread)
4749
TARGET_LINK_LIBRARIES(AlignedTimeseriesSessionExample iotdb_session pthread)
50+
TARGET_LINK_LIBRARIES(TableModelSessionExample iotdb_session pthread)
4851
ENDIF()

example/client-cpp-example/src/TableModelSessionExample.cpp

Lines changed: 62 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ void insertRelationalTablet() {
4040
ColumnCategory::TAG,
4141
ColumnCategory::TAG,
4242
ColumnCategory::ATTRIBUTE,
43-
ColumnCategory::MEASUREMENT,
44-
ColumnCategory::MEASUREMENT
43+
ColumnCategory::FIELD,
44+
ColumnCategory::FIELD
4545
};
4646

4747
Tablet tablet("table1", schemaList, columnTypes, 100);
@@ -67,129 +67,132 @@ void insertRelationalTablet() {
6767
}
6868
}
6969

70-
template<typename T>
71-
inline void Output(vector<T> &columnNames) {
72-
for (auto &name: columnNames) {
73-
cout << name << "\t";
70+
void Output(unique_ptr<SessionDataSet> &dataSet) {
71+
for (const string &name: dataSet->getColumnNames()) {
72+
cout << name << " ";
73+
}
74+
cout << endl;
75+
while (dataSet->hasNext()) {
76+
cout << dataSet->next()->toString();
77+
}
78+
cout << endl;
79+
}
80+
81+
void OutputWithType(unique_ptr<SessionDataSet> &dataSet) {
82+
for (const string &name: dataSet->getColumnNames()) {
83+
cout << name << " ";
84+
}
85+
cout << endl;
86+
for (const string &type: dataSet->getColumnTypeList()) {
87+
cout << type << " ";
88+
}
89+
cout << endl;
90+
while (dataSet->hasNext()) {
91+
cout << dataSet->next()->toString();
7492
}
7593
cout << endl;
7694
}
7795

7896
int main() {
7997
try {
80-
session = new TableSessionBuilder()
81-
.host("127.0.0.1")
82-
.rpcPort(6667)
83-
.username("root")
84-
.password("root")
85-
.build();
86-
87-
cout << "Create Database db1,db2" << endl;
98+
session = (new TableSessionBuilder())
99+
->host("127.0.0.1")
100+
->rpcPort(6667)
101+
->username("root")
102+
->password("root")
103+
->build();
104+
105+
106+
cout << "[Create Database db1,db2]\n" << endl;
88107
try {
89108
session->executeNonQueryStatement("CREATE DATABASE IF NOT EXISTS db1");
90109
session->executeNonQueryStatement("CREATE DATABASE IF NOT EXISTS db2");
91110
} catch (IoTDBException &e) {
92111
cout << e.what() << endl;
93112
}
94113

95-
cout << "Use db1 as database" << endl;
114+
cout << "[Use db1 as database]\n" << endl;
96115
try {
97116
session->executeNonQueryStatement("USE db1");
98117
} catch (IoTDBException &e) {
99118
cout << e.what() << endl;
100119
}
101120

102-
cout << "Create Table table1,table2" << endl;
121+
cout << "[Create Table table1,table2]\n" << endl;
103122
try {
104-
session->executeNonQueryStatement(" db1.table1(region_id STRING TAG, plant_id STRING TAG, device_id STRING TAG, model STRING ATTRIBUTE, temperature FLOAT FIELD, humidity DOUBLE FIELD) with (TTL=3600000)");
123+
session->executeNonQueryStatement("create table db1.table1(region_id STRING TAG, plant_id STRING TAG, device_id STRING TAG, model STRING ATTRIBUTE, temperature FLOAT FIELD, humidity DOUBLE FIELD) with (TTL=3600000)");
105124
session->executeNonQueryStatement("create table db2.table2(region_id STRING TAG, plant_id STRING TAG, color STRING ATTRIBUTE, temperature FLOAT FIELD, speed DOUBLE FIELD) with (TTL=6600000)");
106125
} catch (IoTDBException &e) {
107126
cout << e.what() << endl;
108127
}
109128

110-
cout << "Show Tables" << endl;
129+
cout << "[Show Tables]\n" << endl;
111130
try {
112-
SessionDataSet dataSet = session->executeQueryStatement("SHOW TABLES");
113-
Output(dataSet.getColumnNames());
114-
while(dataSet.hasNext()) {
115-
Output(dataSet.next());
116-
}
131+
unique_ptr<SessionDataSet> dataSet = session->executeQueryStatement("SHOW TABLES");
132+
Output(dataSet);
117133
} catch (IoTDBException &e) {
118134
cout << e.what() << endl;
119135
}
120136

121-
cout << "Show tables from specific database" << endl;
137+
cout << "[Show tables from specific database]\n" << endl;
122138
try {
123-
SessionDataSet dataSet = session->executeQueryStatement("SHOW TABLES FROM db1");
124-
Output(dataSet.getColumnNames());
125-
while(dataSet.hasNext()) {
126-
Output(dataSet.next());
127-
}
139+
unique_ptr<SessionDataSet> dataSet = session->executeQueryStatement("SHOW TABLES FROM db1");
140+
Output(dataSet);
128141
} catch (IoTDBException &e) {
129142
cout << e.what() << endl;
130143
}
131144

132-
cout << "InsertTablet" << endl;
145+
cout << "[InsertTablet]\n" << endl;
133146
try {
134147
insertRelationalTablet();
135148
} catch (IoTDBException &e) {
136149
cout << e.what() << endl;
137150
}
138151

139-
cout << "Query Table Data" << endl;
152+
cout << "[Query Table Data]\n" << endl;
140153
try {
141-
SessionDataSet dataSet = session->executeQueryStatement("SELECT * FROM table1"
154+
unique_ptr<SessionDataSet> dataSet = session->executeQueryStatement("SELECT * FROM table1"
142155
" where region_id = '1' and plant_id in ('3', '5') and device_id = '3'");
143-
Output(dataSet.getColumnNames());
144-
Output(dataSet.getColumnTypeList());
145-
while(dataSet.hasNext()) {
146-
Output(dataSet.next());
147-
}
156+
OutputWithType(dataSet);
148157
} catch (IoTDBException &e) {
149158
cout << e.what() << endl;
150159
}
151160

152161
session->close();
153162

154163
// specify database in constructor
155-
session = new TableSessionBuilder()
156-
.host("127.0.0.1")
157-
.rpcPort(6667)
158-
.username("root")
159-
.password("root")
160-
.database("db1")
161-
.build();
162-
163-
cout << "Show tables from current database(db1)" << endl;
164+
session = (new TableSessionBuilder())
165+
->host("127.0.0.1")
166+
->rpcPort(6667)
167+
->username("root")
168+
->password("root")
169+
->database("db1")
170+
->build();
171+
172+
cout << "[Show tables from current database(db1)]\n" << endl;
164173
try {
165-
SessionDataSet dataSet = session->executeQueryStatement("SHOW TABLES");
166-
Output(dataSet.getColumnNames());
167-
while(dataSet.hasNext()) {
168-
Output(dataSet.next());
169-
}
174+
unique_ptr<SessionDataSet> dataSet = session->executeQueryStatement("SHOW TABLES");
175+
Output(dataSet);
170176
} catch (IoTDBException &e) {
171177
cout << e.what() << endl;
172178
}
173179

174-
cout << "Change database to db2" << endl;
180+
cout << "[Change database to db2]\n" << endl;
175181
try {
176182
session->executeNonQueryStatement("USE db2");
177183
} catch (IoTDBException &e) {
178184
cout << e.what() << endl;
179185
}
180186

181-
cout << "Show tables from current database(db2)" << endl;
187+
cout << "[Show tables from current database(db2)]\n" << endl;
182188
try {
183-
SessionDataSet dataSet = session->executeQueryStatement("SHOW TABLES");
184-
Output(dataSet.getColumnNames());
185-
while(dataSet.hasNext()) {
186-
Output(dataSet.next());
187-
}
189+
unique_ptr<SessionDataSet> dataSet = session->executeQueryStatement("SHOW TABLES");
190+
Output(dataSet);
188191
} catch (IoTDBException &e) {
189192
cout << e.what() << endl;
190193
}
191194

192-
cout << "Drop Database db1,db2" << endl;
195+
cout << "[Drop Database db1,db2]\n" << endl;
193196
try {
194197
session->executeNonQueryStatement("DROP DATABASE db1");
195198
session->executeNonQueryStatement("DROP DATABASE db2");

iotdb-client/client-cpp/src/main/Session.h

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -547,13 +547,13 @@ class Field {
547547
};
548548

549549
enum class ColumnCategory {
550-
ID,
551-
MEASUREMENT,
550+
TAG,
551+
FIELD,
552552
ATTRIBUTE
553553
};
554554

555555
template<typename T, typename Target>
556-
const Target* safe_cast(const T& value) {
556+
void safe_cast(const T& value, Target &target) {
557557
/*
558558
Target Allowed Source Types
559559
BOOLEAN BOOLEAN
@@ -564,24 +564,27 @@ const Target* safe_cast(const T& value) {
564564
TEXT TEXT
565565
*/
566566
if (std::is_same<Target, T>::value) {
567-
return (Target*)&value;
567+
target = *(Target*)&value;
568+
} else if (std::is_same<Target, string>::value && std::is_array<T>::value && std::is_same<char, typename std::remove_extent<T>::type>::value) {
569+
string tmp((const char*)&value);
570+
target = *(Target*)&tmp;
568571
} else if (std::is_same<Target, int64_t>::value && std::is_same<T, int32_t>::value) {
569572
int64_t tmp = *(int32_t*)&value;
570-
return (Target*)&tmp;
573+
target = *(Target*)&tmp;
571574
} else if (std::is_same<Target, float>::value && std::is_same<T, int32_t>::value) {
572575
float tmp = *(int32_t*)&value;
573-
return (Target*)&tmp;
576+
target = *(Target*)&tmp;
574577
} else if (std::is_same<Target, double>::value && std::is_same<T, int32_t>::value) {
575578
double tmp = *(int32_t*)&value;
576-
return (Target*)&tmp;
579+
target = *(Target*)&tmp;
577580
} else if (std::is_same<Target, double>::value && std::is_same<T, int64_t>::value) {
578581
double tmp = *(int64_t*)&value;
579-
return (Target*)&tmp;
582+
target = *(Target*)&tmp;
580583
} else if (std::is_same<Target, double>::value && std::is_same<T, float>::value) {
581584
double tmp = *(float*)&value;
582-
return (Target*)&tmp;
585+
target = *(Target*)&tmp;
583586
} else {
584-
throw UnSupportedDataTypeException("Parameter type " +
587+
throw UnSupportedDataTypeException("Error: Parameter type " +
585588
std::string(typeid(T).name()) + " cannot be converted to DataType" +
586589
std::string(typeid(Target).name()));
587590
}
@@ -652,7 +655,7 @@ class Tablet {
652655
Tablet(const std::string &deviceId,
653656
const std::vector<std::pair<std::string, TSDataType::TSDataType>> &schemas,
654657
int maxRowNumber)
655-
: Tablet(deviceId, schemas, std::vector<ColumnCategory>(schemas.size(), ColumnCategory::MEASUREMENT), maxRowNumber) {}
658+
: Tablet(deviceId, schemas, std::vector<ColumnCategory>(schemas.size(), ColumnCategory::FIELD), maxRowNumber) {}
656659
Tablet(const std::string &deviceId, const std::vector<std::pair<std::string, TSDataType::TSDataType>> &schemas,
657660
const std::vector<ColumnCategory> columnTypes,
658661
size_t maxRowNumber, bool _isAligned = false) : deviceId(deviceId), schemas(schemas), columnTypes(columnTypes),
@@ -699,27 +702,27 @@ class Tablet {
699702
TSDataType::TSDataType dataType = schemas[schemaId].second;
700703
switch (dataType) {
701704
case TSDataType::BOOLEAN: {
702-
((bool*)values[schemaId])[rowIndex] = *safe_cast<T, bool>(value);
705+
safe_cast<T, bool>(value, ((bool*)values[schemaId])[rowIndex]);
703706
break;
704707
}
705708
case TSDataType::INT32: {
706-
((int*)values[schemaId])[rowIndex] = *safe_cast<T, int>(value);
709+
safe_cast<T, int>(value, ((int*)values[schemaId])[rowIndex]);
707710
break;
708711
}
709712
case TSDataType::INT64: {
710-
((int64_t*)values[schemaId])[rowIndex] = *safe_cast<T, int64_t>(value);
713+
safe_cast<T, int64_t>(value, ((int64_t*)values[schemaId])[rowIndex]);
711714
break;
712715
}
713716
case TSDataType::FLOAT: {
714-
((float*)values[schemaId])[rowIndex] = *safe_cast<T, float>(value);
717+
safe_cast<T, float>(value, ((float*)values[schemaId])[rowIndex]);
715718
break;
716719
}
717720
case TSDataType::DOUBLE: {
718-
((double*)values[schemaId])[rowIndex] = *safe_cast<T, double>(value);
721+
safe_cast<T, double>(value, ((double*)values[schemaId])[rowIndex]);
719722
break;
720723
}
721724
case TSDataType::TEXT: {
722-
((string*)values[schemaId])[rowIndex] = *safe_cast<T, string>(value);
725+
safe_cast<T, string>(value, ((string*)values[schemaId])[rowIndex]);
723726
break;
724727
}
725728
default:

iotdb-client/client-cpp/src/main/TableSession.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
#include "TableSession.h"
2323

24-
void TableSession::insert(Tablet &tablet, bool sorted = false) {
24+
void TableSession::insert(Tablet &tablet, bool sorted) {
2525
session->insertRelationalTablet(tablet, sorted);
2626
}
2727
void TableSession::executeNonQueryStatement(const string &sql) {

iotdb-client/client-cpp/src/main/TableSession.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class TableSession {
3131
TableSession(Session* session) {
3232
this->session = session;
3333
}
34-
void insert(Tablet& tablet, bool sorted);
34+
void insert(Tablet& tablet, bool sorted = false);
3535
void executeNonQueryStatement(const std::string& sql);
3636
unique_ptr<SessionDataSet> executeQueryStatement(const std::string& sql);
3737
unique_ptr<SessionDataSet> executeQueryStatement(const std::string& sql, int64_t timeoutInMs);

iotdb-client/client-cpp/src/test/cpp/sessionRelationalIT.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ TEST_CASE("Test insertRelationalTablet", "[testInsertRelationalTablet]") {
7878
schemaList.push_back(make_pair("tag1", TSDataType::TEXT));
7979
schemaList.push_back(make_pair("attr1", TSDataType::TEXT));
8080
schemaList.push_back(make_pair("m1", TSDataType::DOUBLE));
81-
vector<ColumnCategory> columnTypes = {ColumnCategory::ID, ColumnCategory::ATTRIBUTE, ColumnCategory::MEASUREMENT};
81+
vector<ColumnCategory> columnTypes = {ColumnCategory::TAG, ColumnCategory::ATTRIBUTE, ColumnCategory::FIELD};
8282

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

0 commit comments

Comments
 (0)