From dfe6ea8b88e68b5660a01b07aa0c795406e3ea41 Mon Sep 17 00:00:00 2001 From: 761417898 <761417898@qq.com> Date: Tue, 21 Apr 2026 19:04:17 +0800 Subject: [PATCH 1/8] cpp client code format --- .clang-format | 15 + .github/workflows/client-cpp-package.yml | 21 +- .github/workflows/multi-language-client.yml | 22 +- CONTRIBUTING.md | 25 + .../src/AlignedTimeseriesSessionExample.cpp | 619 +- .../src/MultiSvrNodeClient.cpp | 181 +- .../client-cpp-example/src/SessionExample.cpp | 656 +- .../src/TableModelSessionExample.cpp | 338 +- iotdb-client/client-cpp/README.md | 32 + .../src/main/AbstractSessionBuilder.h | 58 +- iotdb-client/client-cpp/src/main/Column.cpp | 463 +- iotdb-client/client-cpp/src/main/Column.h | 387 +- .../client-cpp/src/main/ColumnDecoder.cpp | 265 +- .../client-cpp/src/main/ColumnDecoder.h | 30 +- iotdb-client/client-cpp/src/main/Common.cpp | 602 +- iotdb-client/client-cpp/src/main/Common.h | 576 +- iotdb-client/client-cpp/src/main/DeviceID.h | 202 +- .../client-cpp/src/main/IoTDBRpcDataSet.cpp | 799 +- .../client-cpp/src/main/IoTDBRpcDataSet.h | 218 +- .../client-cpp/src/main/NodesSupplier.cpp | 215 +- .../client-cpp/src/main/NodesSupplier.h | 155 +- iotdb-client/client-cpp/src/main/Session.cpp | 3664 ++- iotdb-client/client-cpp/src/main/Session.h | 1534 +- .../client-cpp/src/main/SessionBuilder.h | 151 +- .../client-cpp/src/main/SessionConnection.cpp | 989 +- .../client-cpp/src/main/SessionConnection.h | 462 +- .../client-cpp/src/main/SessionDataSet.cpp | 309 +- .../client-cpp/src/main/SessionDataSet.h | 156 +- .../client-cpp/src/main/TableSession.cpp | 23 +- .../client-cpp/src/main/TableSession.h | 25 +- .../client-cpp/src/main/TableSessionBuilder.h | 103 +- .../client-cpp/src/main/ThriftConnection.cpp | 244 +- .../client-cpp/src/main/ThriftConnection.h | 64 +- iotdb-client/client-cpp/src/main/TsBlock.cpp | 125 +- iotdb-client/client-cpp/src/main/TsBlock.h | 40 +- .../client-cpp/src/test/catch2/catch.hpp | 26357 ++++++++-------- .../client-cpp/src/test/cpp/sessionIT.cpp | 1533 +- .../src/test/cpp/sessionRelationalIT.cpp | 415 +- iotdb-client/client-cpp/src/test/main.cpp | 36 +- .../client-cpp/src/test/main_Relational.cpp | 29 +- pom.xml | 17 +- 41 files changed, 21183 insertions(+), 20972 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000000000..e1746f669d43c --- /dev/null +++ b/.clang-format @@ -0,0 +1,15 @@ +--- +BasedOnStyle: LLVM +IndentWidth: 2 +TabWidth: 2 +UseTab: Never +ColumnLimit: 100 +BreakBeforeBraces: Attach +AllowShortIfStatementsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Empty +PointerAlignment: Left +DerivePointerAlignment: false +SortIncludes: false +ReflowComments: false +Standard: c++11 +... diff --git a/.github/workflows/client-cpp-package.yml b/.github/workflows/client-cpp-package.yml index c6412b001e7f5..5713dd06335cb 100644 --- a/.github/workflows/client-cpp-package.yml +++ b/.github/workflows/client-cpp-package.yml @@ -87,7 +87,8 @@ jobs: run: | set -euxo pipefail sudo apt-get update - sudo apt-get install -y libboost-all-dev openssl libssl-dev + sudo apt-get install -y libboost-all-dev openssl libssl-dev clang-format-17 + clang-format-17 --version - name: Cache Maven packages uses: actions/cache@v5 with: @@ -99,9 +100,7 @@ jobs: shell: bash run: | set -euxo pipefail - ./mvnw clean package -P with-cpp -pl iotdb-client/client-cpp -am \ - -DskipTests \ - -Dspotless.check.skip=true -Dspotless.apply.skip=true + ./mvnw clean package -P with-cpp -pl iotdb-client/client-cpp -am -DskipTests - name: Upload zip artifact uses: actions/upload-artifact@v6 with: @@ -133,7 +132,9 @@ jobs: shell: bash run: | set -euxo pipefail - brew install boost openssl + brew install boost openssl llvm@17 + echo "$(brew --prefix llvm@17)/bin" >> "$GITHUB_PATH" + clang-format --version - name: Cache Maven packages uses: actions/cache@v5 with: @@ -147,9 +148,7 @@ jobs: MACOSX_DEPLOYMENT_TARGET: "12.0" run: | set -euxo pipefail - ./mvnw clean package -P with-cpp -pl iotdb-client/client-cpp -am \ - -DskipTests \ - -Dspotless.check.skip=true -Dspotless.apply.skip=true + ./mvnw clean package -P with-cpp -pl iotdb-client/client-cpp -am -DskipTests - name: Upload zip artifact uses: actions/upload-artifact@v6 with: @@ -194,6 +193,8 @@ jobs: $sslPath = (Get-ChildItem 'C:\Program Files\OpenSSL*' -Directory | Select-Object -First 1).FullName echo "$sslPath\bin" >> $env:GITHUB_PATH echo "OPENSSL_ROOT_DIR=$sslPath" >> $env:GITHUB_ENV + choco install llvm --version=17.0.6 -y + clang-format --version - name: Cache Maven packages uses: actions/cache@v5 with: @@ -208,9 +209,7 @@ jobs: IOTDB_TOOLS_THRIFT_VERSION: ${{ matrix.iotdb_tools_thrift_version }} run: | set -euxo pipefail - MVN_ARGS=(./mvnw clean package -P with-cpp -pl iotdb-client/client-cpp -am \ - -DskipTests \ - -Dspotless.check.skip=true -Dspotless.apply.skip=true) + MVN_ARGS=(./mvnw clean package -P with-cpp -pl iotdb-client/client-cpp -am -DskipTests) if [ -n "${CMAKE_GENERATOR:-}" ]; then MVN_ARGS+=("-Dcmake.generator=${CMAKE_GENERATOR}") fi diff --git a/.github/workflows/multi-language-client.yml b/.github/workflows/multi-language-client.yml index 831e3e37639b6..0d3fd2f5d3cf9 100644 --- a/.github/workflows/multi-language-client.yml +++ b/.github/workflows/multi-language-client.yml @@ -55,15 +55,18 @@ jobs: shell: bash run: | sudo apt-get update - sudo apt-get install libboost-all-dev - sudo apt-get install openssl libssl-dev + sudo apt-get install -y libboost-all-dev + sudo apt-get install -y openssl libssl-dev clang-format-17 + clang-format-17 --version - name: Install CPP Dependencies (Mac) # remove some xcode to release disk space if: runner.os == 'macOS' shell: bash run: | brew install boost - brew install openssl + brew install openssl llvm@17 + echo "$(brew --prefix llvm@17)/bin" >> "$GITHUB_PATH" + clang-format --version sudo rm -rf /Applications/Xcode_14.3.1.app sudo rm -rf /Applications/Xcode_15.0.1.app sudo rm -rf /Applications/Xcode_15.1.app @@ -72,21 +75,28 @@ jobs: - name: Install CPP Dependencies (Windows) if: runner.os == 'Windows' run: | - choco install winflexbison3 - choco install boost-msvc-14.3 + choco install winflexbison3 -y + choco install boost-msvc-14.3 -y $boost_path = (Get-ChildItem -Path 'C:\local\' -Filter 'boost_*').FullName echo $boost_path >> $env:GITHUB_PATH - choco install openssl + choco install openssl -y $sslPath = (Get-ChildItem 'C:\Program Files\OpenSSL*' -Directory | Select-Object -First 1).FullName echo "$sslPath\bin" >> $env:GITHUB_PATH echo "OPENSSL_ROOT_DIR=$sslPath" >> $env:GITHUB_ENV + choco install llvm --version=17.0.6 -y + clang-format --version - name: Cache Maven packages uses: actions/cache@v5 with: path: ~/.m2 key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} restore-keys: ${{ runner.os }}-m2- + - name: Check C++ format (Spotless) + shell: bash + run: | + ./mvnw -P with-cpp -pl iotdb-client/client-cpp spotless:check + ./mvnw -P with-cpp -pl example/client-cpp-example spotless:check - name: Build IoTDB server shell: bash run: ./mvnw clean install -pl distribution -am -DskipTests diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a80f5284aad76..100973db13844 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -107,6 +107,31 @@ plugin](https://github.com/diffplug/spotless/tree/main/plugin-maven) together wi import static all other imports ``` +### C++ formatting + +- Formatter: `clang-format` only +- Version: `17.0.6` (must match CI) +- Entrypoint: Maven Spotless + +Check only: + +`./mvnw -P with-cpp -pl iotdb-client/client-cpp spotless:check` + +`./mvnw -P with-cpp -pl example/client-cpp-example spotless:check` + +Auto-fix: + +`./mvnw -P with-cpp -pl iotdb-client/client-cpp spotless:apply` + +`./mvnw -P with-cpp -pl example/client-cpp-example spotless:apply` + +On Windows PowerShell, a comma inside `-pl` can be parsed incorrectly; use the two commands above, or quote the full `-pl` value, for example `./mvnw -P with-cpp "-pl=iotdb-client/client-cpp,example/client-cpp-example" spotless:check`. + +Temporarily skip Spotless (not recommended except emergency CI triage): + +`-Dspotless.skip=true` + +Risk: skipping formatting checks can introduce style drift between contributors and CI, and often causes follow-up format-only commits. ## Contributing code diff --git a/example/client-cpp-example/src/AlignedTimeseriesSessionExample.cpp b/example/client-cpp-example/src/AlignedTimeseriesSessionExample.cpp index 2cc04ab3b3946..3675525301ac9 100644 --- a/example/client-cpp-example/src/AlignedTimeseriesSessionExample.cpp +++ b/example/client-cpp-example/src/AlignedTimeseriesSessionExample.cpp @@ -21,393 +21,396 @@ using namespace std; -Session *session; +Session* session; #define DEFAULT_ROW_NUMBER 1000000 void createAlignedTimeseries() { - string alignedDeviceId = "root.sg1.d1"; - vector measurements = {"s1", "s2", "s3"}; - vector alignedTimeseries = {"root.sg1.d1.s1", "root.sg1.d1.s2", "root.sg1.d1.s3"}; - vector dataTypes = {TSDataType::INT32, TSDataType::DOUBLE, TSDataType::BOOLEAN}; - vector encodings = {TSEncoding::PLAIN, TSEncoding::GORILLA, TSEncoding::RLE}; - vector compressors = { - CompressionType::SNAPPY, CompressionType::UNCOMPRESSED, CompressionType::SNAPPY}; - for (const string ×eries: alignedTimeseries) { - if (session->checkTimeseriesExists(timeseries)) { - session->deleteTimeseries(timeseries); - } + string alignedDeviceId = "root.sg1.d1"; + vector measurements = {"s1", "s2", "s3"}; + vector alignedTimeseries = {"root.sg1.d1.s1", "root.sg1.d1.s2", "root.sg1.d1.s3"}; + vector dataTypes = {TSDataType::INT32, TSDataType::DOUBLE, + TSDataType::BOOLEAN}; + vector encodings = {TSEncoding::PLAIN, TSEncoding::GORILLA, + TSEncoding::RLE}; + vector compressors = { + CompressionType::SNAPPY, CompressionType::UNCOMPRESSED, CompressionType::SNAPPY}; + for (const string& timeseries : alignedTimeseries) { + if (session->checkTimeseriesExists(timeseries)) { + session->deleteTimeseries(timeseries); } - session->createAlignedTimeseries(alignedDeviceId, measurements, dataTypes, encodings, compressors); + } + session->createAlignedTimeseries(alignedDeviceId, measurements, dataTypes, encodings, + compressors); } void createSchemaTemplate() { - if (!session->checkTemplateExists("template1")) { - Template temp("template1", false); + if (!session->checkTemplateExists("template1")) { + Template temp("template1", false); - InternalNode iNodeD99("d99", true); + InternalNode iNodeD99("d99", true); - MeasurementNode mNodeS1("s1", TSDataType::INT32, TSEncoding::RLE, CompressionType::SNAPPY); - MeasurementNode mNodeS2("s2", TSDataType::INT64, TSEncoding::RLE, CompressionType::SNAPPY); - MeasurementNode mNodeD99S1("s1", TSDataType::DOUBLE, TSEncoding::RLE, CompressionType::SNAPPY); - MeasurementNode mNodeD99S2("s2", TSDataType::BOOLEAN, TSEncoding::RLE, CompressionType::SNAPPY); + MeasurementNode mNodeS1("s1", TSDataType::INT32, TSEncoding::RLE, CompressionType::SNAPPY); + MeasurementNode mNodeS2("s2", TSDataType::INT64, TSEncoding::RLE, CompressionType::SNAPPY); + MeasurementNode mNodeD99S1("s1", TSDataType::DOUBLE, TSEncoding::RLE, CompressionType::SNAPPY); + MeasurementNode mNodeD99S2("s2", TSDataType::BOOLEAN, TSEncoding::RLE, CompressionType::SNAPPY); - iNodeD99.addChild(mNodeD99S1); - iNodeD99.addChild(mNodeD99S2); + iNodeD99.addChild(mNodeD99S1); + iNodeD99.addChild(mNodeD99S2); - temp.addToTemplate(iNodeD99); - temp.addToTemplate(mNodeS1); - temp.addToTemplate(mNodeS2); + temp.addToTemplate(iNodeD99); + temp.addToTemplate(mNodeS1); + temp.addToTemplate(mNodeS2); - session->createSchemaTemplate(temp); - session->setSchemaTemplate("template1", "root.sg3.d1"); - } + session->createSchemaTemplate(temp); + session->setSchemaTemplate("template1", "root.sg3.d1"); + } } void ActivateTemplate() { - session->executeNonQueryStatement("insert into root.sg3.d1(timestamp,s1, s2) values(200, 1, 1);"); + session->executeNonQueryStatement("insert into root.sg3.d1(timestamp,s1, s2) values(200, 1, 1);"); } void showDevices() { - unique_ptr dataSet = session->executeQueryStatement("show devices with database"); - for (const string &name: dataSet->getColumnNames()) { - cout << name << " "; - } - cout << endl; - - dataSet->setFetchSize(1024); - while (dataSet->hasNext()) { - cout << dataSet->next()->toString(); - } - cout << endl; - - dataSet->closeOperationHandle(); + unique_ptr dataSet = session->executeQueryStatement("show devices with database"); + for (const string& name : dataSet->getColumnNames()) { + cout << name << " "; + } + cout << endl; + + dataSet->setFetchSize(1024); + while (dataSet->hasNext()) { + cout << dataSet->next()->toString(); + } + cout << endl; + + dataSet->closeOperationHandle(); } void showTimeseries() { - unique_ptr dataSet = session->executeQueryStatement("show timeseries"); - for (const string &name: dataSet->getColumnNames()) { - cout << name << " "; - } - cout << endl; - - dataSet->setFetchSize(1024); - while (dataSet->hasNext()) { - cout << dataSet->next()->toString(); - } - cout << endl; - - dataSet->closeOperationHandle(); + unique_ptr dataSet = session->executeQueryStatement("show timeseries"); + for (const string& name : dataSet->getColumnNames()) { + cout << name << " "; + } + cout << endl; + + dataSet->setFetchSize(1024); + while (dataSet->hasNext()) { + cout << dataSet->next()->toString(); + } + cout << endl; + + dataSet->closeOperationHandle(); } void insertAlignedRecord() { - string deviceId = "root.sg1.d1"; - vector measurements; - measurements.emplace_back("s1"); - measurements.emplace_back("s2"); - measurements.emplace_back("s3"); - - for (int64_t time = 0; time < 10; time++) { - vector values; - values.emplace_back("1"); - values.emplace_back("1.0"); - values.emplace_back("true"); - session->insertAlignedRecord(deviceId, time, measurements, values); - } + string deviceId = "root.sg1.d1"; + vector measurements; + measurements.emplace_back("s1"); + measurements.emplace_back("s2"); + measurements.emplace_back("s3"); + + for (int64_t time = 0; time < 10; time++) { + vector values; + values.emplace_back("1"); + values.emplace_back("1.0"); + values.emplace_back("true"); + session->insertAlignedRecord(deviceId, time, measurements, values); + } } void insertAlignedRecords() { - string deviceId = "root.sg1.d1"; - vector measurements; - measurements.emplace_back("s1"); - measurements.emplace_back("s2"); - measurements.emplace_back("s3"); - - vector deviceIds; - vector> measurementsList; - vector> valuesList; - vector timestamps; - - for (int64_t time = 10; time < 20; time++) { - vector values; - values.emplace_back("1"); - values.emplace_back("1.0"); - values.emplace_back("true"); - - deviceIds.push_back(deviceId); - measurementsList.push_back(measurements); - valuesList.push_back(values); - timestamps.push_back(time); - if (time != 10 && time % 10 == 0) { - session->insertAlignedRecords(deviceIds, timestamps, measurementsList, valuesList); - deviceIds.clear(); - measurementsList.clear(); - valuesList.clear(); - timestamps.clear(); - } + string deviceId = "root.sg1.d1"; + vector measurements; + measurements.emplace_back("s1"); + measurements.emplace_back("s2"); + measurements.emplace_back("s3"); + + vector deviceIds; + vector> measurementsList; + vector> valuesList; + vector timestamps; + + for (int64_t time = 10; time < 20; time++) { + vector values; + values.emplace_back("1"); + values.emplace_back("1.0"); + values.emplace_back("true"); + + deviceIds.push_back(deviceId); + measurementsList.push_back(measurements); + valuesList.push_back(values); + timestamps.push_back(time); + if (time != 10 && time % 10 == 0) { + session->insertAlignedRecords(deviceIds, timestamps, measurementsList, valuesList); + deviceIds.clear(); + measurementsList.clear(); + valuesList.clear(); + timestamps.clear(); } + } - session->insertAlignedRecords(deviceIds, timestamps, measurementsList, valuesList); + session->insertAlignedRecords(deviceIds, timestamps, measurementsList, valuesList); } void insertAlignedTablet() { - pair pairA("s1", TSDataType::INT32); - pair pairB("s2", TSDataType::DOUBLE); - pair pairC("s3", TSDataType::DOUBLE); - vector> schemas; - schemas.push_back(pairA); - schemas.push_back(pairB); - schemas.push_back(pairC); - - Tablet tablet("root.sg2.d2", schemas, 100000); - tablet.setAligned(true); - - for (int64_t time = 0; time < DEFAULT_ROW_NUMBER; time++) { - size_t row = tablet.rowSize++; - tablet.timestamps[row] = time; - int randVal1 = 123456; - double randVal2 = 123456.1234; - double randVal3 = 123456.1234; - tablet.addValue(0, row, randVal1); - tablet.addValue(1, row, randVal2); - tablet.addValue(2, row, randVal3); - if (tablet.rowSize == tablet.maxRowNumber) { - session->insertTablet(tablet, true); - tablet.reset(); - } + pair pairA("s1", TSDataType::INT32); + pair pairB("s2", TSDataType::DOUBLE); + pair pairC("s3", TSDataType::DOUBLE); + vector> schemas; + schemas.push_back(pairA); + schemas.push_back(pairB); + schemas.push_back(pairC); + + Tablet tablet("root.sg2.d2", schemas, 100000); + tablet.setAligned(true); + + for (int64_t time = 0; time < DEFAULT_ROW_NUMBER; time++) { + size_t row = tablet.rowSize++; + tablet.timestamps[row] = time; + int randVal1 = 123456; + double randVal2 = 123456.1234; + double randVal3 = 123456.1234; + tablet.addValue(0, row, randVal1); + tablet.addValue(1, row, randVal2); + tablet.addValue(2, row, randVal3); + if (tablet.rowSize == tablet.maxRowNumber) { + session->insertTablet(tablet, true); + tablet.reset(); } + } - if (tablet.rowSize != 0) { - session->insertTablet(tablet); - tablet.reset(); - } + if (tablet.rowSize != 0) { + session->insertTablet(tablet); + tablet.reset(); + } } void insertAlignedTablets() { - pair pairA("s1", TSDataType::INT32); - pair pairB("s2", TSDataType::DOUBLE); - pair pairC("s3", TSDataType::BOOLEAN); - vector> schemas; - schemas.push_back(pairA); - schemas.push_back(pairB); - schemas.push_back(pairC); - - Tablet tablet1("root.sg1.d1", schemas, 100); - Tablet tablet2("root.sg1.d2", schemas, 100); - Tablet tablet3("root.sg1.d3", schemas, 100); - - unordered_map tabletMap; - tabletMap["root.sg1.d1"] = &tablet1; - tabletMap["root.sg1.d2"] = &tablet2; - tabletMap["root.sg1.d3"] = &tablet3; - - for (int64_t time = 0; time < 20; time++) { - size_t row1 = tablet1.rowSize++; - size_t row2 = tablet2.rowSize++; - size_t row3 = tablet3.rowSize++; - tablet1.timestamps[row1] = time; - tablet2.timestamps[row2] = time; - tablet3.timestamps[row3] = time; - - int randVal11 = rand(); - int randVal12 = rand(); - int randVal13 = rand(); - tablet1.addValue(0, row1, randVal11); - tablet2.addValue(0, row2, randVal12); - tablet3.addValue(0, row3, randVal13); - - double randVal21 = rand() / 99.9; - double randVal22 = rand() / 99.9; - double randVal23 = rand() / 99.9; - tablet1.addValue(1, row1, randVal21); - tablet2.addValue(1, row2, randVal22); - tablet3.addValue(1, row3, randVal23); - - bool randVal31 = (bool)(rand() % 2); - bool randVal32 = (bool)(rand() % 2); - bool randVal33 = (bool)(rand() % 2); - tablet1.addValue(2, row1, randVal31); - tablet2.addValue(2, row2, randVal32); - tablet3.addValue(2, row3, randVal33); - - if (tablet1.rowSize == tablet1.maxRowNumber) { - session->insertAlignedTablets(tabletMap, true); - - tablet1.reset(); - tablet2.reset(); - tablet3.reset(); - } - } - - if (tablet1.rowSize != 0) { - session->insertAlignedTablets(tabletMap, true); - tablet1.reset(); - tablet2.reset(); - tablet3.reset(); + pair pairA("s1", TSDataType::INT32); + pair pairB("s2", TSDataType::DOUBLE); + pair pairC("s3", TSDataType::BOOLEAN); + vector> schemas; + schemas.push_back(pairA); + schemas.push_back(pairB); + schemas.push_back(pairC); + + Tablet tablet1("root.sg1.d1", schemas, 100); + Tablet tablet2("root.sg1.d2", schemas, 100); + Tablet tablet3("root.sg1.d3", schemas, 100); + + unordered_map tabletMap; + tabletMap["root.sg1.d1"] = &tablet1; + tabletMap["root.sg1.d2"] = &tablet2; + tabletMap["root.sg1.d3"] = &tablet3; + + for (int64_t time = 0; time < 20; time++) { + size_t row1 = tablet1.rowSize++; + size_t row2 = tablet2.rowSize++; + size_t row3 = tablet3.rowSize++; + tablet1.timestamps[row1] = time; + tablet2.timestamps[row2] = time; + tablet3.timestamps[row3] = time; + + int randVal11 = rand(); + int randVal12 = rand(); + int randVal13 = rand(); + tablet1.addValue(0, row1, randVal11); + tablet2.addValue(0, row2, randVal12); + tablet3.addValue(0, row3, randVal13); + + double randVal21 = rand() / 99.9; + double randVal22 = rand() / 99.9; + double randVal23 = rand() / 99.9; + tablet1.addValue(1, row1, randVal21); + tablet2.addValue(1, row2, randVal22); + tablet3.addValue(1, row3, randVal23); + + bool randVal31 = (bool)(rand() % 2); + bool randVal32 = (bool)(rand() % 2); + bool randVal33 = (bool)(rand() % 2); + tablet1.addValue(2, row1, randVal31); + tablet2.addValue(2, row2, randVal32); + tablet3.addValue(2, row3, randVal33); + + if (tablet1.rowSize == tablet1.maxRowNumber) { + session->insertAlignedTablets(tabletMap, true); + + tablet1.reset(); + tablet2.reset(); + tablet3.reset(); } + } + + if (tablet1.rowSize != 0) { + session->insertAlignedTablets(tabletMap, true); + tablet1.reset(); + tablet2.reset(); + tablet3.reset(); + } } void insertNullableTabletWithAlignedTimeseries() { - pair pairA("s1", TSDataType::INT32); - pair pairB("s2", TSDataType::INT64); - pair pairC("s3", TSDataType::BOOLEAN); - vector> schemas; - schemas.push_back(pairA); - schemas.push_back(pairB); - schemas.push_back(pairC); - - Tablet tablet("root.sg1.d4", schemas, 20); - tablet.setAligned(true); - - for (int64_t time = 0; time < 20; time++) { - size_t row = tablet.rowSize++; - tablet.timestamps[row] = time; - for (int i = 0; i < 3; i++) { - int randVal1 = rand(); - int64_t randVal2 = rand(); - bool randVal3 = (bool)(rand() % 2); - if (i == 0) { - tablet.addValue(i, row, randVal1); - } else if (i == 1) { - tablet.addValue(i, row, randVal2); - } else { - tablet.addValue(i, row, randVal3); - } - // mark null value - if ((row % 3) == (unsigned int) i) { - tablet.bitMaps[i].mark(row); - } - } - if (tablet.rowSize == tablet.maxRowNumber) { - session->insertTablet(tablet, true); - tablet.reset(); - } + pair pairA("s1", TSDataType::INT32); + pair pairB("s2", TSDataType::INT64); + pair pairC("s3", TSDataType::BOOLEAN); + vector> schemas; + schemas.push_back(pairA); + schemas.push_back(pairB); + schemas.push_back(pairC); + + Tablet tablet("root.sg1.d4", schemas, 20); + tablet.setAligned(true); + + for (int64_t time = 0; time < 20; time++) { + size_t row = tablet.rowSize++; + tablet.timestamps[row] = time; + for (int i = 0; i < 3; i++) { + int randVal1 = rand(); + int64_t randVal2 = rand(); + bool randVal3 = (bool)(rand() % 2); + if (i == 0) { + tablet.addValue(i, row, randVal1); + } else if (i == 1) { + tablet.addValue(i, row, randVal2); + } else { + tablet.addValue(i, row, randVal3); + } + // mark null value + if ((row % 3) == (unsigned int)i) { + tablet.bitMaps[i].mark(row); + } } - - if (tablet.rowSize != 0) { - session->insertTablet(tablet); - tablet.reset(); + if (tablet.rowSize == tablet.maxRowNumber) { + session->insertTablet(tablet, true); + tablet.reset(); } + } + + if (tablet.rowSize != 0) { + session->insertTablet(tablet); + tablet.reset(); + } } void query() { - unique_ptr dataSet = session->executeQueryStatement("select * from root.sg1.**"); - cout << "timestamp" << " "; - for (const string &name: dataSet->getColumnNames()) { - cout << name << " "; - } - cout << endl; - - dataSet->setFetchSize(1024); - while (dataSet->hasNext()) { - cout << dataSet->next()->toString(); - } - cout << endl; - - dataSet->closeOperationHandle(); + unique_ptr dataSet = session->executeQueryStatement("select * from root.sg1.**"); + cout << "timestamp" + << " "; + for (const string& name : dataSet->getColumnNames()) { + cout << name << " "; + } + cout << endl; + + dataSet->setFetchSize(1024); + while (dataSet->hasNext()) { + cout << dataSet->next()->toString(); + } + cout << endl; + + dataSet->closeOperationHandle(); } void deleteData() { - string path = "root.**"; - int64_t deleteTime = 49; - session->deleteData(path, deleteTime); + string path = "root.**"; + int64_t deleteTime = 49; + session->deleteData(path, deleteTime); } void deleteTimeseries() { - vector paths; - vector alignedTimeseries = {"root.sg1.d1.s1", "root.sg1.d1.s2", "root.sg1.d1.s3", "root.sg1.d1.s4", - "root.sg1.d2.s1", "root.sg1.d2.s2", "root.sg1.d2.s3", - "root.sg1.d3.s1", "root.sg1.d3.s2", "root.sg1.d3.s3", - "root.sg1.d4.s1", "root.sg1.d4.s2", "root.sg1.d4.s3", - "root.sg2.d2.s1", "root.sg2.d2.s2", "root.sg2.d2.s3", }; - for (const string ×eries: alignedTimeseries) { - if (session->checkTimeseriesExists(timeseries)) { - paths.push_back(timeseries); - } + vector paths; + vector alignedTimeseries = { + "root.sg1.d1.s1", "root.sg1.d1.s2", "root.sg1.d1.s3", "root.sg1.d1.s4", + "root.sg1.d2.s1", "root.sg1.d2.s2", "root.sg1.d2.s3", "root.sg1.d3.s1", + "root.sg1.d3.s2", "root.sg1.d3.s3", "root.sg1.d4.s1", "root.sg1.d4.s2", + "root.sg1.d4.s3", "root.sg2.d2.s1", "root.sg2.d2.s2", "root.sg2.d2.s3", + }; + for (const string& timeseries : alignedTimeseries) { + if (session->checkTimeseriesExists(timeseries)) { + paths.push_back(timeseries); } - session->deleteTimeseries(paths); + } + session->deleteTimeseries(paths); } void deleteStorageGroups() { - vector storageGroups; - storageGroups.emplace_back("root.sg1"); - storageGroups.emplace_back("root.sg2"); - session->deleteStorageGroups(storageGroups); + vector storageGroups; + storageGroups.emplace_back("root.sg1"); + storageGroups.emplace_back("root.sg2"); + session->deleteStorageGroups(storageGroups); } - int main() { - LOG_LEVEL = LEVEL_DEBUG; + LOG_LEVEL = LEVEL_DEBUG; - session = new Session("127.0.0.1", 6667, "root", "root"); + session = new Session("127.0.0.1", 6667, "root", "root"); - cout << "session open\n" << endl; - session->open(false); + cout << "session open\n" << endl; + session->open(false); - cout << "setStorageGroup\n" << endl; - try { - session->setStorageGroup("root.sg1"); - } - catch (IoTDBException &e) { - string errorMessage(e.what()); - if (errorMessage.find("StorageGroupAlreadySetException") == string::npos) { - cout << errorMessage << endl; - //throw e; - } + cout << "setStorageGroup\n" << endl; + try { + session->setStorageGroup("root.sg1"); + } catch (IoTDBException& e) { + string errorMessage(e.what()); + if (errorMessage.find("StorageGroupAlreadySetException") == string::npos) { + cout << errorMessage << endl; + //throw e; } + } - cout << "createAlignedTimeseries\n" << endl; - createAlignedTimeseries(); + cout << "createAlignedTimeseries\n" << endl; + createAlignedTimeseries(); - cout << "createSchemaTemplate\n" << endl; - createSchemaTemplate(); + cout << "createSchemaTemplate\n" << endl; + createSchemaTemplate(); - cout << "ActivateTemplate\n" << endl; - ActivateTemplate(); + cout << "ActivateTemplate\n" << endl; + ActivateTemplate(); - cout << "showDevices\n" << endl; - showDevices(); + cout << "showDevices\n" << endl; + showDevices(); - cout << "showTimeseries\n" << endl; - showTimeseries(); + cout << "showTimeseries\n" << endl; + showTimeseries(); - cout << "insertAlignedRecord\n" << endl; - insertAlignedRecord(); + cout << "insertAlignedRecord\n" << endl; + insertAlignedRecord(); - cout << "insertAlignedRecords\n" << endl; - insertAlignedRecords(); + cout << "insertAlignedRecords\n" << endl; + insertAlignedRecords(); - cout << "insertAlignedTablet" << endl; - cout << "Insert " << DEFAULT_ROW_NUMBER << " records." << endl; - time_t now1 = time(0); - insertAlignedTablet(); - time_t now2 = time(0); - time_t useTime = now2 - now1; - cout << "Use time: " << useTime << "s.\n" << endl; + cout << "insertAlignedTablet" << endl; + cout << "Insert " << DEFAULT_ROW_NUMBER << " records." << endl; + time_t now1 = time(0); + insertAlignedTablet(); + time_t now2 = time(0); + time_t useTime = now2 - now1; + cout << "Use time: " << useTime << "s.\n" << endl; - cout << "insertAlignedTablets\n" << endl; - insertAlignedTablets(); + cout << "insertAlignedTablets\n" << endl; + insertAlignedTablets(); - cout << "insertNullableTabletWithAlignedTimeseries\n" << endl; - insertNullableTabletWithAlignedTimeseries(); + cout << "insertNullableTabletWithAlignedTimeseries\n" << endl; + insertNullableTabletWithAlignedTimeseries(); - cout << "query\n" << endl; - query(); + cout << "query\n" << endl; + query(); - cout << "deleteData\n" << endl; - deleteData(); + cout << "deleteData\n" << endl; + deleteData(); - cout << "deleteTimeseries\n" << endl; - deleteTimeseries(); + cout << "deleteTimeseries\n" << endl; + deleteTimeseries(); - cout << "deleteStorageGroups\n" << endl; - deleteStorageGroups(); + cout << "deleteStorageGroups\n" << endl; + deleteStorageGroups(); - cout << "session close\n" << endl; - session->close(); + cout << "session close\n" << endl; + session->close(); - delete session; + delete session; - cout << "finished\n" << endl; - return 0; + cout << "finished\n" << endl; + return 0; } diff --git a/example/client-cpp-example/src/MultiSvrNodeClient.cpp b/example/client-cpp-example/src/MultiSvrNodeClient.cpp index 68e3e7230ca72..06e96ecd31bdf 100644 --- a/example/client-cpp-example/src/MultiSvrNodeClient.cpp +++ b/example/client-cpp-example/src/MultiSvrNodeClient.cpp @@ -30,118 +30,99 @@ namespace { void RunTreeExample() { - try { - std::vector node_urls = { - "127.0.0.1:6667", "127.0.0.1:6668", "127.0.0.1:6669"}; - - auto builder = std::make_shared(); - auto session = std::shared_ptr( - builder->username("root") - ->password("root") - ->nodeUrls(node_urls) - ->build()); - - session->open(); - if (!session->checkTimeseriesExists("root.test.d1.s1")) { - session->createTimeseries("root.test.d1.s1", TSDataType::INT64, - TSEncoding::RLE, CompressionType::SNAPPY); - } - session->deleteTimeseries("root.test.d1.s1"); - session->close(); - } catch (const std::exception& e) { - std::cout << "Caught exception: " << e.what() << std::endl; + try { + std::vector node_urls = {"127.0.0.1:6667", "127.0.0.1:6668", "127.0.0.1:6669"}; + + auto builder = std::make_shared(); + auto session = std::shared_ptr( + builder->username("root")->password("root")->nodeUrls(node_urls)->build()); + + session->open(); + if (!session->checkTimeseriesExists("root.test.d1.s1")) { + session->createTimeseries("root.test.d1.s1", TSDataType::INT64, TSEncoding::RLE, + CompressionType::SNAPPY); } + session->deleteTimeseries("root.test.d1.s1"); + session->close(); + } catch (const std::exception& e) { + std::cout << "Caught exception: " << e.what() << std::endl; + } } void RunTableExample() { - try { - std::vector node_urls = { - "127.0.0.1:6669", "127.0.0.1:6668", "127.0.0.1:6667"}; - - auto builder = std::make_shared(); - auto session = std::shared_ptr( - builder->username("root") - ->password("root") - ->nodeUrls(node_urls) - ->build()); - - session->open(); - - session->executeNonQueryStatement("DROP DATABASE IF EXISTS db1"); - session->executeNonQueryStatement("CREATE DATABASE db1"); - session->executeNonQueryStatement("DROP DATABASE IF EXISTS db2"); - session->executeNonQueryStatement("CREATE DATABASE db2"); - - session->close(); - } catch (const std::exception& e) { - std::cout << "Caught exception: " << e.what() << std::endl; - } -} + try { + std::vector node_urls = {"127.0.0.1:6669", "127.0.0.1:6668", "127.0.0.1:6667"}; + + auto builder = std::make_shared(); + auto session = std::shared_ptr( + builder->username("root")->password("root")->nodeUrls(node_urls)->build()); + session->open(); + + session->executeNonQueryStatement("DROP DATABASE IF EXISTS db1"); + session->executeNonQueryStatement("CREATE DATABASE db1"); + session->executeNonQueryStatement("DROP DATABASE IF EXISTS db2"); + session->executeNonQueryStatement("CREATE DATABASE db2"); + + session->close(); + } catch (const std::exception& e) { + std::cout << "Caught exception: " << e.what() << std::endl; + } +} // Example: continuously write/query data so you can manually stop a node // to test client failover behavior. void RunResilienceExample() { - try { - std::vector node_urls = { - "127.0.0.1:6667", "127.0.0.1:6668", "127.0.0.1:6669"}; - - auto builder = std::make_shared(); - auto session = std::shared_ptr( - builder->username("root") - ->password("root") - ->nodeUrls(node_urls) - ->build()); - - session->open(); - - if (!session->checkTimeseriesExists("root.resilience.d1.s1")) { - session->createTimeseries("root.resilience.d1.s1", TSDataType::INT64, - TSEncoding::RLE, CompressionType::SNAPPY); - } - - std::cout << "Starting resilience test. " - "Stop one node manually to see failover handling..." - << std::endl; - - for (int i = 0; i < 60; ++i) { // run ~60 seconds - int64_t timestamp = std::chrono::system_clock::now().time_since_epoch() / - std::chrono::milliseconds(1); - std::string value = to_string(i); - const char* value_cstr = value.c_str(); - - try { - session->insertRecord("root.resilience.d1", timestamp, - {"s1"}, {TSDataType::INT64}, - {const_cast(value_cstr)}); - std::cout << "[Insert] ts=" << timestamp << ", value=" << value - << std::endl; - - auto dataset = session->executeQueryStatement( - "SELECT s1 FROM root.resilience.d1 LIMIT 1"); - std::cout << "[Query] Got dataset: " - << (dataset ? "Success" : "Null") << std::endl; - - } catch (const std::exception& e) { - std::cout << "Caught exception during resilience loop: " << e.what() - << std::endl; - } - - std::this_thread::sleep_for(std::chrono::seconds(1)); - } - - session->close(); - } catch (const std::exception& e) { - std::cout << "Caught exception in RunResilienceExample: " << e.what() - << std::endl; + try { + std::vector node_urls = {"127.0.0.1:6667", "127.0.0.1:6668", "127.0.0.1:6669"}; + + auto builder = std::make_shared(); + auto session = std::shared_ptr( + builder->username("root")->password("root")->nodeUrls(node_urls)->build()); + + session->open(); + + if (!session->checkTimeseriesExists("root.resilience.d1.s1")) { + session->createTimeseries("root.resilience.d1.s1", TSDataType::INT64, TSEncoding::RLE, + CompressionType::SNAPPY); } + + std::cout << "Starting resilience test. " + "Stop one node manually to see failover handling..." + << std::endl; + + for (int i = 0; i < 60; ++i) { // run ~60 seconds + int64_t timestamp = + std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1); + std::string value = to_string(i); + const char* value_cstr = value.c_str(); + + try { + session->insertRecord("root.resilience.d1", timestamp, {"s1"}, {TSDataType::INT64}, + {const_cast(value_cstr)}); + std::cout << "[Insert] ts=" << timestamp << ", value=" << value << std::endl; + + auto dataset = session->executeQueryStatement("SELECT s1 FROM root.resilience.d1 LIMIT 1"); + std::cout << "[Query] Got dataset: " << (dataset ? "Success" : "Null") << std::endl; + + } catch (const std::exception& e) { + std::cout << "Caught exception during resilience loop: " << e.what() << std::endl; + } + + std::this_thread::sleep_for(std::chrono::seconds(1)); + } + + session->close(); + } catch (const std::exception& e) { + std::cout << "Caught exception in RunResilienceExample: " << e.what() << std::endl; + } } -} // namespace +} // namespace int main() { - //RunTreeExample(); - //RunTableExample(); - RunResilienceExample(); - return 0; + //RunTreeExample(); + //RunTableExample(); + RunResilienceExample(); + return 0; } diff --git a/example/client-cpp-example/src/SessionExample.cpp b/example/client-cpp-example/src/SessionExample.cpp index 2803d18976cbf..d589759e8ed20 100644 --- a/example/client-cpp-example/src/SessionExample.cpp +++ b/example/client-cpp-example/src/SessionExample.cpp @@ -21,255 +21,256 @@ using namespace std; -Session *session; +Session* session; void createTimeseries() { - if (!session->checkTimeseriesExists("root.sg1.d1.s1")) { - session->createTimeseries("root.sg1.d1.s1", TSDataType::BOOLEAN, TSEncoding::RLE, - CompressionType::SNAPPY); - } - if (!session->checkTimeseriesExists("root.sg1.d1.s2")) { - session->createTimeseries("root.sg1.d1.s2", TSDataType::INT32, TSEncoding::RLE, - CompressionType::SNAPPY); - } - if (!session->checkTimeseriesExists("root.sg1.d1.s3")) { - session->createTimeseries("root.sg1.d1.s3", TSDataType::FLOAT, TSEncoding::RLE, - CompressionType::SNAPPY); - } - - // create timeseries with tags and attributes - if (!session->checkTimeseriesExists("root.sg1.d1.s4")) { - map tags; - tags["tag1"] = "v1"; - map attributes; - attributes["description"] = "v1"; - session->createTimeseries("root.sg1.d1.s4", TSDataType::INT64, TSEncoding::RLE, - CompressionType::SNAPPY, nullptr, &tags, &attributes, "temperature"); - } + if (!session->checkTimeseriesExists("root.sg1.d1.s1")) { + session->createTimeseries("root.sg1.d1.s1", TSDataType::BOOLEAN, TSEncoding::RLE, + CompressionType::SNAPPY); + } + if (!session->checkTimeseriesExists("root.sg1.d1.s2")) { + session->createTimeseries("root.sg1.d1.s2", TSDataType::INT32, TSEncoding::RLE, + CompressionType::SNAPPY); + } + if (!session->checkTimeseriesExists("root.sg1.d1.s3")) { + session->createTimeseries("root.sg1.d1.s3", TSDataType::FLOAT, TSEncoding::RLE, + CompressionType::SNAPPY); + } + + // create timeseries with tags and attributes + if (!session->checkTimeseriesExists("root.sg1.d1.s4")) { + map tags; + tags["tag1"] = "v1"; + map attributes; + attributes["description"] = "v1"; + session->createTimeseries("root.sg1.d1.s4", TSDataType::INT64, TSEncoding::RLE, + CompressionType::SNAPPY, nullptr, &tags, &attributes, "temperature"); + } } void createMultiTimeseries() { - if (!session->checkTimeseriesExists("root.sg1.d2.s1") && !session->checkTimeseriesExists("root.sg1.d2.s2")) { - vector paths; - paths.emplace_back("root.sg1.d2.s1"); - paths.emplace_back("root.sg1.d2.s2"); - vector tsDataTypes; - tsDataTypes.push_back(TSDataType::INT64); - tsDataTypes.push_back(TSDataType::DOUBLE); - vector tsEncodings; - tsEncodings.push_back(TSEncoding::RLE); - tsEncodings.push_back(TSEncoding::RLE); - vector compressionTypes; - compressionTypes.push_back(CompressionType::SNAPPY); - compressionTypes.push_back(CompressionType::SNAPPY); - - vector> tagsList; - map tags; - tags["unit"] = "kg"; - tagsList.push_back(tags); - tagsList.push_back(tags); - - vector> attributesList; - map attributes; - attributes["minValue"] = "1"; - attributes["maxValue"] = "100"; - attributesList.push_back(attributes); - attributesList.push_back(attributes); - - vector alias; - alias.emplace_back("weight1"); - alias.emplace_back("weight2"); - - session->createMultiTimeseries(paths, tsDataTypes, tsEncodings, compressionTypes, nullptr, &tagsList, - &attributesList, &alias); - } + if (!session->checkTimeseriesExists("root.sg1.d2.s1") && + !session->checkTimeseriesExists("root.sg1.d2.s2")) { + vector paths; + paths.emplace_back("root.sg1.d2.s1"); + paths.emplace_back("root.sg1.d2.s2"); + vector tsDataTypes; + tsDataTypes.push_back(TSDataType::INT64); + tsDataTypes.push_back(TSDataType::DOUBLE); + vector tsEncodings; + tsEncodings.push_back(TSEncoding::RLE); + tsEncodings.push_back(TSEncoding::RLE); + vector compressionTypes; + compressionTypes.push_back(CompressionType::SNAPPY); + compressionTypes.push_back(CompressionType::SNAPPY); + + vector> tagsList; + map tags; + tags["unit"] = "kg"; + tagsList.push_back(tags); + tagsList.push_back(tags); + + vector> attributesList; + map attributes; + attributes["minValue"] = "1"; + attributes["maxValue"] = "100"; + attributesList.push_back(attributes); + attributesList.push_back(attributes); + + vector alias; + alias.emplace_back("weight1"); + alias.emplace_back("weight2"); + + session->createMultiTimeseries(paths, tsDataTypes, tsEncodings, compressionTypes, nullptr, + &tagsList, &attributesList, &alias); + } } void createSchemaTemplate() { - if (!session->checkTemplateExists("template1")) { - Template temp("template1", false); + if (!session->checkTemplateExists("template1")) { + Template temp("template1", false); - InternalNode iNodeD99("d99", false); + InternalNode iNodeD99("d99", false); - MeasurementNode mNodeS1("s1", TSDataType::INT32, TSEncoding::RLE, CompressionType::SNAPPY); - MeasurementNode mNodeS2("s2", TSDataType::INT64, TSEncoding::RLE, CompressionType::SNAPPY); - MeasurementNode mNodeD99S1("s1", TSDataType::DOUBLE, TSEncoding::RLE, CompressionType::SNAPPY); - MeasurementNode mNodeD99S2("s2", TSDataType::BOOLEAN, TSEncoding::RLE, CompressionType::SNAPPY); + MeasurementNode mNodeS1("s1", TSDataType::INT32, TSEncoding::RLE, CompressionType::SNAPPY); + MeasurementNode mNodeS2("s2", TSDataType::INT64, TSEncoding::RLE, CompressionType::SNAPPY); + MeasurementNode mNodeD99S1("s1", TSDataType::DOUBLE, TSEncoding::RLE, CompressionType::SNAPPY); + MeasurementNode mNodeD99S2("s2", TSDataType::BOOLEAN, TSEncoding::RLE, CompressionType::SNAPPY); - iNodeD99.addChild(mNodeD99S1); - iNodeD99.addChild(mNodeD99S2); + iNodeD99.addChild(mNodeD99S1); + iNodeD99.addChild(mNodeD99S2); - temp.addToTemplate(iNodeD99); - temp.addToTemplate(mNodeS1); - temp.addToTemplate(mNodeS2); + temp.addToTemplate(iNodeD99); + temp.addToTemplate(mNodeS1); + temp.addToTemplate(mNodeS2); - session->createSchemaTemplate(temp); - session->setSchemaTemplate("template1", "root.sg3.d1"); - } + session->createSchemaTemplate(temp); + session->setSchemaTemplate("template1", "root.sg3.d1"); + } } void ActivateTemplate() { - session->executeNonQueryStatement("insert into root.sg3.d1(timestamp,s1, s2) values(200, 1, 1);"); + session->executeNonQueryStatement("insert into root.sg3.d1(timestamp,s1, s2) values(200, 1, 1);"); } void showTimeseries() { - unique_ptr dataSet = session->executeQueryStatement("show timeseries"); - for (const string &name: dataSet->getColumnNames()) { - cout << name << " "; - } - cout << endl; - - dataSet->setFetchSize(1024); - while (dataSet->hasNext()) { - cout << dataSet->next()->toString(); - } - cout << endl; - - dataSet->closeOperationHandle(); + unique_ptr dataSet = session->executeQueryStatement("show timeseries"); + for (const string& name : dataSet->getColumnNames()) { + cout << name << " "; + } + cout << endl; + + dataSet->setFetchSize(1024); + while (dataSet->hasNext()) { + cout << dataSet->next()->toString(); + } + cout << endl; + + dataSet->closeOperationHandle(); } void insertRecord() { - string deviceId = "root.sg2.d1"; - vector measurements; - measurements.emplace_back("s1"); - measurements.emplace_back("s2"); - measurements.emplace_back("s3"); - for (int64_t time = 0; time < 10; time++) { - vector values; - values.emplace_back("1"); - values.emplace_back("2"); - values.emplace_back("3"); - session->insertRecord(deviceId, time, measurements, values); - } + string deviceId = "root.sg2.d1"; + vector measurements; + measurements.emplace_back("s1"); + measurements.emplace_back("s2"); + measurements.emplace_back("s3"); + for (int64_t time = 0; time < 10; time++) { + vector values; + values.emplace_back("1"); + values.emplace_back("2"); + values.emplace_back("3"); + session->insertRecord(deviceId, time, measurements, values); + } } void insertTablet() { - pair pairA("s1", TSDataType::BOOLEAN); - pair pairB("s2", TSDataType::INT32); - pair pairC("s3", TSDataType::FLOAT); - vector> schemas; - schemas.push_back(pairA); - schemas.push_back(pairB); - schemas.push_back(pairC); + pair pairA("s1", TSDataType::BOOLEAN); + pair pairB("s2", TSDataType::INT32); + pair pairC("s3", TSDataType::FLOAT); + vector> schemas; + schemas.push_back(pairA); + schemas.push_back(pairB); + schemas.push_back(pairC); - Tablet tablet("root.sg1.d1", schemas, 100); + Tablet tablet("root.sg1.d1", schemas, 100); - for (int64_t time = 0; time < 30; time++) { - size_t row = tablet.rowSize++; - tablet.timestamps[row] = time; + for (int64_t time = 0; time < 30; time++) { + size_t row = tablet.rowSize++; + tablet.timestamps[row] = time; - bool randVal1 = rand() % 2; - tablet.addValue(0, row, randVal1); + bool randVal1 = rand() % 2; + tablet.addValue(0, row, randVal1); - int randVal2 = rand(); - tablet.addValue(1, row, randVal2); + int randVal2 = rand(); + tablet.addValue(1, row, randVal2); - float randVal3 = (float)(rand() / 99.9); - tablet.addValue(2, row, randVal3); + float randVal3 = (float)(rand() / 99.9); + tablet.addValue(2, row, randVal3); - if (tablet.rowSize == tablet.maxRowNumber) { - session->insertTablet(tablet, true); - tablet.reset(); - } + if (tablet.rowSize == tablet.maxRowNumber) { + session->insertTablet(tablet, true); + tablet.reset(); } + } - if (tablet.rowSize != 0) { - session->insertTablet(tablet); - tablet.reset(); - } + if (tablet.rowSize != 0) { + session->insertTablet(tablet); + tablet.reset(); + } } void insertRecords() { - string deviceId = "root.sg2.d1"; - vector measurements; - measurements.emplace_back("s1"); - measurements.emplace_back("s2"); - measurements.emplace_back("s3"); - - vector deviceIds; - vector> measurementsList; - vector> valuesList; - vector timestamps; - - for (int64_t time = 10; time < 30; time++) { - vector values; - values.emplace_back("1"); - values.emplace_back("2"); - values.emplace_back("3"); - - deviceIds.push_back(deviceId); - measurementsList.push_back(measurements); - valuesList.push_back(values); - timestamps.push_back(time); - if (time != 20 && time % 10 == 0) { - session->insertRecords(deviceIds, timestamps, measurementsList, valuesList); - deviceIds.clear(); - measurementsList.clear(); - valuesList.clear(); - timestamps.clear(); - } + string deviceId = "root.sg2.d1"; + vector measurements; + measurements.emplace_back("s1"); + measurements.emplace_back("s2"); + measurements.emplace_back("s3"); + + vector deviceIds; + vector> measurementsList; + vector> valuesList; + vector timestamps; + + for (int64_t time = 10; time < 30; time++) { + vector values; + values.emplace_back("1"); + values.emplace_back("2"); + values.emplace_back("3"); + + deviceIds.push_back(deviceId); + measurementsList.push_back(measurements); + valuesList.push_back(values); + timestamps.push_back(time); + if (time != 20 && time % 10 == 0) { + session->insertRecords(deviceIds, timestamps, measurementsList, valuesList); + deviceIds.clear(); + measurementsList.clear(); + valuesList.clear(); + timestamps.clear(); } + } - session->insertRecords(deviceIds, timestamps, measurementsList, valuesList); + session->insertRecords(deviceIds, timestamps, measurementsList, valuesList); } void insertTablets() { - pair pairA("s1", TSDataType::INT64); - pair pairB("s2", TSDataType::DOUBLE); - pair pairC("s3", TSDataType::TEXT); - vector> schemas; - schemas.push_back(pairA); - schemas.push_back(pairB); - schemas.push_back(pairC); + pair pairA("s1", TSDataType::INT64); + pair pairB("s2", TSDataType::DOUBLE); + pair pairC("s3", TSDataType::TEXT); + vector> schemas; + schemas.push_back(pairA); + schemas.push_back(pairB); + schemas.push_back(pairC); - Tablet tablet1("root.sg1.d2", schemas, 100); - Tablet tablet2("root.sg1.d3", schemas, 100); + Tablet tablet1("root.sg1.d2", schemas, 100); + Tablet tablet2("root.sg1.d3", schemas, 100); - unordered_map tabletMap; - tabletMap["root.sg1.d2"] = &tablet1; - tabletMap["root.sg1.d3"] = &tablet2; + unordered_map tabletMap; + tabletMap["root.sg1.d2"] = &tablet1; + tabletMap["root.sg1.d3"] = &tablet2; - for (int64_t time = 0; time < 30; time++) { - size_t row1 = tablet1.rowSize++; - size_t row2 = tablet2.rowSize++; - tablet1.timestamps[row1] = time; - tablet2.timestamps[row2] = time; + for (int64_t time = 0; time < 30; time++) { + size_t row1 = tablet1.rowSize++; + size_t row2 = tablet2.rowSize++; + tablet1.timestamps[row1] = time; + tablet2.timestamps[row2] = time; - int64_t randVal11 = rand(); - tablet1.addValue(0, row1, randVal11); + int64_t randVal11 = rand(); + tablet1.addValue(0, row1, randVal11); - double randVal12 = rand() / 99.9; - tablet1.addValue(1, row1, randVal12); + double randVal12 = rand() / 99.9; + tablet1.addValue(1, row1, randVal12); - string randVal13 = "string" + to_string(rand()); - tablet1.addValue(2, row1, randVal13); + string randVal13 = "string" + to_string(rand()); + tablet1.addValue(2, row1, randVal13); - int64_t randVal21 = rand(); - tablet2.addValue(0, row2, randVal21); + int64_t randVal21 = rand(); + tablet2.addValue(0, row2, randVal21); - double randVal22 = rand() / 99.9; - tablet2.addValue(1, row2, randVal22); + double randVal22 = rand() / 99.9; + tablet2.addValue(1, row2, randVal22); - string randVal23 = "string" + to_string(rand()); - tablet2.addValue(2, row2, randVal23); + string randVal23 = "string" + to_string(rand()); + tablet2.addValue(2, row2, randVal23); - if (tablet1.rowSize == tablet1.maxRowNumber) { - session->insertTablets(tabletMap, true); + if (tablet1.rowSize == tablet1.maxRowNumber) { + session->insertTablets(tabletMap, true); - tablet1.reset(); - tablet2.reset(); - } + tablet1.reset(); + tablet2.reset(); } + } - if (tablet1.rowSize != 0) { - session->insertTablets(tabletMap, true); - tablet1.reset(); - tablet2.reset(); - } + if (tablet1.rowSize != 0) { + session->insertTablets(tabletMap, true); + tablet1.reset(); + tablet2.reset(); + } } void insertTabletWithNullValues() { - /* + /* * A Tablet example: * device1 * time s1, s2, s3 @@ -277,185 +278,184 @@ void insertTabletWithNullValues() { * 2, 2, null, 2 * 3, 3, 3, null */ - pair pairA("s1", TSDataType::INT64); - pair pairB("s2", TSDataType::INT64); - pair pairC("s3", TSDataType::INT64); - vector> schemas; - schemas.push_back(pairA); - schemas.push_back(pairB); - schemas.push_back(pairC); - - Tablet tablet("root.sg1.d4", schemas, 30); - - for (int64_t time = 0; time < 30; time++) { - size_t row = tablet.rowSize++; - tablet.timestamps[row] = time; - for (int i = 0; i < 3; i++) { - int64_t randVal = rand(); - tablet.addValue(i, row, randVal); - // mark null value - if (row % 3 == (unsigned int) i) { - tablet.bitMaps[i].mark(row); - } - } - if (tablet.rowSize == tablet.maxRowNumber) { - session->insertTablet(tablet, true); - tablet.reset(); - } + pair pairA("s1", TSDataType::INT64); + pair pairB("s2", TSDataType::INT64); + pair pairC("s3", TSDataType::INT64); + vector> schemas; + schemas.push_back(pairA); + schemas.push_back(pairB); + schemas.push_back(pairC); + + Tablet tablet("root.sg1.d4", schemas, 30); + + for (int64_t time = 0; time < 30; time++) { + size_t row = tablet.rowSize++; + tablet.timestamps[row] = time; + for (int i = 0; i < 3; i++) { + int64_t randVal = rand(); + tablet.addValue(i, row, randVal); + // mark null value + if (row % 3 == (unsigned int)i) { + tablet.bitMaps[i].mark(row); + } } - - if (tablet.rowSize != 0) { - session->insertTablet(tablet); - tablet.reset(); + if (tablet.rowSize == tablet.maxRowNumber) { + session->insertTablet(tablet, true); + tablet.reset(); } + } + + if (tablet.rowSize != 0) { + session->insertTablet(tablet); + tablet.reset(); + } } void nonQuery() { - session->executeNonQueryStatement("insert into root.sg1.d1(timestamp,s1) values(100, 1);"); + session->executeNonQueryStatement("insert into root.sg1.d1(timestamp,s1) values(100, 1);"); } void query() { - unique_ptr dataSet = session->executeQueryStatement("select s1, s2, s3 from root.**"); - for (const string &name: dataSet->getColumnNames()) { - cout << name << " "; - } - cout << endl; - - dataSet->setFetchSize(1024); - while (dataSet->hasNext()) { - cout << dataSet->next()->toString(); - } - cout << endl; - - dataSet->closeOperationHandle(); + unique_ptr dataSet = + session->executeQueryStatement("select s1, s2, s3 from root.**"); + for (const string& name : dataSet->getColumnNames()) { + cout << name << " "; + } + cout << endl; + + dataSet->setFetchSize(1024); + while (dataSet->hasNext()) { + cout << dataSet->next()->toString(); + } + cout << endl; + + dataSet->closeOperationHandle(); } void deleteData() { - string path = "root.sg1.d1.s1"; - int64_t deleteTime = 99; - session->deleteData(path, deleteTime); + string path = "root.sg1.d1.s1"; + int64_t deleteTime = 99; + session->deleteData(path, deleteTime); } void deleteTimeseries() { - vector paths; - vector timeseriesGrp = { "root.sg1.d1.s1", "root.sg1.d1.s2", "root.sg1.d1.s3", - "root.sg1.d2.s1", "root.sg1.d2.s2", "root.sg1.d2.s3", - "root.sg1.d3.s1", "root.sg1.d3.s2", "root.sg1.d3.s3", - "root.sg1.d4.s1", "root.sg1.d4.s2", "root.sg1.d4.s3", - "root.sg2.d1.s1", "root.sg2.d1.s2", "root.sg2.d1.s3" }; - for (const string& timeseries : timeseriesGrp) { - if (session->checkTimeseriesExists(timeseries)) { - paths.push_back(timeseries); - } + vector paths; + vector timeseriesGrp = { + "root.sg1.d1.s1", "root.sg1.d1.s2", "root.sg1.d1.s3", "root.sg1.d2.s1", "root.sg1.d2.s2", + "root.sg1.d2.s3", "root.sg1.d3.s1", "root.sg1.d3.s2", "root.sg1.d3.s3", "root.sg1.d4.s1", + "root.sg1.d4.s2", "root.sg1.d4.s3", "root.sg2.d1.s1", "root.sg2.d1.s2", "root.sg2.d1.s3"}; + for (const string& timeseries : timeseriesGrp) { + if (session->checkTimeseriesExists(timeseries)) { + paths.push_back(timeseries); } - session->deleteTimeseries(paths); + } + session->deleteTimeseries(paths); } void deleteStorageGroups() { - vector storageGroups; - storageGroups.emplace_back("root.sg1"); - storageGroups.emplace_back("root.sg2"); - session->deleteStorageGroups(storageGroups); + vector storageGroups; + storageGroups.emplace_back("root.sg1"); + storageGroups.emplace_back("root.sg2"); + session->deleteStorageGroups(storageGroups); } void queryLast() { - unique_ptr dataSet = session->executeQueryStatement("select last s1,s2,s3 from root.**"); - for (const string &name: dataSet->getColumnNames()) { - cout << name << " "; - } - cout << endl; - - while (dataSet->hasNext()) { - cout << dataSet->next()->toString(); - } - cout << endl; - - dataSet->closeOperationHandle(); + unique_ptr dataSet = + session->executeQueryStatement("select last s1,s2,s3 from root.**"); + for (const string& name : dataSet->getColumnNames()) { + cout << name << " "; + } + cout << endl; + + while (dataSet->hasNext()) { + cout << dataSet->next()->toString(); + } + cout << endl; + + dataSet->closeOperationHandle(); } int main() { - LOG_LEVEL = LEVEL_DEBUG; - - session = new Session("127.0.0.1", 6667, "root", "root"); - session->open(false); - - cout << "setStorageGroup: root.sg1\n" << endl; - try { - session->setStorageGroup("root.sg1"); - } - catch (IoTDBException &e) { - string errorMessage(e.what()); - if (errorMessage.find("StorageGroupAlreadySetException") == string::npos) { - cout << errorMessage << endl; - } - //throw e; - } - - cout << "setStorageGroup: root.sg2\n" << endl; - try { - session->setStorageGroup("root.sg2"); + LOG_LEVEL = LEVEL_DEBUG; + + session = new Session("127.0.0.1", 6667, "root", "root"); + session->open(false); + + cout << "setStorageGroup: root.sg1\n" << endl; + try { + session->setStorageGroup("root.sg1"); + } catch (IoTDBException& e) { + string errorMessage(e.what()); + if (errorMessage.find("StorageGroupAlreadySetException") == string::npos) { + cout << errorMessage << endl; } - catch (IoTDBException &e) { - string errorMessage(e.what()); - if (errorMessage.find("StorageGroupAlreadySetException") == string::npos) { - cout << errorMessage << endl; - } - //throw e; + //throw e; + } + + cout << "setStorageGroup: root.sg2\n" << endl; + try { + session->setStorageGroup("root.sg2"); + } catch (IoTDBException& e) { + string errorMessage(e.what()); + if (errorMessage.find("StorageGroupAlreadySetException") == string::npos) { + cout << errorMessage << endl; } + //throw e; + } - cout << "createTimeseries\n" << endl; - createTimeseries(); + cout << "createTimeseries\n" << endl; + createTimeseries(); - cout << "createMultiTimeseries\n" << endl; - createMultiTimeseries(); + cout << "createMultiTimeseries\n" << endl; + createMultiTimeseries(); - cout << "createSchemaTemplate\n" << endl; - createSchemaTemplate(); + cout << "createSchemaTemplate\n" << endl; + createSchemaTemplate(); - cout << "ActivateTemplate\n" << endl; - ActivateTemplate(); + cout << "ActivateTemplate\n" << endl; + ActivateTemplate(); - cout << "showTimeseries\n" << endl; - showTimeseries(); + cout << "showTimeseries\n" << endl; + showTimeseries(); - cout << "insertRecord\n" << endl; - insertRecord(); + cout << "insertRecord\n" << endl; + insertRecord(); - cout << "insertTablet\n" << endl; - insertTablet(); + cout << "insertTablet\n" << endl; + insertTablet(); - cout << "insertRecords\n" << endl; - insertRecords(); + cout << "insertRecords\n" << endl; + insertRecords(); - cout << "insertTablets\n" << endl; - insertTablets(); + cout << "insertTablets\n" << endl; + insertTablets(); - cout << "insertTabletWithNullValues\n" << endl; - insertTabletWithNullValues(); + cout << "insertTabletWithNullValues\n" << endl; + insertTabletWithNullValues(); - cout << "nonQuery\n" << endl; - nonQuery(); + cout << "nonQuery\n" << endl; + nonQuery(); - cout << "queryLast\n" << endl; - queryLast(); + cout << "queryLast\n" << endl; + queryLast(); - cout << "query\n" << endl; - query(); + cout << "query\n" << endl; + query(); - cout << "deleteData\n" << endl; - deleteData(); + cout << "deleteData\n" << endl; + deleteData(); - cout << "deleteTimeseries\n" << endl; - deleteTimeseries(); + cout << "deleteTimeseries\n" << endl; + deleteTimeseries(); - cout << "deleteStorageGroups\n" << endl; - deleteStorageGroups(); + cout << "deleteStorageGroups\n" << endl; + deleteStorageGroups(); - cout << "session close\n" << endl; - session->close(); + cout << "session close\n" << endl; + session->close(); - delete session; + delete session; - cout << "finished!\n" << endl; - return 0; + cout << "finished!\n" << endl; + return 0; } diff --git a/example/client-cpp-example/src/TableModelSessionExample.cpp b/example/client-cpp-example/src/TableModelSessionExample.cpp index 7546f97d97052..3ae321e18f882 100644 --- a/example/client-cpp-example/src/TableModelSessionExample.cpp +++ b/example/client-cpp-example/src/TableModelSessionExample.cpp @@ -26,188 +26,184 @@ shared_ptr session; void insertRelationalTablet() { - vector> schemaList { - make_pair("region_id", TSDataType::TEXT), - make_pair("plant_id", TSDataType::TEXT), - make_pair("device_id", TSDataType::TEXT), - make_pair("model", TSDataType::TEXT), - make_pair("temperature", TSDataType::FLOAT), - make_pair("humidity", TSDataType::DOUBLE) - }; - - vector columnTypes = { - ColumnCategory::TAG, - ColumnCategory::TAG, - ColumnCategory::TAG, - ColumnCategory::ATTRIBUTE, - ColumnCategory::FIELD, - ColumnCategory::FIELD - }; - - Tablet tablet("table1", schemaList, columnTypes, 100); - - for (int row = 0; row < 100; row++) { - int rowIndex = tablet.rowSize++; - tablet.timestamps[rowIndex] = row; - tablet.addValue("region_id", rowIndex, "1"); - tablet.addValue("plant_id", rowIndex, "5"); - tablet.addValue("device_id", rowIndex, "3"); - tablet.addValue("model", rowIndex, "A"); - tablet.addValue("temperature", rowIndex, 37.6F); - tablet.addValue("humidity", rowIndex, 111.1); - if (tablet.rowSize == tablet.maxRowNumber) { - session->insert(tablet); - tablet.reset(); - } + vector> schemaList{ + make_pair("region_id", TSDataType::TEXT), make_pair("plant_id", TSDataType::TEXT), + make_pair("device_id", TSDataType::TEXT), make_pair("model", TSDataType::TEXT), + make_pair("temperature", TSDataType::FLOAT), make_pair("humidity", TSDataType::DOUBLE)}; + + vector columnTypes = {ColumnCategory::TAG, ColumnCategory::TAG, + ColumnCategory::TAG, ColumnCategory::ATTRIBUTE, + ColumnCategory::FIELD, ColumnCategory::FIELD}; + + Tablet tablet("table1", schemaList, columnTypes, 100); + + for (int row = 0; row < 100; row++) { + int rowIndex = tablet.rowSize++; + tablet.timestamps[rowIndex] = row; + tablet.addValue("region_id", rowIndex, "1"); + tablet.addValue("plant_id", rowIndex, "5"); + tablet.addValue("device_id", rowIndex, "3"); + tablet.addValue("model", rowIndex, "A"); + tablet.addValue("temperature", rowIndex, 37.6F); + tablet.addValue("humidity", rowIndex, 111.1); + if (tablet.rowSize == tablet.maxRowNumber) { + session->insert(tablet); + tablet.reset(); } + } - if (tablet.rowSize != 0) { - session->insert(tablet); - tablet.reset(); - } + if (tablet.rowSize != 0) { + session->insert(tablet); + tablet.reset(); + } +} + +void Output(unique_ptr& dataSet) { + for (const string& name : dataSet->getColumnNames()) { + cout << name << " "; + } + cout << endl; + while (dataSet->hasNext()) { + cout << dataSet->next()->toString(); + } + cout << endl; +} + +void OutputWithType(unique_ptr& dataSet) { + for (const string& name : dataSet->getColumnNames()) { + cout << name << " "; + } + cout << endl; + for (const string& type : dataSet->getColumnTypeList()) { + cout << type << " "; + } + cout << endl; + while (dataSet->hasNext()) { + cout << dataSet->next()->toString(); + } + cout << endl; } -void Output(unique_ptr &dataSet) { - for (const string &name: dataSet->getColumnNames()) { - cout << name << " "; +int main() { + try { + session = (new TableSessionBuilder()) + ->host("127.0.0.1") + ->rpcPort(6667) + ->username("root") + ->password("root") + ->build(); + + cout << "[Create Database db1,db2]\n" << endl; + try { + session->executeNonQueryStatement("CREATE DATABASE IF NOT EXISTS db1"); + session->executeNonQueryStatement("CREATE DATABASE IF NOT EXISTS db2"); + } catch (IoTDBException& e) { + cout << e.what() << endl; } - cout << endl; - while (dataSet->hasNext()) { - cout << dataSet->next()->toString(); + + cout << "[Use db1 as database]\n" << endl; + try { + session->executeNonQueryStatement("USE db1"); + } catch (IoTDBException& e) { + cout << e.what() << endl; } - cout << endl; -} -void OutputWithType(unique_ptr &dataSet) { - for (const string &name: dataSet->getColumnNames()) { - cout << name << " "; + cout << "[Create Table table1,table2]\n" << endl; + try { + 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)"); + 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)"); + } catch (IoTDBException& e) { + cout << e.what() << endl; } - cout << endl; - for (const string &type: dataSet->getColumnTypeList()) { - cout << type << " "; + + cout << "[Show Tables]\n" << endl; + try { + unique_ptr dataSet = session->executeQueryStatement("SHOW TABLES"); + Output(dataSet); + } catch (IoTDBException& e) { + cout << e.what() << endl; } - cout << endl; - while (dataSet->hasNext()) { - cout << dataSet->next()->toString(); + + cout << "[Show tables from specific database]\n" << endl; + try { + unique_ptr dataSet = session->executeQueryStatement("SHOW TABLES FROM db1"); + Output(dataSet); + } catch (IoTDBException& e) { + cout << e.what() << endl; } - cout << endl; -} -int main() { + cout << "[InsertTablet]\n" << endl; + try { + insertRelationalTablet(); + } catch (IoTDBException& e) { + cout << e.what() << endl; + } + + cout << "[Query Table Data]\n" << endl; + try { + unique_ptr dataSet = session->executeQueryStatement( + "SELECT * FROM table1" + " where region_id = '1' and plant_id in ('3', '5') and device_id = '3'"); + OutputWithType(dataSet); + } catch (IoTDBException& e) { + cout << e.what() << endl; + } + + session->close(); + + // specify database in constructor + session = (new TableSessionBuilder()) + ->host("127.0.0.1") + ->rpcPort(6667) + ->username("root") + ->password("root") + ->database("db1") + ->build(); + + cout << "[Show tables from current database(db1)]\n" << endl; + try { + unique_ptr dataSet = session->executeQueryStatement("SHOW TABLES"); + Output(dataSet); + } catch (IoTDBException& e) { + cout << e.what() << endl; + } + + cout << "[Change database to db2]\n" << endl; try { - session = (new TableSessionBuilder()) - ->host("127.0.0.1") - ->rpcPort(6667) - ->username("root") - ->password("root") - ->build(); - - - cout << "[Create Database db1,db2]\n" << endl; - try { - session->executeNonQueryStatement("CREATE DATABASE IF NOT EXISTS db1"); - session->executeNonQueryStatement("CREATE DATABASE IF NOT EXISTS db2"); - } catch (IoTDBException &e) { - cout << e.what() << endl; - } - - cout << "[Use db1 as database]\n" << endl; - try { - session->executeNonQueryStatement("USE db1"); - } catch (IoTDBException &e) { - cout << e.what() << endl; - } - - cout << "[Create Table table1,table2]\n" << endl; - try { - 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)"); - 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)"); - } catch (IoTDBException &e) { - cout << e.what() << endl; - } - - cout << "[Show Tables]\n" << endl; - try { - unique_ptr dataSet = session->executeQueryStatement("SHOW TABLES"); - Output(dataSet); - } catch (IoTDBException &e) { - cout << e.what() << endl; - } - - cout << "[Show tables from specific database]\n" << endl; - try { - unique_ptr dataSet = session->executeQueryStatement("SHOW TABLES FROM db1"); - Output(dataSet); - } catch (IoTDBException &e) { - cout << e.what() << endl; - } - - cout << "[InsertTablet]\n" << endl; - try { - insertRelationalTablet(); - } catch (IoTDBException &e) { - cout << e.what() << endl; - } - - cout << "[Query Table Data]\n" << endl; - try { - unique_ptr dataSet = session->executeQueryStatement("SELECT * FROM table1" - " where region_id = '1' and plant_id in ('3', '5') and device_id = '3'"); - OutputWithType(dataSet); - } catch (IoTDBException &e) { - cout << e.what() << endl; - } - - session->close(); - - // specify database in constructor - session = (new TableSessionBuilder()) - ->host("127.0.0.1") - ->rpcPort(6667) - ->username("root") - ->password("root") - ->database("db1") - ->build(); - - cout << "[Show tables from current database(db1)]\n" << endl; - try { - unique_ptr dataSet = session->executeQueryStatement("SHOW TABLES"); - Output(dataSet); - } catch (IoTDBException &e) { - cout << e.what() << endl; - } - - cout << "[Change database to db2]\n" << endl; - try { - session->executeNonQueryStatement("USE db2"); - } catch (IoTDBException &e) { - cout << e.what() << endl; - } - - cout << "[Show tables from current database(db2)]\n" << endl; - try { - unique_ptr dataSet = session->executeQueryStatement("SHOW TABLES"); - Output(dataSet); - } catch (IoTDBException &e) { - cout << e.what() << endl; - } - - cout << "[Drop Database db1,db2]\n" << endl; - try { - session->executeNonQueryStatement("DROP DATABASE db1"); - session->executeNonQueryStatement("DROP DATABASE db2"); - } catch (IoTDBException &e) { - cout << e.what() << endl; - } - - cout << "session close\n" << endl; - session->close(); - - cout << "finished!\n" << endl; - } catch (IoTDBConnectionException &e) { - cout << e.what() << endl; - } catch (IoTDBException &e) { - cout << e.what() << endl; + session->executeNonQueryStatement("USE db2"); + } catch (IoTDBException& e) { + cout << e.what() << endl; } - return 0; + + cout << "[Show tables from current database(db2)]\n" << endl; + try { + unique_ptr dataSet = session->executeQueryStatement("SHOW TABLES"); + Output(dataSet); + } catch (IoTDBException& e) { + cout << e.what() << endl; + } + + cout << "[Drop Database db1,db2]\n" << endl; + try { + session->executeNonQueryStatement("DROP DATABASE db1"); + session->executeNonQueryStatement("DROP DATABASE db2"); + } catch (IoTDBException& e) { + cout << e.what() << endl; + } + + cout << "session close\n" << endl; + session->close(); + + cout << "finished!\n" << endl; + } catch (IoTDBConnectionException& e) { + cout << e.what() << endl; + } catch (IoTDBException& e) { + cout << e.what() << endl; + } + return 0; } \ No newline at end of file diff --git a/iotdb-client/client-cpp/README.md b/iotdb-client/client-cpp/README.md index 15b24e9f52043..4806d72cf8aec 100644 --- a/iotdb-client/client-cpp/README.md +++ b/iotdb-client/client-cpp/README.md @@ -96,6 +96,38 @@ After run verify `mvn clean verify -P with-cpp -pl iotdb-client/client-cpp -am` +## Code Formatting + +We use `clang-format` as the only formatter for C++ code and trigger it through Maven Spotless. + +### Required version + +Use `clang-format 17.0.6` locally to keep behavior consistent with CI. + +### Install clang-format 17.0.6 + +- Linux (Ubuntu): `sudo apt-get install -y clang-format-17` +- macOS: `brew install llvm@17` and make sure `clang-format` from `llvm@17` is in `PATH` +- Windows: `choco install llvm --version=17.0.6 -y` + +### Validate only (no changes) + +`./mvnw -P with-cpp -pl iotdb-client/client-cpp spotless:check` + +`./mvnw -P with-cpp -pl example/client-cpp-example spotless:check` + +### Auto-fix formatting + +`./mvnw -P with-cpp -pl iotdb-client/client-cpp spotless:apply` + +`./mvnw -P with-cpp -pl example/client-cpp-example spotless:apply` + +### Windows (PowerShell) + +PowerShell may treat a comma in `-pl` as an argument separator. Prefer the two commands above. If you need a single invocation, quote the whole `-pl` value, for example: + +`./mvnw -P with-cpp "-pl=iotdb-client/client-cpp,example/client-cpp-example" spotless:check` + ## Package Hierarchy If the compilation finishes successfully, the packaged zip file will be placed under diff --git a/iotdb-client/client-cpp/src/main/AbstractSessionBuilder.h b/iotdb-client/client-cpp/src/main/AbstractSessionBuilder.h index 9355e213c81bf..9f59504d3887b 100644 --- a/iotdb-client/client-cpp/src/main/AbstractSessionBuilder.h +++ b/iotdb-client/client-cpp/src/main/AbstractSessionBuilder.h @@ -24,36 +24,36 @@ class AbstractSessionBuilder { public: - static constexpr const char* DEFAULT_HOST = "localhost"; - static constexpr int DEFAULT_RPC_PORT = 6667; - static constexpr const char* DEFAULT_USERNAME = "root"; - static constexpr const char* DEFAULT_PASSWORD = "root"; - static constexpr int DEFAULT_FETCH_SIZE = 10000; - static constexpr int DEFAULT_CONNECT_TIMEOUT_MS = 3 * 1000; - static constexpr int DEFAULT_MAX_RETRIES = 3; - static constexpr int DEFAULT_RETRY_DELAY_MS = 500; - static constexpr const char* DEFAULT_SQL_DIALECT = "tree"; - static constexpr bool DEFAULT_ENABLE_AUTO_FETCH = true; - static constexpr bool DEFAULT_ENABLE_REDIRECTIONS = true; - static constexpr bool DEFAULT_ENABLE_RPC_COMPRESSION = false; + static constexpr const char* DEFAULT_HOST = "localhost"; + static constexpr int DEFAULT_RPC_PORT = 6667; + static constexpr const char* DEFAULT_USERNAME = "root"; + static constexpr const char* DEFAULT_PASSWORD = "root"; + static constexpr int DEFAULT_FETCH_SIZE = 10000; + static constexpr int DEFAULT_CONNECT_TIMEOUT_MS = 3 * 1000; + static constexpr int DEFAULT_MAX_RETRIES = 3; + static constexpr int DEFAULT_RETRY_DELAY_MS = 500; + static constexpr const char* DEFAULT_SQL_DIALECT = "tree"; + static constexpr bool DEFAULT_ENABLE_AUTO_FETCH = true; + static constexpr bool DEFAULT_ENABLE_REDIRECTIONS = true; + static constexpr bool DEFAULT_ENABLE_RPC_COMPRESSION = false; - std::string host = DEFAULT_HOST; - int rpcPort = DEFAULT_RPC_PORT; - std::string username = DEFAULT_USERNAME; - std::string password = DEFAULT_PASSWORD; - std::string zoneId = ""; - int fetchSize = DEFAULT_FETCH_SIZE; - int connectTimeoutMs = DEFAULT_CONNECT_TIMEOUT_MS; - int maxRetries = DEFAULT_MAX_RETRIES; - int retryDelayMs = DEFAULT_RETRY_DELAY_MS; - std::string sqlDialect = DEFAULT_SQL_DIALECT; - std::string database = ""; - bool enableAutoFetch = DEFAULT_ENABLE_AUTO_FETCH; - bool enableRedirections = DEFAULT_ENABLE_REDIRECTIONS; - bool enableRPCCompression = DEFAULT_ENABLE_RPC_COMPRESSION; - std::vector nodeUrls; - bool useSSL = false; - std::string trustCertFilePath; + std::string host = DEFAULT_HOST; + int rpcPort = DEFAULT_RPC_PORT; + std::string username = DEFAULT_USERNAME; + std::string password = DEFAULT_PASSWORD; + std::string zoneId = ""; + int fetchSize = DEFAULT_FETCH_SIZE; + int connectTimeoutMs = DEFAULT_CONNECT_TIMEOUT_MS; + int maxRetries = DEFAULT_MAX_RETRIES; + int retryDelayMs = DEFAULT_RETRY_DELAY_MS; + std::string sqlDialect = DEFAULT_SQL_DIALECT; + std::string database = ""; + bool enableAutoFetch = DEFAULT_ENABLE_AUTO_FETCH; + bool enableRedirections = DEFAULT_ENABLE_REDIRECTIONS; + bool enableRPCCompression = DEFAULT_ENABLE_RPC_COMPRESSION; + std::vector nodeUrls; + bool useSSL = false; + std::string trustCertFilePath; }; #endif // IOTDB_ABSTRACTSESSIONBUILDER_H \ No newline at end of file diff --git a/iotdb-client/client-cpp/src/main/Column.cpp b/iotdb-client/client-cpp/src/main/Column.cpp index 4c533676d075a..203fcefd8d9a5 100644 --- a/iotdb-client/client-cpp/src/main/Column.cpp +++ b/iotdb-client/client-cpp/src/main/Column.cpp @@ -20,340 +20,459 @@ #include "Column.h" #include "ColumnDecoder.h" -TimeColumn::TimeColumn(int32_t arrayOffset, int32_t positionCount, const std::vector& values) +TimeColumn::TimeColumn(int32_t arrayOffset, int32_t positionCount, + const std::vector& values) : arrayOffset_(arrayOffset), positionCount_(positionCount), values_(values) { - if (arrayOffset < 0) throw IoTDBException("arrayOffset is negative"); - if (positionCount < 0) throw IoTDBException("positionCount is negative"); - if (static_cast(values.size()) - arrayOffset < positionCount) { - throw IoTDBException("values length is less than positionCount"); - } + if (arrayOffset < 0) + throw IoTDBException("arrayOffset is negative"); + if (positionCount < 0) + throw IoTDBException("positionCount is negative"); + if (static_cast(values.size()) - arrayOffset < positionCount) { + throw IoTDBException("values length is less than positionCount"); + } } -TSDataType::TSDataType TimeColumn::getDataType() const { return TSDataType::INT64; } -ColumnEncoding TimeColumn::getEncoding() const { return ColumnEncoding::Int64Array; } +TSDataType::TSDataType TimeColumn::getDataType() const { + return TSDataType::INT64; +} +ColumnEncoding TimeColumn::getEncoding() const { + return ColumnEncoding::Int64Array; +} int64_t TimeColumn::getLong(int32_t position) const { - return values_[position + arrayOffset_]; + return values_[position + arrayOffset_]; } -bool TimeColumn::mayHaveNull() const { return false; } -bool TimeColumn::isNull(int32_t position) const { return false; } -std::vector TimeColumn::isNulls() const { return {}; } +bool TimeColumn::mayHaveNull() const { + return false; +} +bool TimeColumn::isNull(int32_t position) const { + return false; +} +std::vector TimeColumn::isNulls() const { + return {}; +} -int32_t TimeColumn::getPositionCount() const { return positionCount_; } +int32_t TimeColumn::getPositionCount() const { + return positionCount_; +} -int64_t TimeColumn::getStartTime() const { return values_[arrayOffset_]; } -int64_t TimeColumn::getEndTime() const { return values_[positionCount_ + arrayOffset_ - 1]; } +int64_t TimeColumn::getStartTime() const { + return values_[arrayOffset_]; +} +int64_t TimeColumn::getEndTime() const { + return values_[positionCount_ + arrayOffset_ - 1]; +} -const std::vector& TimeColumn::getTimes() const { return values_; } -std::vector TimeColumn::getLongs() const { return getTimes(); } +const std::vector& TimeColumn::getTimes() const { + return values_; +} +std::vector TimeColumn::getLongs() const { + return getTimes(); +} BinaryColumn::BinaryColumn(int32_t arrayOffset, int32_t positionCount, - const std::vector& valueIsNull, const std::vector>& values) - : arrayOffset_(arrayOffset), positionCount_(positionCount), - valueIsNull_(valueIsNull), values_(values) { - if (arrayOffset < 0) throw IoTDBException("arrayOffset is negative"); - if (positionCount < 0) throw IoTDBException("positionCount is negative"); - if (static_cast(values.size()) - arrayOffset < positionCount) { - throw IoTDBException("values length is less than positionCount"); - } - if (!valueIsNull.empty() && static_cast(valueIsNull.size()) - arrayOffset < positionCount) { - throw IoTDBException("isNull length is less than positionCount"); - } -} - -TSDataType::TSDataType BinaryColumn::getDataType() const { return TSDataType::TSDataType::TEXT; } -ColumnEncoding BinaryColumn::getEncoding() const { return ColumnEncoding::BinaryArray; } + const std::vector& valueIsNull, + const std::vector>& values) + : arrayOffset_(arrayOffset), positionCount_(positionCount), valueIsNull_(valueIsNull), + values_(values) { + if (arrayOffset < 0) + throw IoTDBException("arrayOffset is negative"); + if (positionCount < 0) + throw IoTDBException("positionCount is negative"); + if (static_cast(values.size()) - arrayOffset < positionCount) { + throw IoTDBException("values length is less than positionCount"); + } + if (!valueIsNull.empty() && + static_cast(valueIsNull.size()) - arrayOffset < positionCount) { + throw IoTDBException("isNull length is less than positionCount"); + } +} + +TSDataType::TSDataType BinaryColumn::getDataType() const { + return TSDataType::TSDataType::TEXT; +} +ColumnEncoding BinaryColumn::getEncoding() const { + return ColumnEncoding::BinaryArray; +} std::shared_ptr BinaryColumn::getBinary(int32_t position) const { - return values_[position + arrayOffset_]; + return values_[position + arrayOffset_]; } -std::vector> BinaryColumn::getBinaries() const { return values_; } - +std::vector> BinaryColumn::getBinaries() const { + return values_; +} -bool BinaryColumn::mayHaveNull() const { return !valueIsNull_.empty(); } +bool BinaryColumn::mayHaveNull() const { + return !valueIsNull_.empty(); +} bool BinaryColumn::isNull(int32_t position) const { - return !valueIsNull_.empty() && valueIsNull_[position + arrayOffset_]; + return !valueIsNull_.empty() && valueIsNull_[position + arrayOffset_]; } std::vector BinaryColumn::isNulls() const { - if (!valueIsNull_.empty()) return valueIsNull_; + if (!valueIsNull_.empty()) + return valueIsNull_; - std::vector result(positionCount_, false); - return result; + std::vector result(positionCount_, false); + return result; } -int32_t BinaryColumn::getPositionCount() const { return positionCount_; } +int32_t BinaryColumn::getPositionCount() const { + return positionCount_; +} IntColumn::IntColumn(int32_t arrayOffset, int32_t positionCount, const std::vector& valueIsNull, const std::vector& values) - : arrayOffset_(arrayOffset), positionCount_(positionCount), - valueNull_(valueIsNull), values_(values) { - if (arrayOffset < 0) throw IoTDBException("arrayOffset is negative"); - if (positionCount < 0) throw IoTDBException("positionCount is negative"); - if (static_cast(values.size()) - arrayOffset < positionCount) { - throw IoTDBException("values length is less than positionCount"); - } - if (!valueIsNull.empty() && static_cast(valueIsNull.size()) - arrayOffset < positionCount) { - throw IoTDBException("isNull length is less than positionCount"); - } + : arrayOffset_(arrayOffset), positionCount_(positionCount), valueNull_(valueIsNull), + values_(values) { + if (arrayOffset < 0) + throw IoTDBException("arrayOffset is negative"); + if (positionCount < 0) + throw IoTDBException("positionCount is negative"); + if (static_cast(values.size()) - arrayOffset < positionCount) { + throw IoTDBException("values length is less than positionCount"); + } + if (!valueIsNull.empty() && + static_cast(valueIsNull.size()) - arrayOffset < positionCount) { + throw IoTDBException("isNull length is less than positionCount"); + } } -TSDataType::TSDataType IntColumn::getDataType() const { return TSDataType::INT32; } -ColumnEncoding IntColumn::getEncoding() const { return ColumnEncoding::Int32Array; } +TSDataType::TSDataType IntColumn::getDataType() const { + return TSDataType::INT32; +} +ColumnEncoding IntColumn::getEncoding() const { + return ColumnEncoding::Int32Array; +} int32_t IntColumn::getInt(int32_t position) const { - return values_[position + arrayOffset_]; + return values_[position + arrayOffset_]; } -std::vector IntColumn::getInts() const { return values_; } +std::vector IntColumn::getInts() const { + return values_; +} -bool IntColumn::mayHaveNull() const { return !valueNull_.empty(); } +bool IntColumn::mayHaveNull() const { + return !valueNull_.empty(); +} bool IntColumn::isNull(int32_t position) const { - return !valueNull_.empty() && valueNull_[position + arrayOffset_]; + return !valueNull_.empty() && valueNull_[position + arrayOffset_]; } std::vector IntColumn::isNulls() const { - if (!valueNull_.empty()) return valueNull_; + if (!valueNull_.empty()) + return valueNull_; - std::vector result(positionCount_, false); - return result; + std::vector result(positionCount_, false); + return result; } -int32_t IntColumn::getPositionCount() const { return positionCount_; } +int32_t IntColumn::getPositionCount() const { + return positionCount_; +} FloatColumn::FloatColumn(int32_t arrayOffset, int32_t positionCount, const std::vector& valueIsNull, const std::vector& values) - : arrayOffset_(arrayOffset), positionCount_(positionCount), - valueIsNull_(valueIsNull), values_(values) { - if (arrayOffset < 0) throw IoTDBException("arrayOffset is negative"); - if (positionCount < 0) throw IoTDBException("positionCount is negative"); - if (static_cast(values.size()) - arrayOffset < positionCount) { - throw IoTDBException("values length is less than positionCount"); - } - if (!valueIsNull.empty() && static_cast(valueIsNull.size()) - arrayOffset < positionCount) { - throw IoTDBException("isNull length is less than positionCount"); - } + : arrayOffset_(arrayOffset), positionCount_(positionCount), valueIsNull_(valueIsNull), + values_(values) { + if (arrayOffset < 0) + throw IoTDBException("arrayOffset is negative"); + if (positionCount < 0) + throw IoTDBException("positionCount is negative"); + if (static_cast(values.size()) - arrayOffset < positionCount) { + throw IoTDBException("values length is less than positionCount"); + } + if (!valueIsNull.empty() && + static_cast(valueIsNull.size()) - arrayOffset < positionCount) { + throw IoTDBException("isNull length is less than positionCount"); + } } -TSDataType::TSDataType FloatColumn::getDataType() const { return TSDataType::TSDataType::FLOAT; } -ColumnEncoding FloatColumn::getEncoding() const { return ColumnEncoding::Int32Array; } +TSDataType::TSDataType FloatColumn::getDataType() const { + return TSDataType::TSDataType::FLOAT; +} +ColumnEncoding FloatColumn::getEncoding() const { + return ColumnEncoding::Int32Array; +} float FloatColumn::getFloat(int32_t position) const { - return values_[position + arrayOffset_]; + return values_[position + arrayOffset_]; } -std::vector FloatColumn::getFloats() const { return values_; } +std::vector FloatColumn::getFloats() const { + return values_; +} -bool FloatColumn::mayHaveNull() const { return !valueIsNull_.empty(); } +bool FloatColumn::mayHaveNull() const { + return !valueIsNull_.empty(); +} bool FloatColumn::isNull(int32_t position) const { - return !valueIsNull_.empty() && valueIsNull_[position + arrayOffset_]; + return !valueIsNull_.empty() && valueIsNull_[position + arrayOffset_]; } std::vector FloatColumn::isNulls() const { - if (!valueIsNull_.empty()) return valueIsNull_; + if (!valueIsNull_.empty()) + return valueIsNull_; - std::vector result(positionCount_, false); - return result; + std::vector result(positionCount_, false); + return result; } -int32_t FloatColumn::getPositionCount() const { return positionCount_; } +int32_t FloatColumn::getPositionCount() const { + return positionCount_; +} LongColumn::LongColumn(int32_t arrayOffset, int32_t positionCount, const std::vector& valueIsNull, const std::vector& values) - : arrayOffset_(arrayOffset), positionCount_(positionCount), - valueIsNull_(valueIsNull), values_(values) { - if (arrayOffset < 0) throw IoTDBException("arrayOffset is negative"); - if (positionCount < 0) throw IoTDBException("positionCount is negative"); - if (static_cast(values.size()) - arrayOffset < positionCount) { - throw IoTDBException("values length is less than positionCount"); - } - if (!valueIsNull.empty() && static_cast(valueIsNull.size()) - arrayOffset < positionCount) { - throw IoTDBException("isNull length is less than positionCount"); - } + : arrayOffset_(arrayOffset), positionCount_(positionCount), valueIsNull_(valueIsNull), + values_(values) { + if (arrayOffset < 0) + throw IoTDBException("arrayOffset is negative"); + if (positionCount < 0) + throw IoTDBException("positionCount is negative"); + if (static_cast(values.size()) - arrayOffset < positionCount) { + throw IoTDBException("values length is less than positionCount"); + } + if (!valueIsNull.empty() && + static_cast(valueIsNull.size()) - arrayOffset < positionCount) { + throw IoTDBException("isNull length is less than positionCount"); + } } -TSDataType::TSDataType LongColumn::getDataType() const { return TSDataType::TSDataType::INT64; } -ColumnEncoding LongColumn::getEncoding() const { return ColumnEncoding::Int64Array; } +TSDataType::TSDataType LongColumn::getDataType() const { + return TSDataType::TSDataType::INT64; +} +ColumnEncoding LongColumn::getEncoding() const { + return ColumnEncoding::Int64Array; +} int64_t LongColumn::getLong(int32_t position) const { - return values_[position + arrayOffset_]; + return values_[position + arrayOffset_]; } -std::vector LongColumn::getLongs() const { return values_; } +std::vector LongColumn::getLongs() const { + return values_; +} -bool LongColumn::mayHaveNull() const { return !valueIsNull_.empty(); } +bool LongColumn::mayHaveNull() const { + return !valueIsNull_.empty(); +} bool LongColumn::isNull(int32_t position) const { - return !valueIsNull_.empty() && valueIsNull_[position + arrayOffset_]; + return !valueIsNull_.empty() && valueIsNull_[position + arrayOffset_]; } std::vector LongColumn::isNulls() const { - if (!valueIsNull_.empty()) return valueIsNull_; + if (!valueIsNull_.empty()) + return valueIsNull_; - std::vector result(positionCount_, false); - return result; + std::vector result(positionCount_, false); + return result; } -int32_t LongColumn::getPositionCount() const { return positionCount_; } +int32_t LongColumn::getPositionCount() const { + return positionCount_; +} DoubleColumn::DoubleColumn(int32_t arrayOffset, int32_t positionCount, const std::vector& valueIsNull, const std::vector& values) - : arrayOffset_(arrayOffset), positionCount_(positionCount), - valueIsNull_(valueIsNull), values_(values) { - if (arrayOffset < 0) throw IoTDBException("arrayOffset is negative"); - if (positionCount < 0) throw IoTDBException("positionCount is negative"); - if (static_cast(values.size()) - arrayOffset < positionCount) { - throw IoTDBException("values length is less than positionCount"); - } - if (!valueIsNull.empty() && static_cast(valueIsNull.size()) - arrayOffset < positionCount) { - throw IoTDBException("isNull length is less than positionCount"); - } + : arrayOffset_(arrayOffset), positionCount_(positionCount), valueIsNull_(valueIsNull), + values_(values) { + if (arrayOffset < 0) + throw IoTDBException("arrayOffset is negative"); + if (positionCount < 0) + throw IoTDBException("positionCount is negative"); + if (static_cast(values.size()) - arrayOffset < positionCount) { + throw IoTDBException("values length is less than positionCount"); + } + if (!valueIsNull.empty() && + static_cast(valueIsNull.size()) - arrayOffset < positionCount) { + throw IoTDBException("isNull length is less than positionCount"); + } } -TSDataType::TSDataType DoubleColumn::getDataType() const { return TSDataType::TSDataType::DOUBLE; } -ColumnEncoding DoubleColumn::getEncoding() const { return ColumnEncoding::Int64Array; } +TSDataType::TSDataType DoubleColumn::getDataType() const { + return TSDataType::TSDataType::DOUBLE; +} +ColumnEncoding DoubleColumn::getEncoding() const { + return ColumnEncoding::Int64Array; +} double DoubleColumn::getDouble(int32_t position) const { - return values_[position + arrayOffset_]; + return values_[position + arrayOffset_]; } -std::vector DoubleColumn::getDoubles() const { return values_; } +std::vector DoubleColumn::getDoubles() const { + return values_; +} -bool DoubleColumn::mayHaveNull() const { return !valueIsNull_.empty(); } +bool DoubleColumn::mayHaveNull() const { + return !valueIsNull_.empty(); +} bool DoubleColumn::isNull(int32_t position) const { - return !valueIsNull_.empty() && valueIsNull_[position + arrayOffset_]; + return !valueIsNull_.empty() && valueIsNull_[position + arrayOffset_]; } std::vector DoubleColumn::isNulls() const { - if (!valueIsNull_.empty()) return valueIsNull_; + if (!valueIsNull_.empty()) + return valueIsNull_; - std::vector result(positionCount_, false); - return result; + std::vector result(positionCount_, false); + return result; } -int32_t DoubleColumn::getPositionCount() const { return positionCount_; } +int32_t DoubleColumn::getPositionCount() const { + return positionCount_; +} BooleanColumn::BooleanColumn(int32_t arrayOffset, int32_t positionCount, const std::vector& valueIsNull, const std::vector& values) - : arrayOffset_(arrayOffset), positionCount_(positionCount), - valueIsNull_(valueIsNull), values_(values) { - if (arrayOffset < 0) throw IoTDBException("arrayOffset is negative"); - if (positionCount < 0) throw IoTDBException("positionCount is negative"); - if (static_cast(values.size()) - arrayOffset < positionCount) { - throw IoTDBException("values length is less than positionCount"); - } - if (!valueIsNull.empty() && static_cast(valueIsNull.size()) - arrayOffset < positionCount) { - throw IoTDBException("isNull length is less than positionCount"); - } + : arrayOffset_(arrayOffset), positionCount_(positionCount), valueIsNull_(valueIsNull), + values_(values) { + if (arrayOffset < 0) + throw IoTDBException("arrayOffset is negative"); + if (positionCount < 0) + throw IoTDBException("positionCount is negative"); + if (static_cast(values.size()) - arrayOffset < positionCount) { + throw IoTDBException("values length is less than positionCount"); + } + if (!valueIsNull.empty() && + static_cast(valueIsNull.size()) - arrayOffset < positionCount) { + throw IoTDBException("isNull length is less than positionCount"); + } } -TSDataType::TSDataType BooleanColumn::getDataType() const { return TSDataType::TSDataType::BOOLEAN; } -ColumnEncoding BooleanColumn::getEncoding() const { return ColumnEncoding::ByteArray; } +TSDataType::TSDataType BooleanColumn::getDataType() const { + return TSDataType::TSDataType::BOOLEAN; +} +ColumnEncoding BooleanColumn::getEncoding() const { + return ColumnEncoding::ByteArray; +} bool BooleanColumn::getBoolean(int32_t position) const { - return values_[position + arrayOffset_]; + return values_[position + arrayOffset_]; } -std::vector BooleanColumn::getBooleans() const { return values_; } +std::vector BooleanColumn::getBooleans() const { + return values_; +} -bool BooleanColumn::mayHaveNull() const { return !valueIsNull_.empty(); } +bool BooleanColumn::mayHaveNull() const { + return !valueIsNull_.empty(); +} bool BooleanColumn::isNull(int32_t position) const { - return !valueIsNull_.empty() && valueIsNull_[position + arrayOffset_]; + return !valueIsNull_.empty() && valueIsNull_[position + arrayOffset_]; } std::vector BooleanColumn::isNulls() const { - if (!valueIsNull_.empty()) return valueIsNull_; + if (!valueIsNull_.empty()) + return valueIsNull_; - std::vector result(positionCount_, false); - return result; + std::vector result(positionCount_, false); + return result; } -int32_t BooleanColumn::getPositionCount() const { return positionCount_; } +int32_t BooleanColumn::getPositionCount() const { + return positionCount_; +} RunLengthEncodedColumn::RunLengthEncodedColumn(std::shared_ptr value, int32_t positionCount) : value_(value), positionCount_(positionCount) { - if (!value) throw IoTDBException("value is null"); - if (value->getPositionCount() != 1) { - throw IoTDBException("Expected value to contain a single position"); - } - if (positionCount < 0) throw IoTDBException("positionCount is negative"); + if (!value) + throw IoTDBException("value is null"); + if (value->getPositionCount() != 1) { + throw IoTDBException("Expected value to contain a single position"); + } + if (positionCount < 0) + throw IoTDBException("positionCount is negative"); } -std::shared_ptr RunLengthEncodedColumn::getValue() const { return value_; } +std::shared_ptr RunLengthEncodedColumn::getValue() const { + return value_; +} -TSDataType::TSDataType RunLengthEncodedColumn::getDataType() const { return value_->getDataType(); } -ColumnEncoding RunLengthEncodedColumn::getEncoding() const { return ColumnEncoding::Rle; } +TSDataType::TSDataType RunLengthEncodedColumn::getDataType() const { + return value_->getDataType(); +} +ColumnEncoding RunLengthEncodedColumn::getEncoding() const { + return ColumnEncoding::Rle; +} bool RunLengthEncodedColumn::getBoolean(int32_t position) const { - return value_->getBoolean(0); + return value_->getBoolean(0); } int32_t RunLengthEncodedColumn::getInt(int32_t position) const { - return value_->getInt(0); + return value_->getInt(0); } int64_t RunLengthEncodedColumn::getLong(int32_t position) const { - return value_->getLong(0); + return value_->getLong(0); } float RunLengthEncodedColumn::getFloat(int32_t position) const { - return value_->getFloat(0); + return value_->getFloat(0); } double RunLengthEncodedColumn::getDouble(int32_t position) const { - return value_->getDouble(0); + return value_->getDouble(0); } std::shared_ptr RunLengthEncodedColumn::getBinary(int32_t position) const { - return value_->getBinary(0); + return value_->getBinary(0); } std::vector RunLengthEncodedColumn::getBooleans() const { - bool v = value_->getBoolean(0); - return std::vector(positionCount_, v); + bool v = value_->getBoolean(0); + return std::vector(positionCount_, v); } std::vector RunLengthEncodedColumn::getInts() const { - int32_t v = value_->getInt(0); - return std::vector(positionCount_, v); + int32_t v = value_->getInt(0); + return std::vector(positionCount_, v); } std::vector RunLengthEncodedColumn::getLongs() const { - int64_t v = value_->getLong(0); - return std::vector(positionCount_, v); + int64_t v = value_->getLong(0); + return std::vector(positionCount_, v); } std::vector RunLengthEncodedColumn::getFloats() const { - float v = value_->getFloat(0); - return std::vector(positionCount_, v); + float v = value_->getFloat(0); + return std::vector(positionCount_, v); } std::vector RunLengthEncodedColumn::getDoubles() const { - double v = value_->getDouble(0); - return std::vector(positionCount_, v); + double v = value_->getDouble(0); + return std::vector(positionCount_, v); } std::vector> RunLengthEncodedColumn::getBinaries() const { - auto v = value_->getBinary(0); - return std::vector>(positionCount_, v); + auto v = value_->getBinary(0); + return std::vector>(positionCount_, v); } -bool RunLengthEncodedColumn::mayHaveNull() const { return value_->mayHaveNull(); } +bool RunLengthEncodedColumn::mayHaveNull() const { + return value_->mayHaveNull(); +} bool RunLengthEncodedColumn::isNull(int32_t position) const { - return value_->isNull(0); + return value_->isNull(0); } std::vector RunLengthEncodedColumn::isNulls() const { - bool v = value_->isNull(0); - return std::vector(positionCount_, v); + bool v = value_->isNull(0); + return std::vector(positionCount_, v); } -int32_t RunLengthEncodedColumn::getPositionCount() const { return positionCount_; } +int32_t RunLengthEncodedColumn::getPositionCount() const { + return positionCount_; +} diff --git a/iotdb-client/client-cpp/src/main/Column.h b/iotdb-client/client-cpp/src/main/Column.h index 04f611f41a874..8794e7b8d5e34 100644 --- a/iotdb-client/client-cpp/src/main/Column.h +++ b/iotdb-client/client-cpp/src/main/Column.h @@ -27,327 +27,318 @@ #include "Common.h" #include "ColumnDecoder.h" -enum class ColumnEncoding : uint8_t { - ByteArray, - Int32Array, - Int64Array, - BinaryArray, - Rle -}; +enum class ColumnEncoding : uint8_t { ByteArray, Int32Array, Int64Array, BinaryArray, Rle }; class Binary { public: - explicit Binary(std::vector data) : data_(std::move(data)) { - } + explicit Binary(std::vector data) : data_(std::move(data)) {} - const std::vector& getData() const { return data_; } + const std::vector& getData() const { + return data_; + } - std::string getStringValue() const { - return {data_.begin(), data_.end()}; - } + std::string getStringValue() const { + return {data_.begin(), data_.end()}; + } private: - std::vector data_; + std::vector data_; }; - const std::map> kEncodingToDecoder = { {ColumnEncoding::Int32Array, std::make_shared()}, {ColumnEncoding::Int64Array, std::make_shared()}, {ColumnEncoding::ByteArray, std::make_shared()}, {ColumnEncoding::BinaryArray, std::make_shared()}, - {ColumnEncoding::Rle, std::make_shared()} -}; + {ColumnEncoding::Rle, std::make_shared()}}; -const std::map kByteToEncoding = { - {0, ColumnEncoding::ByteArray}, - {1, ColumnEncoding::Int32Array}, - {2, ColumnEncoding::Int64Array}, - {3, ColumnEncoding::BinaryArray}, - {4, ColumnEncoding::Rle} -}; +const std::map kByteToEncoding = {{0, ColumnEncoding::ByteArray}, + {1, ColumnEncoding::Int32Array}, + {2, ColumnEncoding::Int64Array}, + {3, ColumnEncoding::BinaryArray}, + {4, ColumnEncoding::Rle}}; inline std::shared_ptr getColumnDecoder(ColumnEncoding encoding) { - auto it = kEncodingToDecoder.find(encoding); - if (it == kEncodingToDecoder.end()) { - throw IoTDBException("Unsupported column encoding"); - } - return it->second; + auto it = kEncodingToDecoder.find(encoding); + if (it == kEncodingToDecoder.end()) { + throw IoTDBException("Unsupported column encoding"); + } + return it->second; } inline ColumnEncoding getColumnEncodingByByte(uint8_t b) { - auto it = kByteToEncoding.find(b); - if (it == kByteToEncoding.end()) { - throw IoTDBException("Invalid encoding value: " + std::to_string(b)); - } - return it->second; + auto it = kByteToEncoding.find(b); + if (it == kByteToEncoding.end()) { + throw IoTDBException("Invalid encoding value: " + std::to_string(b)); + } + return it->second; } class Column { public: - virtual ~Column() = default; + virtual ~Column() = default; - virtual TSDataType::TSDataType getDataType() const = 0; - virtual ColumnEncoding getEncoding() const = 0; + virtual TSDataType::TSDataType getDataType() const = 0; + virtual ColumnEncoding getEncoding() const = 0; - virtual bool getBoolean(int32_t position) const { - throw IoTDBException("Unsupported operation: getBoolean"); - } + virtual bool getBoolean(int32_t position) const { + throw IoTDBException("Unsupported operation: getBoolean"); + } - virtual int32_t getInt(int32_t position) const { - throw IoTDBException("Unsupported operation: getInt"); - } + virtual int32_t getInt(int32_t position) const { + throw IoTDBException("Unsupported operation: getInt"); + } - virtual int64_t getLong(int32_t position) const { - throw IoTDBException("Unsupported operation: getLong"); - } + virtual int64_t getLong(int32_t position) const { + throw IoTDBException("Unsupported operation: getLong"); + } - virtual float getFloat(int32_t position) const { - throw IoTDBException("Unsupported operation: getFloat"); - } + virtual float getFloat(int32_t position) const { + throw IoTDBException("Unsupported operation: getFloat"); + } - virtual double getDouble(int32_t position) const { - throw IoTDBException("Unsupported operation: getDouble"); - } + virtual double getDouble(int32_t position) const { + throw IoTDBException("Unsupported operation: getDouble"); + } - virtual std::shared_ptr getBinary(int32_t position) const { - throw IoTDBException("Unsupported operation: getBinary"); - } + virtual std::shared_ptr getBinary(int32_t position) const { + throw IoTDBException("Unsupported operation: getBinary"); + } - virtual std::vector getBooleans() const { - throw IoTDBException("Unsupported operation: getBooleans"); - } + virtual std::vector getBooleans() const { + throw IoTDBException("Unsupported operation: getBooleans"); + } - virtual std::vector getInts() const { - throw IoTDBException("Unsupported operation: getInts"); - } + virtual std::vector getInts() const { + throw IoTDBException("Unsupported operation: getInts"); + } - virtual std::vector getLongs() const { - throw IoTDBException("Unsupported operation: getLongs"); - } + virtual std::vector getLongs() const { + throw IoTDBException("Unsupported operation: getLongs"); + } - virtual std::vector getFloats() const { - throw IoTDBException("Unsupported operation: getFloats"); - } + virtual std::vector getFloats() const { + throw IoTDBException("Unsupported operation: getFloats"); + } - virtual std::vector getDoubles() const { - throw IoTDBException("Unsupported operation: getDoubles"); - } + virtual std::vector getDoubles() const { + throw IoTDBException("Unsupported operation: getDoubles"); + } - virtual std::vector> getBinaries() const { - throw IoTDBException("Unsupported operation: getBinaries"); - } + virtual std::vector> getBinaries() const { + throw IoTDBException("Unsupported operation: getBinaries"); + } - virtual bool mayHaveNull() const = 0; - virtual bool isNull(int32_t position) const = 0; - virtual std::vector isNulls() const = 0; + virtual bool mayHaveNull() const = 0; + virtual bool isNull(int32_t position) const = 0; + virtual std::vector isNulls() const = 0; - virtual int32_t getPositionCount() const = 0; + virtual int32_t getPositionCount() const = 0; }; class TimeColumn : public Column { public: - TimeColumn(int32_t arrayOffset, int32_t positionCount, const std::vector& values); + TimeColumn(int32_t arrayOffset, int32_t positionCount, const std::vector& values); - TSDataType::TSDataType getDataType() const override; - ColumnEncoding getEncoding() const override; + TSDataType::TSDataType getDataType() const override; + ColumnEncoding getEncoding() const override; - int64_t getLong(int32_t position) const override; + int64_t getLong(int32_t position) const override; - bool mayHaveNull() const override; - bool isNull(int32_t position) const override; - std::vector isNulls() const override; + bool mayHaveNull() const override; + bool isNull(int32_t position) const override; + std::vector isNulls() const override; - int32_t getPositionCount() const override; + int32_t getPositionCount() const override; - int64_t getStartTime() const; - int64_t getEndTime() const; + int64_t getStartTime() const; + int64_t getEndTime() const; - const std::vector& getTimes() const; - std::vector getLongs() const override; + const std::vector& getTimes() const; + std::vector getLongs() const override; private: - int32_t arrayOffset_; - int32_t positionCount_; - std::vector values_; + int32_t arrayOffset_; + int32_t positionCount_; + std::vector values_; }; class BinaryColumn : public Column { public: - BinaryColumn(int32_t arrayOffset, int32_t positionCount, - const std::vector& valueIsNull, const std::vector>& values); + BinaryColumn(int32_t arrayOffset, int32_t positionCount, const std::vector& valueIsNull, + const std::vector>& values); - TSDataType::TSDataType getDataType() const override; - ColumnEncoding getEncoding() const override; + TSDataType::TSDataType getDataType() const override; + ColumnEncoding getEncoding() const override; - std::shared_ptr getBinary(int32_t position) const override; - std::vector> getBinaries() const override; + std::shared_ptr getBinary(int32_t position) const override; + std::vector> getBinaries() const override; - bool mayHaveNull() const override; - bool isNull(int32_t position) const override; - std::vector isNulls() const override; + bool mayHaveNull() const override; + bool isNull(int32_t position) const override; + std::vector isNulls() const override; - int32_t getPositionCount() const override; + int32_t getPositionCount() const override; private: - int32_t arrayOffset_; - int32_t positionCount_; - std::vector valueIsNull_; - std::vector> values_; + int32_t arrayOffset_; + int32_t positionCount_; + std::vector valueIsNull_; + std::vector> values_; }; class IntColumn : public Column { public: - IntColumn(int32_t arrayOffset, int32_t positionCount, - const std::vector& valueIsNull, const std::vector& values); + IntColumn(int32_t arrayOffset, int32_t positionCount, const std::vector& valueIsNull, + const std::vector& values); - TSDataType::TSDataType getDataType() const override; - ColumnEncoding getEncoding() const override; + TSDataType::TSDataType getDataType() const override; + ColumnEncoding getEncoding() const override; - int32_t getInt(int32_t position) const override; - std::vector getInts() const override; + int32_t getInt(int32_t position) const override; + std::vector getInts() const override; - bool mayHaveNull() const override; - bool isNull(int32_t position) const override; - std::vector isNulls() const override; + bool mayHaveNull() const override; + bool isNull(int32_t position) const override; + std::vector isNulls() const override; - int32_t getPositionCount() const override; + int32_t getPositionCount() const override; private: - int32_t arrayOffset_; - int32_t positionCount_; - std::vector valueNull_; - std::vector values_; + int32_t arrayOffset_; + int32_t positionCount_; + std::vector valueNull_; + std::vector values_; }; class FloatColumn : public Column { public: - FloatColumn(int32_t arrayOffset, int32_t positionCount, - const std::vector& valueIsNull, const std::vector& values); + FloatColumn(int32_t arrayOffset, int32_t positionCount, const std::vector& valueIsNull, + const std::vector& values); - TSDataType::TSDataType getDataType() const override; - ColumnEncoding getEncoding() const override; + TSDataType::TSDataType getDataType() const override; + ColumnEncoding getEncoding() const override; - float getFloat(int32_t position) const override; - std::vector getFloats() const override; + float getFloat(int32_t position) const override; + std::vector getFloats() const override; - bool mayHaveNull() const override; - bool isNull(int32_t position) const override; - std::vector isNulls() const override; + bool mayHaveNull() const override; + bool isNull(int32_t position) const override; + std::vector isNulls() const override; - int32_t getPositionCount() const override; + int32_t getPositionCount() const override; private: - int32_t arrayOffset_; - int32_t positionCount_; - std::vector valueIsNull_; - std::vector values_; + int32_t arrayOffset_; + int32_t positionCount_; + std::vector valueIsNull_; + std::vector values_; }; class LongColumn : public Column { public: - LongColumn(int32_t arrayOffset, int32_t positionCount, - const std::vector& valueIsNull, const std::vector& values); + LongColumn(int32_t arrayOffset, int32_t positionCount, const std::vector& valueIsNull, + const std::vector& values); - TSDataType::TSDataType getDataType() const override; - ColumnEncoding getEncoding() const override; + TSDataType::TSDataType getDataType() const override; + ColumnEncoding getEncoding() const override; - int64_t getLong(int32_t position) const override; - std::vector getLongs() const override; + int64_t getLong(int32_t position) const override; + std::vector getLongs() const override; - bool mayHaveNull() const override; - bool isNull(int32_t position) const override; - std::vector isNulls() const override; + bool mayHaveNull() const override; + bool isNull(int32_t position) const override; + std::vector isNulls() const override; - int32_t getPositionCount() const override; + int32_t getPositionCount() const override; private: - int32_t arrayOffset_; - int32_t positionCount_; - std::vector valueIsNull_; - std::vector values_; + int32_t arrayOffset_; + int32_t positionCount_; + std::vector valueIsNull_; + std::vector values_; }; class DoubleColumn : public Column { public: - DoubleColumn(int32_t arrayOffset, int32_t positionCount, - const std::vector& valueIsNull, const std::vector& values); + DoubleColumn(int32_t arrayOffset, int32_t positionCount, const std::vector& valueIsNull, + const std::vector& values); - TSDataType::TSDataType getDataType() const override; - ColumnEncoding getEncoding() const override; + TSDataType::TSDataType getDataType() const override; + ColumnEncoding getEncoding() const override; - double getDouble(int32_t position) const override; - std::vector getDoubles() const override; + double getDouble(int32_t position) const override; + std::vector getDoubles() const override; - bool mayHaveNull() const override; - bool isNull(int32_t position) const override; - std::vector isNulls() const override; + bool mayHaveNull() const override; + bool isNull(int32_t position) const override; + std::vector isNulls() const override; - int32_t getPositionCount() const override; + int32_t getPositionCount() const override; private: - int32_t arrayOffset_; - int32_t positionCount_; - std::vector valueIsNull_; - std::vector values_; + int32_t arrayOffset_; + int32_t positionCount_; + std::vector valueIsNull_; + std::vector values_; }; class BooleanColumn : public Column { public: - BooleanColumn(int32_t arrayOffset, int32_t positionCount, - const std::vector& valueIsNull, const std::vector& values); + BooleanColumn(int32_t arrayOffset, int32_t positionCount, const std::vector& valueIsNull, + const std::vector& values); - TSDataType::TSDataType getDataType() const override; - ColumnEncoding getEncoding() const override; + TSDataType::TSDataType getDataType() const override; + ColumnEncoding getEncoding() const override; - bool getBoolean(int32_t position) const override; - std::vector getBooleans() const override; + bool getBoolean(int32_t position) const override; + std::vector getBooleans() const override; - bool mayHaveNull() const override; - bool isNull(int32_t position) const override; - std::vector isNulls() const override; + bool mayHaveNull() const override; + bool isNull(int32_t position) const override; + std::vector isNulls() const override; - int32_t getPositionCount() const override; + int32_t getPositionCount() const override; private: - int32_t arrayOffset_; - int32_t positionCount_; - std::vector valueIsNull_; - std::vector values_; + int32_t arrayOffset_; + int32_t positionCount_; + std::vector valueIsNull_; + std::vector values_; }; class RunLengthEncodedColumn : public Column { public: - RunLengthEncodedColumn(std::shared_ptr value, int32_t positionCount); + RunLengthEncodedColumn(std::shared_ptr value, int32_t positionCount); - std::shared_ptr getValue() const; + std::shared_ptr getValue() const; - TSDataType::TSDataType getDataType() const override; - ColumnEncoding getEncoding() const override; + TSDataType::TSDataType getDataType() const override; + ColumnEncoding getEncoding() const override; - bool getBoolean(int32_t position) const override; - int32_t getInt(int32_t position) const override; - int64_t getLong(int32_t position) const override; - float getFloat(int32_t position) const override; - double getDouble(int32_t position) const override; - std::shared_ptr getBinary(int32_t position) const override; + bool getBoolean(int32_t position) const override; + int32_t getInt(int32_t position) const override; + int64_t getLong(int32_t position) const override; + float getFloat(int32_t position) const override; + double getDouble(int32_t position) const override; + std::shared_ptr getBinary(int32_t position) const override; - std::vector getBooleans() const override; - std::vector getInts() const override; - std::vector getLongs() const override; - std::vector getFloats() const override; - std::vector getDoubles() const override; - std::vector> getBinaries() const override; + std::vector getBooleans() const override; + std::vector getInts() const override; + std::vector getLongs() const override; + std::vector getFloats() const override; + std::vector getDoubles() const override; + std::vector> getBinaries() const override; - bool mayHaveNull() const override; - bool isNull(int32_t position) const override; - std::vector isNulls() const override; + bool mayHaveNull() const override; + bool isNull(int32_t position) const override; + std::vector isNulls() const override; - int32_t getPositionCount() const override; + int32_t getPositionCount() const override; private: - std::shared_ptr value_; - int32_t positionCount_; + std::shared_ptr value_; + int32_t positionCount_; }; #endif diff --git a/iotdb-client/client-cpp/src/main/ColumnDecoder.cpp b/iotdb-client/client-cpp/src/main/ColumnDecoder.cpp index 32f29d876f368..42f13fcc27eed 100644 --- a/iotdb-client/client-cpp/src/main/ColumnDecoder.cpp +++ b/iotdb-client/client-cpp/src/main/ColumnDecoder.cpp @@ -22,160 +22,175 @@ #include "Column.h" std::vector deserializeNullIndicators(MyStringBuffer& buffer, int32_t positionCount) { - uint8_t mayHaveNullByte = buffer.getChar(); + uint8_t mayHaveNullByte = buffer.getChar(); - bool mayHaveNull = mayHaveNullByte != 0; - if (!mayHaveNull) { - return {}; - } + bool mayHaveNull = mayHaveNullByte != 0; + if (!mayHaveNull) { + return {}; + } - return deserializeBooleanArray(buffer, positionCount); + return deserializeBooleanArray(buffer, positionCount); } std::vector deserializeBooleanArray(MyStringBuffer& buffer, int32_t size) { - const int32_t packedSize = (size + 7) / 8; - std::vector packedBytes(packedSize); - for (int i = 0; i < packedSize; i++) { - packedBytes[i] = buffer.getChar(); - } - - std::vector output(size); - int currentByte = 0; - const int fullGroups = size & ~0b111; - - for (int pos = 0; pos < fullGroups; pos += 8) { - const uint8_t b = packedBytes[currentByte++]; - output[pos + 0] = (b & 0b10000000) != 0; - output[pos + 1] = (b & 0b01000000) != 0; - output[pos + 2] = (b & 0b00100000) != 0; - output[pos + 3] = (b & 0b00010000) != 0; - output[pos + 4] = (b & 0b00001000) != 0; - output[pos + 5] = (b & 0b00000100) != 0; - output[pos + 6] = (b & 0b00000010) != 0; - output[pos + 7] = (b & 0b00000001) != 0; - } - - if ((size & 0b111) > 0) { - const uint8_t b = packedBytes.back(); - uint8_t mask = 0b10000000; - - for (int pos = fullGroups; pos < size; pos++) { - output[pos] = (b & mask) != 0; - mask >>= 1; - } + const int32_t packedSize = (size + 7) / 8; + std::vector packedBytes(packedSize); + for (int i = 0; i < packedSize; i++) { + packedBytes[i] = buffer.getChar(); + } + + std::vector output(size); + int currentByte = 0; + const int fullGroups = size & ~0b111; + + for (int pos = 0; pos < fullGroups; pos += 8) { + const uint8_t b = packedBytes[currentByte++]; + output[pos + 0] = (b & 0b10000000) != 0; + output[pos + 1] = (b & 0b01000000) != 0; + output[pos + 2] = (b & 0b00100000) != 0; + output[pos + 3] = (b & 0b00010000) != 0; + output[pos + 4] = (b & 0b00001000) != 0; + output[pos + 5] = (b & 0b00000100) != 0; + output[pos + 6] = (b & 0b00000010) != 0; + output[pos + 7] = (b & 0b00000001) != 0; + } + + if ((size & 0b111) > 0) { + const uint8_t b = packedBytes.back(); + uint8_t mask = 0b10000000; + + for (int pos = fullGroups; pos < size; pos++) { + output[pos] = (b & mask) != 0; + mask >>= 1; } + } - return output; + return output; } -std::unique_ptr BaseColumnDecoder::readColumn( - MyStringBuffer& buffer, TSDataType::TSDataType dataType, int32_t positionCount) { - return nullptr; +std::unique_ptr BaseColumnDecoder::readColumn(MyStringBuffer& buffer, + TSDataType::TSDataType dataType, + int32_t positionCount) { + return nullptr; } -std::unique_ptr Int32ArrayColumnDecoder::readColumn( - MyStringBuffer& buffer, TSDataType::TSDataType dataType, int32_t positionCount) { - auto nullIndicators = deserializeNullIndicators(buffer, positionCount); - - switch (dataType) { - case TSDataType::INT32: - case TSDataType::DATE: { - std::vector intValues(positionCount); - for (int32_t i = 0; i < positionCount; i++) { - if (!nullIndicators.empty() && nullIndicators[i]) continue; - intValues[i] = buffer.getInt(); - } - return std::unique_ptr(new IntColumn(0, positionCount, nullIndicators, intValues)); - } - case TSDataType::FLOAT: { - std::vector floatValues(positionCount); - for (int32_t i = 0; i < positionCount; i++) { - if (!nullIndicators.empty() && nullIndicators[i]) continue; - floatValues[i] = buffer.getFloat(); - } - return std::unique_ptr(new FloatColumn(0, positionCount, nullIndicators, floatValues)); +std::unique_ptr Int32ArrayColumnDecoder::readColumn(MyStringBuffer& buffer, + TSDataType::TSDataType dataType, + int32_t positionCount) { + auto nullIndicators = deserializeNullIndicators(buffer, positionCount); + + switch (dataType) { + case TSDataType::INT32: + case TSDataType::DATE: { + std::vector intValues(positionCount); + for (int32_t i = 0; i < positionCount; i++) { + if (!nullIndicators.empty() && nullIndicators[i]) + continue; + intValues[i] = buffer.getInt(); } - default: - throw IoTDBException("Invalid data type for Int32ArrayColumnDecoder"); + return std::unique_ptr(new IntColumn(0, positionCount, nullIndicators, intValues)); + } + case TSDataType::FLOAT: { + std::vector floatValues(positionCount); + for (int32_t i = 0; i < positionCount; i++) { + if (!nullIndicators.empty() && nullIndicators[i]) + continue; + floatValues[i] = buffer.getFloat(); } + return std::unique_ptr( + new FloatColumn(0, positionCount, nullIndicators, floatValues)); + } + default: + throw IoTDBException("Invalid data type for Int32ArrayColumnDecoder"); + } } -std::unique_ptr Int64ArrayColumnDecoder::readColumn( - MyStringBuffer& buffer, TSDataType::TSDataType dataType, int32_t positionCount) { - auto nullIndicators = deserializeNullIndicators(buffer, positionCount); - - switch (dataType) { - case TSDataType::INT64: - case TSDataType::TIMESTAMP: { - std::vector values(positionCount); - for (int32_t i = 0; i < positionCount; i++) { - if (!nullIndicators.empty() && nullIndicators[i]) continue; - values[i] = buffer.getInt64(); - } - return std::unique_ptr(new LongColumn(0, positionCount, nullIndicators, values)); - } - case TSDataType::DOUBLE: { - std::vector values(positionCount); - for (int32_t i = 0; i < positionCount; i++) { - if (!nullIndicators.empty() && nullIndicators[i]) continue; - values[i] = buffer.getDouble(); - } - return std::unique_ptr(new DoubleColumn(0, positionCount, nullIndicators, values)); +std::unique_ptr Int64ArrayColumnDecoder::readColumn(MyStringBuffer& buffer, + TSDataType::TSDataType dataType, + int32_t positionCount) { + auto nullIndicators = deserializeNullIndicators(buffer, positionCount); + + switch (dataType) { + case TSDataType::INT64: + case TSDataType::TIMESTAMP: { + std::vector values(positionCount); + for (int32_t i = 0; i < positionCount; i++) { + if (!nullIndicators.empty() && nullIndicators[i]) + continue; + values[i] = buffer.getInt64(); } - default: - throw IoTDBException("Invalid data type for Int64ArrayColumnDecoder"); + return std::unique_ptr(new LongColumn(0, positionCount, nullIndicators, values)); + } + case TSDataType::DOUBLE: { + std::vector values(positionCount); + for (int32_t i = 0; i < positionCount; i++) { + if (!nullIndicators.empty() && nullIndicators[i]) + continue; + values[i] = buffer.getDouble(); } + return std::unique_ptr( + new DoubleColumn(0, positionCount, nullIndicators, values)); + } + default: + throw IoTDBException("Invalid data type for Int64ArrayColumnDecoder"); + } } -std::unique_ptr ByteArrayColumnDecoder::readColumn( - MyStringBuffer& buffer, TSDataType::TSDataType dataType, int32_t positionCount) { - if (dataType != TSDataType::BOOLEAN) { - throw IoTDBException("Invalid data type for ByteArrayColumnDecoder"); - } - - auto nullIndicators = deserializeNullIndicators(buffer, positionCount); - auto values = deserializeBooleanArray(buffer, positionCount); - return std::unique_ptr(new BooleanColumn(0, positionCount, nullIndicators, values)); +std::unique_ptr ByteArrayColumnDecoder::readColumn(MyStringBuffer& buffer, + TSDataType::TSDataType dataType, + int32_t positionCount) { + if (dataType != TSDataType::BOOLEAN) { + throw IoTDBException("Invalid data type for ByteArrayColumnDecoder"); + } + + auto nullIndicators = deserializeNullIndicators(buffer, positionCount); + auto values = deserializeBooleanArray(buffer, positionCount); + return std::unique_ptr( + new BooleanColumn(0, positionCount, nullIndicators, values)); } -std::unique_ptr BinaryArrayColumnDecoder::readColumn( - MyStringBuffer& buffer, TSDataType::TSDataType dataType, int32_t positionCount) { - if (dataType != TSDataType::TEXT) { - throw IoTDBException("Invalid data type for BinaryArrayColumnDecoder"); - } +std::unique_ptr BinaryArrayColumnDecoder::readColumn(MyStringBuffer& buffer, + TSDataType::TSDataType dataType, + int32_t positionCount) { + if (dataType != TSDataType::TEXT) { + throw IoTDBException("Invalid data type for BinaryArrayColumnDecoder"); + } - auto nullIndicators = deserializeNullIndicators(buffer, positionCount); - std::vector> values(positionCount); + auto nullIndicators = deserializeNullIndicators(buffer, positionCount); + std::vector> values(positionCount); - for (int32_t i = 0; i < positionCount; i++) { - if (!nullIndicators.empty() && nullIndicators[i]) continue; - - int32_t length = buffer.getInt(); - if (length < 0) { - throw IoTDBException("BinaryArrayColumnDecoder: negative TEXT length"); - } + for (int32_t i = 0; i < positionCount; i++) { + if (!nullIndicators.empty() && nullIndicators[i]) + continue; - std::vector value(length); - for (int32_t j = 0; j < length; j++) { - value[j] = buffer.getChar(); - } + int32_t length = buffer.getInt(); + if (length < 0) { + throw IoTDBException("BinaryArrayColumnDecoder: negative TEXT length"); + } - values[i] = std::make_shared(value); + std::vector value(length); + for (int32_t j = 0; j < length; j++) { + value[j] = buffer.getChar(); } - return std::unique_ptr(new BinaryColumn(0, positionCount, nullIndicators, values)); + values[i] = std::make_shared(value); + } + + return std::unique_ptr(new BinaryColumn(0, positionCount, nullIndicators, values)); } -std::unique_ptr RunLengthColumnDecoder::readColumn( - MyStringBuffer& buffer, TSDataType::TSDataType dataType, int32_t positionCount) { - uint8_t encodingByte = buffer.getChar(); +std::unique_ptr RunLengthColumnDecoder::readColumn(MyStringBuffer& buffer, + TSDataType::TSDataType dataType, + int32_t positionCount) { + uint8_t encodingByte = buffer.getChar(); - auto columnEncoding = static_cast(encodingByte); - auto decoder = getColumnDecoder(columnEncoding); + auto columnEncoding = static_cast(encodingByte); + auto decoder = getColumnDecoder(columnEncoding); - auto column = decoder->readColumn(buffer, dataType, 1); - if (!column) { - throw IoTDBException("Failed to read inner column"); - } - return std::unique_ptr(new RunLengthEncodedColumn(move(column), positionCount)); + auto column = decoder->readColumn(buffer, dataType, 1); + if (!column) { + throw IoTDBException("Failed to read inner column"); + } + return std::unique_ptr( + new RunLengthEncodedColumn(move(column), positionCount)); } diff --git a/iotdb-client/client-cpp/src/main/ColumnDecoder.h b/iotdb-client/client-cpp/src/main/ColumnDecoder.h index f5340d1e400ac..5765d1a113470 100644 --- a/iotdb-client/client-cpp/src/main/ColumnDecoder.h +++ b/iotdb-client/client-cpp/src/main/ColumnDecoder.h @@ -28,9 +28,9 @@ class Column; class ColumnDecoder { public: - virtual ~ColumnDecoder() = default; - virtual std::unique_ptr readColumn( - MyStringBuffer& buffer, TSDataType::TSDataType dataType, int32_t positionCount) = 0; + virtual ~ColumnDecoder() = default; + virtual std::unique_ptr + readColumn(MyStringBuffer& buffer, TSDataType::TSDataType dataType, int32_t positionCount) = 0; }; std::vector deserializeNullIndicators(MyStringBuffer& buffer, int32_t positionCount); @@ -38,38 +38,38 @@ std::vector deserializeBooleanArray(MyStringBuffer& buffer, int32_t size); class BaseColumnDecoder : public ColumnDecoder { public: - std::unique_ptr readColumn( - MyStringBuffer& buffer, TSDataType::TSDataType dataType, int32_t positionCount) override; + std::unique_ptr readColumn(MyStringBuffer& buffer, TSDataType::TSDataType dataType, + int32_t positionCount) override; }; class Int32ArrayColumnDecoder : public BaseColumnDecoder { public: - std::unique_ptr readColumn( - MyStringBuffer& buffer, TSDataType::TSDataType dataType, int32_t positionCount) override; + std::unique_ptr readColumn(MyStringBuffer& buffer, TSDataType::TSDataType dataType, + int32_t positionCount) override; }; class Int64ArrayColumnDecoder : public BaseColumnDecoder { public: - std::unique_ptr readColumn( - MyStringBuffer& buffer, TSDataType::TSDataType dataType, int32_t positionCount) override; + std::unique_ptr readColumn(MyStringBuffer& buffer, TSDataType::TSDataType dataType, + int32_t positionCount) override; }; class ByteArrayColumnDecoder : public BaseColumnDecoder { public: - std::unique_ptr readColumn( - MyStringBuffer& buffer, TSDataType::TSDataType dataType, int32_t positionCount) override; + std::unique_ptr readColumn(MyStringBuffer& buffer, TSDataType::TSDataType dataType, + int32_t positionCount) override; }; class BinaryArrayColumnDecoder : public BaseColumnDecoder { public: - std::unique_ptr readColumn( - MyStringBuffer& buffer, TSDataType::TSDataType dataType, int32_t positionCount) override; + std::unique_ptr readColumn(MyStringBuffer& buffer, TSDataType::TSDataType dataType, + int32_t positionCount) override; }; class RunLengthColumnDecoder : public BaseColumnDecoder { public: - std::unique_ptr readColumn( - MyStringBuffer& buffer, TSDataType::TSDataType dataType, int32_t positionCount) override; + std::unique_ptr readColumn(MyStringBuffer& buffer, TSDataType::TSDataType dataType, + int32_t positionCount) override; }; #endif diff --git a/iotdb-client/client-cpp/src/main/Common.cpp b/iotdb-client/client-cpp/src/main/Common.cpp index f7eeb2a218e49..ccdb2553831b7 100644 --- a/iotdb-client/client-cpp/src/main/Common.cpp +++ b/iotdb-client/client-cpp/src/main/Common.cpp @@ -21,497 +21,505 @@ #include int32_t parseDateExpressionToInt(const boost::gregorian::date& date) { - if (date.is_not_a_date()) { - throw IoTDBException("Date expression is null or empty."); - } + if (date.is_not_a_date()) { + throw IoTDBException("Date expression is null or empty."); + } - const int year = date.year(); - if (year < 1000 || year > 9999) { - throw DateTimeParseException( - "Year must be between 1000 and 9999.", - boost::gregorian::to_iso_extended_string(date), - 0 - ); - } + const int year = date.year(); + if (year < 1000 || year > 9999) { + throw DateTimeParseException("Year must be between 1000 and 9999.", + boost::gregorian::to_iso_extended_string(date), 0); + } - const int64_t result = static_cast(year) * 10000 + - date.month() * 100 + - date.day(); - if (result > INT32_MAX || result < INT32_MIN) { - throw DateTimeParseException( - "Date value overflow. ", - boost::gregorian::to_iso_extended_string(date), - 0 - ); - } - return static_cast(result); + const int64_t result = static_cast(year) * 10000 + date.month() * 100 + date.day(); + if (result > INT32_MAX || result < INT32_MIN) { + throw DateTimeParseException("Date value overflow. ", + boost::gregorian::to_iso_extended_string(date), 0); + } + return static_cast(result); } boost::gregorian::date parseIntToDate(int32_t dateInt) { - if (dateInt == EMPTY_DATE_INT) { - return boost::gregorian::date(boost::date_time::not_a_date_time); - } - int year = dateInt / 10000; - int month = (dateInt % 10000) / 100; - int day = dateInt % 100; - return boost::gregorian::date(year, month, day); + if (dateInt == EMPTY_DATE_INT) { + return boost::gregorian::date(boost::date_time::not_a_date_time); + } + int year = dateInt / 10000; + int month = (dateInt % 10000) / 100; + int day = dateInt % 100; + return boost::gregorian::date(year, month, day); } std::string getTimePrecision(int32_t timeFactor) { - if (timeFactor >= 1000000) return "us"; - if (timeFactor >= 1000) return "ms"; - return "s"; + if (timeFactor >= 1000000) + return "us"; + if (timeFactor >= 1000) + return "ms"; + return "s"; } std::string formatDatetime(const std::string& format, const std::string& precision, int64_t timestamp, const std::string& zoneId) { - // Simplified implementation - in real code you'd use proper timezone handling - std::time_t time = static_cast(timestamp); - std::tm* tm = std::localtime(&time); - char buffer[80]; - strftime(buffer, sizeof(buffer), format.c_str(), tm); - return std::string(buffer); + // Simplified implementation - in real code you'd use proper timezone handling + std::time_t time = static_cast(timestamp); + std::tm* tm = std::localtime(&time); + char buffer[80]; + strftime(buffer, sizeof(buffer), format.c_str(), tm); + return std::string(buffer); } std::tm convertToTimestamp(int64_t value, int32_t timeFactor) { - std::time_t time = static_cast(value / timeFactor); - return *std::localtime(&time); + std::time_t time = static_cast(value / timeFactor); + return *std::localtime(&time); } TSDataType::TSDataType getDataTypeByStr(const std::string& typeStr) { - if (typeStr == "BOOLEAN") return TSDataType::BOOLEAN; - if (typeStr == "INT32") return TSDataType::INT32; - if (typeStr == "INT64") return TSDataType::INT64; - if (typeStr == "FLOAT") return TSDataType::FLOAT; - if (typeStr == "DOUBLE") return TSDataType::DOUBLE; - if (typeStr == "TEXT") return TSDataType::TEXT; - if (typeStr == "TIMESTAMP") return TSDataType::TIMESTAMP; - if (typeStr == "DATE") return TSDataType::DATE; - if (typeStr == "BLOB") return TSDataType::BLOB; - if (typeStr == "STRING") return TSDataType::STRING; - if (typeStr == "OBJECT") return TSDataType::OBJECT; - return TSDataType::UNKNOWN; + if (typeStr == "BOOLEAN") + return TSDataType::BOOLEAN; + if (typeStr == "INT32") + return TSDataType::INT32; + if (typeStr == "INT64") + return TSDataType::INT64; + if (typeStr == "FLOAT") + return TSDataType::FLOAT; + if (typeStr == "DOUBLE") + return TSDataType::DOUBLE; + if (typeStr == "TEXT") + return TSDataType::TEXT; + if (typeStr == "TIMESTAMP") + return TSDataType::TIMESTAMP; + if (typeStr == "DATE") + return TSDataType::DATE; + if (typeStr == "BLOB") + return TSDataType::BLOB; + if (typeStr == "STRING") + return TSDataType::STRING; + if (typeStr == "OBJECT") + return TSDataType::OBJECT; + return TSDataType::UNKNOWN; } std::tm int32ToDate(int32_t value) { - // Convert days since epoch (1970-01-01) to tm struct - std::time_t time = static_cast(value) * 86400; // seconds per day - return *std::localtime(&time); + // Convert days since epoch (1970-01-01) to tm struct + std::time_t time = static_cast(value) * 86400; // seconds per day + return *std::localtime(&time); } void RpcUtils::verifySuccess(const TSStatus& status) { - if (status.code == TSStatusCode::MULTIPLE_ERROR) { - verifySuccess(status.subStatus); - return; - } - if (status.code != TSStatusCode::SUCCESS_STATUS - && status.code != TSStatusCode::REDIRECTION_RECOMMEND) { - throw ExecutionException(to_string(status.code) + ": " + status.message, status); - } + if (status.code == TSStatusCode::MULTIPLE_ERROR) { + verifySuccess(status.subStatus); + return; + } + if (status.code != TSStatusCode::SUCCESS_STATUS && + status.code != TSStatusCode::REDIRECTION_RECOMMEND) { + throw ExecutionException(to_string(status.code) + ": " + status.message, status); + } } void RpcUtils::verifySuccessWithRedirection(const TSStatus& status) { - verifySuccess(status); - if (status.__isset.redirectNode) { - throw RedirectException(to_string(status.code) + ": " + status.message, status.redirectNode); + verifySuccess(status); + if (status.__isset.redirectNode) { + throw RedirectException(to_string(status.code) + ": " + status.message, status.redirectNode); + } + if (status.__isset.subStatus) { + auto statusSubStatus = status.subStatus; + vector endPointList(statusSubStatus.size()); + int count = 0; + for (TSStatus subStatus : statusSubStatus) { + if (subStatus.__isset.redirectNode) { + endPointList[count++] = subStatus.redirectNode; + } else { + TEndPoint endPoint; + endPointList[count++] = endPoint; + } } - if (status.__isset.subStatus) { - auto statusSubStatus = status.subStatus; - vector endPointList(statusSubStatus.size()); - int count = 0; - for (TSStatus subStatus : statusSubStatus) { - if (subStatus.__isset.redirectNode) { - endPointList[count++] = subStatus.redirectNode; - } - else { - TEndPoint endPoint; - endPointList[count++] = endPoint; - } - } - if (!endPointList.empty()) { - throw RedirectException(to_string(status.code) + ": " + status.message, endPointList); - } + if (!endPointList.empty()) { + throw RedirectException(to_string(status.code) + ": " + status.message, endPointList); } -} - -void RpcUtils::verifySuccessWithRedirectionForMultiDevices(const TSStatus& status, vector devices) { - verifySuccess(status); - - if (status.code == TSStatusCode::MULTIPLE_ERROR - || status.code == TSStatusCode::REDIRECTION_RECOMMEND) { - map deviceEndPointMap; - vector statusSubStatus; - for (int i = 0; i < statusSubStatus.size(); i++) { - TSStatus subStatus = statusSubStatus[i]; - if (subStatus.__isset.redirectNode) { - deviceEndPointMap.insert(make_pair(devices[i], subStatus.redirectNode)); - } - } - throw RedirectException(to_string(status.code) + ": " + status.message, deviceEndPointMap); + } +} + +void RpcUtils::verifySuccessWithRedirectionForMultiDevices(const TSStatus& status, + vector devices) { + verifySuccess(status); + + if (status.code == TSStatusCode::MULTIPLE_ERROR || + status.code == TSStatusCode::REDIRECTION_RECOMMEND) { + map deviceEndPointMap; + vector statusSubStatus; + for (int i = 0; i < statusSubStatus.size(); i++) { + TSStatus subStatus = statusSubStatus[i]; + if (subStatus.__isset.redirectNode) { + deviceEndPointMap.insert(make_pair(devices[i], subStatus.redirectNode)); + } } - - if (status.__isset.redirectNode) { - throw RedirectException(to_string(status.code) + ": " + status.message, status.redirectNode); + throw RedirectException(to_string(status.code) + ": " + status.message, deviceEndPointMap); + } + + if (status.__isset.redirectNode) { + throw RedirectException(to_string(status.code) + ": " + status.message, status.redirectNode); + } + if (status.__isset.subStatus) { + auto statusSubStatus = status.subStatus; + vector endPointList(statusSubStatus.size()); + int count = 0; + for (TSStatus subStatus : statusSubStatus) { + if (subStatus.__isset.redirectNode) { + endPointList[count++] = subStatus.redirectNode; + } else { + TEndPoint endPoint; + endPointList[count++] = endPoint; + } } - if (status.__isset.subStatus) { - auto statusSubStatus = status.subStatus; - vector endPointList(statusSubStatus.size()); - int count = 0; - for (TSStatus subStatus : statusSubStatus) { - if (subStatus.__isset.redirectNode) { - endPointList[count++] = subStatus.redirectNode; - } - else { - TEndPoint endPoint; - endPointList[count++] = endPoint; - } - } - if (!endPointList.empty()) { - throw RedirectException(to_string(status.code) + ": " + status.message, endPointList); - } + if (!endPointList.empty()) { + throw RedirectException(to_string(status.code) + ": " + status.message, endPointList); } + } } void RpcUtils::verifySuccess(const vector& statuses) { - for (const TSStatus& status : statuses) { - if (status.code != TSStatusCode::SUCCESS_STATUS) { - throw BatchExecutionException(status.message, statuses); - } + for (const TSStatus& status : statuses) { + if (status.code != TSStatusCode::SUCCESS_STATUS) { + throw BatchExecutionException(status.message, statuses); } + } } TSStatus RpcUtils::getStatus(TSStatusCode::TSStatusCode tsStatusCode) { - TSStatus status; - status.__set_code(tsStatusCode); - return status; + TSStatus status; + status.__set_code(tsStatusCode); + return status; } TSStatus RpcUtils::getStatus(int code, const string& message) { - TSStatus status; - status.__set_code(code); - status.__set_message(message); - return status; + TSStatus status; + status.__set_code(code); + status.__set_message(message); + return status; } -shared_ptr RpcUtils::getTSExecuteStatementResp(TSStatusCode::TSStatusCode tsStatusCode) { - TSStatus status = getStatus(tsStatusCode); - return getTSExecuteStatementResp(status); +shared_ptr +RpcUtils::getTSExecuteStatementResp(TSStatusCode::TSStatusCode tsStatusCode) { + TSStatus status = getStatus(tsStatusCode); + return getTSExecuteStatementResp(status); } shared_ptr -RpcUtils::getTSExecuteStatementResp(TSStatusCode::TSStatusCode tsStatusCode, const string& message) { - TSStatus status = getStatus(tsStatusCode, message); - return getTSExecuteStatementResp(status); +RpcUtils::getTSExecuteStatementResp(TSStatusCode::TSStatusCode tsStatusCode, + const string& message) { + TSStatus status = getStatus(tsStatusCode, message); + return getTSExecuteStatementResp(status); } shared_ptr RpcUtils::getTSExecuteStatementResp(const TSStatus& status) { - shared_ptr resp(new TSExecuteStatementResp()); - TSStatus tsStatus(status); - resp->__set_status(status); - return resp; + shared_ptr resp(new TSExecuteStatementResp()); + TSStatus tsStatus(status); + resp->__set_status(status); + return resp; } -shared_ptr RpcUtils::getTSFetchResultsResp(TSStatusCode::TSStatusCode tsStatusCode) { - TSStatus status = getStatus(tsStatusCode); - return getTSFetchResultsResp(status); +shared_ptr +RpcUtils::getTSFetchResultsResp(TSStatusCode::TSStatusCode tsStatusCode) { + TSStatus status = getStatus(tsStatusCode); + return getTSFetchResultsResp(status); } shared_ptr -RpcUtils::getTSFetchResultsResp(TSStatusCode::TSStatusCode tsStatusCode, const string& appendMessage) { - TSStatus status = getStatus(tsStatusCode, appendMessage); - return getTSFetchResultsResp(status); +RpcUtils::getTSFetchResultsResp(TSStatusCode::TSStatusCode tsStatusCode, + const string& appendMessage) { + TSStatus status = getStatus(tsStatusCode, appendMessage); + return getTSFetchResultsResp(status); } shared_ptr RpcUtils::getTSFetchResultsResp(const TSStatus& status) { - shared_ptr resp(new TSFetchResultsResp()); - TSStatus tsStatus(status); - resp->__set_status(tsStatus); - return resp; + shared_ptr resp(new TSFetchResultsResp()); + TSStatus tsStatus(status); + resp->__set_status(tsStatus); + return resp; } MyStringBuffer::MyStringBuffer() : pos(0) { - checkBigEndian(); + checkBigEndian(); } MyStringBuffer::MyStringBuffer(const std::string& str) : str(str), pos(0) { - checkBigEndian(); + checkBigEndian(); } void MyStringBuffer::reserve(size_t n) { - str.reserve(n); + str.reserve(n); } void MyStringBuffer::clear() { - str.clear(); - pos = 0; + str.clear(); + pos = 0; } bool MyStringBuffer::hasRemaining() { - return pos < str.size(); + return pos < str.size(); } int MyStringBuffer::getInt() { - return *(int*)getOrderedByte(4); + return *(int*)getOrderedByte(4); } boost::gregorian::date MyStringBuffer::getDate() { - return parseIntToDate(getInt()); + return parseIntToDate(getInt()); } int64_t MyStringBuffer::getInt64() { #ifdef ARCH32 - const char *buf_addr = getOrderedByte(8); - if (reinterpret_cast(buf_addr) % 4 == 0) { - return *(int64_t *)buf_addr; - } else { - char tmp_buf[8]; - memcpy(tmp_buf, buf_addr, 8); - return *(int64_t*)tmp_buf; - } + const char* buf_addr = getOrderedByte(8); + if (reinterpret_cast(buf_addr) % 4 == 0) { + return *(int64_t*)buf_addr; + } else { + char tmp_buf[8]; + memcpy(tmp_buf, buf_addr, 8); + return *(int64_t*)tmp_buf; + } #else - return *(int64_t*)getOrderedByte(8); + return *(int64_t*)getOrderedByte(8); #endif } float MyStringBuffer::getFloat() { - return *(float*)getOrderedByte(4); + return *(float*)getOrderedByte(4); } double MyStringBuffer::getDouble() { #ifdef ARCH32 - const char *buf_addr = getOrderedByte(8); - if (reinterpret_cast(buf_addr) % 4 == 0) { - return *(double*)buf_addr; - } else { - char tmp_buf[8]; - memcpy(tmp_buf, buf_addr, 8); - return *(double*)tmp_buf; - } + const char* buf_addr = getOrderedByte(8); + if (reinterpret_cast(buf_addr) % 4 == 0) { + return *(double*)buf_addr; + } else { + char tmp_buf[8]; + memcpy(tmp_buf, buf_addr, 8); + return *(double*)tmp_buf; + } #else - return *(double*)getOrderedByte(8); + return *(double*)getOrderedByte(8); #endif } char MyStringBuffer::getChar() { - if (pos >= str.size()) { - throw IoTDBException("MyStringBuffer::getChar: read past end (pos=" + std::to_string(pos) + - ", size=" + std::to_string(str.size()) + ")"); - } - return str[pos++]; + if (pos >= str.size()) { + throw IoTDBException("MyStringBuffer::getChar: read past end (pos=" + std::to_string(pos) + + ", size=" + std::to_string(str.size()) + ")"); + } + return str[pos++]; } bool MyStringBuffer::getBool() { - return getChar() == 1; + return getChar() == 1; } std::string MyStringBuffer::getString() { - const int lenInt = getInt(); - if (lenInt < 0) { - throw IoTDBException("MyStringBuffer::getString: negative length"); - } - const size_t len = static_cast(lenInt); - if (pos > str.size() || len > str.size() - pos) { - throw IoTDBException("MyStringBuffer::getString: length exceeds buffer (pos=" + std::to_string(pos) + - ", len=" + std::to_string(len) + ", size=" + std::to_string(str.size()) + ")"); - } - const size_t tmpPos = pos; - pos += len; - return str.substr(tmpPos, len); + const int lenInt = getInt(); + if (lenInt < 0) { + throw IoTDBException("MyStringBuffer::getString: negative length"); + } + const size_t len = static_cast(lenInt); + if (pos > str.size() || len > str.size() - pos) { + throw IoTDBException( + "MyStringBuffer::getString: length exceeds buffer (pos=" + std::to_string(pos) + + ", len=" + std::to_string(len) + ", size=" + std::to_string(str.size()) + ")"); + } + const size_t tmpPos = pos; + pos += len; + return str.substr(tmpPos, len); } void MyStringBuffer::putInt(int ins) { - putOrderedByte((char*)&ins, 4); + putOrderedByte((char*)&ins, 4); } void MyStringBuffer::putDate(boost::gregorian::date date) { - putInt(parseDateExpressionToInt(date)); + putInt(parseDateExpressionToInt(date)); } void MyStringBuffer::putInt64(int64_t ins) { - putOrderedByte((char*)&ins, 8); + putOrderedByte((char*)&ins, 8); } void MyStringBuffer::putFloat(float ins) { - putOrderedByte((char*)&ins, 4); + putOrderedByte((char*)&ins, 4); } void MyStringBuffer::putDouble(double ins) { - putOrderedByte((char*)&ins, 8); + putOrderedByte((char*)&ins, 8); } void MyStringBuffer::putChar(char ins) { - str += ins; + str += ins; } void MyStringBuffer::putBool(bool ins) { - char tmp = ins ? 1 : 0; - str += tmp; + char tmp = ins ? 1 : 0; + str += tmp; } void MyStringBuffer::putString(const std::string& ins) { - putInt((int)(ins.size())); - str += ins; + putInt((int)(ins.size())); + str += ins; } void MyStringBuffer::concat(const std::string& ins) { - str.append(ins); + str.append(ins); } void MyStringBuffer::checkBigEndian() { - static int chk = 0x0201; //used to distinguish CPU's type (BigEndian or LittleEndian) - isBigEndian = (0x01 != *(char*)(&chk)); + static int chk = 0x0201; //used to distinguish CPU's type (BigEndian or LittleEndian) + isBigEndian = (0x01 != *(char*)(&chk)); } const char* MyStringBuffer::getOrderedByte(size_t len) { - if (pos > str.size() || len > str.size() - pos) { - throw IoTDBException("MyStringBuffer::getOrderedByte: read past end (pos=" + std::to_string(pos) + - ", len=" + std::to_string(len) + ", size=" + std::to_string(str.size()) + ")"); + if (pos > str.size() || len > str.size() - pos) { + throw IoTDBException( + "MyStringBuffer::getOrderedByte: read past end (pos=" + std::to_string(pos) + + ", len=" + std::to_string(len) + ", size=" + std::to_string(str.size()) + ")"); + } + const char* p = nullptr; + if (isBigEndian) { + p = str.c_str() + pos; + } else { + const char* tmp = str.c_str(); + for (size_t i = pos; i < pos + len; i++) { + numericBuf[pos + len - 1 - i] = tmp[i]; } - const char* p = nullptr; - if (isBigEndian) { - p = str.c_str() + pos; - } - else { - const char* tmp = str.c_str(); - for (size_t i = pos; i < pos + len; i++) { - numericBuf[pos + len - 1 - i] = tmp[i]; - } - p = numericBuf; - } - pos += len; - return p; + p = numericBuf; + } + pos += len; + return p; } void MyStringBuffer::putOrderedByte(char* buf, int len) { - if (isBigEndian) { - str.assign(buf, len); - } - else { - for (int i = len - 1; i > -1; i--) { - str += buf[i]; - } + if (isBigEndian) { + str.assign(buf, len); + } else { + for (int i = len - 1; i > -1; i--) { + str += buf[i]; } + } } BitMap::BitMap(size_t size) { - resize(size); + resize(size); } void BitMap::resize(size_t size) { - this->size = size; - this->bits.resize((size >> 3) + 1); // equal to "size/8 + 1" - reset(); + this->size = size; + this->bits.resize((size >> 3) + 1); // equal to "size/8 + 1" + reset(); } bool BitMap::mark(size_t position) { - if (position >= size) - return false; + if (position >= size) + return false; - bits[position >> 3] |= (char)1 << (position % 8); - return true; + bits[position >> 3] |= (char)1 << (position % 8); + return true; } bool BitMap::unmark(size_t position) { - if (position >= size) - return false; + if (position >= size) + return false; - bits[position >> 3] &= ~((char)1 << (position % 8)); - return true; + bits[position >> 3] &= ~((char)1 << (position % 8)); + return true; } void BitMap::markAll() { - std::fill(bits.begin(), bits.end(), (char)0XFF); + std::fill(bits.begin(), bits.end(), (char)0XFF); } void BitMap::reset() { - std::fill(bits.begin(), bits.end(), (char)0); + std::fill(bits.begin(), bits.end(), (char)0); } bool BitMap::isMarked(size_t position) const { - if (position >= size) - return false; + if (position >= size) + return false; - return (bits[position >> 3] & ((char)1 << (position % 8))) != 0; + return (bits[position >> 3] & ((char)1 << (position % 8))) != 0; } bool BitMap::isAllUnmarked() const { - size_t j; - for (j = 0; j < size >> 3; j++) { - if (bits[j] != (char)0) { - return false; - } + size_t j; + for (j = 0; j < size >> 3; j++) { + if (bits[j] != (char)0) { + return false; } - for (j = 0; j < size % 8; j++) { - if ((bits[size >> 3] & ((char)1 << j)) != 0) { - return false; - } + } + for (j = 0; j < size % 8; j++) { + if ((bits[size >> 3] & ((char)1 << j)) != 0) { + return false; } - return true; + } + return true; } bool BitMap::isAllMarked() const { - size_t j; - for (j = 0; j < size >> 3; j++) { - if (bits[j] != (char)0XFF) { - return false; - } + size_t j; + for (j = 0; j < size >> 3; j++) { + if (bits[j] != (char)0XFF) { + return false; } - for (j = 0; j < size % 8; j++) { - if ((bits[size >> 3] & ((char)1 << j)) == 0) { - return false; - } + } + for (j = 0; j < size % 8; j++) { + if ((bits[size >> 3] & ((char)1 << j)) == 0) { + return false; } - return true; + } + return true; } const std::vector& BitMap::getByteArray() const { - return this->bits; + return this->bits; } size_t BitMap::getSize() const { - return this->size; + return this->size; } const std::string UrlUtils::PORT_SEPARATOR = ":"; const std::string UrlUtils::ABB_COLON = "["; TEndPoint UrlUtils::parseTEndPointIpv4AndIpv6Url(const std::string& endPointUrl) { - TEndPoint endPoint; + TEndPoint endPoint; - // Return default TEndPoint if input is empty - if (endPointUrl.empty()) { - return endPoint; - } + // Return default TEndPoint if input is empty + if (endPointUrl.empty()) { + return endPoint; + } - size_t portSeparatorPos = endPointUrl.find_last_of(PORT_SEPARATOR); + size_t portSeparatorPos = endPointUrl.find_last_of(PORT_SEPARATOR); - // If no port separator found, treat entire string as IP - if (portSeparatorPos == std::string::npos) { - endPoint.__set_ip(endPointUrl); - return endPoint; - } + // If no port separator found, treat entire string as IP + if (portSeparatorPos == std::string::npos) { + endPoint.__set_ip(endPointUrl); + return endPoint; + } - // Extract port part - std::string portStr = endPointUrl.substr(portSeparatorPos + 1); + // Extract port part + std::string portStr = endPointUrl.substr(portSeparatorPos + 1); - // Extract IP part - std::string ip = endPointUrl.substr(0, portSeparatorPos); + // Extract IP part + std::string ip = endPointUrl.substr(0, portSeparatorPos); - // Handle IPv6 addresses with brackets - if (ip.find(ABB_COLON) != std::string::npos) { - // Remove surrounding square brackets for IPv6 - if (ip.size() >= 2 && ip.front() == '[' && ip.back() == ']') { - ip = ip.substr(1, ip.size() - 2); - } + // Handle IPv6 addresses with brackets + if (ip.find(ABB_COLON) != std::string::npos) { + // Remove surrounding square brackets for IPv6 + if (ip.size() >= 2 && ip.front() == '[' && ip.back() == ']') { + ip = ip.substr(1, ip.size() - 2); } + } - try { - int port = std::stoi(portStr); - endPoint.__set_ip(ip); - endPoint.__set_port(port); - } catch (const std::exception& e) { - endPoint.__set_ip(endPointUrl); - } + try { + int port = std::stoi(portStr); + endPoint.__set_ip(ip); + endPoint.__set_port(port); + } catch (const std::exception& e) { + endPoint.__set_ip(endPointUrl); + } - return endPoint; + return endPoint; } diff --git a/iotdb-client/client-cpp/src/main/Common.h b/iotdb-client/client-cpp/src/main/Common.h index aee4449860572..0f9de9996e9a6 100644 --- a/iotdb-client/client-cpp/src/main/Common.h +++ b/iotdb-client/client-cpp/src/main/Common.h @@ -37,14 +37,14 @@ using namespace std; +using ::apache::thrift::TException; using ::apache::thrift::protocol::TBinaryProtocol; using ::apache::thrift::protocol::TCompactProtocol; +using ::apache::thrift::transport::TBufferedTransport; +using ::apache::thrift::transport::TFramedTransport; using ::apache::thrift::transport::TSocket; using ::apache::thrift::transport::TTransport; using ::apache::thrift::transport::TTransportException; -using ::apache::thrift::transport::TBufferedTransport; -using ::apache::thrift::transport::TFramedTransport; -using ::apache::thrift::TException; using namespace std; @@ -62,41 +62,39 @@ std::tm convertToTimestamp(int64_t value, int32_t timeFactor); std::tm int32ToDate(int32_t value); namespace Version { -enum Version { - V_0_12, V_0_13, V_1_0 -}; +enum Version { V_0_12, V_0_13, V_1_0 }; } namespace CompressionType { enum CompressionType { - UNCOMPRESSED = (char)0, - SNAPPY = (char)1, - GZIP = (char)2, - LZO = (char)3, - SDT = (char)4, - PAA = (char)5, - PLA = (char)6, - LZ4 = (char)7, - ZSTD = (char)8, - LZMA2 = (char)9, + UNCOMPRESSED = (char)0, + SNAPPY = (char)1, + GZIP = (char)2, + LZO = (char)3, + SDT = (char)4, + PAA = (char)5, + PLA = (char)6, + LZ4 = (char)7, + ZSTD = (char)8, + LZMA2 = (char)9, }; } namespace TSDataType { enum TSDataType { - BOOLEAN = (char)0, - INT32 = (char)1, - INT64 = (char)2, - FLOAT = (char)3, - DOUBLE = (char)4, - TEXT = (char)5, - VECTOR = (char)6, - UNKNOWN = (char)7, - TIMESTAMP = (char)8, - DATE = (char)9, - BLOB = (char)10, - STRING = (char)11, - OBJECT = (char)12 + BOOLEAN = (char)0, + INT32 = (char)1, + INT64 = (char)2, + FLOAT = (char)3, + DOUBLE = (char)4, + TEXT = (char)5, + VECTOR = (char)6, + UNKNOWN = (char)7, + TIMESTAMP = (char)8, + DATE = (char)9, + BLOB = (char)10, + STRING = (char)11, + OBJECT = (char)12 }; } @@ -104,401 +102,385 @@ TSDataType::TSDataType getDataTypeByStr(const std::string& typeStr); namespace TSEncoding { enum TSEncoding { - PLAIN = (char)0, - DICTIONARY = (char)1, - RLE = (char)2, - DIFF = (char)3, - TS_2DIFF = (char)4, - BITMAP = (char)5, - GORILLA_V1 = (char)6, - REGULAR = (char)7, - GORILLA = (char)8, - ZIGZAG = (char)9, - FREQ = (char)10, - INVALID_ENCODING = (char)255 + PLAIN = (char)0, + DICTIONARY = (char)1, + RLE = (char)2, + DIFF = (char)3, + TS_2DIFF = (char)4, + BITMAP = (char)5, + GORILLA_V1 = (char)6, + REGULAR = (char)7, + GORILLA = (char)8, + ZIGZAG = (char)9, + FREQ = (char)10, + INVALID_ENCODING = (char)255 }; } namespace TSStatusCode { enum TSStatusCode { - SUCCESS_STATUS = 200, - - // System level - INCOMPATIBLE_VERSION = 201, - CONFIGURATION_ERROR = 202, - START_UP_ERROR = 203, - SHUT_DOWN_ERROR = 204, - - // General Error - UNSUPPORTED_OPERATION = 300, - EXECUTE_STATEMENT_ERROR = 301, - MULTIPLE_ERROR = 302, - ILLEGAL_PARAMETER = 303, - OVERLAP_WITH_EXISTING_TASK = 304, - INTERNAL_SERVER_ERROR = 305, - - // Client, - REDIRECTION_RECOMMEND = 400, - - // Schema Engine - DATABASE_NOT_EXIST = 500, - DATABASE_ALREADY_EXISTS = 501, - SERIES_OVERFLOW = 502, - TIMESERIES_ALREADY_EXIST = 503, - TIMESERIES_IN_BLACK_LIST = 504, - ALIAS_ALREADY_EXIST = 505, - PATH_ALREADY_EXIST = 506, - METADATA_ERROR = 507, - PATH_NOT_EXIST = 508, - ILLEGAL_PATH = 509, - CREATE_TEMPLATE_ERROR = 510, - DUPLICATED_TEMPLATE = 511, - UNDEFINED_TEMPLATE = 512, - TEMPLATE_NOT_SET = 513, - DIFFERENT_TEMPLATE = 514, - TEMPLATE_IS_IN_USE = 515, - TEMPLATE_INCOMPATIBLE = 516, - SEGMENT_NOT_FOUND = 517, - PAGE_OUT_OF_SPACE = 518, - RECORD_DUPLICATED = 519, - SEGMENT_OUT_OF_SPACE = 520, - PBTREE_FILE_NOT_EXISTS = 521, - OVERSIZE_RECORD = 522, - PBTREE_FILE_REDO_LOG_BROKEN = 523, - TEMPLATE_NOT_ACTIVATED = 524, - - // Storage Engine - SYSTEM_READ_ONLY = 600, - STORAGE_ENGINE_ERROR = 601, - STORAGE_ENGINE_NOT_READY = 602, - - // Query Engine - PLAN_FAILED_NETWORK_PARTITION = 721 + SUCCESS_STATUS = 200, + + // System level + INCOMPATIBLE_VERSION = 201, + CONFIGURATION_ERROR = 202, + START_UP_ERROR = 203, + SHUT_DOWN_ERROR = 204, + + // General Error + UNSUPPORTED_OPERATION = 300, + EXECUTE_STATEMENT_ERROR = 301, + MULTIPLE_ERROR = 302, + ILLEGAL_PARAMETER = 303, + OVERLAP_WITH_EXISTING_TASK = 304, + INTERNAL_SERVER_ERROR = 305, + + // Client, + REDIRECTION_RECOMMEND = 400, + + // Schema Engine + DATABASE_NOT_EXIST = 500, + DATABASE_ALREADY_EXISTS = 501, + SERIES_OVERFLOW = 502, + TIMESERIES_ALREADY_EXIST = 503, + TIMESERIES_IN_BLACK_LIST = 504, + ALIAS_ALREADY_EXIST = 505, + PATH_ALREADY_EXIST = 506, + METADATA_ERROR = 507, + PATH_NOT_EXIST = 508, + ILLEGAL_PATH = 509, + CREATE_TEMPLATE_ERROR = 510, + DUPLICATED_TEMPLATE = 511, + UNDEFINED_TEMPLATE = 512, + TEMPLATE_NOT_SET = 513, + DIFFERENT_TEMPLATE = 514, + TEMPLATE_IS_IN_USE = 515, + TEMPLATE_INCOMPATIBLE = 516, + SEGMENT_NOT_FOUND = 517, + PAGE_OUT_OF_SPACE = 518, + RECORD_DUPLICATED = 519, + SEGMENT_OUT_OF_SPACE = 520, + PBTREE_FILE_NOT_EXISTS = 521, + OVERSIZE_RECORD = 522, + PBTREE_FILE_REDO_LOG_BROKEN = 523, + TEMPLATE_NOT_ACTIVATED = 524, + + // Storage Engine + SYSTEM_READ_ONLY = 600, + STORAGE_ENGINE_ERROR = 601, + STORAGE_ENGINE_NOT_READY = 602, + + // Query Engine + PLAN_FAILED_NETWORK_PARTITION = 721 }; } class Field { public: - TSDataType::TSDataType dataType = TSDataType::UNKNOWN; - boost::optional boolV; - boost::optional intV; - boost::optional dateV; - boost::optional longV; - boost::optional floatV; - boost::optional doubleV; - boost::optional stringV; - - explicit Field(TSDataType::TSDataType a) { - dataType = a; - } - - Field() = default; + TSDataType::TSDataType dataType = TSDataType::UNKNOWN; + boost::optional boolV; + boost::optional intV; + boost::optional dateV; + boost::optional longV; + boost::optional floatV; + boost::optional doubleV; + boost::optional stringV; + + explicit Field(TSDataType::TSDataType a) { + dataType = a; + } + + Field() = default; }; -enum class ColumnCategory { - TAG, - FIELD, - ATTRIBUTE -}; +enum class ColumnCategory { TAG, FIELD, ATTRIBUTE }; class MyStringBuffer { public: - MyStringBuffer(); - explicit MyStringBuffer(const std::string& str); - - void reserve(size_t n); - void clear(); - bool hasRemaining(); - int getInt(); - boost::gregorian::date getDate(); - int64_t getInt64(); - float getFloat(); - double getDouble(); - char getChar(); - bool getBool(); - std::string getString(); - - void putInt(int ins); - void putDate(boost::gregorian::date date); - void putInt64(int64_t ins); - void putFloat(float ins); - void putDouble(double ins); - void putChar(char ins); - void putBool(bool ins); - void putString(const std::string& ins); - void concat(const std::string& ins); + MyStringBuffer(); + explicit MyStringBuffer(const std::string& str); + + void reserve(size_t n); + void clear(); + bool hasRemaining(); + int getInt(); + boost::gregorian::date getDate(); + int64_t getInt64(); + float getFloat(); + double getDouble(); + char getChar(); + bool getBool(); + std::string getString(); + + void putInt(int ins); + void putDate(boost::gregorian::date date); + void putInt64(int64_t ins); + void putFloat(float ins); + void putDouble(double ins); + void putChar(char ins); + void putBool(bool ins); + void putString(const std::string& ins); + void concat(const std::string& ins); public: - std::string str; - size_t pos; + std::string str; + size_t pos; private: - void checkBigEndian(); - const char* getOrderedByte(size_t len); - void putOrderedByte(char* buf, int len); + void checkBigEndian(); + const char* getOrderedByte(size_t len); + void putOrderedByte(char* buf, int len); private: - bool isBigEndian{}; - char numericBuf[8]{}; //only be used by int, long, float, double etc. + bool isBigEndian{}; + char numericBuf[8]{}; //only be used by int, long, float, double etc. }; class BitMap { public: - explicit BitMap(size_t size = 0); - void resize(size_t size); - bool mark(size_t position); - bool unmark(size_t position); - void markAll(); - void reset(); - bool isMarked(size_t position) const; - bool isAllUnmarked() const; - bool isAllMarked() const; - const std::vector& getByteArray() const; - size_t getSize() const; + explicit BitMap(size_t size = 0); + void resize(size_t size); + bool mark(size_t position); + bool unmark(size_t position); + void markAll(); + void reset(); + bool isMarked(size_t position) const; + bool isAllUnmarked() const; + bool isAllMarked() const; + const std::vector& getByteArray() const; + size_t getSize() const; private: - size_t size; - std::vector bits; + size_t size; + std::vector bits; }; class IoTDBException : public std::exception { public: - IoTDBException() = default; + IoTDBException() = default; - explicit IoTDBException(const std::string& m) : message(m) { - } + explicit IoTDBException(const std::string& m) : message(m) {} - explicit IoTDBException(const char* m) : message(m) { - } + explicit IoTDBException(const char* m) : message(m) {} - virtual const char* what() const noexcept override { - return message.c_str(); - } + virtual const char* what() const noexcept override { + return message.c_str(); + } private: - std::string message; + std::string message; }; class DateTimeParseException : public IoTDBException { private: - std::string parsedString; - int errorIndex; + std::string parsedString; + int errorIndex; public: - explicit DateTimeParseException(const std::string& message, - std::string parsedData, - int errorIndex) - : IoTDBException(message), - parsedString(std::move(parsedData)), - errorIndex(errorIndex) { - } - - explicit DateTimeParseException(const std::string& message, - std::string parsedData, - int errorIndex, - const std::exception& cause) - : IoTDBException(message + " [Caused by: " + cause.what() + "]"), - parsedString(std::move(parsedData)), - errorIndex(errorIndex) { - } - - const std::string& getParsedString() const noexcept { - return parsedString; - } - - int getErrorIndex() const noexcept { - return errorIndex; - } - - const char* what() const noexcept override { - static std::string fullMsg; - fullMsg = std::string(IoTDBException::what()) + - "\nParsed data: " + parsedString + - "\nError index: " + std::to_string(errorIndex); - return fullMsg.c_str(); - } + explicit DateTimeParseException(const std::string& message, std::string parsedData, + int errorIndex) + : IoTDBException(message), parsedString(std::move(parsedData)), errorIndex(errorIndex) {} + + explicit DateTimeParseException(const std::string& message, std::string parsedData, + int errorIndex, const std::exception& cause) + : IoTDBException(message + " [Caused by: " + cause.what() + "]"), + parsedString(std::move(parsedData)), errorIndex(errorIndex) {} + + const std::string& getParsedString() const noexcept { + return parsedString; + } + + int getErrorIndex() const noexcept { + return errorIndex; + } + + const char* what() const noexcept override { + static std::string fullMsg; + fullMsg = std::string(IoTDBException::what()) + "\nParsed data: " + parsedString + + "\nError index: " + std::to_string(errorIndex); + return fullMsg.c_str(); + } }; class IoTDBConnectionException : public IoTDBException { public: - IoTDBConnectionException() { - } + IoTDBConnectionException() {} - explicit IoTDBConnectionException(const char* m) : IoTDBException(m) { - } + explicit IoTDBConnectionException(const char* m) : IoTDBException(m) {} - explicit IoTDBConnectionException(const std::string& m) : IoTDBException(m) { - } + explicit IoTDBConnectionException(const std::string& m) : IoTDBException(m) {} }; class ExecutionException : public IoTDBException { public: - ExecutionException() { - } + ExecutionException() {} - explicit ExecutionException(const char* m) : IoTDBException(m) { - } + explicit ExecutionException(const char* m) : IoTDBException(m) {} - explicit ExecutionException(const std::string& m) : IoTDBException(m) { - } + explicit ExecutionException(const std::string& m) : IoTDBException(m) {} - explicit ExecutionException(const std::string& m, const TSStatus& tsStatus) : IoTDBException(m), status(tsStatus) { - } + explicit ExecutionException(const std::string& m, const TSStatus& tsStatus) + : IoTDBException(m), status(tsStatus) {} - TSStatus status; + TSStatus status; }; class BatchExecutionException : public IoTDBException { public: - BatchExecutionException() { - } + BatchExecutionException() {} - explicit BatchExecutionException(const char* m) : IoTDBException(m) { - } + explicit BatchExecutionException(const char* m) : IoTDBException(m) {} - explicit BatchExecutionException(const std::string& m) : IoTDBException(m) { - } + explicit BatchExecutionException(const std::string& m) : IoTDBException(m) {} - explicit BatchExecutionException(const std::vector& statusList) : statusList(statusList) { - } + explicit BatchExecutionException(const std::vector& statusList) + : statusList(statusList) {} - BatchExecutionException(const std::string& m, const std::vector& statusList) : IoTDBException(m), - statusList(statusList) { - } + BatchExecutionException(const std::string& m, const std::vector& statusList) + : IoTDBException(m), statusList(statusList) {} - std::vector statusList; + std::vector statusList; }; class RedirectException : public IoTDBException { public: - RedirectException() { - } + RedirectException() {} - explicit RedirectException(const char* m) : IoTDBException(m) { - } + explicit RedirectException(const char* m) : IoTDBException(m) {} - explicit RedirectException(const std::string& m) : IoTDBException(m) { - } + explicit RedirectException(const std::string& m) : IoTDBException(m) {} - RedirectException(const std::string& m, const TEndPoint& endPoint) : IoTDBException(m), endPoint(endPoint) { - } + RedirectException(const std::string& m, const TEndPoint& endPoint) + : IoTDBException(m), endPoint(endPoint) {} - RedirectException(const std::string& m, const map& deviceEndPointMap) : IoTDBException(m), - deviceEndPointMap(deviceEndPointMap) { - } + RedirectException(const std::string& m, const map& deviceEndPointMap) + : IoTDBException(m), deviceEndPointMap(deviceEndPointMap) {} - RedirectException(const std::string& m, const vector& endPointList) : IoTDBException(m), - endPointList(endPointList) { - } + RedirectException(const std::string& m, const vector& endPointList) + : IoTDBException(m), endPointList(endPointList) {} - TEndPoint endPoint; - map deviceEndPointMap; - vector endPointList; + TEndPoint endPoint; + map deviceEndPointMap; + vector endPointList; }; class UnSupportedDataTypeException : public IoTDBException { public: - UnSupportedDataTypeException() { - } + UnSupportedDataTypeException() {} - explicit UnSupportedDataTypeException(const char* m) : IoTDBException(m) { - } + explicit UnSupportedDataTypeException(const char* m) : IoTDBException(m) {} - explicit UnSupportedDataTypeException(const std::string& m) : IoTDBException("UnSupported dataType: " + m) { - } + explicit UnSupportedDataTypeException(const std::string& m) + : IoTDBException("UnSupported dataType: " + m) {} }; class SchemaNotFoundException : public IoTDBException { public: - SchemaNotFoundException() { - } + SchemaNotFoundException() {} - explicit SchemaNotFoundException(const char* m) : IoTDBException(m) { - } + explicit SchemaNotFoundException(const char* m) : IoTDBException(m) {} - explicit SchemaNotFoundException(const std::string& m) : IoTDBException(m) { - } + explicit SchemaNotFoundException(const std::string& m) : IoTDBException(m) {} }; class StatementExecutionException : public IoTDBException { public: - StatementExecutionException() { - } + StatementExecutionException() {} - explicit StatementExecutionException(const char* m) : IoTDBException(m) { - } + explicit StatementExecutionException(const char* m) : IoTDBException(m) {} - explicit StatementExecutionException(const std::string& m) : IoTDBException(m) { - } + explicit StatementExecutionException(const std::string& m) : IoTDBException(m) {} }; -enum LogLevelType { - LEVEL_DEBUG = 0, - LEVEL_INFO, - LEVEL_WARN, - LEVEL_ERROR -}; +enum LogLevelType { LEVEL_DEBUG = 0, LEVEL_INFO, LEVEL_WARN, LEVEL_ERROR }; extern LogLevelType LOG_LEVEL; -#define log_debug(fmt,...) do {if(LOG_LEVEL <= LEVEL_DEBUG) {string s=string("[DEBUG] %s:%d (%s) - ") + fmt + "\n"; printf(s.c_str(), __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__);}} while(0) -#define log_info(fmt,...) do {if(LOG_LEVEL <= LEVEL_INFO) {string s=string("[INFO] %s:%d (%s) - ") + fmt + "\n"; printf(s.c_str(), __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__);}} while(0) -#define log_warn(fmt,...) do {if(LOG_LEVEL <= LEVEL_WARN) {string s=string("[WARN] %s:%d (%s) - ") + fmt + "\n"; printf(s.c_str(), __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__);}} while(0) -#define log_error(fmt,...) do {if(LOG_LEVEL <= LEVEL_ERROR) {string s=string("[ERROR] %s:%d (%s) - ") + fmt + "\n"; printf(s.c_str(), __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__);}} while(0) +#define log_debug(fmt, ...) \ + do { \ + if (LOG_LEVEL <= LEVEL_DEBUG) { \ + string s = string("[DEBUG] %s:%d (%s) - ") + fmt + "\n"; \ + printf(s.c_str(), __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \ + } \ + } while (0) +#define log_info(fmt, ...) \ + do { \ + if (LOG_LEVEL <= LEVEL_INFO) { \ + string s = string("[INFO] %s:%d (%s) - ") + fmt + "\n"; \ + printf(s.c_str(), __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \ + } \ + } while (0) +#define log_warn(fmt, ...) \ + do { \ + if (LOG_LEVEL <= LEVEL_WARN) { \ + string s = string("[WARN] %s:%d (%s) - ") + fmt + "\n"; \ + printf(s.c_str(), __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \ + } \ + } while (0) +#define log_error(fmt, ...) \ + do { \ + if (LOG_LEVEL <= LEVEL_ERROR) { \ + string s = string("[ERROR] %s:%d (%s) - ") + fmt + "\n"; \ + printf(s.c_str(), __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); \ + } \ + } while (0) class RpcUtils { public: - std::shared_ptr SUCCESS_STATUS; + std::shared_ptr SUCCESS_STATUS; - RpcUtils() { - SUCCESS_STATUS = std::make_shared(); - SUCCESS_STATUS->__set_code(TSStatusCode::SUCCESS_STATUS); - } + RpcUtils() { + SUCCESS_STATUS = std::make_shared(); + SUCCESS_STATUS->__set_code(TSStatusCode::SUCCESS_STATUS); + } - static void verifySuccess(const TSStatus& status); + static void verifySuccess(const TSStatus& status); - static void verifySuccessWithRedirection(const TSStatus& status); + static void verifySuccessWithRedirection(const TSStatus& status); - static void verifySuccessWithRedirectionForMultiDevices(const TSStatus& status, vector devices); + static void verifySuccessWithRedirectionForMultiDevices(const TSStatus& status, + vector devices); - static void verifySuccess(const std::vector& statuses); + static void verifySuccess(const std::vector& statuses); - static TSStatus getStatus(TSStatusCode::TSStatusCode tsStatusCode); + static TSStatus getStatus(TSStatusCode::TSStatusCode tsStatusCode); - static TSStatus getStatus(int code, const std::string& message); + static TSStatus getStatus(int code, const std::string& message); - static std::shared_ptr getTSExecuteStatementResp(TSStatusCode::TSStatusCode tsStatusCode); + static std::shared_ptr + getTSExecuteStatementResp(TSStatusCode::TSStatusCode tsStatusCode); - static std::shared_ptr - getTSExecuteStatementResp(TSStatusCode::TSStatusCode tsStatusCode, const std::string& message); + static std::shared_ptr + getTSExecuteStatementResp(TSStatusCode::TSStatusCode tsStatusCode, const std::string& message); - static std::shared_ptr getTSExecuteStatementResp(const TSStatus& status); + static std::shared_ptr getTSExecuteStatementResp(const TSStatus& status); - static std::shared_ptr getTSFetchResultsResp(TSStatusCode::TSStatusCode tsStatusCode); + static std::shared_ptr + getTSFetchResultsResp(TSStatusCode::TSStatusCode tsStatusCode); - static std::shared_ptr - getTSFetchResultsResp(TSStatusCode::TSStatusCode tsStatusCode, const std::string& appendMessage); + static std::shared_ptr + getTSFetchResultsResp(TSStatusCode::TSStatusCode tsStatusCode, const std::string& appendMessage); - static std::shared_ptr getTSFetchResultsResp(const TSStatus& status); + static std::shared_ptr getTSFetchResultsResp(const TSStatus& status); }; class UrlUtils { private: - static const std::string PORT_SEPARATOR; - static const std::string ABB_COLON; + static const std::string PORT_SEPARATOR; + static const std::string ABB_COLON; - UrlUtils() = delete; - ~UrlUtils() = delete; + UrlUtils() = delete; + ~UrlUtils() = delete; public: - /** + /** * Parse TEndPoint from a given TEndPointUrl * example:[D80:0000:0000:0000:ABAA:0000:00C2:0002]:22227 * * @param endPointUrl ip:port * @return TEndPoint with default values if parse error */ - static TEndPoint parseTEndPointIpv4AndIpv6Url(const std::string& endPointUrl); + static TEndPoint parseTEndPointIpv4AndIpv6Url(const std::string& endPointUrl); }; #endif diff --git a/iotdb-client/client-cpp/src/main/DeviceID.h b/iotdb-client/client-cpp/src/main/DeviceID.h index df2682cd5199e..6903a71b8e88b 100644 --- a/iotdb-client/client-cpp/src/main/DeviceID.h +++ b/iotdb-client/client-cpp/src/main/DeviceID.h @@ -32,130 +32,134 @@ static const std::string PATH_SEPARATOR = "."; class IDeviceID { public: - virtual ~IDeviceID() = default; - virtual std::string get_table_name() { return ""; } - virtual int segment_num() { return 0; } - virtual const std::vector& get_segments() const { - return empty_segments_; - } - virtual std::string get_device_name() const { return ""; }; - virtual bool operator<(const IDeviceID& other) { return 0; } - virtual bool operator==(const IDeviceID& other) { return false; } - virtual bool operator!=(const IDeviceID& other) { return false; } + virtual ~IDeviceID() = default; + virtual std::string get_table_name() { + return ""; + } + virtual int segment_num() { + return 0; + } + virtual const std::vector& get_segments() const { + return empty_segments_; + } + virtual std::string get_device_name() const { + return ""; + }; + virtual bool operator<(const IDeviceID& other) { + return 0; + } + virtual bool operator==(const IDeviceID& other) { + return false; + } + virtual bool operator!=(const IDeviceID& other) { + return false; + } protected: - IDeviceID() : empty_segments_() {} + IDeviceID() : empty_segments_() {} private: - const std::vector empty_segments_; + const std::vector empty_segments_; }; struct IDeviceIDComparator { - bool operator()(const std::shared_ptr& lhs, - const std::shared_ptr& rhs) const { - return *lhs < *rhs; - } + bool operator()(const std::shared_ptr& lhs, + const std::shared_ptr& rhs) const { + return *lhs < *rhs; + } }; class StringArrayDeviceID : public IDeviceID { public: - explicit StringArrayDeviceID(const std::vector& segments) - : segments_(formalize(segments)) {} + explicit StringArrayDeviceID(const std::vector& segments) + : segments_(formalize(segments)) {} - explicit StringArrayDeviceID() : segments_() {} + explicit StringArrayDeviceID() : segments_() {} - ~StringArrayDeviceID() override = default; + ~StringArrayDeviceID() override = default; - std::string get_device_name() const override { - return segments_.empty() ? "" : std::accumulate(std::next(segments_.begin()), segments_.end(), - segments_.front(), - [](std::string a, const std::string& b) { - return std::move(a) + "." + b; - }); - }; + std::string get_device_name() const override { + return segments_.empty() + ? "" + : std::accumulate( + std::next(segments_.begin()), segments_.end(), segments_.front(), + [](std::string a, const std::string& b) { return std::move(a) + "." + b; }); + }; - std::string get_table_name() override { - return segments_.empty() ? "" : segments_[0]; - } + std::string get_table_name() override { + return segments_.empty() ? "" : segments_[0]; + } - int segment_num() override { return static_cast(segments_.size()); } + int segment_num() override { + return static_cast(segments_.size()); + } - const std::vector& get_segments() const override { - return segments_; - } + const std::vector& get_segments() const override { + return segments_; + } - bool operator<(const IDeviceID& other) override { - auto other_segments = other.get_segments(); - return std::lexicographical_compare(segments_.begin(), segments_.end(), - other_segments.begin(), - other_segments.end()); - } + bool operator<(const IDeviceID& other) override { + auto other_segments = other.get_segments(); + return std::lexicographical_compare(segments_.begin(), segments_.end(), other_segments.begin(), + other_segments.end()); + } - bool operator==(const IDeviceID& other) override { - auto other_segments = other.get_segments(); - return (segments_.size() == other_segments.size()) && - std::equal(segments_.begin(), segments_.end(), - other_segments.begin()); - } + bool operator==(const IDeviceID& other) override { + auto other_segments = other.get_segments(); + return (segments_.size() == other_segments.size()) && + std::equal(segments_.begin(), segments_.end(), other_segments.begin()); + } - bool operator!=(const IDeviceID& other) override { - return !(*this == other); - } + bool operator!=(const IDeviceID& other) override { + return !(*this == other); + } private: - std::vector segments_; - - std::vector formalize( - const std::vector& segments) { - auto it = - std::find_if(segments.rbegin(), segments.rend(), - [](const std::string& seg) { return !seg.empty(); }); - return std::vector(segments.begin(), it.base()); + std::vector segments_; + + std::vector formalize(const std::vector& segments) { + auto it = std::find_if(segments.rbegin(), segments.rend(), + [](const std::string& seg) { return !seg.empty(); }); + return std::vector(segments.begin(), it.base()); + } + + std::vector split_device_id_string(const std::vector& splits) { + size_t segment_cnt = splits.size(); + std::vector final_segments; + + if (segment_cnt == 0) { + return final_segments; } - std::vector split_device_id_string( - const std::vector& splits) { - size_t segment_cnt = splits.size(); - std::vector final_segments; - - if (segment_cnt == 0) { - return final_segments; - } - - if (segment_cnt == 1) { - // "root" -> {"root"} - final_segments.push_back(splits[0]); - } else if (segment_cnt < static_cast( - DEFAULT_SEGMENT_NUM_FOR_TABLE_NAME + 1)) { - // "root.a" -> {"root", "a"} - // "root.a.b" -> {"root.a", "b"} - std::string table_name = std::accumulate( - splits.begin(), splits.end() - 1, std::string(), - [](const std::string& a, const std::string& b) { - return a.empty() ? b : a + PATH_SEPARATOR + b; - }); - final_segments.push_back(table_name); - final_segments.push_back(splits.back()); - } else { - // "root.a.b.c" -> {"root.a.b", "c"} - // "root.a.b.c.d" -> {"root.a.b", "c", "d"} - std::string table_name = std::accumulate( - splits.begin(), - splits.begin() + DEFAULT_SEGMENT_NUM_FOR_TABLE_NAME, - std::string(), [](const std::string& a, const std::string& b) { - return a.empty() ? b : a + PATH_SEPARATOR + b; - }); - - final_segments.emplace_back(std::move(table_name)); - final_segments.insert( - final_segments.end(), - splits.begin() + DEFAULT_SEGMENT_NUM_FOR_TABLE_NAME, - splits.end()); - } - - return final_segments; + if (segment_cnt == 1) { + // "root" -> {"root"} + final_segments.push_back(splits[0]); + } else if (segment_cnt < static_cast(DEFAULT_SEGMENT_NUM_FOR_TABLE_NAME + 1)) { + // "root.a" -> {"root", "a"} + // "root.a.b" -> {"root.a", "b"} + std::string table_name = std::accumulate(splits.begin(), splits.end() - 1, std::string(), + [](const std::string& a, const std::string& b) { + return a.empty() ? b : a + PATH_SEPARATOR + b; + }); + final_segments.push_back(table_name); + final_segments.push_back(splits.back()); + } else { + // "root.a.b.c" -> {"root.a.b", "c"} + // "root.a.b.c.d" -> {"root.a.b", "c", "d"} + std::string table_name = + std::accumulate(splits.begin(), splits.begin() + DEFAULT_SEGMENT_NUM_FOR_TABLE_NAME, + std::string(), [](const std::string& a, const std::string& b) { + return a.empty() ? b : a + PATH_SEPARATOR + b; + }); + + final_segments.emplace_back(std::move(table_name)); + final_segments.insert(final_segments.end(), + splits.begin() + DEFAULT_SEGMENT_NUM_FOR_TABLE_NAME, splits.end()); } + + return final_segments; + } }; -} +} // namespace storage #endif \ No newline at end of file diff --git a/iotdb-client/client-cpp/src/main/IoTDBRpcDataSet.cpp b/iotdb-client/client-cpp/src/main/IoTDBRpcDataSet.cpp index 2afef2b99684b..8b02a35607950 100644 --- a/iotdb-client/client-cpp/src/main/IoTDBRpcDataSet.cpp +++ b/iotdb-client/client-cpp/src/main/IoTDBRpcDataSet.cpp @@ -35,240 +35,214 @@ const std::string IoTDBRpcDataSet::MILLISECOND = "ms"; const std::string IoTDBRpcDataSet::MICROSECOND = "us"; const std::string IoTDBRpcDataSet::NANOSECOND = "ns"; -IoTDBRpcDataSet::IoTDBRpcDataSet(const std::string& sql, - const std::vector& columnNameList, - const std::vector& columnTypeList, - const std::map& columnNameIndex, - bool ignoreTimestamp, - bool moreData, - int64_t queryId, - int64_t statementId, - std::shared_ptr client, - int64_t sessionId, - const std::vector& queryResult, - int32_t fetchSize, - const int64_t timeout, - const std::string& zoneId, - const std::string& timeFormat, - int32_t timeFactor, - std::vector& columnIndex2TsBlockColumnIndexList) - : sql_(sql), - isClosed_(false), - client_(client), - fetchSize_(fetchSize), - timeout_(timeout), - hasCachedRecord_(false), - lastReadWasNull_(false), - columnSize_(static_cast(columnNameList.size())), - sessionId_(sessionId), - queryId_(queryId), - statementId_(statementId), - time_(0), - ignoreTimestamp_(ignoreTimestamp), - moreData_(moreData), - queryResult_(queryResult), - curTsBlock_(nullptr), - queryResultSize_(static_cast(queryResult.size())), - queryResultIndex_(0), - tsBlockSize_(0), - tsBlockIndex_(-1), - timeZoneId_(zoneId), - timeFormat_(timeFormat), +IoTDBRpcDataSet::IoTDBRpcDataSet( + const std::string& sql, const std::vector& columnNameList, + const std::vector& columnTypeList, + const std::map& columnNameIndex, bool ignoreTimestamp, bool moreData, + int64_t queryId, int64_t statementId, std::shared_ptr client, + int64_t sessionId, const std::vector& queryResult, int32_t fetchSize, + const int64_t timeout, const std::string& zoneId, const std::string& timeFormat, + int32_t timeFactor, std::vector& columnIndex2TsBlockColumnIndexList) + : sql_(sql), isClosed_(false), client_(client), fetchSize_(fetchSize), timeout_(timeout), + hasCachedRecord_(false), lastReadWasNull_(false), + columnSize_(static_cast(columnNameList.size())), sessionId_(sessionId), + queryId_(queryId), statementId_(statementId), time_(0), ignoreTimestamp_(ignoreTimestamp), + moreData_(moreData), queryResult_(queryResult), curTsBlock_(nullptr), + queryResultSize_(static_cast(queryResult.size())), queryResultIndex_(0), + tsBlockSize_(0), tsBlockIndex_(-1), timeZoneId_(zoneId), timeFormat_(timeFormat), timeFactor_(timeFactor) { - int columnStartIndex = 1; - int resultSetColumnSize = columnNameList_.size(); - // newly generated or updated columnIndex2TsBlockColumnIndexList.size() may not be equal to - // columnNameList.size() - // so we need startIndexForColumnIndex2TsBlockColumnIndexList to adjust the mapping relation - int startIndexForColumnIndex2TsBlockColumnIndexList = 0; - // for Time Column in tree model which should always be the first column and its index for - // TsBlockColumn is -1 + int columnStartIndex = 1; + int resultSetColumnSize = columnNameList_.size(); + // newly generated or updated columnIndex2TsBlockColumnIndexList.size() may not be equal to + // columnNameList.size() + // so we need startIndexForColumnIndex2TsBlockColumnIndexList to adjust the mapping relation + int startIndexForColumnIndex2TsBlockColumnIndexList = 0; + // for Time Column in tree model which should always be the first column and its index for + // TsBlockColumn is -1 + if (!ignoreTimestamp) { + columnNameList_.push_back(TimestampColumnName); + columnTypeList_.push_back("INT64"); + columnName2TsBlockColumnIndexMap_[TimestampColumnName] = -1; + columnOrdinalMap_[TimestampColumnName] = 1; + if (!columnIndex2TsBlockColumnIndexList.empty()) { + columnIndex2TsBlockColumnIndexList.insert(columnIndex2TsBlockColumnIndexList.begin(), -1); + startIndexForColumnIndex2TsBlockColumnIndexList = 1; + } + columnStartIndex++; + resultSetColumnSize++; + } + + if (columnIndex2TsBlockColumnIndexList.empty()) { + columnIndex2TsBlockColumnIndexList.reserve(resultSetColumnSize); if (!ignoreTimestamp) { - columnNameList_.push_back(TimestampColumnName); - columnTypeList_.push_back("INT64"); - columnName2TsBlockColumnIndexMap_[TimestampColumnName] = -1; - columnOrdinalMap_[TimestampColumnName] = 1; - if (!columnIndex2TsBlockColumnIndexList.empty()) { - columnIndex2TsBlockColumnIndexList.insert(columnIndex2TsBlockColumnIndexList.begin(), -1); - startIndexForColumnIndex2TsBlockColumnIndexList = 1; - } - columnStartIndex++; - resultSetColumnSize++; + startIndexForColumnIndex2TsBlockColumnIndexList = 1; + columnIndex2TsBlockColumnIndexList.push_back(-1); } - - if (columnIndex2TsBlockColumnIndexList.empty()) { - columnIndex2TsBlockColumnIndexList.reserve(resultSetColumnSize); - if (!ignoreTimestamp) { - startIndexForColumnIndex2TsBlockColumnIndexList = 1; - columnIndex2TsBlockColumnIndexList.push_back(-1); - } - for (size_t i = 0; i < columnNameList.size(); ++i) { - columnIndex2TsBlockColumnIndexList.push_back(i); - } + for (size_t i = 0; i < columnNameList.size(); ++i) { + columnIndex2TsBlockColumnIndexList.push_back(i); } + } + + columnNameList_.insert(columnNameList_.end(), columnNameList.begin(), columnNameList.end()); + columnTypeList_.insert(columnTypeList_.end(), columnTypeList.begin(), columnTypeList.end()); - columnNameList_.insert(columnNameList_.end(), columnNameList.begin(), columnNameList.end()); - columnTypeList_.insert(columnTypeList_.end(), columnTypeList.begin(), columnTypeList.end()); + // Initialize data types for TsBlock columns + int32_t tsBlockColumnSize = 0; + for (auto value : columnIndex2TsBlockColumnIndexList) { + if (value > tsBlockColumnSize) { + tsBlockColumnSize = value; + } + } + tsBlockColumnSize += 1; + dataTypeForTsBlockColumn_.resize(tsBlockColumnSize); - // Initialize data types for TsBlock columns - int32_t tsBlockColumnSize = 0; - for (auto value : columnIndex2TsBlockColumnIndexList) { - if (value > tsBlockColumnSize) { - tsBlockColumnSize = value; - } + // Populate data types and maps + for (size_t i = 0; i < columnNameList.size(); i++) { + auto columnName = columnNameList[i]; + int32_t tsBlockColumnIndex = + columnIndex2TsBlockColumnIndexList[i + startIndexForColumnIndex2TsBlockColumnIndexList]; + if (tsBlockColumnIndex != -1) { + dataTypeForTsBlockColumn_[tsBlockColumnIndex] = getDataTypeByStr(columnTypeList[i]); } - tsBlockColumnSize += 1; - dataTypeForTsBlockColumn_.resize(tsBlockColumnSize); - - // Populate data types and maps - for (size_t i = 0; i < columnNameList.size(); i++) { - auto columnName = columnNameList[i]; - int32_t tsBlockColumnIndex = columnIndex2TsBlockColumnIndexList[i + - startIndexForColumnIndex2TsBlockColumnIndexList]; - if (tsBlockColumnIndex != -1) { - dataTypeForTsBlockColumn_[tsBlockColumnIndex] = getDataTypeByStr(columnTypeList[i]); - } - - if (columnName2TsBlockColumnIndexMap_.find(columnName) == columnName2TsBlockColumnIndexMap_.end()) { - columnOrdinalMap_[columnName] = i + columnStartIndex; - columnName2TsBlockColumnIndexMap_[columnName] = tsBlockColumnIndex; - } + + if (columnName2TsBlockColumnIndexMap_.find(columnName) == + columnName2TsBlockColumnIndexMap_.end()) { + columnOrdinalMap_[columnName] = i + columnStartIndex; + columnName2TsBlockColumnIndexMap_[columnName] = tsBlockColumnIndex; } + } - timePrecision_ = getTimePrecision(timeFactor_); - columnIndex2TsBlockColumnIndexList_ = columnIndex2TsBlockColumnIndexList; + timePrecision_ = getTimePrecision(timeFactor_); + columnIndex2TsBlockColumnIndexList_ = columnIndex2TsBlockColumnIndexList; } IoTDBRpcDataSet::~IoTDBRpcDataSet() { - if (!isClosed_) { - close(); - } + if (!isClosed_) { + close(); + } } bool IoTDBRpcDataSet::next() { - if (hasCachedBlock()) { - lastReadWasNull_ = false; - constructOneRow(); - return true; - } + if (hasCachedBlock()) { + lastReadWasNull_ = false; + constructOneRow(); + return true; + } - if (hasCachedByteBuffer()) { - constructOneTsBlock(); - constructOneRow(); - return true; - } + if (hasCachedByteBuffer()) { + constructOneTsBlock(); + constructOneRow(); + return true; + } - if (moreData_) { - bool hasResultSet = fetchResults(); - if (hasResultSet && hasCachedByteBuffer()) { - constructOneTsBlock(); - constructOneRow(); - return true; - } + if (moreData_) { + bool hasResultSet = fetchResults(); + if (hasResultSet && hasCachedByteBuffer()) { + constructOneTsBlock(); + constructOneRow(); + return true; } + } - close(); - return false; + close(); + return false; } void IoTDBRpcDataSet::close(bool forceClose) { - if ((!forceClose) && isClosed_) { - return; - } - TSCloseOperationReq closeReq; - closeReq.__set_sessionId(sessionId_); - closeReq.__set_statementId(statementId_); - closeReq.__set_queryId(queryId_); - TSStatus tsStatus; - try { - client_->closeOperation(tsStatus, closeReq); - RpcUtils::verifySuccess(tsStatus); - } - catch (const TTransportException& e) { - log_debug(e.what()); - throw IoTDBConnectionException(e.what()); - } catch (const IoTDBException& e) { - log_debug(e.what()); - throw; - } catch (exception& e) { - log_debug(e.what()); - throw IoTDBException(e.what()); - } - isClosed_ = true; - client_ = nullptr; + if ((!forceClose) && isClosed_) { + return; + } + TSCloseOperationReq closeReq; + closeReq.__set_sessionId(sessionId_); + closeReq.__set_statementId(statementId_); + closeReq.__set_queryId(queryId_); + TSStatus tsStatus; + try { + client_->closeOperation(tsStatus, closeReq); + RpcUtils::verifySuccess(tsStatus); + } catch (const TTransportException& e) { + log_debug(e.what()); + throw IoTDBConnectionException(e.what()); + } catch (const IoTDBException& e) { + log_debug(e.what()); + throw; + } catch (exception& e) { + log_debug(e.what()); + throw IoTDBException(e.what()); + } + isClosed_ = true; + client_ = nullptr; } bool IoTDBRpcDataSet::fetchResults() { - if (isClosed_) { - throw IoTDBException("This data set is already closed"); - } - - TSFetchResultsReq req; - req.__set_sessionId(sessionId_); - req.__set_statement(sql_); - req.__set_fetchSize(fetchSize_); - req.__set_queryId(queryId_); - req.__set_isAlign(true); - req.__set_timeout(timeout_); - TSFetchResultsResp resp; - client_->fetchResultsV2(resp, req); - RpcUtils::verifySuccess(resp.status); - moreData_ = resp.moreData; - if (!resp.hasResultSet) { - close(); - } - else { - queryResult_ = resp.queryResult; - queryResultIndex_ = 0; - if (!queryResult_.empty()) { - queryResultSize_ = queryResult_.size(); - } - else { - queryResultSize_ = 0; - } - tsBlockIndex_ = -1; - tsBlockSize_ = 0; + if (isClosed_) { + throw IoTDBException("This data set is already closed"); + } + + TSFetchResultsReq req; + req.__set_sessionId(sessionId_); + req.__set_statement(sql_); + req.__set_fetchSize(fetchSize_); + req.__set_queryId(queryId_); + req.__set_isAlign(true); + req.__set_timeout(timeout_); + TSFetchResultsResp resp; + client_->fetchResultsV2(resp, req); + RpcUtils::verifySuccess(resp.status); + moreData_ = resp.moreData; + if (!resp.hasResultSet) { + close(); + } else { + queryResult_ = resp.queryResult; + queryResultIndex_ = 0; + if (!queryResult_.empty()) { + queryResultSize_ = queryResult_.size(); + } else { + queryResultSize_ = 0; } - return resp.hasResultSet; + tsBlockIndex_ = -1; + tsBlockSize_ = 0; + } + return resp.hasResultSet; } void IoTDBRpcDataSet::constructOneRow() { - tsBlockIndex_++; - hasCachedRecord_ = true; - time_ = curTsBlock_->getTimeColumn()->getLong(tsBlockIndex_); + tsBlockIndex_++; + hasCachedRecord_ = true; + time_ = curTsBlock_->getTimeColumn()->getLong(tsBlockIndex_); } void IoTDBRpcDataSet::constructOneTsBlock() { - lastReadWasNull_ = false; - const auto& curTsBlockBytes = queryResult_[queryResultIndex_]; - queryResultIndex_++; - curTsBlock_ = TsBlock::deserialize(curTsBlockBytes); - tsBlockIndex_ = -1; - tsBlockSize_ = curTsBlock_->getPositionCount(); + lastReadWasNull_ = false; + const auto& curTsBlockBytes = queryResult_[queryResultIndex_]; + queryResultIndex_++; + curTsBlock_ = TsBlock::deserialize(curTsBlockBytes); + tsBlockIndex_ = -1; + tsBlockSize_ = curTsBlock_->getPositionCount(); } bool IoTDBRpcDataSet::isNullByIndex(int32_t columnIndex) { - int32_t index = getTsBlockColumnIndexForColumnIndex(columnIndex); - return isNull(index, tsBlockIndex_); + int32_t index = getTsBlockColumnIndexForColumnIndex(columnIndex); + return isNull(index, tsBlockIndex_); } bool IoTDBRpcDataSet::isNullByColumnName(const std::string& columnName) { - int32_t index = getTsBlockColumnIndexForColumnName(columnName); - return isNull(index, tsBlockIndex_); + int32_t index = getTsBlockColumnIndexForColumnName(columnName); + return isNull(index, tsBlockIndex_); } bool IoTDBRpcDataSet::isNull(int32_t index, int32_t rowNum) { - return index >= 0 && curTsBlock_->getColumn(index)->isNull(rowNum); + return index >= 0 && curTsBlock_->getColumn(index)->isNull(rowNum); } boost::optional IoTDBRpcDataSet::getBooleanByIndex(int32_t columnIndex) { - int32_t index = getTsBlockColumnIndexForColumnIndex(columnIndex); - return getBooleanByTsBlockColumnIndex(index); + int32_t index = getTsBlockColumnIndexForColumnIndex(columnIndex); + return getBooleanByTsBlockColumnIndex(index); } boost::optional IoTDBRpcDataSet::getBoolean(const std::string& columnName) { - int32_t index = getTsBlockColumnIndexForColumnName(columnName); - return getBooleanByTsBlockColumnIndex(index); + int32_t index = getTsBlockColumnIndexForColumnName(columnName); + return getBooleanByTsBlockColumnIndex(index); } // Note: tsBlockColumnIndex < 0 indicates the time pseudo-column in tree model. @@ -276,366 +250,361 @@ boost::optional IoTDBRpcDataSet::getBoolean(const std::string& columnName) // All other typed getters throw IoTDBException to prevent undefined behavior // from accessing valueColumns_ with a negative index. boost::optional IoTDBRpcDataSet::getBooleanByTsBlockColumnIndex(int32_t tsBlockColumnIndex) { - checkRecord(); - if (tsBlockColumnIndex < 0) { - throw IoTDBException("Cannot read boolean from time column"); - } - if (!isNull(tsBlockColumnIndex, tsBlockIndex_)) { - lastReadWasNull_ = false; - return curTsBlock_->getColumn(tsBlockColumnIndex)->getBoolean(tsBlockIndex_); - } - else { - lastReadWasNull_ = true; - return boost::none; - } + checkRecord(); + if (tsBlockColumnIndex < 0) { + throw IoTDBException("Cannot read boolean from time column"); + } + if (!isNull(tsBlockColumnIndex, tsBlockIndex_)) { + lastReadWasNull_ = false; + return curTsBlock_->getColumn(tsBlockColumnIndex)->getBoolean(tsBlockIndex_); + } else { + lastReadWasNull_ = true; + return boost::none; + } } boost::optional IoTDBRpcDataSet::getDoubleByIndex(int32_t columnIndex) { - int32_t index = getTsBlockColumnIndexForColumnIndex(columnIndex); - return getDoubleByTsBlockColumnIndex(index); + int32_t index = getTsBlockColumnIndexForColumnIndex(columnIndex); + return getDoubleByTsBlockColumnIndex(index); } boost::optional IoTDBRpcDataSet::getDouble(const std::string& columnName) { - int32_t index = getTsBlockColumnIndexForColumnName(columnName); - return getDoubleByTsBlockColumnIndex(index); + int32_t index = getTsBlockColumnIndexForColumnName(columnName); + return getDoubleByTsBlockColumnIndex(index); } boost::optional IoTDBRpcDataSet::getDoubleByTsBlockColumnIndex(int32_t tsBlockColumnIndex) { - checkRecord(); - if (tsBlockColumnIndex < 0) { - throw IoTDBException("Cannot read double from time column"); - } - if (!isNull(tsBlockColumnIndex, tsBlockIndex_)) { - lastReadWasNull_ = false; - return curTsBlock_->getColumn(tsBlockColumnIndex)->getDouble(tsBlockIndex_); - } - else { - lastReadWasNull_ = true; - return boost::none; - } + checkRecord(); + if (tsBlockColumnIndex < 0) { + throw IoTDBException("Cannot read double from time column"); + } + if (!isNull(tsBlockColumnIndex, tsBlockIndex_)) { + lastReadWasNull_ = false; + return curTsBlock_->getColumn(tsBlockColumnIndex)->getDouble(tsBlockIndex_); + } else { + lastReadWasNull_ = true; + return boost::none; + } } boost::optional IoTDBRpcDataSet::getFloatByIndex(int32_t columnIndex) { - int32_t index = getTsBlockColumnIndexForColumnIndex(columnIndex); - return getFloatByTsBlockColumnIndex(index); + int32_t index = getTsBlockColumnIndexForColumnIndex(columnIndex); + return getFloatByTsBlockColumnIndex(index); } boost::optional IoTDBRpcDataSet::getFloat(const std::string& columnName) { - int32_t index = getTsBlockColumnIndexForColumnName(columnName); - return getFloatByTsBlockColumnIndex(index); + int32_t index = getTsBlockColumnIndexForColumnName(columnName); + return getFloatByTsBlockColumnIndex(index); } boost::optional IoTDBRpcDataSet::getFloatByTsBlockColumnIndex(int32_t tsBlockColumnIndex) { - checkRecord(); - if (tsBlockColumnIndex < 0) { - throw IoTDBException("Cannot read float from time column"); - } - if (!isNull(tsBlockColumnIndex, tsBlockIndex_)) { - lastReadWasNull_ = false; - return curTsBlock_->getColumn(tsBlockColumnIndex)->getFloat(tsBlockIndex_); - } - else { - lastReadWasNull_ = true; - return boost::none; - } + checkRecord(); + if (tsBlockColumnIndex < 0) { + throw IoTDBException("Cannot read float from time column"); + } + if (!isNull(tsBlockColumnIndex, tsBlockIndex_)) { + lastReadWasNull_ = false; + return curTsBlock_->getColumn(tsBlockColumnIndex)->getFloat(tsBlockIndex_); + } else { + lastReadWasNull_ = true; + return boost::none; + } } boost::optional IoTDBRpcDataSet::getIntByIndex(int32_t columnIndex) { - int32_t index = getTsBlockColumnIndexForColumnIndex(columnIndex); - return getIntByTsBlockColumnIndex(index); + int32_t index = getTsBlockColumnIndexForColumnIndex(columnIndex); + return getIntByTsBlockColumnIndex(index); } boost::optional IoTDBRpcDataSet::getInt(const std::string& columnName) { - int32_t index = getTsBlockColumnIndexForColumnName(columnName); - return getIntByTsBlockColumnIndex(index); + int32_t index = getTsBlockColumnIndexForColumnName(columnName); + return getIntByTsBlockColumnIndex(index); } boost::optional IoTDBRpcDataSet::getIntByTsBlockColumnIndex(int32_t tsBlockColumnIndex) { - checkRecord(); - if (tsBlockColumnIndex < 0) { - throw IoTDBException("Cannot read int32 from time column"); - } - if (!isNull(tsBlockColumnIndex, tsBlockIndex_)) { - lastReadWasNull_ = false; - TSDataType::TSDataType dataType = curTsBlock_->getColumn(tsBlockColumnIndex)->getDataType(); - if (dataType == TSDataType::INT64) { - return static_cast(curTsBlock_->getColumn(tsBlockColumnIndex)->getLong(tsBlockIndex_)); - } - return curTsBlock_->getColumn(tsBlockColumnIndex)->getInt(tsBlockIndex_); - } - else { - lastReadWasNull_ = true; - return boost::none; + checkRecord(); + if (tsBlockColumnIndex < 0) { + throw IoTDBException("Cannot read int32 from time column"); + } + if (!isNull(tsBlockColumnIndex, tsBlockIndex_)) { + lastReadWasNull_ = false; + TSDataType::TSDataType dataType = curTsBlock_->getColumn(tsBlockColumnIndex)->getDataType(); + if (dataType == TSDataType::INT64) { + return static_cast( + curTsBlock_->getColumn(tsBlockColumnIndex)->getLong(tsBlockIndex_)); } + return curTsBlock_->getColumn(tsBlockColumnIndex)->getInt(tsBlockIndex_); + } else { + lastReadWasNull_ = true; + return boost::none; + } } boost::optional IoTDBRpcDataSet::getLongByIndex(int32_t columnIndex) { - int32_t index = getTsBlockColumnIndexForColumnIndex(columnIndex); - return getLongByTsBlockColumnIndex(index); + int32_t index = getTsBlockColumnIndexForColumnIndex(columnIndex); + return getLongByTsBlockColumnIndex(index); } boost::optional IoTDBRpcDataSet::getLong(const std::string& columnName) { - int32_t index = getTsBlockColumnIndexForColumnName(columnName); - return getLongByTsBlockColumnIndex(index); + int32_t index = getTsBlockColumnIndexForColumnName(columnName); + return getLongByTsBlockColumnIndex(index); } boost::optional IoTDBRpcDataSet::getLongByTsBlockColumnIndex(int32_t tsBlockColumnIndex) { - checkRecord(); - if (tsBlockColumnIndex < 0) { - lastReadWasNull_ = false; - return curTsBlock_->getTimeByIndex(tsBlockIndex_); - } - if (!isNull(tsBlockColumnIndex, tsBlockIndex_)) { - lastReadWasNull_ = false; - TSDataType::TSDataType dataType = curTsBlock_->getColumn(tsBlockColumnIndex)->getDataType(); - if (dataType == TSDataType::INT32) { - return static_cast(curTsBlock_->getColumn(tsBlockColumnIndex)->getInt(tsBlockIndex_)); - } - return curTsBlock_->getColumn(tsBlockColumnIndex)->getLong(tsBlockIndex_); - } - else { - lastReadWasNull_ = true; - return boost::none; + checkRecord(); + if (tsBlockColumnIndex < 0) { + lastReadWasNull_ = false; + return curTsBlock_->getTimeByIndex(tsBlockIndex_); + } + if (!isNull(tsBlockColumnIndex, tsBlockIndex_)) { + lastReadWasNull_ = false; + TSDataType::TSDataType dataType = curTsBlock_->getColumn(tsBlockColumnIndex)->getDataType(); + if (dataType == TSDataType::INT32) { + return static_cast( + curTsBlock_->getColumn(tsBlockColumnIndex)->getInt(tsBlockIndex_)); } + return curTsBlock_->getColumn(tsBlockColumnIndex)->getLong(tsBlockIndex_); + } else { + lastReadWasNull_ = true; + return boost::none; + } } std::shared_ptr IoTDBRpcDataSet::getBinaryByIndex(int32_t columnIndex) { - int32_t index = getTsBlockColumnIndexForColumnIndex(columnIndex); - return getBinaryByTsBlockColumnIndex(index); + int32_t index = getTsBlockColumnIndexForColumnIndex(columnIndex); + return getBinaryByTsBlockColumnIndex(index); } std::shared_ptr IoTDBRpcDataSet::getBinary(const std::string& columnName) { - int32_t index = getTsBlockColumnIndexForColumnName(columnName); - return getBinaryByTsBlockColumnIndex(index); + int32_t index = getTsBlockColumnIndexForColumnName(columnName); + return getBinaryByTsBlockColumnIndex(index); } std::shared_ptr IoTDBRpcDataSet::getBinaryByTsBlockColumnIndex(int32_t tsBlockColumnIndex) { - checkRecord(); - if (tsBlockColumnIndex < 0) { - throw IoTDBException("Cannot read binary from time column"); - } - if (!isNull(tsBlockColumnIndex, tsBlockIndex_)) { - lastReadWasNull_ = false; - return curTsBlock_->getColumn(tsBlockColumnIndex)->getBinary(tsBlockIndex_); - } - else { - lastReadWasNull_ = true; - return nullptr; - } + checkRecord(); + if (tsBlockColumnIndex < 0) { + throw IoTDBException("Cannot read binary from time column"); + } + if (!isNull(tsBlockColumnIndex, tsBlockIndex_)) { + lastReadWasNull_ = false; + return curTsBlock_->getColumn(tsBlockColumnIndex)->getBinary(tsBlockIndex_); + } else { + lastReadWasNull_ = true; + return nullptr; + } } boost::optional IoTDBRpcDataSet::getStringByIndex(int32_t columnIndex) { - int32_t index = getTsBlockColumnIndexForColumnIndex(columnIndex); - return getStringByTsBlockColumnIndex(index); + int32_t index = getTsBlockColumnIndexForColumnIndex(columnIndex); + return getStringByTsBlockColumnIndex(index); } boost::optional IoTDBRpcDataSet::getString(const std::string& columnName) { - int32_t index = getTsBlockColumnIndexForColumnName(columnName); - return getStringByTsBlockColumnIndex(index); -} - -boost::optional IoTDBRpcDataSet::getStringByTsBlockColumnIndex(int32_t tsBlockColumnIndex) { - checkRecord(); - if (tsBlockColumnIndex < 0) { - int64_t timestamp = curTsBlock_->getTimeByIndex(tsBlockIndex_); - return std::to_string(timestamp); - } - if (isNull(tsBlockColumnIndex, tsBlockIndex_)) { - lastReadWasNull_ = true; - return boost::none; - } - lastReadWasNull_ = false; - return getStringByTsBlockColumnIndexAndDataType(tsBlockColumnIndex, - getDataTypeByTsBlockColumnIndex(tsBlockColumnIndex)); -} - -std::string IoTDBRpcDataSet::getStringByTsBlockColumnIndexAndDataType(int32_t index, - TSDataType::TSDataType tsDataType) { - switch (tsDataType) { - case TSDataType::BOOLEAN: - return std::to_string(curTsBlock_->getColumn(index)->getBoolean(tsBlockIndex_)); - case TSDataType::INT32: - return std::to_string(curTsBlock_->getColumn(index)->getInt(tsBlockIndex_)); - case TSDataType::INT64: - return std::to_string(curTsBlock_->getColumn(index)->getLong(tsBlockIndex_)); - case TSDataType::TIMESTAMP: { - int64_t value = curTsBlock_->getColumn(index)->getLong(tsBlockIndex_); - return formatDatetime(timeFormat_, timePrecision_, value, timeZoneId_); - } - case TSDataType::FLOAT: - return std::to_string(curTsBlock_->getColumn(index)->getFloat(tsBlockIndex_)); - case TSDataType::DOUBLE: - return std::to_string(curTsBlock_->getColumn(index)->getDouble(tsBlockIndex_)); - case TSDataType::TEXT: - case TSDataType::STRING: - case TSDataType::OBJECT: - case TSDataType::BLOB: { - auto binary = curTsBlock_->getColumn(index)->getBinary(tsBlockIndex_); - return binary->getStringValue(); - } - case TSDataType::DATE: { - int32_t value = curTsBlock_->getColumn(index)->getInt(tsBlockIndex_); - auto date = parseIntToDate(value); - return boost::gregorian::to_iso_extended_string(date); - } - default: - return ""; - } + int32_t index = getTsBlockColumnIndexForColumnName(columnName); + return getStringByTsBlockColumnIndex(index); +} + +boost::optional +IoTDBRpcDataSet::getStringByTsBlockColumnIndex(int32_t tsBlockColumnIndex) { + checkRecord(); + if (tsBlockColumnIndex < 0) { + int64_t timestamp = curTsBlock_->getTimeByIndex(tsBlockIndex_); + return std::to_string(timestamp); + } + if (isNull(tsBlockColumnIndex, tsBlockIndex_)) { + lastReadWasNull_ = true; + return boost::none; + } + lastReadWasNull_ = false; + return getStringByTsBlockColumnIndexAndDataType( + tsBlockColumnIndex, getDataTypeByTsBlockColumnIndex(tsBlockColumnIndex)); +} + +std::string +IoTDBRpcDataSet::getStringByTsBlockColumnIndexAndDataType(int32_t index, + TSDataType::TSDataType tsDataType) { + switch (tsDataType) { + case TSDataType::BOOLEAN: + return std::to_string(curTsBlock_->getColumn(index)->getBoolean(tsBlockIndex_)); + case TSDataType::INT32: + return std::to_string(curTsBlock_->getColumn(index)->getInt(tsBlockIndex_)); + case TSDataType::INT64: + return std::to_string(curTsBlock_->getColumn(index)->getLong(tsBlockIndex_)); + case TSDataType::TIMESTAMP: { + int64_t value = curTsBlock_->getColumn(index)->getLong(tsBlockIndex_); + return formatDatetime(timeFormat_, timePrecision_, value, timeZoneId_); + } + case TSDataType::FLOAT: + return std::to_string(curTsBlock_->getColumn(index)->getFloat(tsBlockIndex_)); + case TSDataType::DOUBLE: + return std::to_string(curTsBlock_->getColumn(index)->getDouble(tsBlockIndex_)); + case TSDataType::TEXT: + case TSDataType::STRING: + case TSDataType::OBJECT: + case TSDataType::BLOB: { + auto binary = curTsBlock_->getColumn(index)->getBinary(tsBlockIndex_); + return binary->getStringValue(); + } + case TSDataType::DATE: { + int32_t value = curTsBlock_->getColumn(index)->getInt(tsBlockIndex_); + auto date = parseIntToDate(value); + return boost::gregorian::to_iso_extended_string(date); + } + default: + return ""; + } } boost::optional IoTDBRpcDataSet::getTimestampByIndex(int32_t columnIndex) { - int32_t index = getTsBlockColumnIndexForColumnIndex(columnIndex); - return getTimestampByTsBlockColumnIndex(index); + int32_t index = getTsBlockColumnIndexForColumnIndex(columnIndex); + return getTimestampByTsBlockColumnIndex(index); } boost::optional IoTDBRpcDataSet::getTimestamp(const std::string& columnName) { - int32_t index = getTsBlockColumnIndexForColumnName(columnName); - return getTimestampByTsBlockColumnIndex(index); + int32_t index = getTsBlockColumnIndexForColumnName(columnName); + return getTimestampByTsBlockColumnIndex(index); } -boost::optional IoTDBRpcDataSet::getTimestampByTsBlockColumnIndex(int32_t tsBlockColumnIndex) { - return getLongByTsBlockColumnIndex(tsBlockColumnIndex); +boost::optional +IoTDBRpcDataSet::getTimestampByTsBlockColumnIndex(int32_t tsBlockColumnIndex) { + return getLongByTsBlockColumnIndex(tsBlockColumnIndex); } boost::optional IoTDBRpcDataSet::getDateByIndex(int32_t columnIndex) { - int32_t index = getTsBlockColumnIndexForColumnIndex(columnIndex); - return getDateByTsBlockColumnIndex(index); + int32_t index = getTsBlockColumnIndexForColumnIndex(columnIndex); + return getDateByTsBlockColumnIndex(index); } boost::optional IoTDBRpcDataSet::getDate(const std::string& columnName) { - int32_t index = getTsBlockColumnIndexForColumnName(columnName); - return getDateByTsBlockColumnIndex(index); + int32_t index = getTsBlockColumnIndexForColumnName(columnName); + return getDateByTsBlockColumnIndex(index); } -boost::optional IoTDBRpcDataSet::getDateByTsBlockColumnIndex(int32_t tsBlockColumnIndex) { - auto value = getIntByTsBlockColumnIndex(tsBlockColumnIndex); - if (!value.is_initialized()) { - return boost::none; - } - return parseIntToDate(value.value()); +boost::optional +IoTDBRpcDataSet::getDateByTsBlockColumnIndex(int32_t tsBlockColumnIndex) { + auto value = getIntByTsBlockColumnIndex(tsBlockColumnIndex); + if (!value.is_initialized()) { + return boost::none; + } + return parseIntToDate(value.value()); } TSDataType::TSDataType IoTDBRpcDataSet::getDataTypeByIndex(int32_t columnIndex) { - int32_t index = getTsBlockColumnIndexForColumnIndex(columnIndex); - return getDataTypeByTsBlockColumnIndex(index); + int32_t index = getTsBlockColumnIndexForColumnIndex(columnIndex); + return getDataTypeByTsBlockColumnIndex(index); } TSDataType::TSDataType IoTDBRpcDataSet::getDataType(const std::string& columnName) { - int32_t index = getTsBlockColumnIndexForColumnName(columnName); - return getDataTypeByTsBlockColumnIndex(index); + int32_t index = getTsBlockColumnIndexForColumnName(columnName); + return getDataTypeByTsBlockColumnIndex(index); } int32_t IoTDBRpcDataSet::getTsBlockColumnIndexForColumnIndex(int32_t columnIndex) { - const int32_t adjusted_index = columnIndex - 1; - if (adjusted_index >= static_cast(columnIndex2TsBlockColumnIndexList_.size()) || - adjusted_index < 0) { - throw std::out_of_range("Index " + std::to_string(adjusted_index) + - " out of range [0, " + - std::to_string(columnIndex2TsBlockColumnIndexList_.size()) + ")"); - } - return columnIndex2TsBlockColumnIndexList_[adjusted_index]; + const int32_t adjusted_index = columnIndex - 1; + if (adjusted_index >= static_cast(columnIndex2TsBlockColumnIndexList_.size()) || + adjusted_index < 0) { + throw std::out_of_range("Index " + std::to_string(adjusted_index) + " out of range [0, " + + std::to_string(columnIndex2TsBlockColumnIndexList_.size()) + ")"); + } + return columnIndex2TsBlockColumnIndexList_[adjusted_index]; } -TSDataType::TSDataType IoTDBRpcDataSet::getDataTypeByTsBlockColumnIndex(int32_t tsBlockColumnIndex) { - if (tsBlockColumnIndex < 0) { - return TSDataType::TIMESTAMP; - } - else { - return dataTypeForTsBlockColumn_[tsBlockColumnIndex]; - } +TSDataType::TSDataType +IoTDBRpcDataSet::getDataTypeByTsBlockColumnIndex(int32_t tsBlockColumnIndex) { + if (tsBlockColumnIndex < 0) { + return TSDataType::TIMESTAMP; + } else { + return dataTypeForTsBlockColumn_[tsBlockColumnIndex]; + } } int32_t IoTDBRpcDataSet::findColumn(const std::string& columnName) { - auto it = columnOrdinalMap_.find(columnName); - if (it != columnOrdinalMap_.end()) { - return it->second; - } - return -1; + auto it = columnOrdinalMap_.find(columnName); + if (it != columnOrdinalMap_.end()) { + return it->second; + } + return -1; } std::string IoTDBRpcDataSet::findColumnNameByIndex(int32_t columnIndex) { - if (columnIndex <= 0) { - throw IoTDBException("column index should start from 1"); - } - if (columnIndex > static_cast(columnNameList_.size())) { - throw IoTDBException( - "Column index " + std::to_string(columnIndex) + - " is out of range. Valid range is 0 to " + - std::to_string(columnNameList_.size() - 1) - ); - } - return columnNameList_[columnIndex - 1]; + if (columnIndex <= 0) { + throw IoTDBException("column index should start from 1"); + } + if (columnIndex > static_cast(columnNameList_.size())) { + throw IoTDBException("Column index " + std::to_string(columnIndex) + + " is out of range. Valid range is 0 to " + + std::to_string(columnNameList_.size() - 1)); + } + return columnNameList_[columnIndex - 1]; } int32_t IoTDBRpcDataSet::getTsBlockColumnIndexForColumnName(const std::string& columnName) { - auto it = columnName2TsBlockColumnIndexMap_.find(columnName); - if (it == columnName2TsBlockColumnIndexMap_.end()) { - throw IoTDBException("unknown column name: " + columnName); - } - return it->second; + auto it = columnName2TsBlockColumnIndexMap_.find(columnName); + if (it == columnName2TsBlockColumnIndexMap_.end()) { + throw IoTDBException("unknown column name: " + columnName); + } + return it->second; } void IoTDBRpcDataSet::checkRecord() { - if (queryResultIndex_ > queryResultSize_ || - tsBlockIndex_ >= tsBlockSize_ || - queryResult_.empty() || - !curTsBlock_) { - throw IoTDBException("no record remains"); - } + if (queryResultIndex_ > queryResultSize_ || tsBlockIndex_ >= tsBlockSize_ || + queryResult_.empty() || !curTsBlock_) { + throw IoTDBException("no record remains"); + } } int32_t IoTDBRpcDataSet::getValueColumnStartIndex() const { - return ignoreTimestamp_ ? 0 : 1; + return ignoreTimestamp_ ? 0 : 1; } int32_t IoTDBRpcDataSet::getColumnSize() const { - return static_cast(columnNameList_.size()); + return static_cast(columnNameList_.size()); } const std::vector& IoTDBRpcDataSet::getColumnTypeList() const { - return columnTypeList_; + return columnTypeList_; } const std::vector& IoTDBRpcDataSet::getColumnNameList() const { - return columnNameList_; + return columnNameList_; } bool IoTDBRpcDataSet::isClosed() const { - return isClosed_; + return isClosed_; } int32_t IoTDBRpcDataSet::getFetchSize() const { - return fetchSize_; + return fetchSize_; } void IoTDBRpcDataSet::setFetchSize(int32_t fetchSize) { - fetchSize_ = fetchSize; + fetchSize_ = fetchSize; } bool IoTDBRpcDataSet::hasCachedRecord() const { - return hasCachedRecord_; + return hasCachedRecord_; } void IoTDBRpcDataSet::setHasCachedRecord(bool hasCachedRecord) { - hasCachedRecord_ = hasCachedRecord; + hasCachedRecord_ = hasCachedRecord; } bool IoTDBRpcDataSet::isLastReadWasNull() const { - return lastReadWasNull_; + return lastReadWasNull_; } int64_t IoTDBRpcDataSet::getCurrentRowTime() const { - return time_; + return time_; } bool IoTDBRpcDataSet::isIgnoreTimestamp() const { - return ignoreTimestamp_; + return ignoreTimestamp_; } bool IoTDBRpcDataSet::hasCachedBlock() const { - return curTsBlock_ && tsBlockIndex_ < tsBlockSize_ - 1; + return curTsBlock_ && tsBlockIndex_ < tsBlockSize_ - 1; } bool IoTDBRpcDataSet::hasCachedByteBuffer() const { - return !queryResult_.empty() && queryResultIndex_ < queryResultSize_; + return !queryResult_.empty() && queryResultIndex_ < queryResultSize_; } diff --git a/iotdb-client/client-cpp/src/main/IoTDBRpcDataSet.h b/iotdb-client/client-cpp/src/main/IoTDBRpcDataSet.h index 7ec8af5049f32..0e6855f4049ab 100644 --- a/iotdb-client/client-cpp/src/main/IoTDBRpcDataSet.h +++ b/iotdb-client/client-cpp/src/main/IoTDBRpcDataSet.h @@ -31,129 +31,121 @@ class IoTDBRpcDataSet { public: - static const int32_t startIndex; - static const std::string TimestampColumnName; + static const int32_t startIndex; + static const std::string TimestampColumnName; - static const std::string DEFAULT_TIME_FORMAT; - static const std::string TIME_PRECISION; - static const std::string MILLISECOND; - static const std::string MICROSECOND; - static const std::string NANOSECOND; + static const std::string DEFAULT_TIME_FORMAT; + static const std::string TIME_PRECISION; + static const std::string MILLISECOND; + static const std::string MICROSECOND; + static const std::string NANOSECOND; - IoTDBRpcDataSet(const std::string& sql, - const std::vector& columnNameList, - const std::vector& columnTypeList, - const std::map& columnNameIndex, - bool ignoreTimestamp, - bool moreData, - int64_t queryId, - int64_t statementId, - std::shared_ptr client, - int64_t sessionId, - const std::vector& queryResult, - int32_t fetchSize, - int64_t timeout, - const std::string& zoneId, - const std::string& timeFormat, - int32_t timeFactor, - std::vector& columnIndex2TsBlockColumnIndexList); + IoTDBRpcDataSet(const std::string& sql, const std::vector& columnNameList, + const std::vector& columnTypeList, + const std::map& columnNameIndex, bool ignoreTimestamp, + bool moreData, int64_t queryId, int64_t statementId, + std::shared_ptr client, int64_t sessionId, + const std::vector& queryResult, int32_t fetchSize, int64_t timeout, + const std::string& zoneId, const std::string& timeFormat, int32_t timeFactor, + std::vector& columnIndex2TsBlockColumnIndexList); - ~IoTDBRpcDataSet(); + ~IoTDBRpcDataSet(); - bool next(); - void close(bool forceClose = false); + bool next(); + void close(bool forceClose = false); - bool hasCachedBlock() const; - bool hasCachedByteBuffer() const; + bool hasCachedBlock() const; + bool hasCachedByteBuffer() const; - bool isNull(int32_t index, int32_t rowNum); - bool isNullByIndex(int32_t columnIndex); - bool isNullByColumnName(const std::string& columnName); - boost::optional getBooleanByIndex(int32_t columnIndex); - boost::optional getBoolean(const std::string& columnName); - boost::optional getDoubleByIndex(int32_t columnIndex); - boost::optional getDouble(const std::string& columnName); - boost::optional getFloatByIndex(int32_t columnIndex); - boost::optional getFloat(const std::string& columnName); - boost::optional getIntByIndex(int32_t columnIndex); - boost::optional getInt(const std::string& columnName); - boost::optional getLongByIndex(int32_t columnIndex); - boost::optional getLong(const std::string& columnName); - std::shared_ptr getBinaryByIndex(int32_t columnIndex); - std::shared_ptr getBinary(const std::string& columnName); - boost::optional getStringByIndex(int32_t columnIndex); - boost::optional getString(const std::string& columnName); - boost::optional getTimestampByIndex(int32_t columnIndex); - boost::optional getTimestamp(const std::string& columnName); - boost::optional getDateByIndex(int32_t columnIndex); - boost::optional getDate(const std::string& columnName); + bool isNull(int32_t index, int32_t rowNum); + bool isNullByIndex(int32_t columnIndex); + bool isNullByColumnName(const std::string& columnName); + boost::optional getBooleanByIndex(int32_t columnIndex); + boost::optional getBoolean(const std::string& columnName); + boost::optional getDoubleByIndex(int32_t columnIndex); + boost::optional getDouble(const std::string& columnName); + boost::optional getFloatByIndex(int32_t columnIndex); + boost::optional getFloat(const std::string& columnName); + boost::optional getIntByIndex(int32_t columnIndex); + boost::optional getInt(const std::string& columnName); + boost::optional getLongByIndex(int32_t columnIndex); + boost::optional getLong(const std::string& columnName); + std::shared_ptr getBinaryByIndex(int32_t columnIndex); + std::shared_ptr getBinary(const std::string& columnName); + boost::optional getStringByIndex(int32_t columnIndex); + boost::optional getString(const std::string& columnName); + boost::optional getTimestampByIndex(int32_t columnIndex); + boost::optional getTimestamp(const std::string& columnName); + boost::optional getDateByIndex(int32_t columnIndex); + boost::optional getDate(const std::string& columnName); - TSDataType::TSDataType getDataTypeByIndex(int32_t columnIndex); - TSDataType::TSDataType getDataType(const std::string& columnName); - int32_t findColumn(const std::string& columnName); - std::string findColumnNameByIndex(int32_t columnIndex); - int32_t getValueColumnStartIndex() const; - int32_t getColumnSize() const; - const std::vector& getColumnTypeList() const; - const std::vector& getColumnNameList() const; - bool isClosed() const; - int32_t getFetchSize() const; - void setFetchSize(int32_t fetchSize); - bool hasCachedRecord() const; - void setHasCachedRecord(bool hasCachedRecord); - bool isLastReadWasNull() const; - int64_t getCurrentRowTime() const; - bool isIgnoreTimestamp() const; + TSDataType::TSDataType getDataTypeByIndex(int32_t columnIndex); + TSDataType::TSDataType getDataType(const std::string& columnName); + int32_t findColumn(const std::string& columnName); + std::string findColumnNameByIndex(int32_t columnIndex); + int32_t getValueColumnStartIndex() const; + int32_t getColumnSize() const; + const std::vector& getColumnTypeList() const; + const std::vector& getColumnNameList() const; + bool isClosed() const; + int32_t getFetchSize() const; + void setFetchSize(int32_t fetchSize); + bool hasCachedRecord() const; + void setHasCachedRecord(bool hasCachedRecord); + bool isLastReadWasNull() const; + int64_t getCurrentRowTime() const; + bool isIgnoreTimestamp() const; private: - bool fetchResults(); - void constructOneRow(); - void constructOneTsBlock(); - int32_t getTsBlockColumnIndexForColumnName(const std::string& columnName); - int32_t getTsBlockColumnIndexForColumnIndex(int32_t columnIndex); - void checkRecord(); - TSDataType::TSDataType getDataTypeByTsBlockColumnIndex(int32_t tsBlockColumnIndex); - boost::optional getBooleanByTsBlockColumnIndex(int32_t tsBlockColumnIndex); - std::string getStringByTsBlockColumnIndexAndDataType(int32_t index, TSDataType::TSDataType tsDataType); - boost::optional getDoubleByTsBlockColumnIndex(int32_t tsBlockColumnIndex); - boost::optional getFloatByTsBlockColumnIndex(int32_t tsBlockColumnIndex); - boost::optional getIntByTsBlockColumnIndex(int32_t tsBlockColumnIndex); - boost::optional getLongByTsBlockColumnIndex(int32_t tsBlockColumnIndex); - std::shared_ptr getBinaryByTsBlockColumnIndex(int32_t tsBlockColumnIndex); - boost::optional getStringByTsBlockColumnIndex(int32_t tsBlockColumnIndex); - boost::optional getDateByTsBlockColumnIndex(int32_t tsBlockColumnIndex); - boost::optional getTimestampByTsBlockColumnIndex(int32_t tsBlockColumnIndex); + bool fetchResults(); + void constructOneRow(); + void constructOneTsBlock(); + int32_t getTsBlockColumnIndexForColumnName(const std::string& columnName); + int32_t getTsBlockColumnIndexForColumnIndex(int32_t columnIndex); + void checkRecord(); + TSDataType::TSDataType getDataTypeByTsBlockColumnIndex(int32_t tsBlockColumnIndex); + boost::optional getBooleanByTsBlockColumnIndex(int32_t tsBlockColumnIndex); + std::string getStringByTsBlockColumnIndexAndDataType(int32_t index, + TSDataType::TSDataType tsDataType); + boost::optional getDoubleByTsBlockColumnIndex(int32_t tsBlockColumnIndex); + boost::optional getFloatByTsBlockColumnIndex(int32_t tsBlockColumnIndex); + boost::optional getIntByTsBlockColumnIndex(int32_t tsBlockColumnIndex); + boost::optional getLongByTsBlockColumnIndex(int32_t tsBlockColumnIndex); + std::shared_ptr getBinaryByTsBlockColumnIndex(int32_t tsBlockColumnIndex); + boost::optional getStringByTsBlockColumnIndex(int32_t tsBlockColumnIndex); + boost::optional getDateByTsBlockColumnIndex(int32_t tsBlockColumnIndex); + boost::optional getTimestampByTsBlockColumnIndex(int32_t tsBlockColumnIndex); - std::string sql_; - bool isClosed_; - std::shared_ptr client_; - std::vector columnNameList_; - std::vector columnTypeList_; - std::map columnOrdinalMap_; - std::map columnName2TsBlockColumnIndexMap_; - std::vector columnIndex2TsBlockColumnIndexList_; - std::vector dataTypeForTsBlockColumn_; - int32_t fetchSize_; - int64_t timeout_; - bool hasCachedRecord_; - bool lastReadWasNull_; - int32_t columnSize_; - int64_t sessionId_; - int64_t queryId_; - int64_t statementId_; - int64_t time_; - bool ignoreTimestamp_; - bool moreData_; - std::vector queryResult_; - std::shared_ptr curTsBlock_; - int32_t queryResultSize_; - int32_t queryResultIndex_; - int32_t tsBlockSize_; - int32_t tsBlockIndex_; - std::string timeZoneId_; - std::string timeFormat_; - int32_t timeFactor_; - std::string timePrecision_; + std::string sql_; + bool isClosed_; + std::shared_ptr client_; + std::vector columnNameList_; + std::vector columnTypeList_; + std::map columnOrdinalMap_; + std::map columnName2TsBlockColumnIndexMap_; + std::vector columnIndex2TsBlockColumnIndexList_; + std::vector dataTypeForTsBlockColumn_; + int32_t fetchSize_; + int64_t timeout_; + bool hasCachedRecord_; + bool lastReadWasNull_; + int32_t columnSize_; + int64_t sessionId_; + int64_t queryId_; + int64_t statementId_; + int64_t time_; + bool ignoreTimestamp_; + bool moreData_; + std::vector queryResult_; + std::shared_ptr curTsBlock_; + int32_t queryResultSize_; + int32_t queryResultIndex_; + int32_t tsBlockSize_; + int32_t tsBlockIndex_; + std::string timeZoneId_; + std::string timeFormat_; + int32_t timeFactor_; + std::string timePrecision_; }; #endif // IOTDB_RPC_DATA_SET_H diff --git a/iotdb-client/client-cpp/src/main/NodesSupplier.cpp b/iotdb-client/client-cpp/src/main/NodesSupplier.cpp index ae414e19577ee..3f3a807dae8db 100644 --- a/iotdb-client/client-cpp/src/main/NodesSupplier.cpp +++ b/iotdb-client/client-cpp/src/main/NodesSupplier.cpp @@ -37,144 +37,118 @@ const int NodesSupplier::THRIFT_MAX_FRAME_SIZE = 1048576; const int NodesSupplier::CONNECTION_TIMEOUT_IN_MS = 1000; TEndPoint RoundRobinPolicy::select(const std::vector& nodes) { - static std::atomic_uint index{0}; + static std::atomic_uint index{0}; - if (nodes.empty()) { - throw IoTDBException("No available nodes"); - } + if (nodes.empty()) { + throw IoTDBException("No available nodes"); + } - return nodes[index++ % nodes.size()]; + return nodes[index++ % nodes.size()]; } StaticNodesSupplier::StaticNodesSupplier(const std::vector& nodes, - NodeSelectionPolicy policy) + NodeSelectionPolicy policy) : availableNodes_(nodes), policy_(std::move(policy)) {} boost::optional StaticNodesSupplier::getQueryEndPoint() { - try { - if (availableNodes_.empty()) { - return boost::none; - } - return policy_(availableNodes_); - } catch (const IoTDBException& e) { - return boost::none; + try { + if (availableNodes_.empty()) { + return boost::none; } + return policy_(availableNodes_); + } catch (const IoTDBException& e) { + return boost::none; + } } std::vector StaticNodesSupplier::getEndPointList() { - return availableNodes_; + return availableNodes_; } StaticNodesSupplier::~StaticNodesSupplier() = default; std::shared_ptr NodesSupplier::create( - const std::vector& endpoints, - const std::string& userName, - const std::string& password, - bool useSSL, - const std::string& trustCertFilePath, - const std::string& zoneId, - int32_t thriftDefaultBufferSize, - int32_t thriftMaxFrameSize, - int32_t connectionTimeoutInMs, - bool enableRPCCompression, - const std::string& version, - std::chrono::milliseconds refreshInterval, - NodeSelectionPolicy policy) { - if (endpoints.empty()) { - return nullptr; - } - auto supplier = std::make_shared( - userName, password, useSSL, trustCertFilePath, zoneId, - thriftDefaultBufferSize, thriftMaxFrameSize, connectionTimeoutInMs, - enableRPCCompression, - version, endpoints, policy - ); - supplier->startBackgroundRefresh(refreshInterval); - return supplier; + const std::vector& endpoints, const std::string& userName, + const std::string& password, bool useSSL, const std::string& trustCertFilePath, + const std::string& zoneId, int32_t thriftDefaultBufferSize, int32_t thriftMaxFrameSize, + int32_t connectionTimeoutInMs, bool enableRPCCompression, const std::string& version, + std::chrono::milliseconds refreshInterval, NodeSelectionPolicy policy) { + if (endpoints.empty()) { + return nullptr; + } + auto supplier = std::make_shared( + userName, password, useSSL, trustCertFilePath, zoneId, thriftDefaultBufferSize, + thriftMaxFrameSize, connectionTimeoutInMs, enableRPCCompression, version, endpoints, policy); + supplier->startBackgroundRefresh(refreshInterval); + return supplier; } -NodesSupplier::NodesSupplier( - const std::string& userName, - const std::string& password, - bool useSSL, - const std::string& trustCertFilePath, - const std::string& zoneId, - int32_t thriftDefaultBufferSize, - int32_t thriftMaxFrameSize, - int32_t connectionTimeoutInMs, - bool enableRPCCompression, - const std::string& version, - const std::vector& endpoints, - NodeSelectionPolicy policy) - : userName_(userName) - , password_(password) - , zoneId_(zoneId) - , thriftDefaultBufferSize_(thriftDefaultBufferSize) - , thriftMaxFrameSize_(thriftMaxFrameSize) - , connectionTimeoutInMs_(connectionTimeoutInMs) - , useSSL_(useSSL) - , trustCertFilePath_(trustCertFilePath) - , enableRPCCompression_(enableRPCCompression) - , version_(version) - , endpoints_(endpoints) - , selectionPolicy_(policy) { - deduplicateEndpoints(); +NodesSupplier::NodesSupplier(const std::string& userName, const std::string& password, bool useSSL, + const std::string& trustCertFilePath, const std::string& zoneId, + int32_t thriftDefaultBufferSize, int32_t thriftMaxFrameSize, + int32_t connectionTimeoutInMs, bool enableRPCCompression, + const std::string& version, const std::vector& endpoints, + NodeSelectionPolicy policy) + : userName_(userName), password_(password), zoneId_(zoneId), + thriftDefaultBufferSize_(thriftDefaultBufferSize), thriftMaxFrameSize_(thriftMaxFrameSize), + connectionTimeoutInMs_(connectionTimeoutInMs), useSSL_(useSSL), + trustCertFilePath_(trustCertFilePath), enableRPCCompression_(enableRPCCompression), + version_(version), endpoints_(endpoints), selectionPolicy_(policy) { + deduplicateEndpoints(); } std::vector NodesSupplier::getEndPointList() { - std::lock_guard lock(mutex_); - return endpoints_; + std::lock_guard lock(mutex_); + return endpoints_; } TEndPoint NodesSupplier::selectQueryEndpoint() { - std::lock_guard lock(mutex_); - try { - return selectionPolicy_(endpoints_); - } catch (const std::exception& e) { - log_error("NodesSupplier::selectQueryEndpoint exception: %s", e.what()); - throw IoTDBException("NodesSupplier::selectQueryEndpoint exception, " + std::string(e.what())); - } + std::lock_guard lock(mutex_); + try { + return selectionPolicy_(endpoints_); + } catch (const std::exception& e) { + log_error("NodesSupplier::selectQueryEndpoint exception: %s", e.what()); + throw IoTDBException("NodesSupplier::selectQueryEndpoint exception, " + std::string(e.what())); + } } boost::optional NodesSupplier::getQueryEndPoint() { - try { - return selectQueryEndpoint(); - } catch (const IoTDBException& e) { - return boost::none; - } + try { + return selectQueryEndpoint(); + } catch (const IoTDBException& e) { + return boost::none; + } } NodesSupplier::~NodesSupplier() { - stopBackgroundRefresh(); - if (client_ != nullptr) { - client_->close(); - } + stopBackgroundRefresh(); + if (client_ != nullptr) { + client_->close(); + } } void NodesSupplier::deduplicateEndpoints() { - std::vector uniqueEndpoints; - uniqueEndpoints.reserve(endpoints_.size()); - for (const auto& endpoint : endpoints_) { - if (std::find(uniqueEndpoints.begin(), uniqueEndpoints.end(), endpoint) == uniqueEndpoints.end()) { - uniqueEndpoints.push_back(endpoint); - } + std::vector uniqueEndpoints; + uniqueEndpoints.reserve(endpoints_.size()); + for (const auto& endpoint : endpoints_) { + if (std::find(uniqueEndpoints.begin(), uniqueEndpoints.end(), endpoint) == + uniqueEndpoints.end()) { + uniqueEndpoints.push_back(endpoint); } - endpoints_ = std::move(uniqueEndpoints); + } + endpoints_ = std::move(uniqueEndpoints); } void NodesSupplier::startBackgroundRefresh(std::chrono::milliseconds interval) { - isRunning_ = true; - refreshEndpointList(); - refreshThread_ = std::thread([this, interval] { - while (isRunning_) { - refreshEndpointList(); - std::unique_lock cvLock(this->mutex_); - refreshCondition_.wait_for(cvLock, interval, [this]() { - return !isRunning_.load(); - }); - } - }); + isRunning_ = true; + refreshEndpointList(); + refreshThread_ = std::thread([this, interval] { + while (isRunning_) { + refreshEndpointList(); + std::unique_lock cvLock(this->mutex_); + refreshCondition_.wait_for(cvLock, interval, [this]() { return !isRunning_.load(); }); + } + }); } std::vector NodesSupplier::fetchLatestEndpoints() { @@ -182,7 +156,8 @@ std::vector NodesSupplier::fetchLatestEndpoints() { try { if (client_ == nullptr) { client_ = std::make_shared(endpoint); - client_->init(userName_, password_, enableRPCCompression_, useSSL_, trustCertFilePath_, zoneId_, version_); + client_->init(userName_, password_, enableRPCCompression_, useSSL_, trustCertFilePath_, + zoneId_, version_); } auto sessionDataSet = client_->executeQueryStatement(SHOW_AVAILABLE_URLS_COMMAND); @@ -223,37 +198,37 @@ std::vector NodesSupplier::fetchLatestEndpoints() { newEndpoint.port = port; ret.emplace_back(newEndpoint); } - return ret; // success + return ret; // success } catch (const std::exception& e) { log_warn("Failed to fetch endpoints from " + endpoint.ip + ":" + std::to_string(endpoint.port) + " , error=" + e.what()); - client_.reset(); // reset client before retrying next endpoint - continue; // try next endpoint + client_.reset(); // reset client before retrying next endpoint + continue; // try next endpoint } } throw IoTDBException("NodesSupplier::fetchLatestEndpoints failed: all nodes unreachable."); } void NodesSupplier::refreshEndpointList() { - try { - auto newEndpoints = fetchLatestEndpoints(); - if (newEndpoints.empty()) { - return; - } - - std::lock_guard lock(mutex_); - endpoints_.swap(newEndpoints); - deduplicateEndpoints(); - } catch (const IoTDBException& e) { - log_error(std::string("NodesSupplier::refreshEndpointList failed: ") + e.what()); + try { + auto newEndpoints = fetchLatestEndpoints(); + if (newEndpoints.empty()) { + return; } + + std::lock_guard lock(mutex_); + endpoints_.swap(newEndpoints); + deduplicateEndpoints(); + } catch (const IoTDBException& e) { + log_error(std::string("NodesSupplier::refreshEndpointList failed: ") + e.what()); + } } void NodesSupplier::stopBackgroundRefresh() noexcept { - if (isRunning_.exchange(false)) { - refreshCondition_.notify_all(); - if (refreshThread_.joinable()) { - refreshThread_.join(); - } + if (isRunning_.exchange(false)) { + refreshCondition_.notify_all(); + if (refreshThread_.joinable()) { + refreshThread_.join(); } + } } \ No newline at end of file diff --git a/iotdb-client/client-cpp/src/main/NodesSupplier.h b/iotdb-client/client-cpp/src/main/NodesSupplier.h index f4718f0f02bd3..7c56f8e75ba86 100644 --- a/iotdb-client/client-cpp/src/main/NodesSupplier.h +++ b/iotdb-client/client-cpp/src/main/NodesSupplier.h @@ -35,118 +35,105 @@ class TEndPoint; class RoundRobinPolicy { public: - static TEndPoint select(const std::vector& nodes); + static TEndPoint select(const std::vector& nodes); }; class INodesSupplier { public: - virtual ~INodesSupplier() = default; - virtual boost::optional getQueryEndPoint() = 0; - virtual std::vector getEndPointList() = 0; - using NodeSelectionPolicy = std::function&)>; + virtual ~INodesSupplier() = default; + virtual boost::optional getQueryEndPoint() = 0; + virtual std::vector getEndPointList() = 0; + using NodeSelectionPolicy = std::function&)>; }; class StaticNodesSupplier : public INodesSupplier { public: - explicit StaticNodesSupplier(const std::vector& nodes, - NodeSelectionPolicy policy = RoundRobinPolicy::select); + explicit StaticNodesSupplier(const std::vector& nodes, + NodeSelectionPolicy policy = RoundRobinPolicy::select); - boost::optional getQueryEndPoint() override; + boost::optional getQueryEndPoint() override; - std::vector getEndPointList() override; + std::vector getEndPointList() override; - ~StaticNodesSupplier() override; + ~StaticNodesSupplier() override; private: - const std::vector availableNodes_; - NodeSelectionPolicy policy_; + const std::vector availableNodes_; + NodeSelectionPolicy policy_; }; class NodesSupplier : public INodesSupplier { public: - static const std::string SHOW_AVAILABLE_URLS_COMMAND; - static const std::string RUNNING_STATUS; - static const std::string STATUS_COLUMN_NAME; - static const std::string IP_COLUMN_NAME; - static const std::string PORT_COLUMN_NAME; - static const std::string REMOVING_STATUS; - - static const int64_t TIMEOUT_IN_MS; - static const int FETCH_SIZE; - static const int THRIFT_DEFAULT_BUFFER_SIZE; - static const int THRIFT_MAX_FRAME_SIZE; - static const int CONNECTION_TIMEOUT_IN_MS; - - static std::shared_ptr create( - const std::vector& endpoints, - const std::string& userName, - const std::string& password, - bool useSSL = false, - const std::string& trustCertFilePath = "", - const std::string& zoneId = "", - int32_t thriftDefaultBufferSize = ThriftConnection::THRIFT_DEFAULT_BUFFER_SIZE, - int32_t thriftMaxFrameSize = ThriftConnection::THRIFT_MAX_FRAME_SIZE, - int32_t connectionTimeoutInMs = ThriftConnection::CONNECTION_TIMEOUT_IN_MS, - bool enableRPCCompression = false, - const std::string& version = "V_1_0", - std::chrono::milliseconds refreshInterval = std::chrono::milliseconds(TIMEOUT_IN_MS), - NodeSelectionPolicy policy = RoundRobinPolicy::select - ); - - NodesSupplier( - const std::string& userName, - const std::string& password, - bool useSSL, - const std::string& trustCertFilePath, - const std::string& zoneId, - int32_t thriftDefaultBufferSize, - int32_t thriftMaxFrameSize, - int32_t connectionTimeoutInMs, - bool enableRPCCompression, - const std::string& version, - const std::vector& endpoints, - NodeSelectionPolicy policy - ); - - std::vector getEndPointList() override; - - boost::optional getQueryEndPoint() override; - - ~NodesSupplier() override; + static const std::string SHOW_AVAILABLE_URLS_COMMAND; + static const std::string RUNNING_STATUS; + static const std::string STATUS_COLUMN_NAME; + static const std::string IP_COLUMN_NAME; + static const std::string PORT_COLUMN_NAME; + static const std::string REMOVING_STATUS; + + static const int64_t TIMEOUT_IN_MS; + static const int FETCH_SIZE; + static const int THRIFT_DEFAULT_BUFFER_SIZE; + static const int THRIFT_MAX_FRAME_SIZE; + static const int CONNECTION_TIMEOUT_IN_MS; + + static std::shared_ptr + create(const std::vector& endpoints, const std::string& userName, + const std::string& password, bool useSSL = false, + const std::string& trustCertFilePath = "", const std::string& zoneId = "", + int32_t thriftDefaultBufferSize = ThriftConnection::THRIFT_DEFAULT_BUFFER_SIZE, + int32_t thriftMaxFrameSize = ThriftConnection::THRIFT_MAX_FRAME_SIZE, + int32_t connectionTimeoutInMs = ThriftConnection::CONNECTION_TIMEOUT_IN_MS, + bool enableRPCCompression = false, const std::string& version = "V_1_0", + std::chrono::milliseconds refreshInterval = std::chrono::milliseconds(TIMEOUT_IN_MS), + NodeSelectionPolicy policy = RoundRobinPolicy::select); + + NodesSupplier(const std::string& userName, const std::string& password, bool useSSL, + const std::string& trustCertFilePath, const std::string& zoneId, + int32_t thriftDefaultBufferSize, int32_t thriftMaxFrameSize, + int32_t connectionTimeoutInMs, bool enableRPCCompression, + const std::string& version, const std::vector& endpoints, + NodeSelectionPolicy policy); + + std::vector getEndPointList() override; + + boost::optional getQueryEndPoint() override; + + ~NodesSupplier() override; private: - std::string userName_; - std::string password_; - int32_t thriftDefaultBufferSize_; - int32_t thriftMaxFrameSize_; - int32_t connectionTimeoutInMs_; - bool useSSL_; - std::string trustCertFilePath_; - bool enableRPCCompression_; - std::string version_; - std::string zoneId_; + std::string userName_; + std::string password_; + int32_t thriftDefaultBufferSize_; + int32_t thriftMaxFrameSize_; + int32_t connectionTimeoutInMs_; + bool useSSL_; + std::string trustCertFilePath_; + bool enableRPCCompression_; + std::string version_; + std::string zoneId_; - std::mutex mutex_; - std::vector endpoints_; - NodeSelectionPolicy selectionPolicy_; + std::mutex mutex_; + std::vector endpoints_; + NodeSelectionPolicy selectionPolicy_; - std::atomic isRunning_{false}; - std::thread refreshThread_; - std::condition_variable refreshCondition_; + std::atomic isRunning_{false}; + std::thread refreshThread_; + std::condition_variable refreshCondition_; - std::shared_ptr client_; + std::shared_ptr client_; - void deduplicateEndpoints(); + void deduplicateEndpoints(); - void startBackgroundRefresh(std::chrono::milliseconds interval); + void startBackgroundRefresh(std::chrono::milliseconds interval); - std::vector fetchLatestEndpoints(); + std::vector fetchLatestEndpoints(); - void refreshEndpointList(); + void refreshEndpointList(); - TEndPoint selectQueryEndpoint(); + TEndPoint selectQueryEndpoint(); - void stopBackgroundRefresh() noexcept; + void stopBackgroundRefresh() noexcept; }; #endif \ No newline at end of file diff --git a/iotdb-client/client-cpp/src/main/Session.cpp b/iotdb-client/client-cpp/src/main/Session.cpp index 41744286bb0a8..d1e70e747de58 100644 --- a/iotdb-client/client-cpp/src/main/Session.cpp +++ b/iotdb-client/client-cpp/src/main/Session.cpp @@ -38,468 +38,462 @@ 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, STRING, BLOB, TIMESTAMP, DATE, 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::TIMESTAMP; - } else if (str == "DATE") { - return TSDataType::DATE; - } else if (str == "BLOB") { - return TSDataType::BLOB; - } else if (str == "STRING") { - return TSDataType::STRING; - } else if (str == "OBJECT") { - return TSDataType::OBJECT; - } - return TSDataType::UNKNOWN; + // BOOLEAN, INT32, INT64, FLOAT, DOUBLE, TEXT, STRING, BLOB, TIMESTAMP, DATE, 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::TIMESTAMP; + } else if (str == "DATE") { + return TSDataType::DATE; + } else if (str == "BLOB") { + return TSDataType::BLOB; + } else if (str == "STRING") { + return TSDataType::STRING; + } else if (str == "OBJECT") { + return TSDataType::OBJECT; + } + return TSDataType::UNKNOWN; } void Tablet::createColumns() { - for (size_t i = 0; i < schemas.size(); i++) { - TSDataType::TSDataType dataType = schemas[i].second; - switch (dataType) { - case TSDataType::BOOLEAN: - values[i] = new bool[maxRowNumber]; - break; - case TSDataType::DATE: - values[i] = new boost::gregorian::date[maxRowNumber]; - break; - case TSDataType::INT32: - values[i] = new int[maxRowNumber]; - break; - case TSDataType::TIMESTAMP: - case TSDataType::INT64: - values[i] = new int64_t[maxRowNumber]; - break; - case TSDataType::FLOAT: - values[i] = new float[maxRowNumber]; - break; - case TSDataType::DOUBLE: - values[i] = new double[maxRowNumber]; - break; - case TSDataType::STRING: - case TSDataType::BLOB: - case TSDataType::OBJECT: - case TSDataType::TEXT: - values[i] = new string[maxRowNumber]; - break; - default: - throw UnSupportedDataTypeException(string("Data type ") + to_string(dataType) + " is not supported."); - } - } -} - -void Tablet::deleteColumns() { - for (size_t i = 0; i < schemas.size(); i++) { - if (!values[i]) continue; - TSDataType::TSDataType dataType = schemas[i].second; - switch (dataType) { - case TSDataType::BOOLEAN: { - bool* valueBuf = (bool*)(values[i]); - delete[] valueBuf; - break; - } - case TSDataType::INT32: { - int* valueBuf = (int*)(values[i]); - delete[] valueBuf; - break; - } - case TSDataType::DATE: { - boost::gregorian::date* valueBuf = (boost::gregorian::date*)(values[i]); - delete[] valueBuf; - break; - } - case TSDataType::TIMESTAMP: - case TSDataType::INT64: { - int64_t* valueBuf = (int64_t*)(values[i]); - delete[] valueBuf; - break; - } - case TSDataType::FLOAT: { - float* valueBuf = (float*)(values[i]); - delete[] valueBuf; - break; - } - case TSDataType::DOUBLE: { - double* valueBuf = (double*)(values[i]); - delete[] valueBuf; - break; - } - case TSDataType::STRING: - case TSDataType::BLOB: - case TSDataType::OBJECT: - case TSDataType::TEXT: { - string* valueBuf = (string*)(values[i]); - delete[] valueBuf; - break; - } - default: - throw UnSupportedDataTypeException(string("Data type ") + to_string(dataType) + " is not supported."); - } - values[i] = nullptr; - } -} - -void Tablet::deepCopyTabletColValue(void* const* srcPtr, void** destPtr, TSDataType::TSDataType type, int maxRowNumber) { - void *src = *srcPtr; - switch (type) { + for (size_t i = 0; i < schemas.size(); i++) { + TSDataType::TSDataType dataType = schemas[i].second; + switch (dataType) { case TSDataType::BOOLEAN: - *destPtr = new bool[maxRowNumber]; - memcpy(*destPtr, src, maxRowNumber * sizeof(bool)); - break; + values[i] = new bool[maxRowNumber]; + break; + case TSDataType::DATE: + values[i] = new boost::gregorian::date[maxRowNumber]; + break; case TSDataType::INT32: - *destPtr = new int32_t[maxRowNumber]; - memcpy(*destPtr, src, maxRowNumber * sizeof(int32_t)); - break; - case TSDataType::INT64: + values[i] = new int[maxRowNumber]; + break; case TSDataType::TIMESTAMP: - *destPtr = new int64_t[maxRowNumber]; - memcpy(*destPtr, src, maxRowNumber * sizeof(int64_t)); - break; + case TSDataType::INT64: + values[i] = new int64_t[maxRowNumber]; + break; case TSDataType::FLOAT: - *destPtr = new float[maxRowNumber]; - memcpy(*destPtr, src, maxRowNumber * sizeof(float)); - break; + values[i] = new float[maxRowNumber]; + break; case TSDataType::DOUBLE: - *destPtr = new double[maxRowNumber]; - memcpy(*destPtr, src, maxRowNumber * sizeof(double)); - break; + values[i] = new double[maxRowNumber]; + break; + case TSDataType::STRING: + case TSDataType::BLOB: + case TSDataType::OBJECT: + case TSDataType::TEXT: + values[i] = new string[maxRowNumber]; + break; + default: + throw UnSupportedDataTypeException(string("Data type ") + to_string(dataType) + + " is not supported."); + } + } +} + +void Tablet::deleteColumns() { + for (size_t i = 0; i < schemas.size(); i++) { + if (!values[i]) + continue; + TSDataType::TSDataType dataType = schemas[i].second; + switch (dataType) { + case TSDataType::BOOLEAN: { + bool* valueBuf = (bool*)(values[i]); + delete[] valueBuf; + break; + } + case TSDataType::INT32: { + int* valueBuf = (int*)(values[i]); + delete[] valueBuf; + break; + } case TSDataType::DATE: { - *destPtr = new boost::gregorian::date[maxRowNumber]; - boost::gregorian::date* srcDate = static_cast(src); - boost::gregorian::date* destDate = static_cast(*destPtr); - for (size_t j = 0; j < maxRowNumber; ++j) { - destDate[j] = srcDate[j]; - } - break; + boost::gregorian::date* valueBuf = (boost::gregorian::date*)(values[i]); + delete[] valueBuf; + break; + } + case TSDataType::TIMESTAMP: + case TSDataType::INT64: { + int64_t* valueBuf = (int64_t*)(values[i]); + delete[] valueBuf; + break; + } + case TSDataType::FLOAT: { + float* valueBuf = (float*)(values[i]); + delete[] valueBuf; + break; + } + case TSDataType::DOUBLE: { + double* valueBuf = (double*)(values[i]); + delete[] valueBuf; + break; } case TSDataType::STRING: - case TSDataType::TEXT: + case TSDataType::BLOB: case TSDataType::OBJECT: - case TSDataType::BLOB: { - *destPtr = new std::string[maxRowNumber]; - std::string* srcStr = static_cast(src); - std::string* destStr = static_cast(*destPtr); - for (size_t j = 0; j < maxRowNumber; ++j) { - destStr[j] = srcStr[j]; - } - break; + case TSDataType::TEXT: { + string* valueBuf = (string*)(values[i]); + delete[] valueBuf; + break; } default: - break; - } + throw UnSupportedDataTypeException(string("Data type ") + to_string(dataType) + + " is not supported."); + } + values[i] = nullptr; + } +} + +void Tablet::deepCopyTabletColValue(void* const* srcPtr, void** destPtr, + TSDataType::TSDataType type, int maxRowNumber) { + void* src = *srcPtr; + switch (type) { + case TSDataType::BOOLEAN: + *destPtr = new bool[maxRowNumber]; + memcpy(*destPtr, src, maxRowNumber * sizeof(bool)); + break; + case TSDataType::INT32: + *destPtr = new int32_t[maxRowNumber]; + memcpy(*destPtr, src, maxRowNumber * sizeof(int32_t)); + break; + case TSDataType::INT64: + case TSDataType::TIMESTAMP: + *destPtr = new int64_t[maxRowNumber]; + memcpy(*destPtr, src, maxRowNumber * sizeof(int64_t)); + break; + case TSDataType::FLOAT: + *destPtr = new float[maxRowNumber]; + memcpy(*destPtr, src, maxRowNumber * sizeof(float)); + break; + case TSDataType::DOUBLE: + *destPtr = new double[maxRowNumber]; + memcpy(*destPtr, src, maxRowNumber * sizeof(double)); + break; + case TSDataType::DATE: { + *destPtr = new boost::gregorian::date[maxRowNumber]; + boost::gregorian::date* srcDate = static_cast(src); + boost::gregorian::date* destDate = static_cast(*destPtr); + for (size_t j = 0; j < maxRowNumber; ++j) { + destDate[j] = srcDate[j]; + } + break; + } + case TSDataType::STRING: + case TSDataType::TEXT: + case TSDataType::OBJECT: + case TSDataType::BLOB: { + *destPtr = new std::string[maxRowNumber]; + std::string* srcStr = static_cast(src); + std::string* destStr = static_cast(*destPtr); + for (size_t j = 0; j < maxRowNumber; ++j) { + destStr[j] = srcStr[j]; + } + break; + } + default: + break; + } } void Tablet::reset() { - rowSize = 0; - for (size_t i = 0; i < schemas.size(); i++) { - bitMaps[i].reset(); - } + rowSize = 0; + for (size_t i = 0; i < schemas.size(); i++) { + bitMaps[i].reset(); + } } size_t Tablet::getTimeBytesSize() { - return rowSize * 8; + return rowSize * 8; } size_t Tablet::getValueByteSize() { - size_t valueOccupation = 0; - for (size_t i = 0; i < schemas.size(); i++) { - switch (schemas[i].second) { - case TSDataType::BOOLEAN: - valueOccupation += rowSize; - break; - case TSDataType::INT32: - valueOccupation += rowSize * 4; - break; - case TSDataType::DATE: - valueOccupation += rowSize * 4; - break; - case TSDataType::TIMESTAMP: - case TSDataType::INT64: - valueOccupation += rowSize * 8; - break; - case TSDataType::FLOAT: - valueOccupation += rowSize * 4; - break; - case TSDataType::DOUBLE: - valueOccupation += rowSize * 8; - break; - case TSDataType::STRING: - case TSDataType::BLOB: - case TSDataType::OBJECT: - case TSDataType::TEXT: { - valueOccupation += rowSize * 4; - string* valueBuf = (string*)(values[i]); - for (size_t j = 0; j < rowSize; j++) { - valueOccupation += valueBuf[j].size(); - } - break; - } - default: - throw UnSupportedDataTypeException( - string("Data type ") + to_string(schemas[i].second) + " is not supported."); - } + size_t valueOccupation = 0; + for (size_t i = 0; i < schemas.size(); i++) { + switch (schemas[i].second) { + case TSDataType::BOOLEAN: + valueOccupation += rowSize; + break; + case TSDataType::INT32: + valueOccupation += rowSize * 4; + break; + case TSDataType::DATE: + valueOccupation += rowSize * 4; + break; + case TSDataType::TIMESTAMP: + case TSDataType::INT64: + valueOccupation += rowSize * 8; + break; + case TSDataType::FLOAT: + valueOccupation += rowSize * 4; + break; + case TSDataType::DOUBLE: + valueOccupation += rowSize * 8; + break; + case TSDataType::STRING: + case TSDataType::BLOB: + case TSDataType::OBJECT: + case TSDataType::TEXT: { + valueOccupation += rowSize * 4; + string* valueBuf = (string*)(values[i]); + for (size_t j = 0; j < rowSize; j++) { + valueOccupation += valueBuf[j].size(); + } + break; } - return valueOccupation; + default: + throw UnSupportedDataTypeException(string("Data type ") + to_string(schemas[i].second) + + " is not supported."); + } + } + return valueOccupation; } void Tablet::setAligned(bool isAligned) { - this->isAligned = isAligned; + this->isAligned = isAligned; } std::shared_ptr Tablet::getDeviceID(int row) { - std::vector id_array(idColumnIndexes.size() + 1); - size_t idArrayIdx = 0; - id_array[idArrayIdx++] = this->deviceId; - for (auto idColumnIndex : idColumnIndexes) { - void* strPtr = getValue(idColumnIndex, row, TSDataType::TEXT); - id_array[idArrayIdx++] = *static_cast(strPtr); - } - return std::make_shared(id_array); + std::vector id_array(idColumnIndexes.size() + 1); + size_t idArrayIdx = 0; + id_array[idArrayIdx++] = this->deviceId; + for (auto idColumnIndex : idColumnIndexes) { + void* strPtr = getValue(idColumnIndex, row, TSDataType::TEXT); + id_array[idArrayIdx++] = *static_cast(strPtr); + } + return std::make_shared(id_array); } string SessionUtils::getTime(const Tablet& tablet) { - MyStringBuffer timeBuffer; - unsigned int n = 8u * tablet.rowSize; - if (n > timeBuffer.str.capacity()) { - timeBuffer.reserve(n); - } + MyStringBuffer timeBuffer; + unsigned int n = 8u * tablet.rowSize; + if (n > timeBuffer.str.capacity()) { + timeBuffer.reserve(n); + } - for (size_t i = 0; i < tablet.rowSize; i++) { - timeBuffer.putInt64(tablet.timestamps[i]); - } - return timeBuffer.str; + for (size_t i = 0; i < tablet.rowSize; i++) { + timeBuffer.putInt64(tablet.timestamps[i]); + } + return timeBuffer.str; } string SessionUtils::getValue(const Tablet& tablet) { - MyStringBuffer valueBuffer; - unsigned int n = 8u * tablet.schemas.size() * tablet.rowSize; - if (n > valueBuffer.str.capacity()) { - valueBuffer.reserve(n); - } - for (size_t i = 0; i < tablet.schemas.size(); i++) { - TSDataType::TSDataType dataType = tablet.schemas[i].second; - const BitMap& bitMap = tablet.bitMaps[i]; - switch (dataType) { - case TSDataType::BOOLEAN: { - bool* valueBuf = (bool*)(tablet.values[i]); - for (size_t index = 0; index < tablet.rowSize; index++) { - if (!bitMap.isMarked(index)) { - valueBuffer.putBool(valueBuf[index]); - } - else { - valueBuffer.putBool(false); - } - } - break; - } - case TSDataType::INT32: { - int* valueBuf = (int*)(tablet.values[i]); - for (size_t index = 0; index < tablet.rowSize; index++) { - if (!bitMap.isMarked(index)) { - valueBuffer.putInt(valueBuf[index]); - } - else { - valueBuffer.putInt((numeric_limits::min)()); - } - } - break; - } - case TSDataType::DATE: { - boost::gregorian::date* valueBuf = (boost::gregorian::date*)(tablet.values[i]); - for (size_t index = 0; index < tablet.rowSize; index++) { - if (!bitMap.isMarked(index)) { - valueBuffer.putDate(valueBuf[index]); - } - else { - valueBuffer.putInt(EMPTY_DATE_INT); - } - } - break; - } - case TSDataType::TIMESTAMP: - case TSDataType::INT64: { - int64_t* valueBuf = (int64_t*)(tablet.values[i]); - for (size_t index = 0; index < tablet.rowSize; index++) { - if (!bitMap.isMarked(index)) { - valueBuffer.putInt64(valueBuf[index]); - } - else { - valueBuffer.putInt64((numeric_limits::min)()); - } - } - break; - } - case TSDataType::FLOAT: { - float* valueBuf = (float*)(tablet.values[i]); - for (size_t index = 0; index < tablet.rowSize; index++) { - if (!bitMap.isMarked(index)) { - valueBuffer.putFloat(valueBuf[index]); - } - else { - valueBuffer.putFloat((numeric_limits::min)()); - } - } - break; - } - case TSDataType::DOUBLE: { - double* valueBuf = (double*)(tablet.values[i]); - for (size_t index = 0; index < tablet.rowSize; index++) { - if (!bitMap.isMarked(index)) { - valueBuffer.putDouble(valueBuf[index]); - } - else { - valueBuffer.putDouble((numeric_limits::min)()); - } - } - break; - } - case TSDataType::STRING: - case TSDataType::BLOB: - case TSDataType::OBJECT: - case TSDataType::TEXT: { - string* valueBuf = (string*)(tablet.values[i]); - for (size_t index = 0; index < tablet.rowSize; index++) { - if (!bitMap.isMarked(index)) { - valueBuffer.putString(valueBuf[index]); - } - else { - valueBuffer.putString(""); - } - } - break; - } - default: - throw UnSupportedDataTypeException(string("Data type ") + to_string(dataType) + " is not supported."); + MyStringBuffer valueBuffer; + unsigned int n = 8u * tablet.schemas.size() * tablet.rowSize; + if (n > valueBuffer.str.capacity()) { + valueBuffer.reserve(n); + } + for (size_t i = 0; i < tablet.schemas.size(); i++) { + TSDataType::TSDataType dataType = tablet.schemas[i].second; + const BitMap& bitMap = tablet.bitMaps[i]; + switch (dataType) { + case TSDataType::BOOLEAN: { + bool* valueBuf = (bool*)(tablet.values[i]); + for (size_t index = 0; index < tablet.rowSize; index++) { + if (!bitMap.isMarked(index)) { + valueBuffer.putBool(valueBuf[index]); + } else { + valueBuffer.putBool(false); + } + } + break; + } + case TSDataType::INT32: { + int* valueBuf = (int*)(tablet.values[i]); + for (size_t index = 0; index < tablet.rowSize; index++) { + if (!bitMap.isMarked(index)) { + valueBuffer.putInt(valueBuf[index]); + } else { + valueBuffer.putInt((numeric_limits::min)()); + } + } + break; + } + case TSDataType::DATE: { + boost::gregorian::date* valueBuf = (boost::gregorian::date*)(tablet.values[i]); + for (size_t index = 0; index < tablet.rowSize; index++) { + if (!bitMap.isMarked(index)) { + valueBuffer.putDate(valueBuf[index]); + } else { + valueBuffer.putInt(EMPTY_DATE_INT); } + } + break; } - for (size_t i = 0; i < tablet.schemas.size(); i++) { - const BitMap& bitMap = tablet.bitMaps[i]; - bool columnHasNull = !bitMap.isAllUnmarked(); - valueBuffer.putChar(columnHasNull ? (char)1 : (char)0); - if (columnHasNull) { - const vector& bytes = bitMap.getByteArray(); - for (size_t index = 0; index < tablet.rowSize / 8 + 1; index++) { - valueBuffer.putChar(bytes[index]); - } + case TSDataType::TIMESTAMP: + case TSDataType::INT64: { + int64_t* valueBuf = (int64_t*)(tablet.values[i]); + for (size_t index = 0; index < tablet.rowSize; index++) { + if (!bitMap.isMarked(index)) { + valueBuffer.putInt64(valueBuf[index]); + } else { + valueBuffer.putInt64((numeric_limits::min)()); + } + } + break; + } + case TSDataType::FLOAT: { + float* valueBuf = (float*)(tablet.values[i]); + for (size_t index = 0; index < tablet.rowSize; index++) { + if (!bitMap.isMarked(index)) { + valueBuffer.putFloat(valueBuf[index]); + } else { + valueBuffer.putFloat((numeric_limits::min)()); + } + } + break; + } + case TSDataType::DOUBLE: { + double* valueBuf = (double*)(tablet.values[i]); + for (size_t index = 0; index < tablet.rowSize; index++) { + if (!bitMap.isMarked(index)) { + valueBuffer.putDouble(valueBuf[index]); + } else { + valueBuffer.putDouble((numeric_limits::min)()); + } + } + break; + } + case TSDataType::STRING: + case TSDataType::BLOB: + case TSDataType::OBJECT: + case TSDataType::TEXT: { + string* valueBuf = (string*)(tablet.values[i]); + for (size_t index = 0; index < tablet.rowSize; index++) { + if (!bitMap.isMarked(index)) { + valueBuffer.putString(valueBuf[index]); + } else { + valueBuffer.putString(""); } + } + break; } - return valueBuffer.str; + default: + throw UnSupportedDataTypeException(string("Data type ") + to_string(dataType) + + " is not supported."); + } + } + for (size_t i = 0; i < tablet.schemas.size(); i++) { + const BitMap& bitMap = tablet.bitMaps[i]; + bool columnHasNull = !bitMap.isAllUnmarked(); + valueBuffer.putChar(columnHasNull ? (char)1 : (char)0); + if (columnHasNull) { + const vector& bytes = bitMap.getByteArray(); + for (size_t index = 0; index < tablet.rowSize / 8 + 1; index++) { + valueBuffer.putChar(bytes[index]); + } + } + } + return valueBuffer.str; } bool SessionUtils::isTabletContainsSingleDevice(Tablet tablet) { - if (tablet.rowSize == 1) { - return true; - } - auto firstDeviceId = tablet.getDeviceID(0); - for (int i = 1; i < tablet.rowSize; ++i) { - if (*firstDeviceId != *tablet.getDeviceID(i)) { - return false; - } - } + if (tablet.rowSize == 1) { return true; + } + auto firstDeviceId = tablet.getDeviceID(0); + for (int i = 1; i < tablet.rowSize; ++i) { + if (*firstDeviceId != *tablet.getDeviceID(i)) { + return false; + } + } + return true; } string MeasurementNode::serialize() const { - MyStringBuffer buffer; - buffer.putString(getName()); - buffer.putChar(getDataType()); - buffer.putChar(getEncoding()); - buffer.putChar(getCompressionType()); - return buffer.str; + MyStringBuffer buffer; + buffer.putString(getName()); + buffer.putChar(getDataType()); + buffer.putChar(getEncoding()); + buffer.putChar(getCompressionType()); + return buffer.str; } string Template::serialize() const { - MyStringBuffer buffer; - stack>> stack; - unordered_set alignedPrefix; - buffer.putString(getName()); - buffer.putBool(isAligned()); - if (isAligned()) { - alignedPrefix.emplace(""); - } - - for (const auto& child : children_) { - stack.push(make_pair("", child.second)); - } - - while (!stack.empty()) { - auto cur = stack.top(); - stack.pop(); - - string prefix = cur.first; - shared_ptr cur_node_ptr = cur.second; - string fullPath(prefix); - - if (!cur_node_ptr->isMeasurement()) { - if (!prefix.empty()) { - fullPath.append("."); - } - fullPath.append(cur_node_ptr->getName()); - if (cur_node_ptr->isAligned()) { - alignedPrefix.emplace(fullPath); - } - for (const auto& child : cur_node_ptr->getChildren()) { - stack.push(make_pair(fullPath, child.second)); - } - } - else { - buffer.putString(prefix); - buffer.putBool(alignedPrefix.find(prefix) != alignedPrefix.end()); - buffer.concat(cur_node_ptr->serialize()); - } - } - - return buffer.str; + MyStringBuffer buffer; + stack>> stack; + unordered_set alignedPrefix; + buffer.putString(getName()); + buffer.putBool(isAligned()); + if (isAligned()) { + alignedPrefix.emplace(""); + } + + for (const auto& child : children_) { + stack.push(make_pair("", child.second)); + } + + while (!stack.empty()) { + auto cur = stack.top(); + stack.pop(); + + string prefix = cur.first; + shared_ptr cur_node_ptr = cur.second; + string fullPath(prefix); + + if (!cur_node_ptr->isMeasurement()) { + if (!prefix.empty()) { + fullPath.append("."); + } + fullPath.append(cur_node_ptr->getName()); + if (cur_node_ptr->isAligned()) { + alignedPrefix.emplace(fullPath); + } + for (const auto& child : cur_node_ptr->getChildren()) { + stack.push(make_pair(fullPath, child.second)); + } + } else { + buffer.putString(prefix); + buffer.putBool(alignedPrefix.find(prefix) != alignedPrefix.end()); + buffer.concat(cur_node_ptr->serialize()); + } + } + + return buffer.str; } /** * When delete variable, make sure release all resource. */ Session::~Session() { - try { - close(); - } - catch (const exception& e) { - log_debug(e.what()); - } + try { + close(); + } catch (const exception& e) { + log_debug(e.what()); + } } void Session::removeBrokenSessionConnection(shared_ptr sessionConnection) { - if (enableRedirection_) { - this->endPointToSessionConnection.erase(sessionConnection->getEndPoint()); - } + if (enableRedirection_) { + this->endPointToSessionConnection.erase(sessionConnection->getEndPoint()); + } - auto it1 = deviceIdToEndpoint.begin(); - while (it1 != deviceIdToEndpoint.end()) { - if (it1->second == sessionConnection->getEndPoint()) { - it1 = deviceIdToEndpoint.erase(it1); - } - else { - ++it1; - } + auto it1 = deviceIdToEndpoint.begin(); + while (it1 != deviceIdToEndpoint.end()) { + if (it1->second == sessionConnection->getEndPoint()) { + it1 = deviceIdToEndpoint.erase(it1); + } else { + ++it1; } + } - auto it2 = tableModelDeviceIdToEndpoint.begin(); - while (it2 != tableModelDeviceIdToEndpoint.end()) { - if (it2->second == sessionConnection->getEndPoint()) { - it2 = tableModelDeviceIdToEndpoint.erase(it2); - } - else { - ++it2; - } + auto it2 = tableModelDeviceIdToEndpoint.begin(); + while (it2 != tableModelDeviceIdToEndpoint.end()) { + if (it2->second == sessionConnection->getEndPoint()) { + it2 = tableModelDeviceIdToEndpoint.erase(it2); + } else { + ++it2; } + } } /** @@ -508,1339 +502,1270 @@ void Session::removeBrokenSessionConnection(shared_ptr sessio * @return whether the batch has been sorted */ bool Session::checkSorted(const Tablet& tablet) { - for (size_t i = 1; i < tablet.rowSize; i++) { - if (tablet.timestamps[i] < tablet.timestamps[i - 1]) { - return false; - } + for (size_t i = 1; i < tablet.rowSize; i++) { + if (tablet.timestamps[i] < tablet.timestamps[i - 1]) { + return false; } - return true; + } + return true; } bool Session::checkSorted(const vector& times) { - for (size_t i = 1; i < times.size(); i++) { - if (times[i] < times[i - 1]) { - return false; - } + for (size_t i = 1; i < times.size(); i++) { + if (times[i] < times[i - 1]) { + return false; } - return true; + } + return true; } template std::vector sortList(const std::vector& valueList, const int* index, int indexLength) { - std::vector sortedValues(valueList.size()); - for (int i = 0; i < indexLength; i++) { - sortedValues[i] = valueList[index[i]]; - } - return sortedValues; + std::vector sortedValues(valueList.size()); + for (int i = 0; i < indexLength; i++) { + sortedValues[i] = valueList[index[i]]; + } + return sortedValues; } -template -void sortValuesList(T* valueList, const int* index, size_t indexLength) { - T* sortedValues = new T[indexLength]; - for (int i = 0; i < indexLength; i++) { - sortedValues[i] = valueList[index[i]]; - } - for (int i = 0; i < indexLength; i++) { - valueList[i] = sortedValues[i]; - } - delete[] sortedValues; +template void sortValuesList(T* valueList, const int* index, size_t indexLength) { + T* sortedValues = new T[indexLength]; + for (int i = 0; i < indexLength; i++) { + sortedValues[i] = valueList[index[i]]; + } + for (int i = 0; i < indexLength; i++) { + valueList[i] = sortedValues[i]; + } + delete[] sortedValues; } void Session::sortTablet(Tablet& tablet) { - /* + /* * following part of code sort the batch data by time, * so we can insert continuous data in value list to get a better performance */ - // sort to get index, and use index to sort value list - int* index = new int[tablet.rowSize]; - for (size_t i = 0; i < tablet.rowSize; i++) { - index[i] = i; - } - - sortIndexByTimestamp(index, tablet.timestamps, tablet.rowSize); - tablet.timestamps = sortList(tablet.timestamps, index, tablet.rowSize); - for (size_t i = 0; i < tablet.schemas.size(); i++) { - TSDataType::TSDataType dataType = tablet.schemas[i].second; - switch (dataType) { - case TSDataType::BOOLEAN: { - sortValuesList((bool*)(tablet.values[i]), index, tablet.rowSize); - break; - } - case TSDataType::INT32: { - sortValuesList((int*)(tablet.values[i]), index, tablet.rowSize); - break; - } - case TSDataType::DATE: { - sortValuesList((boost::gregorian::date*)(tablet.values[i]), index, tablet.rowSize); - break; - } - case TSDataType::TIMESTAMP: - case TSDataType::INT64: { - sortValuesList((int64_t*)(tablet.values[i]), index, tablet.rowSize); - break; - } - case TSDataType::FLOAT: { - sortValuesList((float*)(tablet.values[i]), index, tablet.rowSize); - break; - } - case TSDataType::DOUBLE: { - sortValuesList((double*)(tablet.values[i]), index, tablet.rowSize); - break; - } - case TSDataType::STRING: - case TSDataType::BLOB: - case TSDataType::OBJECT: - case TSDataType::TEXT: { - sortValuesList((string*)(tablet.values[i]), index, tablet.rowSize); - break; - } - default: - throw UnSupportedDataTypeException(string("Data type ") + to_string(dataType) + " is not supported."); - } + // sort to get index, and use index to sort value list + int* index = new int[tablet.rowSize]; + for (size_t i = 0; i < tablet.rowSize; i++) { + index[i] = i; + } + + sortIndexByTimestamp(index, tablet.timestamps, tablet.rowSize); + tablet.timestamps = sortList(tablet.timestamps, index, tablet.rowSize); + for (size_t i = 0; i < tablet.schemas.size(); i++) { + TSDataType::TSDataType dataType = tablet.schemas[i].second; + switch (dataType) { + case TSDataType::BOOLEAN: { + sortValuesList((bool*)(tablet.values[i]), index, tablet.rowSize); + break; + } + case TSDataType::INT32: { + sortValuesList((int*)(tablet.values[i]), index, tablet.rowSize); + break; + } + case TSDataType::DATE: { + sortValuesList((boost::gregorian::date*)(tablet.values[i]), index, tablet.rowSize); + break; + } + case TSDataType::TIMESTAMP: + case TSDataType::INT64: { + sortValuesList((int64_t*)(tablet.values[i]), index, tablet.rowSize); + break; + } + case TSDataType::FLOAT: { + sortValuesList((float*)(tablet.values[i]), index, tablet.rowSize); + break; + } + case TSDataType::DOUBLE: { + sortValuesList((double*)(tablet.values[i]), index, tablet.rowSize); + break; + } + case TSDataType::STRING: + case TSDataType::BLOB: + case TSDataType::OBJECT: + case TSDataType::TEXT: { + sortValuesList((string*)(tablet.values[i]), index, tablet.rowSize); + break; } + default: + throw UnSupportedDataTypeException(string("Data type ") + to_string(dataType) + + " is not supported."); + } + } - delete[] index; + delete[] index; } void Session::sortIndexByTimestamp(int* index, std::vector& timestamps, int length) { - if (length <= 1) { - return; - } + if (length <= 1) { + return; + } - TsCompare tsCompareObj(timestamps); - std::sort(&index[0], &index[length], tsCompareObj); + TsCompare tsCompareObj(timestamps); + std::sort(&index[0], &index[length], tsCompareObj); } /** * Append value into buffer in Big Endian order to comply with IoTDB server */ void Session::appendValues(string& buffer, const char* value, int size) { - static bool hasCheckedEndianFlag = false; - static bool localCpuIsBigEndian = false; - if (!hasCheckedEndianFlag) { - hasCheckedEndianFlag = true; - int chk = 0x0201; //used to distinguish CPU's type (BigEndian or LittleEndian) - localCpuIsBigEndian = (0x01 != *(char*)(&chk)); - } - - if (localCpuIsBigEndian) { - buffer.append(value, size); - } - else { - for (int i = size - 1; i >= 0; i--) { - buffer.append(value + i, 1); - } - } -} - -void -Session::putValuesIntoBuffer(const vector& types, const vector& values, string& buf) { - int32_t date; - for (size_t i = 0; i < values.size(); i++) { - int8_t typeNum = getDataTypeNumber(types[i]); - buf.append((char*)(&typeNum), sizeof(int8_t)); - switch (types[i]) { - case TSDataType::BOOLEAN: - buf.append(values[i], 1); - break; - case TSDataType::INT32: - appendValues(buf, values[i], sizeof(int32_t)); - break; - case TSDataType::DATE: - date = parseDateExpressionToInt(*(boost::gregorian::date*)values[i]); - appendValues(buf, (char*)&date, sizeof(int32_t)); - break; - case TSDataType::TIMESTAMP: - case TSDataType::INT64: - appendValues(buf, values[i], sizeof(int64_t)); - break; - case TSDataType::FLOAT: - appendValues(buf, values[i], sizeof(float)); - break; - case TSDataType::DOUBLE: - appendValues(buf, values[i], sizeof(double)); - break; - case TSDataType::STRING: - case TSDataType::BLOB: - case TSDataType::OBJECT: - case TSDataType::TEXT: { - int32_t len = (uint32_t)strlen(values[i]); - appendValues(buf, (char*)(&len), sizeof(uint32_t)); - // no need to change the byte order of string value - buf.append(values[i], len); - break; - } - default: - break; - } - } -} - -int8_t Session::getDataTypeNumber(TSDataType::TSDataType type) { - switch (type) { + static bool hasCheckedEndianFlag = false; + static bool localCpuIsBigEndian = false; + if (!hasCheckedEndianFlag) { + hasCheckedEndianFlag = true; + int chk = 0x0201; //used to distinguish CPU's type (BigEndian or LittleEndian) + localCpuIsBigEndian = (0x01 != *(char*)(&chk)); + } + + if (localCpuIsBigEndian) { + buffer.append(value, size); + } else { + for (int i = size - 1; i >= 0; i--) { + buffer.append(value + i, 1); + } + } +} + +void Session::putValuesIntoBuffer(const vector& types, + const vector& values, string& buf) { + int32_t date; + for (size_t i = 0; i < values.size(); i++) { + int8_t typeNum = getDataTypeNumber(types[i]); + buf.append((char*)(&typeNum), sizeof(int8_t)); + switch (types[i]) { case TSDataType::BOOLEAN: - return 0; + buf.append(values[i], 1); + break; case TSDataType::INT32: - return 1; + appendValues(buf, values[i], sizeof(int32_t)); + break; + case TSDataType::DATE: + date = parseDateExpressionToInt(*(boost::gregorian::date*)values[i]); + appendValues(buf, (char*)&date, sizeof(int32_t)); + break; + case TSDataType::TIMESTAMP: case TSDataType::INT64: - return 2; + appendValues(buf, values[i], sizeof(int64_t)); + break; case TSDataType::FLOAT: - return 3; + appendValues(buf, values[i], sizeof(float)); + break; case TSDataType::DOUBLE: - return 4; - case TSDataType::TEXT: - return 5; - case TSDataType::TIMESTAMP: - return 8; - case TSDataType::DATE: - return 9; - case TSDataType::BLOB: - return 10; + appendValues(buf, values[i], sizeof(double)); + break; case TSDataType::STRING: - return 11; + case TSDataType::BLOB: case TSDataType::OBJECT: - return 12; + case TSDataType::TEXT: { + int32_t len = (uint32_t)strlen(values[i]); + appendValues(buf, (char*)(&len), sizeof(uint32_t)); + // no need to change the byte order of string value + buf.append(values[i], len); + break; + } default: - return -1; + break; } + } +} + +int8_t Session::getDataTypeNumber(TSDataType::TSDataType type) { + switch (type) { + case TSDataType::BOOLEAN: + return 0; + case TSDataType::INT32: + return 1; + case TSDataType::INT64: + return 2; + case TSDataType::FLOAT: + return 3; + case TSDataType::DOUBLE: + return 4; + case TSDataType::TEXT: + return 5; + case TSDataType::TIMESTAMP: + return 8; + case TSDataType::DATE: + return 9; + case TSDataType::BLOB: + return 10; + case TSDataType::STRING: + return 11; + case TSDataType::OBJECT: + return 12; + default: + return -1; + } } string Session::getVersionString(Version::Version version) { - switch (version) { - case Version::V_0_12: - return "V_0_12"; - case Version::V_0_13: - return "V_0_13"; - case Version::V_1_0: - return "V_1_0"; - default: - return "V_0_12"; - } + switch (version) { + case Version::V_0_12: + return "V_0_12"; + case Version::V_0_13: + return "V_0_13"; + case Version::V_1_0: + return "V_1_0"; + default: + return "V_0_12"; + } } void Session::initZoneId() { - if (!zoneId_.empty()) { - return; - } - - time_t ts = 0; - struct tm tmv; -#if defined(_WIN64) || defined (WIN32) || defined (_WIN32) - localtime_s(&tmv, &ts); + if (!zoneId_.empty()) { + return; + } + + time_t ts = 0; + struct tm tmv; +#if defined(_WIN64) || defined(WIN32) || defined(_WIN32) + localtime_s(&tmv, &ts); #else - localtime_r(&ts, &tmv); + localtime_r(&ts, &tmv); #endif - char zoneStr[32]; - strftime(zoneStr, sizeof(zoneStr), "%z", &tmv); - zoneId_ = zoneStr; + char zoneStr[32]; + strftime(zoneStr, sizeof(zoneStr), "%z", &tmv); + zoneId_ = zoneStr; } void Session::initNodesSupplier(const std::vector& nodeUrls) { - std::vector endPoints; - std::unordered_set uniqueEndpoints; - - if (nodeUrls.empty() && host_.empty()) { - throw IoTDBException("No available nodes"); - } - - // Process provided node URLs - if (!nodeUrls.empty()) { - for (auto& url : nodeUrls) { - try { - TEndPoint endPoint = UrlUtils::parseTEndPointIpv4AndIpv6Url(url); - if (endPoint.port == 0) continue; // Skip invalid endpoints - - std::string endpointKey = endPoint.ip + ":" + std::to_string(endPoint.port); - if (uniqueEndpoints.find(endpointKey) == uniqueEndpoints.end()) { - endPoints.emplace_back(std::move(endPoint)); - uniqueEndpoints.insert(std::move(endpointKey)); - } - } catch (...) { - continue; // Skip malformed URLs - } - } - } - - // Fallback to local endpoint if no valid endpoints found - if (endPoints.empty()) { - if (host_.empty() || rpcPort_ == 0) { - throw IoTDBException("No valid endpoints available"); - } - TEndPoint endPoint; - endPoint.__set_ip(host_); - endPoint.__set_port(rpcPort_); - endPoints.emplace_back(std::move(endPoint)); - } - - if (enableAutoFetch_) { - nodesSupplier_ = NodesSupplier::create(endPoints, username_, password_, useSSL_, trustCertFilePath_); - } - else { - nodesSupplier_ = make_shared(endPoints); - } + std::vector endPoints; + std::unordered_set uniqueEndpoints; + + if (nodeUrls.empty() && host_.empty()) { + throw IoTDBException("No available nodes"); + } + + // Process provided node URLs + if (!nodeUrls.empty()) { + for (auto& url : nodeUrls) { + try { + TEndPoint endPoint = UrlUtils::parseTEndPointIpv4AndIpv6Url(url); + if (endPoint.port == 0) + continue; // Skip invalid endpoints + + std::string endpointKey = endPoint.ip + ":" + std::to_string(endPoint.port); + if (uniqueEndpoints.find(endpointKey) == uniqueEndpoints.end()) { + endPoints.emplace_back(std::move(endPoint)); + uniqueEndpoints.insert(std::move(endpointKey)); + } + } catch (...) { + continue; // Skip malformed URLs + } + } + } + + // Fallback to local endpoint if no valid endpoints found + if (endPoints.empty()) { + if (host_.empty() || rpcPort_ == 0) { + throw IoTDBException("No valid endpoints available"); + } + TEndPoint endPoint; + endPoint.__set_ip(host_); + endPoint.__set_port(rpcPort_); + endPoints.emplace_back(std::move(endPoint)); + } + + if (enableAutoFetch_) { + nodesSupplier_ = + NodesSupplier::create(endPoints, username_, password_, useSSL_, trustCertFilePath_); + } else { + nodesSupplier_ = make_shared(endPoints); + } } void Session::initDefaultSessionConnection() { - // Try all endpoints from supplier until a connection is established. - auto endpoints = nodesSupplier_->getEndPointList(); - bool connected = false; - - for (const auto& endpoint : endpoints) { - try { - host_ = endpoint.ip; - rpcPort_ = endpoint.port; - - defaultEndPoint_.__set_ip(host_); - defaultEndPoint_.__set_port(rpcPort_); - - defaultSessionConnection_ = std::make_shared( - this, defaultEndPoint_, zoneId_, nodesSupplier_, fetchSize_, - 3, - 500, connectTimeoutMs_, - sqlDialect_, database_); - - connected = true; - break; - } catch (const IoTDBException& e) { - log_debug(e.what()); - throw; - } catch (const std::exception& e) { - log_warn(e.what()); - } - } + // Try all endpoints from supplier until a connection is established. + auto endpoints = nodesSupplier_->getEndPointList(); + bool connected = false; + + for (const auto& endpoint : endpoints) { + try { + host_ = endpoint.ip; + rpcPort_ = endpoint.port; - if (!connected) { - throw std::runtime_error("No available node to establish SessionConnection."); + defaultEndPoint_.__set_ip(host_); + defaultEndPoint_.__set_port(rpcPort_); + + defaultSessionConnection_ = std::make_shared( + this, defaultEndPoint_, zoneId_, nodesSupplier_, fetchSize_, 3, 500, connectTimeoutMs_, + sqlDialect_, database_); + + connected = true; + break; + } catch (const IoTDBException& e) { + log_debug(e.what()); + throw; + } catch (const std::exception& e) { + log_warn(e.what()); } + } + + if (!connected) { + throw std::runtime_error("No available node to establish SessionConnection."); + } } void Session::insertStringRecordsWithLeaderCache(vector deviceIds, vector times, vector> measurementsList, - vector> valuesList, bool isAligned) { - std::unordered_map, TSInsertStringRecordsReq> recordsGroup; - for (int i = 0; i < deviceIds.size(); i++) { - auto connection = getSessionConnection(deviceIds[i]); - if (recordsGroup.find(connection) == recordsGroup.end()) { - TSInsertStringRecordsReq request; - std::vector emptyPrefixPaths; - std::vector> emptyMeasurementsList; - vector> emptyValuesList; - std::vector emptyTimestamps; - request.__set_isAligned(isAligned); - request.__set_prefixPaths(emptyPrefixPaths); - request.__set_timestamps(emptyTimestamps); - request.__set_measurementsList(emptyMeasurementsList); - request.__set_valuesList(emptyValuesList); - recordsGroup.insert(make_pair(connection, request)); - } - TSInsertStringRecordsReq& existingReq = recordsGroup[connection]; - existingReq.prefixPaths.emplace_back(deviceIds[i]); - existingReq.timestamps.emplace_back(times[i]); - existingReq.measurementsList.emplace_back(measurementsList[i]); - existingReq.valuesList.emplace_back(valuesList[i]); - } - std::function, const TSInsertStringRecordsReq&)> consumer = - [](const std::shared_ptr& c, const TSInsertStringRecordsReq& r) { - c->insertStringRecords(r); - }; - if (recordsGroup.size() == 1) { - insertOnce(recordsGroup, consumer); - } - else { - insertByGroup(recordsGroup, consumer); - } + vector> valuesList, + bool isAligned) { + std::unordered_map, TSInsertStringRecordsReq> recordsGroup; + for (int i = 0; i < deviceIds.size(); i++) { + auto connection = getSessionConnection(deviceIds[i]); + if (recordsGroup.find(connection) == recordsGroup.end()) { + TSInsertStringRecordsReq request; + std::vector emptyPrefixPaths; + std::vector> emptyMeasurementsList; + vector> emptyValuesList; + std::vector emptyTimestamps; + request.__set_isAligned(isAligned); + request.__set_prefixPaths(emptyPrefixPaths); + request.__set_timestamps(emptyTimestamps); + request.__set_measurementsList(emptyMeasurementsList); + request.__set_valuesList(emptyValuesList); + recordsGroup.insert(make_pair(connection, request)); + } + TSInsertStringRecordsReq& existingReq = recordsGroup[connection]; + existingReq.prefixPaths.emplace_back(deviceIds[i]); + existingReq.timestamps.emplace_back(times[i]); + existingReq.measurementsList.emplace_back(measurementsList[i]); + existingReq.valuesList.emplace_back(valuesList[i]); + } + std::function, const TSInsertStringRecordsReq&)> + consumer = [](const std::shared_ptr& c, + const TSInsertStringRecordsReq& r) { c->insertStringRecords(r); }; + if (recordsGroup.size() == 1) { + insertOnce(recordsGroup, consumer); + } else { + insertByGroup(recordsGroup, consumer); + } } void Session::insertRecordsWithLeaderCache(vector deviceIds, vector times, vector> measurementsList, const vector>& typesList, vector> valuesList, bool isAligned) { - std::unordered_map, TSInsertRecordsReq> recordsGroup; - for (int i = 0; i < deviceIds.size(); i++) { - auto connection = getSessionConnection(deviceIds[i]); - if (recordsGroup.find(connection) == recordsGroup.end()) { - TSInsertRecordsReq request; - std::vector emptyPrefixPaths; - std::vector> emptyMeasurementsList; - std::vector emptyValuesList; - std::vector emptyTimestamps; - request.__set_isAligned(isAligned); - request.__set_prefixPaths(emptyPrefixPaths); - request.__set_timestamps(emptyTimestamps); - request.__set_measurementsList(emptyMeasurementsList); - request.__set_valuesList(emptyValuesList); - recordsGroup.insert(make_pair(connection, request)); - } - TSInsertRecordsReq& existingReq = recordsGroup[connection]; - existingReq.prefixPaths.emplace_back(deviceIds[i]); - existingReq.timestamps.emplace_back(times[i]); - existingReq.measurementsList.emplace_back(measurementsList[i]); - vector bufferList; - string buffer; - putValuesIntoBuffer(typesList[i], valuesList[i], buffer); - existingReq.valuesList.emplace_back(buffer); - recordsGroup[connection] = existingReq; - } - std::function, const TSInsertRecordsReq&)> consumer = - [](const std::shared_ptr& c, const TSInsertRecordsReq& r) { + std::unordered_map, TSInsertRecordsReq> recordsGroup; + for (int i = 0; i < deviceIds.size(); i++) { + auto connection = getSessionConnection(deviceIds[i]); + if (recordsGroup.find(connection) == recordsGroup.end()) { + TSInsertRecordsReq request; + std::vector emptyPrefixPaths; + std::vector> emptyMeasurementsList; + std::vector emptyValuesList; + std::vector emptyTimestamps; + request.__set_isAligned(isAligned); + request.__set_prefixPaths(emptyPrefixPaths); + request.__set_timestamps(emptyTimestamps); + request.__set_measurementsList(emptyMeasurementsList); + request.__set_valuesList(emptyValuesList); + recordsGroup.insert(make_pair(connection, request)); + } + TSInsertRecordsReq& existingReq = recordsGroup[connection]; + existingReq.prefixPaths.emplace_back(deviceIds[i]); + existingReq.timestamps.emplace_back(times[i]); + existingReq.measurementsList.emplace_back(measurementsList[i]); + vector bufferList; + string buffer; + putValuesIntoBuffer(typesList[i], valuesList[i], buffer); + existingReq.valuesList.emplace_back(buffer); + recordsGroup[connection] = existingReq; + } + std::function, const TSInsertRecordsReq&)> consumer = + [](const std::shared_ptr& c, const TSInsertRecordsReq& r) { c->insertRecords(r); - }; - if (recordsGroup.size() == 1) { - insertOnce(recordsGroup, consumer); - } - else { - insertByGroup(recordsGroup, consumer); - } -} - -void Session::insertTabletsWithLeaderCache(unordered_map tablets, bool sorted, bool isAligned) { - std::unordered_map, TSInsertTabletsReq> tabletsGroup; - if (tablets.empty()) { - throw BatchExecutionException("No tablet is inserting!"); - } - for (const auto& item : tablets) { - if (isAligned != item.second->isAligned) { - throw BatchExecutionException("The tablets should be all aligned or non-aligned!"); - } - if (!checkSorted(*(item.second))) { - sortTablet(*(item.second)); - } - auto deviceId = item.first; - auto tablet = item.second; - auto connection = getSessionConnection(deviceId); - auto it = tabletsGroup.find(connection); - if (it == tabletsGroup.end()) { - TSInsertTabletsReq request; - tabletsGroup[connection] = request; - } - TSInsertTabletsReq& existingReq = tabletsGroup[connection]; - existingReq.prefixPaths.emplace_back(tablet->deviceId); - existingReq.timestampsList.emplace_back(move(SessionUtils::getTime(*tablet))); - existingReq.valuesList.emplace_back(move(SessionUtils::getValue(*tablet))); - existingReq.sizeList.emplace_back(tablet->rowSize); - vector dataTypes; - vector measurements; - for (pair schema : tablet->schemas) { - measurements.push_back(schema.first); - dataTypes.push_back(schema.second); - } - existingReq.measurementsList.emplace_back(measurements); - existingReq.typesList.emplace_back(dataTypes); - } - - std::function, const TSInsertTabletsReq&)> consumer = - [](const std::shared_ptr& c, const TSInsertTabletsReq& r) { + }; + if (recordsGroup.size() == 1) { + insertOnce(recordsGroup, consumer); + } else { + insertByGroup(recordsGroup, consumer); + } +} + +void Session::insertTabletsWithLeaderCache(unordered_map tablets, bool sorted, + bool isAligned) { + std::unordered_map, TSInsertTabletsReq> tabletsGroup; + if (tablets.empty()) { + throw BatchExecutionException("No tablet is inserting!"); + } + for (const auto& item : tablets) { + if (isAligned != item.second->isAligned) { + throw BatchExecutionException("The tablets should be all aligned or non-aligned!"); + } + if (!checkSorted(*(item.second))) { + sortTablet(*(item.second)); + } + auto deviceId = item.first; + auto tablet = item.second; + auto connection = getSessionConnection(deviceId); + auto it = tabletsGroup.find(connection); + if (it == tabletsGroup.end()) { + TSInsertTabletsReq request; + tabletsGroup[connection] = request; + } + TSInsertTabletsReq& existingReq = tabletsGroup[connection]; + existingReq.prefixPaths.emplace_back(tablet->deviceId); + existingReq.timestampsList.emplace_back(move(SessionUtils::getTime(*tablet))); + existingReq.valuesList.emplace_back(move(SessionUtils::getValue(*tablet))); + existingReq.sizeList.emplace_back(tablet->rowSize); + vector dataTypes; + vector measurements; + for (pair schema : tablet->schemas) { + measurements.push_back(schema.first); + dataTypes.push_back(schema.second); + } + existingReq.measurementsList.emplace_back(measurements); + existingReq.typesList.emplace_back(dataTypes); + } + + std::function, const TSInsertTabletsReq&)> consumer = + [](const std::shared_ptr& c, const TSInsertTabletsReq& r) { c->insertTablets(r); - }; - if (tabletsGroup.size() == 1) { - insertOnce(tabletsGroup, consumer); - } - else { - insertByGroup(tabletsGroup, consumer); - } + }; + if (tabletsGroup.size() == 1) { + insertOnce(tabletsGroup, consumer); + } else { + insertByGroup(tabletsGroup, consumer); + } } void Session::open() { - open(false, DEFAULT_TIMEOUT_MS); + open(false, DEFAULT_TIMEOUT_MS); } void Session::open(bool enableRPCCompression) { - open(enableRPCCompression, DEFAULT_TIMEOUT_MS); + open(enableRPCCompression, DEFAULT_TIMEOUT_MS); } void Session::open(bool enableRPCCompression, int connectionTimeoutInMs) { - if (!isClosed_) { - return; - } + if (!isClosed_) { + return; + } - try { - initDefaultSessionConnection(); - } - catch (const exception& e) { - log_debug(e.what()); - throw IoTDBException(e.what()); - } - zoneId_ = defaultSessionConnection_->zoneId; + try { + initDefaultSessionConnection(); + } catch (const exception& e) { + log_debug(e.what()); + throw IoTDBException(e.what()); + } + zoneId_ = defaultSessionConnection_->zoneId; - if (enableRedirection_) { - endPointToSessionConnection.insert(make_pair(defaultEndPoint_, defaultSessionConnection_)); - } + if (enableRedirection_) { + endPointToSessionConnection.insert(make_pair(defaultEndPoint_, defaultSessionConnection_)); + } - isClosed_ = false; + isClosed_ = false; } - void Session::close() { - if (isClosed_) { - return; - } - isClosed_ = true; + if (isClosed_) { + return; + } + isClosed_ = true; } - -void Session::insertRecord(const string& deviceId, int64_t time, - const vector& measurements, +void Session::insertRecord(const string& deviceId, int64_t time, const vector& measurements, const vector& values) { - TSInsertStringRecordReq req; - req.__set_prefixPath(deviceId); - req.__set_timestamp(time); - req.__set_measurements(measurements); - req.__set_values(values); - req.__set_isAligned(false); - try { - getSessionConnection(deviceId)->insertStringRecord(req); - } - catch (RedirectException& e) { - handleRedirection(deviceId, e.endPoint); - } catch (const IoTDBConnectionException& e) { - if (enableRedirection_ && deviceIdToEndpoint.find(deviceId) != deviceIdToEndpoint.end()) { - deviceIdToEndpoint.erase(deviceId); - try { - defaultSessionConnection_->insertStringRecord(req); - } - catch (RedirectException& e) { - } - } - else { - throw e; - } - } -} - -void Session::insertRecord(const string& deviceId, int64_t time, - const vector& measurements, + TSInsertStringRecordReq req; + req.__set_prefixPath(deviceId); + req.__set_timestamp(time); + req.__set_measurements(measurements); + req.__set_values(values); + req.__set_isAligned(false); + try { + getSessionConnection(deviceId)->insertStringRecord(req); + } catch (RedirectException& e) { + handleRedirection(deviceId, e.endPoint); + } catch (const IoTDBConnectionException& e) { + if (enableRedirection_ && deviceIdToEndpoint.find(deviceId) != deviceIdToEndpoint.end()) { + deviceIdToEndpoint.erase(deviceId); + try { + defaultSessionConnection_->insertStringRecord(req); + } catch (RedirectException& e) { + } + } else { + throw e; + } + } +} + +void Session::insertRecord(const string& deviceId, int64_t time, const vector& measurements, const vector& types, const vector& values) { - TSInsertRecordReq req; - req.__set_prefixPath(deviceId); - req.__set_timestamp(time); - req.__set_measurements(measurements); - string buffer; - putValuesIntoBuffer(types, values, buffer); - req.__set_values(buffer); - req.__set_isAligned(false); - try { - getSessionConnection(deviceId)->insertRecord(req); - } - catch (RedirectException& e) { - handleRedirection(deviceId, e.endPoint); - } catch (const IoTDBConnectionException& e) { - if (enableRedirection_ && deviceIdToEndpoint.find(deviceId) != deviceIdToEndpoint.end()) { - deviceIdToEndpoint.erase(deviceId); - try { - defaultSessionConnection_->insertRecord(req); - } - catch (RedirectException& e) { - } - } - else { - throw e; - } - } + TSInsertRecordReq req; + req.__set_prefixPath(deviceId); + req.__set_timestamp(time); + req.__set_measurements(measurements); + string buffer; + putValuesIntoBuffer(types, values, buffer); + req.__set_values(buffer); + req.__set_isAligned(false); + try { + getSessionConnection(deviceId)->insertRecord(req); + } catch (RedirectException& e) { + handleRedirection(deviceId, e.endPoint); + } catch (const IoTDBConnectionException& e) { + if (enableRedirection_ && deviceIdToEndpoint.find(deviceId) != deviceIdToEndpoint.end()) { + deviceIdToEndpoint.erase(deviceId); + try { + defaultSessionConnection_->insertRecord(req); + } catch (RedirectException& e) { + } + } else { + throw e; + } + } } void Session::insertAlignedRecord(const string& deviceId, int64_t time, const vector& measurements, const vector& values) { - TSInsertStringRecordReq req; - req.__set_prefixPath(deviceId); - req.__set_timestamp(time); - req.__set_measurements(measurements); - req.__set_values(values); - req.__set_isAligned(true); - try { - getSessionConnection(deviceId)->insertStringRecord(req); - } - catch (RedirectException& e) { - handleRedirection(deviceId, e.endPoint); - } catch (const IoTDBConnectionException& e) { - if (enableRedirection_ && deviceIdToEndpoint.find(deviceId) != deviceIdToEndpoint.end()) { - deviceIdToEndpoint.erase(deviceId); - try { - defaultSessionConnection_->insertStringRecord(req); - } - catch (RedirectException& e) { - } - } - else { - throw e; - } - } + TSInsertStringRecordReq req; + req.__set_prefixPath(deviceId); + req.__set_timestamp(time); + req.__set_measurements(measurements); + req.__set_values(values); + req.__set_isAligned(true); + try { + getSessionConnection(deviceId)->insertStringRecord(req); + } catch (RedirectException& e) { + handleRedirection(deviceId, e.endPoint); + } catch (const IoTDBConnectionException& e) { + if (enableRedirection_ && deviceIdToEndpoint.find(deviceId) != deviceIdToEndpoint.end()) { + deviceIdToEndpoint.erase(deviceId); + try { + defaultSessionConnection_->insertStringRecord(req); + } catch (RedirectException& e) { + } + } else { + throw e; + } + } } void Session::insertAlignedRecord(const string& deviceId, int64_t time, const vector& measurements, const vector& types, const vector& values) { - TSInsertRecordReq req; - req.__set_prefixPath(deviceId); - req.__set_timestamp(time); - req.__set_measurements(measurements); - string buffer; - putValuesIntoBuffer(types, values, buffer); - req.__set_values(buffer); - req.__set_isAligned(false); - try { - getSessionConnection(deviceId)->insertRecord(req); - } - catch (RedirectException& e) { - handleRedirection(deviceId, e.endPoint); - } catch (const IoTDBConnectionException& e) { - if (enableRedirection_ && deviceIdToEndpoint.find(deviceId) != deviceIdToEndpoint.end()) { - deviceIdToEndpoint.erase(deviceId); - try { - defaultSessionConnection_->insertRecord(req); - } - catch (RedirectException& e) { - } - } - else { - throw e; - } - } -} - -void Session::insertRecords(const vector& deviceIds, - const vector& times, + TSInsertRecordReq req; + req.__set_prefixPath(deviceId); + req.__set_timestamp(time); + req.__set_measurements(measurements); + string buffer; + putValuesIntoBuffer(types, values, buffer); + req.__set_values(buffer); + req.__set_isAligned(false); + try { + getSessionConnection(deviceId)->insertRecord(req); + } catch (RedirectException& e) { + handleRedirection(deviceId, e.endPoint); + } catch (const IoTDBConnectionException& e) { + if (enableRedirection_ && deviceIdToEndpoint.find(deviceId) != deviceIdToEndpoint.end()) { + deviceIdToEndpoint.erase(deviceId); + try { + defaultSessionConnection_->insertRecord(req); + } catch (RedirectException& e) { + } + } else { + throw e; + } + } +} + +void Session::insertRecords(const vector& deviceIds, const vector& times, const vector>& measurementsList, const vector>& valuesList) { - size_t len = deviceIds.size(); - if (len != times.size() || len != measurementsList.size() || len != valuesList.size()) { - logic_error e("deviceIds, times, measurementsList and valuesList's size should be equal"); - throw exception(e); - } - - if (enableRedirection_) { - insertStringRecordsWithLeaderCache(deviceIds, times, measurementsList, valuesList, false); - } - else { - TSInsertStringRecordsReq request; - request.__set_prefixPaths(deviceIds); - request.__set_timestamps(times); - request.__set_measurementsList(measurementsList); - request.__set_valuesList(valuesList); - request.__set_isAligned(false); - try { - defaultSessionConnection_->insertStringRecords(request); - } - catch (RedirectException& e) { - } + size_t len = deviceIds.size(); + if (len != times.size() || len != measurementsList.size() || len != valuesList.size()) { + logic_error e("deviceIds, times, measurementsList and valuesList's size should be equal"); + throw exception(e); + } + + if (enableRedirection_) { + insertStringRecordsWithLeaderCache(deviceIds, times, measurementsList, valuesList, false); + } else { + TSInsertStringRecordsReq request; + request.__set_prefixPaths(deviceIds); + request.__set_timestamps(times); + request.__set_measurementsList(measurementsList); + request.__set_valuesList(valuesList); + request.__set_isAligned(false); + try { + defaultSessionConnection_->insertStringRecords(request); + } catch (RedirectException& e) { } + } } -void Session::insertRecords(const vector& deviceIds, - const vector& times, +void Session::insertRecords(const vector& deviceIds, const vector& times, const vector>& measurementsList, const vector>& typesList, const vector>& valuesList) { - size_t len = deviceIds.size(); - if (len != times.size() || len != measurementsList.size() || len != valuesList.size()) { - logic_error e("deviceIds, times, measurementsList and valuesList's size should be equal"); - throw exception(e); - } - - if (enableRedirection_) { - insertRecordsWithLeaderCache(deviceIds, times, measurementsList, typesList, valuesList, false); - } - else { - TSInsertRecordsReq request; - request.__set_prefixPaths(deviceIds); - request.__set_timestamps(times); - request.__set_measurementsList(measurementsList); - vector bufferList; - for (size_t i = 0; i < valuesList.size(); i++) { - string buffer; - putValuesIntoBuffer(typesList[i], valuesList[i], buffer); - bufferList.push_back(buffer); - } - request.__set_valuesList(bufferList); - request.__set_isAligned(false); - try { - defaultSessionConnection_->insertRecords(request); - } - catch (RedirectException& e) { - } + size_t len = deviceIds.size(); + if (len != times.size() || len != measurementsList.size() || len != valuesList.size()) { + logic_error e("deviceIds, times, measurementsList and valuesList's size should be equal"); + throw exception(e); + } + + if (enableRedirection_) { + insertRecordsWithLeaderCache(deviceIds, times, measurementsList, typesList, valuesList, false); + } else { + TSInsertRecordsReq request; + request.__set_prefixPaths(deviceIds); + request.__set_timestamps(times); + request.__set_measurementsList(measurementsList); + vector bufferList; + for (size_t i = 0; i < valuesList.size(); i++) { + string buffer; + putValuesIntoBuffer(typesList[i], valuesList[i], buffer); + bufferList.push_back(buffer); } -} - -void Session::insertAlignedRecords(const vector& deviceIds, - const vector& times, - const vector>& measurementsList, - const vector>& valuesList) { - size_t len = deviceIds.size(); - if (len != times.size() || len != measurementsList.size() || len != valuesList.size()) { - logic_error e("deviceIds, times, measurementsList and valuesList's size should be equal"); - throw exception(e); - } - - if (enableRedirection_) { - insertStringRecordsWithLeaderCache(deviceIds, times, measurementsList, valuesList, true); - } - else { - TSInsertStringRecordsReq request; - request.__set_prefixPaths(deviceIds); - request.__set_timestamps(times); - request.__set_measurementsList(measurementsList); - request.__set_valuesList(valuesList); - request.__set_isAligned(true); - try { - defaultSessionConnection_->insertStringRecords(request); - } - catch (RedirectException& e) { - } + request.__set_valuesList(bufferList); + request.__set_isAligned(false); + try { + defaultSessionConnection_->insertRecords(request); + } catch (RedirectException& e) { } + } } -void Session::insertAlignedRecords(const vector& deviceIds, - const vector& times, +void Session::insertAlignedRecords(const vector& deviceIds, const vector& times, const vector>& measurementsList, - const vector>& typesList, - const vector>& valuesList) { - size_t len = deviceIds.size(); - if (len != times.size() || len != measurementsList.size() || len != valuesList.size()) { - logic_error e("deviceIds, times, measurementsList and valuesList's size should be equal"); - throw exception(e); - } - - if (enableRedirection_) { - insertRecordsWithLeaderCache(deviceIds, times, measurementsList, typesList, valuesList, true); - } - else { - TSInsertRecordsReq request; - request.__set_prefixPaths(deviceIds); - request.__set_timestamps(times); - request.__set_measurementsList(measurementsList); - vector bufferList; - for (size_t i = 0; i < valuesList.size(); i++) { - string buffer; - putValuesIntoBuffer(typesList[i], valuesList[i], buffer); - bufferList.push_back(buffer); - } - request.__set_valuesList(bufferList); - request.__set_isAligned(false); - try { - defaultSessionConnection_->insertRecords(request); - } - catch (RedirectException& e) { - } + const vector>& valuesList) { + size_t len = deviceIds.size(); + if (len != times.size() || len != measurementsList.size() || len != valuesList.size()) { + logic_error e("deviceIds, times, measurementsList and valuesList's size should be equal"); + throw exception(e); + } + + if (enableRedirection_) { + insertStringRecordsWithLeaderCache(deviceIds, times, measurementsList, valuesList, true); + } else { + TSInsertStringRecordsReq request; + request.__set_prefixPaths(deviceIds); + request.__set_timestamps(times); + request.__set_measurementsList(measurementsList); + request.__set_valuesList(valuesList); + request.__set_isAligned(true); + try { + defaultSessionConnection_->insertStringRecords(request); + } catch (RedirectException& e) { } + } } -void Session::insertRecordsOfOneDevice(const string& deviceId, - vector& times, - vector>& measurementsList, - vector>& typesList, - vector>& valuesList) { - insertRecordsOfOneDevice(deviceId, times, measurementsList, typesList, valuesList, false); -} - -void Session::insertRecordsOfOneDevice(const string& deviceId, - vector& times, - vector>& measurementsList, - vector>& typesList, - vector>& valuesList, - bool sorted) { - if (!checkSorted(times)) { - int* index = new int[times.size()]; - for (size_t i = 0; i < times.size(); i++) { - index[i] = (int)i; - } - - sortIndexByTimestamp(index, times, (int)(times.size())); - times = sortList(times, index, (int)(times.size())); - measurementsList = sortList(measurementsList, index, (int)(times.size())); - typesList = sortList(typesList, index, (int)(times.size())); - valuesList = sortList(valuesList, index, (int)(times.size())); - delete[] index; - } - TSInsertRecordsOfOneDeviceReq request; - request.__set_prefixPath(deviceId); +void Session::insertAlignedRecords(const vector& deviceIds, const vector& times, + const vector>& measurementsList, + const vector>& typesList, + const vector>& valuesList) { + size_t len = deviceIds.size(); + if (len != times.size() || len != measurementsList.size() || len != valuesList.size()) { + logic_error e("deviceIds, times, measurementsList and valuesList's size should be equal"); + throw exception(e); + } + + if (enableRedirection_) { + insertRecordsWithLeaderCache(deviceIds, times, measurementsList, typesList, valuesList, true); + } else { + TSInsertRecordsReq request; + request.__set_prefixPaths(deviceIds); request.__set_timestamps(times); request.__set_measurementsList(measurementsList); vector bufferList; for (size_t i = 0; i < valuesList.size(); i++) { - string buffer; - putValuesIntoBuffer(typesList[i], valuesList[i], buffer); - bufferList.push_back(buffer); + string buffer; + putValuesIntoBuffer(typesList[i], valuesList[i], buffer); + bufferList.push_back(buffer); } request.__set_valuesList(bufferList); request.__set_isAligned(false); - TSStatus respStatus; try { - getSessionConnection(deviceId)->insertRecordsOfOneDevice(request); - } - catch (RedirectException& e) { - handleRedirection(deviceId, e.endPoint); - } catch (const IoTDBConnectionException& e) { - if (enableRedirection_ && deviceIdToEndpoint.find(deviceId) != deviceIdToEndpoint.end()) { - deviceIdToEndpoint.erase(deviceId); - try { - defaultSessionConnection_->insertRecordsOfOneDevice(request); - } - catch (RedirectException& e) { - } - } - else { - throw e; - } + defaultSessionConnection_->insertRecords(request); + } catch (RedirectException& e) { } + } +} + +void Session::insertRecordsOfOneDevice(const string& deviceId, vector& times, + vector>& measurementsList, + vector>& typesList, + vector>& valuesList) { + insertRecordsOfOneDevice(deviceId, times, measurementsList, typesList, valuesList, false); } -void Session::insertAlignedRecordsOfOneDevice(const string& deviceId, - vector& times, +void Session::insertRecordsOfOneDevice(const string& deviceId, vector& times, + vector>& measurementsList, + vector>& typesList, + vector>& valuesList, bool sorted) { + if (!checkSorted(times)) { + int* index = new int[times.size()]; + for (size_t i = 0; i < times.size(); i++) { + index[i] = (int)i; + } + + sortIndexByTimestamp(index, times, (int)(times.size())); + times = sortList(times, index, (int)(times.size())); + measurementsList = sortList(measurementsList, index, (int)(times.size())); + typesList = sortList(typesList, index, (int)(times.size())); + valuesList = sortList(valuesList, index, (int)(times.size())); + delete[] index; + } + TSInsertRecordsOfOneDeviceReq request; + request.__set_prefixPath(deviceId); + request.__set_timestamps(times); + request.__set_measurementsList(measurementsList); + vector bufferList; + for (size_t i = 0; i < valuesList.size(); i++) { + string buffer; + putValuesIntoBuffer(typesList[i], valuesList[i], buffer); + bufferList.push_back(buffer); + } + request.__set_valuesList(bufferList); + request.__set_isAligned(false); + TSStatus respStatus; + try { + getSessionConnection(deviceId)->insertRecordsOfOneDevice(request); + } catch (RedirectException& e) { + handleRedirection(deviceId, e.endPoint); + } catch (const IoTDBConnectionException& e) { + if (enableRedirection_ && deviceIdToEndpoint.find(deviceId) != deviceIdToEndpoint.end()) { + deviceIdToEndpoint.erase(deviceId); + try { + defaultSessionConnection_->insertRecordsOfOneDevice(request); + } catch (RedirectException& e) { + } + } else { + throw e; + } + } +} + +void Session::insertAlignedRecordsOfOneDevice(const string& deviceId, vector& times, vector>& measurementsList, vector>& typesList, vector>& valuesList) { - insertAlignedRecordsOfOneDevice(deviceId, times, measurementsList, typesList, valuesList, false); + insertAlignedRecordsOfOneDevice(deviceId, times, measurementsList, typesList, valuesList, false); } -void Session::insertAlignedRecordsOfOneDevice(const string& deviceId, - vector& times, +void Session::insertAlignedRecordsOfOneDevice(const string& deviceId, vector& times, vector>& measurementsList, vector>& typesList, - vector>& valuesList, - bool sorted) { - if (!checkSorted(times)) { - int* index = new int[times.size()]; - for (size_t i = 0; i < times.size(); i++) { - index[i] = (int)i; - } - - sortIndexByTimestamp(index, times, (int)(times.size())); - times = sortList(times, index, (int)(times.size())); - measurementsList = sortList(measurementsList, index, (int)(times.size())); - typesList = sortList(typesList, index, (int)(times.size())); - valuesList = sortList(valuesList, index, (int)(times.size())); - delete[] index; - } - TSInsertRecordsOfOneDeviceReq request; - request.__set_prefixPath(deviceId); - request.__set_timestamps(times); - request.__set_measurementsList(measurementsList); - vector bufferList; - for (size_t i = 0; i < valuesList.size(); i++) { - string buffer; - putValuesIntoBuffer(typesList[i], valuesList[i], buffer); - bufferList.push_back(buffer); - } - request.__set_valuesList(bufferList); - request.__set_isAligned(true); - TSStatus respStatus; - try { - getSessionConnection(deviceId)->insertRecordsOfOneDevice(request); - } - catch (RedirectException& e) { - handleRedirection(deviceId, e.endPoint); - } catch (const IoTDBConnectionException& e) { - if (enableRedirection_ && deviceIdToEndpoint.find(deviceId) != deviceIdToEndpoint.end()) { - deviceIdToEndpoint.erase(deviceId); - try { - defaultSessionConnection_->insertRecordsOfOneDevice(request); - } - catch (RedirectException& e) { - } - } - else { - throw e; - } - } + vector>& valuesList, bool sorted) { + if (!checkSorted(times)) { + int* index = new int[times.size()]; + for (size_t i = 0; i < times.size(); i++) { + index[i] = (int)i; + } + + sortIndexByTimestamp(index, times, (int)(times.size())); + times = sortList(times, index, (int)(times.size())); + measurementsList = sortList(measurementsList, index, (int)(times.size())); + typesList = sortList(typesList, index, (int)(times.size())); + valuesList = sortList(valuesList, index, (int)(times.size())); + delete[] index; + } + TSInsertRecordsOfOneDeviceReq request; + request.__set_prefixPath(deviceId); + request.__set_timestamps(times); + request.__set_measurementsList(measurementsList); + vector bufferList; + for (size_t i = 0; i < valuesList.size(); i++) { + string buffer; + putValuesIntoBuffer(typesList[i], valuesList[i], buffer); + bufferList.push_back(buffer); + } + request.__set_valuesList(bufferList); + request.__set_isAligned(true); + TSStatus respStatus; + try { + getSessionConnection(deviceId)->insertRecordsOfOneDevice(request); + } catch (RedirectException& e) { + handleRedirection(deviceId, e.endPoint); + } catch (const IoTDBConnectionException& e) { + if (enableRedirection_ && deviceIdToEndpoint.find(deviceId) != deviceIdToEndpoint.end()) { + deviceIdToEndpoint.erase(deviceId); + try { + defaultSessionConnection_->insertRecordsOfOneDevice(request); + } catch (RedirectException& e) { + } + } else { + throw e; + } + } } void Session::insertTablet(Tablet& tablet) { - try { - insertTablet(tablet, false); - } - catch (const exception& e) { - log_debug(e.what()); - logic_error error(e.what()); - throw exception(error); - } + try { + insertTablet(tablet, false); + } catch (const exception& e) { + log_debug(e.what()); + logic_error error(e.what()); + throw exception(error); + } } void Session::buildInsertTabletReq(TSInsertTabletReq& request, Tablet& tablet, bool sorted) { - if ((!sorted) && !checkSorted(tablet)) { - sortTablet(tablet); - } - - request.__set_prefixPath(tablet.deviceId); - - std::vector reqMeasurements; - reqMeasurements.reserve(tablet.schemas.size()); - std::vector types; - types.reserve(tablet.schemas.size()); - for (pair schema : tablet.schemas) { - reqMeasurements.push_back(schema.first); - types.push_back(schema.second); - } - request.__set_measurements(reqMeasurements); - request.__set_types(types); - request.__set_values(SessionUtils::getValue(tablet)); - request.__set_timestamps(SessionUtils::getTime(tablet)); - request.__set_size(tablet.rowSize); - request.__set_isAligned(tablet.isAligned); + if ((!sorted) && !checkSorted(tablet)) { + sortTablet(tablet); + } + + request.__set_prefixPath(tablet.deviceId); + + std::vector reqMeasurements; + reqMeasurements.reserve(tablet.schemas.size()); + std::vector types; + types.reserve(tablet.schemas.size()); + for (pair schema : tablet.schemas) { + reqMeasurements.push_back(schema.first); + types.push_back(schema.second); + } + request.__set_measurements(reqMeasurements); + request.__set_types(types); + request.__set_values(SessionUtils::getValue(tablet)); + request.__set_timestamps(SessionUtils::getTime(tablet)); + request.__set_size(tablet.rowSize); + request.__set_isAligned(tablet.isAligned); } void Session::insertTablet(TSInsertTabletReq request) { - auto deviceId = request.prefixPath; - try { - getSessionConnection(deviceId)->insertTablet(request); - } - catch (RedirectException& e) { - handleRedirection(deviceId, e.endPoint); - } catch (const IoTDBConnectionException& e) { - if (enableRedirection_ && deviceIdToEndpoint.find(deviceId) != deviceIdToEndpoint.end()) { - deviceIdToEndpoint.erase(deviceId); - try { - defaultSessionConnection_->insertTablet(request); - } - catch (RedirectException& e) { - } - } - else { - throw e; - } - } + auto deviceId = request.prefixPath; + try { + getSessionConnection(deviceId)->insertTablet(request); + } catch (RedirectException& e) { + handleRedirection(deviceId, e.endPoint); + } catch (const IoTDBConnectionException& e) { + if (enableRedirection_ && deviceIdToEndpoint.find(deviceId) != deviceIdToEndpoint.end()) { + deviceIdToEndpoint.erase(deviceId); + try { + defaultSessionConnection_->insertTablet(request); + } catch (RedirectException& e) { + } + } else { + throw e; + } + } } void Session::insertTablet(Tablet& tablet, bool sorted) { - TSInsertTabletReq request; - buildInsertTabletReq(request, tablet, sorted); - insertTablet(request); + TSInsertTabletReq request; + buildInsertTabletReq(request, tablet, sorted); + insertTablet(request); } void Session::insertRelationalTablet(Tablet& tablet, bool sorted) { - std::unordered_map, Tablet> relationalTabletGroup; - if (tableModelDeviceIdToEndpoint.empty()) { - relationalTabletGroup.insert(make_pair(defaultSessionConnection_, tablet)); - } - else if (SessionUtils::isTabletContainsSingleDevice(tablet)) { - relationalTabletGroup.insert(make_pair(getSessionConnection(tablet.getDeviceID(0)), tablet)); - } - else { - for (int row = 0; row < tablet.rowSize; row++) { - auto iDeviceID = tablet.getDeviceID(row); - std::shared_ptr connection = getSessionConnection(iDeviceID); - - auto it = relationalTabletGroup.find(connection); - if (it == relationalTabletGroup.end()) { - Tablet newTablet(tablet.deviceId, tablet.schemas, tablet.columnTypes, tablet.rowSize); - it = relationalTabletGroup.insert(std::make_pair(connection, newTablet)).first; - } - - Tablet& currentTablet = it->second; - int rowIndex = currentTablet.rowSize++; - currentTablet.timestamps[rowIndex] = tablet.timestamps[row]; - for (int col = 0; col < tablet.schemas.size(); col++) { - switch (tablet.schemas[col].second) { - case TSDataType::BOOLEAN: - currentTablet.addValue(tablet.schemas[col].first, rowIndex, - *(bool*)tablet.getValue(col, row, tablet.schemas[col].second)); - break; - case TSDataType::INT32: - currentTablet.addValue(tablet.schemas[col].first, rowIndex, - *(int32_t*)tablet.getValue(col, row, tablet.schemas[col].second)); - break; - case TSDataType::INT64: - case TSDataType::TIMESTAMP: - currentTablet.addValue(tablet.schemas[col].first, rowIndex, - *(int64_t*)tablet.getValue(col, row, tablet.schemas[col].second)); - break; - case TSDataType::FLOAT: - currentTablet.addValue(tablet.schemas[col].first, rowIndex, - *(float*)tablet.getValue(col, row, tablet.schemas[col].second)); - break; - case TSDataType::DOUBLE: - currentTablet.addValue(tablet.schemas[col].first, rowIndex, - *(double*)tablet.getValue(col, row, tablet.schemas[col].second)); - break; - case TSDataType::DATE: { - currentTablet.addValue(tablet.schemas[col].first, rowIndex, - *(boost::gregorian::date*)tablet.getValue(col, row, tablet.schemas[col].second)); - break; - } - case TSDataType::STRING: - case TSDataType::TEXT: - case TSDataType::OBJECT: - case TSDataType::BLOB: { - currentTablet.addValue(tablet.schemas[col].first, rowIndex, - *(string*)tablet.getValue(col, row, tablet.schemas[col].second)); - break; - } - default: - break; - } - - } + std::unordered_map, Tablet> relationalTabletGroup; + if (tableModelDeviceIdToEndpoint.empty()) { + relationalTabletGroup.insert(make_pair(defaultSessionConnection_, tablet)); + } else if (SessionUtils::isTabletContainsSingleDevice(tablet)) { + relationalTabletGroup.insert(make_pair(getSessionConnection(tablet.getDeviceID(0)), tablet)); + } else { + for (int row = 0; row < tablet.rowSize; row++) { + auto iDeviceID = tablet.getDeviceID(row); + std::shared_ptr connection = getSessionConnection(iDeviceID); + + auto it = relationalTabletGroup.find(connection); + if (it == relationalTabletGroup.end()) { + Tablet newTablet(tablet.deviceId, tablet.schemas, tablet.columnTypes, tablet.rowSize); + it = relationalTabletGroup.insert(std::make_pair(connection, newTablet)).first; + } + + Tablet& currentTablet = it->second; + int rowIndex = currentTablet.rowSize++; + currentTablet.timestamps[rowIndex] = tablet.timestamps[row]; + for (int col = 0; col < tablet.schemas.size(); col++) { + switch (tablet.schemas[col].second) { + case TSDataType::BOOLEAN: + currentTablet.addValue(tablet.schemas[col].first, rowIndex, + *(bool*)tablet.getValue(col, row, tablet.schemas[col].second)); + break; + case TSDataType::INT32: + currentTablet.addValue(tablet.schemas[col].first, rowIndex, + *(int32_t*)tablet.getValue(col, row, tablet.schemas[col].second)); + break; + case TSDataType::INT64: + case TSDataType::TIMESTAMP: + currentTablet.addValue(tablet.schemas[col].first, rowIndex, + *(int64_t*)tablet.getValue(col, row, tablet.schemas[col].second)); + break; + case TSDataType::FLOAT: + currentTablet.addValue(tablet.schemas[col].first, rowIndex, + *(float*)tablet.getValue(col, row, tablet.schemas[col].second)); + break; + case TSDataType::DOUBLE: + currentTablet.addValue(tablet.schemas[col].first, rowIndex, + *(double*)tablet.getValue(col, row, tablet.schemas[col].second)); + break; + case TSDataType::DATE: { + currentTablet.addValue( + tablet.schemas[col].first, rowIndex, + *(boost::gregorian::date*)tablet.getValue(col, row, tablet.schemas[col].second)); + break; } + case TSDataType::STRING: + case TSDataType::TEXT: + case TSDataType::OBJECT: + case TSDataType::BLOB: { + currentTablet.addValue(tablet.schemas[col].first, rowIndex, + *(string*)tablet.getValue(col, row, tablet.schemas[col].second)); + break; + } + default: + break; + } + } } - if (relationalTabletGroup.size() == 1) { - insertRelationalTabletOnce(relationalTabletGroup, sorted); - } - else { - insertRelationalTabletByGroup(relationalTabletGroup, sorted); - } + } + if (relationalTabletGroup.size() == 1) { + insertRelationalTabletOnce(relationalTabletGroup, sorted); + } else { + insertRelationalTabletByGroup(relationalTabletGroup, sorted); + } } void Session::insertRelationalTablet(Tablet& tablet) { - insertRelationalTablet(tablet, false); -} - -void Session::insertRelationalTabletOnce(const std::unordered_map, Tablet>& - relationalTabletGroup, bool sorted) { - auto iter = relationalTabletGroup.begin(); + insertRelationalTablet(tablet, false); +} + +void Session::insertRelationalTabletOnce( + const std::unordered_map, Tablet>& relationalTabletGroup, + bool sorted) { + auto iter = relationalTabletGroup.begin(); + auto connection = iter->first; + auto tablet = iter->second; + TSInsertTabletReq request; + buildInsertTabletReq(request, tablet, sorted); + request.__set_writeToTable(true); + std::vector columnCategories; + for (auto& category : tablet.columnTypes) { + columnCategories.push_back(static_cast(category)); + } + request.__set_columnCategories(columnCategories); + try { + TSStatus respStatus; + connection->getSessionClient()->insertTablet(respStatus, request); + RpcUtils::verifySuccess(respStatus); + } catch (RedirectException& e) { + auto endPointList = e.endPointList; + for (int i = 0; i < endPointList.size(); i++) { + auto deviceID = tablet.getDeviceID(i); + handleRedirection(deviceID, endPointList[i]); + } + } catch (const IoTDBConnectionException& e) { + if (endPointToSessionConnection.size() > 1) { + removeBrokenSessionConnection(connection); + try { + TSStatus respStatus; + defaultSessionConnection_->getSessionClient()->insertTablet(respStatus, request); + RpcUtils::verifySuccess(respStatus); + } catch (RedirectException& e) { + } + } else { + throw IoTDBConnectionException(e.what()); + } + } 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::insertRelationalTabletByGroup( + const std::unordered_map, Tablet>& relationalTabletGroup, + bool sorted) { + // Create a vector to store future objects for asynchronous operations + std::vector> futures; + + for (auto iter = relationalTabletGroup.begin(); iter != relationalTabletGroup.end(); iter++) { auto connection = iter->first; auto tablet = iter->second; - TSInsertTabletReq request; - buildInsertTabletReq(request, tablet, sorted); - request.__set_writeToTable(true); - std::vector columnCategories; - for (auto& category : tablet.columnTypes) { + + // Launch asynchronous task for each tablet insertion + futures.emplace_back(std::async(std::launch::async, [=]() mutable { + TSInsertTabletReq request; + buildInsertTabletReq(request, tablet, sorted); + request.__set_writeToTable(true); + + std::vector columnCategories; + for (auto& category : tablet.columnTypes) { columnCategories.push_back(static_cast(category)); - } - request.__set_columnCategories(columnCategories); - try { + } + request.__set_columnCategories(columnCategories); + + try { TSStatus respStatus; connection->getSessionClient()->insertTablet(respStatus, request); RpcUtils::verifySuccess(respStatus); - } - catch (RedirectException& e) { - auto endPointList = e.endPointList; - for (int i = 0; i < endPointList.size(); i++) { - auto deviceID = tablet.getDeviceID(i); - handleRedirection(deviceID, endPointList[i]); - } - } catch (const IoTDBConnectionException& e) { - if (endPointToSessionConnection.size() > 1) { - removeBrokenSessionConnection(connection); - try { - TSStatus respStatus; - defaultSessionConnection_->getSessionClient()->insertTablet(respStatus, request); - RpcUtils::verifySuccess(respStatus); - } - catch (RedirectException& e) { - } - } - else { - throw IoTDBConnectionException(e.what()); - } - } catch (const TTransportException& e) { + } catch (const TTransportException& e) { log_debug(e.what()); throw IoTDBConnectionException(e.what()); - } catch (const IoTDBException& e) { + } catch (const IoTDBException& e) { log_debug(e.what()); throw; - } catch (const exception& e) { + } catch (const exception& e) { log_debug(e.what()); throw IoTDBException(e.what()); - } -} - -void Session::insertRelationalTabletByGroup(const std::unordered_map, Tablet>& - relationalTabletGroup, bool sorted) { - // Create a vector to store future objects for asynchronous operations - std::vector> futures; + } + })); + } - for (auto iter = relationalTabletGroup.begin(); iter != relationalTabletGroup.end(); iter++) { - auto connection = iter->first; - auto tablet = iter->second; - - // Launch asynchronous task for each tablet insertion - futures.emplace_back(std::async(std::launch::async, [=]() mutable { - TSInsertTabletReq request; - buildInsertTabletReq(request, tablet, sorted); - request.__set_writeToTable(true); - - std::vector columnCategories; - for (auto& category : tablet.columnTypes) { - columnCategories.push_back(static_cast(category)); - } - request.__set_columnCategories(columnCategories); - - try { - TSStatus respStatus; - connection->getSessionClient()->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()); - } - })); - } - - for (auto& f : futures) { - f.get(); - } + for (auto& f : futures) { + f.get(); + } } void Session::insertAlignedTablet(Tablet& tablet) { - insertAlignedTablet(tablet, false); + insertAlignedTablet(tablet, false); } void Session::insertAlignedTablet(Tablet& tablet, bool sorted) { - tablet.setAligned(true); - try { - insertTablet(tablet, sorted); - } - catch (const exception& e) { - log_debug(e.what()); - logic_error error(e.what()); - throw exception(error); - } + tablet.setAligned(true); + try { + insertTablet(tablet, sorted); + } catch (const exception& e) { + log_debug(e.what()); + logic_error error(e.what()); + throw exception(error); + } } void Session::insertTablets(unordered_map& tablets) { - try { - insertTablets(tablets, false); - } - catch (const exception& e) { - log_debug(e.what()); - logic_error error(e.what()); - throw exception(error); - } + try { + insertTablets(tablets, false); + } catch (const exception& e) { + log_debug(e.what()); + logic_error error(e.what()); + throw exception(error); + } } void Session::insertTablets(unordered_map& tablets, bool sorted) { - if (tablets.empty()) { - throw BatchExecutionException("No tablet is inserting!"); - } - auto beginIter = tablets.begin(); - bool isAligned = ((*beginIter).second)->isAligned; - if (enableRedirection_) { - insertTabletsWithLeaderCache(tablets, sorted, isAligned); - } - else { - TSInsertTabletsReq request; - for (const auto& item : tablets) { - if (isAligned != item.second->isAligned) { - throw BatchExecutionException("The tablets should be all aligned or non-aligned!"); - } - if (!checkSorted(*(item.second))) { - sortTablet(*(item.second)); - } - request.prefixPaths.push_back(item.second->deviceId); - vector measurements; - vector dataTypes; - for (pair schema : item.second->schemas) { - measurements.push_back(schema.first); - dataTypes.push_back(schema.second); - } - request.measurementsList.push_back(measurements); - request.typesList.push_back(dataTypes); - request.timestampsList.push_back(move(SessionUtils::getTime(*(item.second)))); - request.valuesList.push_back(move(SessionUtils::getValue(*(item.second)))); - request.sizeList.push_back(item.second->rowSize); - } - request.__set_isAligned(isAligned); - try { - TSStatus respStatus; - defaultSessionConnection_->insertTablets(request); - RpcUtils::verifySuccess(respStatus); - } - catch (RedirectException& e) { - } - } -} - - -void Session::insertAlignedTablets(unordered_map& tablets, bool sorted) { - for (auto iter = tablets.begin(); iter != tablets.end(); iter++) { - iter->second->setAligned(true); - } + if (tablets.empty()) { + throw BatchExecutionException("No tablet is inserting!"); + } + auto beginIter = tablets.begin(); + bool isAligned = ((*beginIter).second)->isAligned; + if (enableRedirection_) { + insertTabletsWithLeaderCache(tablets, sorted, isAligned); + } else { + TSInsertTabletsReq request; + for (const auto& item : tablets) { + if (isAligned != item.second->isAligned) { + throw BatchExecutionException("The tablets should be all aligned or non-aligned!"); + } + if (!checkSorted(*(item.second))) { + sortTablet(*(item.second)); + } + request.prefixPaths.push_back(item.second->deviceId); + vector measurements; + vector dataTypes; + for (pair schema : item.second->schemas) { + measurements.push_back(schema.first); + dataTypes.push_back(schema.second); + } + request.measurementsList.push_back(measurements); + request.typesList.push_back(dataTypes); + request.timestampsList.push_back(move(SessionUtils::getTime(*(item.second)))); + request.valuesList.push_back(move(SessionUtils::getValue(*(item.second)))); + request.sizeList.push_back(item.second->rowSize); + } + request.__set_isAligned(isAligned); try { - insertTablets(tablets, sorted); - } - catch (const exception& e) { - log_debug(e.what()); - logic_error error(e.what()); - throw exception(error); + TSStatus respStatus; + defaultSessionConnection_->insertTablets(request); + RpcUtils::verifySuccess(respStatus); + } catch (RedirectException& e) { } + } } -void Session::testInsertRecord(const string& deviceId, int64_t time, const vector& measurements, - const vector& values) { - TSInsertStringRecordReq req; - req.__set_prefixPath(deviceId); - req.__set_timestamp(time); - req.__set_measurements(measurements); - req.__set_values(values); - TSStatus tsStatus; - try { - defaultSessionConnection_->testInsertStringRecord(req); - RpcUtils::verifySuccess(tsStatus); - } - 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::insertAlignedTablets(unordered_map& tablets, bool sorted) { + for (auto iter = tablets.begin(); iter != tablets.end(); iter++) { + iter->second->setAligned(true); + } + try { + insertTablets(tablets, sorted); + } catch (const exception& e) { + log_debug(e.what()); + logic_error error(e.what()); + throw exception(error); + } +} + +void Session::testInsertRecord(const string& deviceId, int64_t time, + const vector& measurements, const vector& values) { + TSInsertStringRecordReq req; + req.__set_prefixPath(deviceId); + req.__set_timestamp(time); + req.__set_measurements(measurements); + req.__set_values(values); + TSStatus tsStatus; + try { + defaultSessionConnection_->testInsertStringRecord(req); + RpcUtils::verifySuccess(tsStatus); + } 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::testInsertTablet(const Tablet& tablet) { - TSInsertTabletReq request; - request.prefixPath = tablet.deviceId; - for (pair schema : tablet.schemas) { - request.measurements.push_back(schema.first); - request.types.push_back(schema.second); - } - request.__set_timestamps(move(SessionUtils::getTime(tablet))); - request.__set_values(move(SessionUtils::getValue(tablet))); - request.__set_size(tablet.rowSize); - try { - TSStatus tsStatus; - defaultSessionConnection_->testInsertTablet(request); - RpcUtils::verifySuccess(tsStatus); - } - 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::testInsertRecords(const vector& deviceIds, - const vector& times, + TSInsertTabletReq request; + request.prefixPath = tablet.deviceId; + for (pair schema : tablet.schemas) { + request.measurements.push_back(schema.first); + request.types.push_back(schema.second); + } + request.__set_timestamps(move(SessionUtils::getTime(tablet))); + request.__set_values(move(SessionUtils::getValue(tablet))); + request.__set_size(tablet.rowSize); + try { + TSStatus tsStatus; + defaultSessionConnection_->testInsertTablet(request); + RpcUtils::verifySuccess(tsStatus); + } 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::testInsertRecords(const vector& deviceIds, const vector& times, const vector>& measurementsList, const vector>& valuesList) { - size_t len = deviceIds.size(); - if (len != times.size() || len != measurementsList.size() || len != valuesList.size()) { - logic_error error("deviceIds, times, measurementsList and valuesList's size should be equal"); - throw exception(error); - } - TSInsertStringRecordsReq request; - request.__set_prefixPaths(deviceIds); - request.__set_timestamps(times); - request.__set_measurementsList(measurementsList); - request.__set_valuesList(valuesList); - - try { - TSStatus tsStatus; - defaultSessionConnection_->getSessionClient()->insertStringRecords(tsStatus, request); - RpcUtils::verifySuccess(tsStatus); - } - 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()); - } + size_t len = deviceIds.size(); + if (len != times.size() || len != measurementsList.size() || len != valuesList.size()) { + logic_error error("deviceIds, times, measurementsList and valuesList's size should be equal"); + throw exception(error); + } + TSInsertStringRecordsReq request; + request.__set_prefixPaths(deviceIds); + request.__set_timestamps(times); + request.__set_measurementsList(measurementsList); + request.__set_valuesList(valuesList); + + try { + TSStatus tsStatus; + defaultSessionConnection_->getSessionClient()->insertStringRecords(tsStatus, request); + RpcUtils::verifySuccess(tsStatus); + } 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::deleteTimeseries(const string& path) { - vector paths; - paths.push_back(path); - deleteTimeseries(paths); + vector paths; + paths.push_back(path); + deleteTimeseries(paths); } void Session::deleteTimeseries(const vector& paths) { - defaultSessionConnection_->deleteTimeseries(paths); + defaultSessionConnection_->deleteTimeseries(paths); } void Session::deleteData(const string& path, int64_t endTime) { - vector paths; - paths.push_back(path); - deleteData(paths, LONG_LONG_MIN, endTime); + vector paths; + paths.push_back(path); + deleteData(paths, LONG_LONG_MIN, endTime); } void Session::deleteData(const vector& paths, int64_t endTime) { - deleteData(paths, LONG_LONG_MIN, endTime); + deleteData(paths, LONG_LONG_MIN, endTime); } void Session::deleteData(const vector& paths, int64_t startTime, int64_t endTime) { - TSDeleteDataReq req; - req.__set_paths(paths); - req.__set_startTime(startTime); - req.__set_endTime(endTime); - defaultSessionConnection_->deleteData(req); + TSDeleteDataReq req; + req.__set_paths(paths); + req.__set_startTime(startTime); + req.__set_endTime(endTime); + defaultSessionConnection_->deleteData(req); } void Session::setStorageGroup(const string& storageGroupId) { - defaultSessionConnection_->setStorageGroup(storageGroupId); + defaultSessionConnection_->setStorageGroup(storageGroupId); } void Session::deleteStorageGroup(const string& storageGroup) { - vector storageGroups; - storageGroups.push_back(storageGroup); - deleteStorageGroups(storageGroups); + vector storageGroups; + storageGroups.push_back(storageGroup); + deleteStorageGroups(storageGroups); } void Session::deleteStorageGroups(const vector& storageGroups) { - defaultSessionConnection_->deleteStorageGroups(storageGroups); + defaultSessionConnection_->deleteStorageGroups(storageGroups); } void Session::createDatabase(const string& database) { - this->setStorageGroup(database); + this->setStorageGroup(database); } void Session::deleteDatabase(const string& database) { - this->deleteStorageGroups(vector{database}); + this->deleteStorageGroups(vector{database}); } void Session::deleteDatabases(const vector& databases) { - this->deleteStorageGroups(databases); + this->deleteStorageGroups(databases); } -void Session::createTimeseries(const string& path, - TSDataType::TSDataType dataType, +void Session::createTimeseries(const string& path, TSDataType::TSDataType dataType, TSEncoding::TSEncoding encoding, CompressionType::CompressionType compressor) { - try { - createTimeseries(path, dataType, encoding, compressor, nullptr, nullptr, nullptr, ""); - } - catch (const exception& e) { - log_debug(e.what()); - throw IoTDBException(e.what()); - } + try { + createTimeseries(path, dataType, encoding, compressor, nullptr, nullptr, nullptr, ""); + } catch (const exception& e) { + log_debug(e.what()); + throw IoTDBException(e.what()); + } } -void Session::createTimeseries(const string& path, - TSDataType::TSDataType dataType, +void Session::createTimeseries(const string& path, TSDataType::TSDataType dataType, TSEncoding::TSEncoding encoding, CompressionType::CompressionType compressor, - map* props, - map* tags, - map* attributes, - const string& measurementAlias) { - TSCreateTimeseriesReq req; - req.__set_path(path); - req.__set_dataType(dataType); - req.__set_encoding(encoding); - req.__set_compressor(compressor); - if (props != nullptr) { - req.__set_props(*props); - } - - if (tags != nullptr) { - req.__set_tags(*tags); - } - if (attributes != nullptr) { - req.__set_attributes(*attributes); - } - if (!measurementAlias.empty()) { - req.__set_measurementAlias(measurementAlias); - } - defaultSessionConnection_->createTimeseries(req); + map* props, map* tags, + map* attributes, const string& measurementAlias) { + TSCreateTimeseriesReq req; + req.__set_path(path); + req.__set_dataType(dataType); + req.__set_encoding(encoding); + req.__set_compressor(compressor); + if (props != nullptr) { + req.__set_props(*props); + } + + if (tags != nullptr) { + req.__set_tags(*tags); + } + if (attributes != nullptr) { + req.__set_attributes(*attributes); + } + if (!measurementAlias.empty()) { + req.__set_measurementAlias(measurementAlias); + } + defaultSessionConnection_->createTimeseries(req); } void Session::createMultiTimeseries(const vector& paths, @@ -1851,444 +1776,453 @@ void Session::createMultiTimeseries(const vector& paths, vector>* tagsList, vector>* attributesList, vector* measurementAliasList) { - TSCreateMultiTimeseriesReq request; - request.__set_paths(paths); - - vector dataTypesOrdinal; - dataTypesOrdinal.reserve(dataTypes.size()); - for (TSDataType::TSDataType dataType : dataTypes) { - dataTypesOrdinal.push_back(dataType); - } - request.__set_dataTypes(dataTypesOrdinal); - - vector encodingsOrdinal; - encodingsOrdinal.reserve(encodings.size()); - for (TSEncoding::TSEncoding encoding : encodings) { - encodingsOrdinal.push_back(encoding); - } - request.__set_encodings(encodingsOrdinal); - - vector compressorsOrdinal; - compressorsOrdinal.reserve(compressors.size()); - for (CompressionType::CompressionType compressor : compressors) { - compressorsOrdinal.push_back(compressor); - } - request.__set_compressors(compressorsOrdinal); - - if (propsList != nullptr) { - request.__set_propsList(*propsList); - } - - if (tagsList != nullptr) { - request.__set_tagsList(*tagsList); - } - if (attributesList != nullptr) { - request.__set_attributesList(*attributesList); - } - if (measurementAliasList != nullptr) { - request.__set_measurementAliasList(*measurementAliasList); - } - - defaultSessionConnection_->createMultiTimeseries(request); -} - -void Session::createAlignedTimeseries(const std::string& deviceId, - const std::vector& measurements, - const std::vector& dataTypes, - const std::vector& encodings, - const std::vector& compressors) { - TSCreateAlignedTimeseriesReq request; - request.__set_prefixPath(deviceId); - request.__set_measurements(measurements); - - vector dataTypesOrdinal; - dataTypesOrdinal.reserve(dataTypes.size()); - for (TSDataType::TSDataType dataType : dataTypes) { - dataTypesOrdinal.push_back(dataType); - } - request.__set_dataTypes(dataTypesOrdinal); - - vector encodingsOrdinal; - encodingsOrdinal.reserve(encodings.size()); - for (TSEncoding::TSEncoding encoding : encodings) { - encodingsOrdinal.push_back(encoding); - } - request.__set_encodings(encodingsOrdinal); - - vector compressorsOrdinal; - compressorsOrdinal.reserve(compressors.size()); - for (CompressionType::CompressionType compressor : compressors) { - compressorsOrdinal.push_back(compressor); - } - request.__set_compressors(compressorsOrdinal); - - defaultSessionConnection_->createAlignedTimeseries(request); + TSCreateMultiTimeseriesReq request; + request.__set_paths(paths); + + vector dataTypesOrdinal; + dataTypesOrdinal.reserve(dataTypes.size()); + for (TSDataType::TSDataType dataType : dataTypes) { + dataTypesOrdinal.push_back(dataType); + } + request.__set_dataTypes(dataTypesOrdinal); + + vector encodingsOrdinal; + encodingsOrdinal.reserve(encodings.size()); + for (TSEncoding::TSEncoding encoding : encodings) { + encodingsOrdinal.push_back(encoding); + } + request.__set_encodings(encodingsOrdinal); + + vector compressorsOrdinal; + compressorsOrdinal.reserve(compressors.size()); + for (CompressionType::CompressionType compressor : compressors) { + compressorsOrdinal.push_back(compressor); + } + request.__set_compressors(compressorsOrdinal); + + if (propsList != nullptr) { + request.__set_propsList(*propsList); + } + + if (tagsList != nullptr) { + request.__set_tagsList(*tagsList); + } + if (attributesList != nullptr) { + request.__set_attributesList(*attributesList); + } + if (measurementAliasList != nullptr) { + request.__set_measurementAliasList(*measurementAliasList); + } + + defaultSessionConnection_->createMultiTimeseries(request); +} + +void Session::createAlignedTimeseries( + const std::string& deviceId, const std::vector& measurements, + const std::vector& dataTypes, + const std::vector& encodings, + const std::vector& compressors) { + TSCreateAlignedTimeseriesReq request; + request.__set_prefixPath(deviceId); + request.__set_measurements(measurements); + + vector dataTypesOrdinal; + dataTypesOrdinal.reserve(dataTypes.size()); + for (TSDataType::TSDataType dataType : dataTypes) { + dataTypesOrdinal.push_back(dataType); + } + request.__set_dataTypes(dataTypesOrdinal); + + vector encodingsOrdinal; + encodingsOrdinal.reserve(encodings.size()); + for (TSEncoding::TSEncoding encoding : encodings) { + encodingsOrdinal.push_back(encoding); + } + request.__set_encodings(encodingsOrdinal); + + vector compressorsOrdinal; + compressorsOrdinal.reserve(compressors.size()); + for (CompressionType::CompressionType compressor : compressors) { + compressorsOrdinal.push_back(compressor); + } + request.__set_compressors(compressorsOrdinal); + + defaultSessionConnection_->createAlignedTimeseries(request); } bool Session::checkTimeseriesExists(const string& path) { - try { - std::unique_ptr dataset = executeQueryStatement("SHOW TIMESERIES " + path); - if (dataset == nullptr) { - throw IoTDBException("executeQueryStatement failed"); - } - bool isExisted = dataset->hasNext(); - dataset->closeOperationHandle(); - return isExisted; - } - catch (const exception& e) { - log_debug(e.what()); - throw IoTDBException(e.what()); + try { + std::unique_ptr dataset = executeQueryStatement("SHOW TIMESERIES " + path); + if (dataset == nullptr) { + throw IoTDBException("executeQueryStatement failed"); } + bool isExisted = dataset->hasNext(); + dataset->closeOperationHandle(); + return isExisted; + } catch (const exception& e) { + log_debug(e.what()); + throw IoTDBException(e.what()); + } } shared_ptr Session::getQuerySessionConnection() { - auto endPoint = nodesSupplier_->getQueryEndPoint(); - if (!endPoint.is_initialized() || endPointToSessionConnection.empty()) { - return defaultSessionConnection_; - } - - auto it = endPointToSessionConnection.find(endPoint.value()); - if (it != endPointToSessionConnection.end()) { - return it->second; - } - - shared_ptr newConnection; - try { - newConnection = make_shared(this, endPoint.value(), zoneId_, nodesSupplier_, - fetchSize_, 60, 500, connectTimeoutMs_, sqlDialect_, database_); - endPointToSessionConnection.emplace(endPoint.value(), newConnection); - return newConnection; - } - catch (exception& e) { - log_debug("Session::getQuerySessionConnection() exception: " + e.what()); - return newConnection; - } + auto endPoint = nodesSupplier_->getQueryEndPoint(); + if (!endPoint.is_initialized() || endPointToSessionConnection.empty()) { + return defaultSessionConnection_; + } + + auto it = endPointToSessionConnection.find(endPoint.value()); + if (it != endPointToSessionConnection.end()) { + return it->second; + } + + shared_ptr newConnection; + try { + newConnection = + make_shared(this, endPoint.value(), zoneId_, nodesSupplier_, fetchSize_, + 60, 500, connectTimeoutMs_, sqlDialect_, database_); + endPointToSessionConnection.emplace(endPoint.value(), newConnection); + return newConnection; + } catch (exception& e) { + log_debug("Session::getQuerySessionConnection() exception: " + e.what()); + return newConnection; + } } shared_ptr Session::getSessionConnection(std::string deviceId) { - if (!enableRedirection_ || - deviceIdToEndpoint.find(deviceId) == deviceIdToEndpoint.end() || - endPointToSessionConnection.find(deviceIdToEndpoint[deviceId]) == endPointToSessionConnection.end()) { - return defaultSessionConnection_; - } - return endPointToSessionConnection.find(deviceIdToEndpoint[deviceId])->second; + if (!enableRedirection_ || deviceIdToEndpoint.find(deviceId) == deviceIdToEndpoint.end() || + endPointToSessionConnection.find(deviceIdToEndpoint[deviceId]) == + endPointToSessionConnection.end()) { + return defaultSessionConnection_; + } + return endPointToSessionConnection.find(deviceIdToEndpoint[deviceId])->second; } -shared_ptr Session::getSessionConnection(std::shared_ptr deviceId) { - if (!enableRedirection_ || - tableModelDeviceIdToEndpoint.find(deviceId) == tableModelDeviceIdToEndpoint.end() || - endPointToSessionConnection.find(tableModelDeviceIdToEndpoint[deviceId]) == endPointToSessionConnection.end()) { - return defaultSessionConnection_; - } - return endPointToSessionConnection.find(tableModelDeviceIdToEndpoint[deviceId])->second; +shared_ptr +Session::getSessionConnection(std::shared_ptr deviceId) { + if (!enableRedirection_ || + tableModelDeviceIdToEndpoint.find(deviceId) == tableModelDeviceIdToEndpoint.end() || + endPointToSessionConnection.find(tableModelDeviceIdToEndpoint[deviceId]) == + endPointToSessionConnection.end()) { + return defaultSessionConnection_; + } + return endPointToSessionConnection.find(tableModelDeviceIdToEndpoint[deviceId])->second; } string Session::getTimeZone() { - auto ret = defaultSessionConnection_->getTimeZone(); - return ret.timeZone; + auto ret = defaultSessionConnection_->getTimeZone(); + return ret.timeZone; } void Session::setTimeZone(const string& zoneId) { - TSSetTimeZoneReq req; - req.__set_sessionId(defaultSessionConnection_->sessionId); - req.__set_timeZone(zoneId); - defaultSessionConnection_->setTimeZone(req); + TSSetTimeZoneReq req; + req.__set_sessionId(defaultSessionConnection_->sessionId); + req.__set_timeZone(zoneId); + defaultSessionConnection_->setTimeZone(req); } unique_ptr Session::executeQueryStatement(const string& sql) { - return executeQueryStatementMayRedirect(sql, QUERY_TIMEOUT_MS); + return executeQueryStatementMayRedirect(sql, QUERY_TIMEOUT_MS); } unique_ptr Session::executeQueryStatement(const string& sql, int64_t timeoutInMs) { - return executeQueryStatementMayRedirect(sql, timeoutInMs); + return executeQueryStatementMayRedirect(sql, timeoutInMs); } void Session::handleQueryRedirection(TEndPoint endPoint) { - if (!enableRedirection_) return; - shared_ptr newConnection; - auto it = endPointToSessionConnection.find(endPoint); - if (it != endPointToSessionConnection.end()) { - newConnection = it->second; - } - else { - try { - newConnection = make_shared(this, endPoint, zoneId_, nodesSupplier_, - fetchSize_, 60, 500, connectTimeoutMs_, sqlDialect_, database_); - - endPointToSessionConnection.emplace(endPoint, newConnection); - } - catch (exception& e) { - throw IoTDBConnectionException(e.what()); - } + if (!enableRedirection_) + return; + shared_ptr newConnection; + auto it = endPointToSessionConnection.find(endPoint); + if (it != endPointToSessionConnection.end()) { + newConnection = it->second; + } else { + try { + newConnection = + make_shared(this, endPoint, zoneId_, nodesSupplier_, fetchSize_, 60, + 500, connectTimeoutMs_, sqlDialect_, database_); + + endPointToSessionConnection.emplace(endPoint, newConnection); + } catch (exception& e) { + throw IoTDBConnectionException(e.what()); } - defaultSessionConnection_ = newConnection; + } + defaultSessionConnection_ = newConnection; } void Session::handleRedirection(const std::string& deviceId, TEndPoint endPoint) { - if (!enableRedirection_) return; - if (endPoint.ip == "127.0.0.1") return; - deviceIdToEndpoint[deviceId] = endPoint; - - shared_ptr newConnection; - auto it = endPointToSessionConnection.find(endPoint); - if (it != endPointToSessionConnection.end()) { - newConnection = it->second; - } - else { - try { - newConnection = make_shared(this, endPoint, zoneId_, nodesSupplier_, - fetchSize_, 60, 500, 1000, sqlDialect_, database_); - endPointToSessionConnection.emplace(endPoint, newConnection); - } - catch (exception& e) { - deviceIdToEndpoint.erase(deviceId); - throw IoTDBConnectionException(e.what()); - } + if (!enableRedirection_) + return; + if (endPoint.ip == "127.0.0.1") + return; + deviceIdToEndpoint[deviceId] = endPoint; + + shared_ptr newConnection; + auto it = endPointToSessionConnection.find(endPoint); + if (it != endPointToSessionConnection.end()) { + newConnection = it->second; + } else { + try { + newConnection = + make_shared(this, endPoint, zoneId_, nodesSupplier_, fetchSize_, 60, + 500, 1000, sqlDialect_, database_); + endPointToSessionConnection.emplace(endPoint, newConnection); + } catch (exception& e) { + deviceIdToEndpoint.erase(deviceId); + throw IoTDBConnectionException(e.what()); } + } } -void Session::handleRedirection(const std::shared_ptr& deviceId, TEndPoint endPoint) { - if (!enableRedirection_) return; - if (endPoint.ip == "127.0.0.1") return; - tableModelDeviceIdToEndpoint[deviceId] = endPoint; - - shared_ptr newConnection; - auto it = endPointToSessionConnection.find(endPoint); - if (it != endPointToSessionConnection.end()) { - newConnection = it->second; - } - else { - try { - newConnection = make_shared(this, endPoint, zoneId_, nodesSupplier_, - fetchSize_, 3, 500, connectTimeoutMs_, sqlDialect_, database_); - endPointToSessionConnection.emplace(endPoint, newConnection); - } - catch (exception& e) { - tableModelDeviceIdToEndpoint.erase(deviceId); - throw IoTDBConnectionException(e.what()); - } - } -} +void Session::handleRedirection(const std::shared_ptr& deviceId, + TEndPoint endPoint) { + if (!enableRedirection_) + return; + if (endPoint.ip == "127.0.0.1") + return; + tableModelDeviceIdToEndpoint[deviceId] = endPoint; -std::unique_ptr Session::executeQueryStatementMayRedirect(const std::string& sql, int64_t timeoutInMs) { - auto sessionConnection = getQuerySessionConnection(); - if (!sessionConnection) { - log_warn("Session connection not found"); - return nullptr; - } + shared_ptr newConnection; + auto it = endPointToSessionConnection.find(endPoint); + if (it != endPointToSessionConnection.end()) { + newConnection = it->second; + } else { try { - return sessionConnection->executeQueryStatement(sql, timeoutInMs); - } - catch (RedirectException& e) { - log_warn("Session connection redirect exception: " + e.what()); - handleQueryRedirection(e.endPoint); - try { - return defaultSessionConnection_->executeQueryStatement(sql, timeoutInMs); - } - catch (exception& e) { - log_error("Exception while executing redirected query statement: %s", e.what()); - throw ExecutionException(e.what()); - } + newConnection = + make_shared(this, endPoint, zoneId_, nodesSupplier_, fetchSize_, 3, + 500, connectTimeoutMs_, sqlDialect_, database_); + endPointToSessionConnection.emplace(endPoint, newConnection); + } catch (exception& e) { + tableModelDeviceIdToEndpoint.erase(deviceId); + throw IoTDBConnectionException(e.what()); + } + } +} + +std::unique_ptr Session::executeQueryStatementMayRedirect(const std::string& sql, + int64_t timeoutInMs) { + auto sessionConnection = getQuerySessionConnection(); + if (!sessionConnection) { + log_warn("Session connection not found"); + return nullptr; + } + try { + return sessionConnection->executeQueryStatement(sql, timeoutInMs); + } catch (RedirectException& e) { + log_warn("Session connection redirect exception: " + e.what()); + handleQueryRedirection(e.endPoint); + try { + return defaultSessionConnection_->executeQueryStatement(sql, timeoutInMs); } catch (exception& e) { - log_error("Exception while executing query statement: %s", e.what()); - throw e; + log_error("Exception while executing redirected query statement: %s", e.what()); + throw ExecutionException(e.what()); } + } catch (exception& e) { + log_error("Exception while executing query statement: %s", e.what()); + throw e; + } } void Session::executeNonQueryStatement(const string& sql) { - try { - defaultSessionConnection_->executeNonQueryStatement(sql); - } - catch (const exception& e) { - throw IoTDBException(e.what()); - } + try { + defaultSessionConnection_->executeNonQueryStatement(sql); + } catch (const exception& e) { + throw IoTDBException(e.what()); + } } -unique_ptr -Session::executeRawDataQuery(const vector& paths, int64_t startTime, int64_t endTime) { - return defaultSessionConnection_->executeRawDataQuery(paths, startTime, endTime); +unique_ptr Session::executeRawDataQuery(const vector& paths, + int64_t startTime, int64_t endTime) { + return defaultSessionConnection_->executeRawDataQuery(paths, startTime, endTime); } - unique_ptr Session::executeLastDataQuery(const vector& paths) { - return executeLastDataQuery(paths, LONG_LONG_MIN); + return executeLastDataQuery(paths, LONG_LONG_MIN); } -unique_ptr Session::executeLastDataQuery(const vector& paths, int64_t lastTime) { - return defaultSessionConnection_->executeLastDataQuery(paths, lastTime); +unique_ptr Session::executeLastDataQuery(const vector& paths, + int64_t lastTime) { + return defaultSessionConnection_->executeLastDataQuery(paths, lastTime); } void Session::createSchemaTemplate(const Template& templ) { - TSCreateSchemaTemplateReq req; - req.__set_name(templ.getName()); - req.__set_serializedTemplate(templ.serialize()); - defaultSessionConnection_->createSchemaTemplate(req); + TSCreateSchemaTemplateReq req; + req.__set_name(templ.getName()); + req.__set_serializedTemplate(templ.serialize()); + defaultSessionConnection_->createSchemaTemplate(req); } void Session::setSchemaTemplate(const string& template_name, const string& prefix_path) { - TSSetSchemaTemplateReq req; - req.__set_templateName(template_name); - req.__set_prefixPath(prefix_path); - defaultSessionConnection_->setSchemaTemplate(req); + TSSetSchemaTemplateReq req; + req.__set_templateName(template_name); + req.__set_prefixPath(prefix_path); + defaultSessionConnection_->setSchemaTemplate(req); } void Session::unsetSchemaTemplate(const string& prefix_path, const string& template_name) { - TSUnsetSchemaTemplateReq req; - req.__set_templateName(template_name); - req.__set_prefixPath(prefix_path); - defaultSessionConnection_->unsetSchemaTemplate(req); -} - -void Session::addAlignedMeasurementsInTemplate(const string& template_name, const vector& measurements, - const vector& dataTypes, - const vector& encodings, - const vector& compressors) { - TSAppendSchemaTemplateReq req; - req.__set_name(template_name); - req.__set_measurements(measurements); - req.__set_isAligned(true); - - vector dataTypesOrdinal; - dataTypesOrdinal.reserve(dataTypes.size()); - for (TSDataType::TSDataType dataType : dataTypes) { - dataTypesOrdinal.push_back(dataType); - } - req.__set_dataTypes(dataTypesOrdinal); - - vector encodingsOrdinal; - encodingsOrdinal.reserve(encodings.size()); - for (TSEncoding::TSEncoding encoding : encodings) { - encodingsOrdinal.push_back(encoding); - } - req.__set_encodings(encodingsOrdinal); - - vector compressorsOrdinal; - compressorsOrdinal.reserve(compressors.size()); - for (CompressionType::CompressionType compressor : compressors) { - compressorsOrdinal.push_back(compressor); - } - req.__set_compressors(compressorsOrdinal); - - defaultSessionConnection_->appendSchemaTemplate(req); -} - -void Session::addAlignedMeasurementsInTemplate(const string& template_name, const string& measurement, - TSDataType::TSDataType dataType, TSEncoding::TSEncoding encoding, + TSUnsetSchemaTemplateReq req; + req.__set_templateName(template_name); + req.__set_prefixPath(prefix_path); + defaultSessionConnection_->unsetSchemaTemplate(req); +} + +void Session::addAlignedMeasurementsInTemplate( + const string& template_name, const vector& measurements, + const vector& dataTypes, + const vector& encodings, + const vector& compressors) { + TSAppendSchemaTemplateReq req; + req.__set_name(template_name); + req.__set_measurements(measurements); + req.__set_isAligned(true); + + vector dataTypesOrdinal; + dataTypesOrdinal.reserve(dataTypes.size()); + for (TSDataType::TSDataType dataType : dataTypes) { + dataTypesOrdinal.push_back(dataType); + } + req.__set_dataTypes(dataTypesOrdinal); + + vector encodingsOrdinal; + encodingsOrdinal.reserve(encodings.size()); + for (TSEncoding::TSEncoding encoding : encodings) { + encodingsOrdinal.push_back(encoding); + } + req.__set_encodings(encodingsOrdinal); + + vector compressorsOrdinal; + compressorsOrdinal.reserve(compressors.size()); + for (CompressionType::CompressionType compressor : compressors) { + compressorsOrdinal.push_back(compressor); + } + req.__set_compressors(compressorsOrdinal); + + defaultSessionConnection_->appendSchemaTemplate(req); +} + +void Session::addAlignedMeasurementsInTemplate(const string& template_name, + const string& measurement, + TSDataType::TSDataType dataType, + TSEncoding::TSEncoding encoding, CompressionType::CompressionType compressor) { - vector measurements(1, measurement); - vector dataTypes(1, dataType); - vector encodings(1, encoding); - vector compressors(1, compressor); - addAlignedMeasurementsInTemplate(template_name, measurements, dataTypes, encodings, compressors); -} - -void Session::addUnalignedMeasurementsInTemplate(const string& template_name, const vector& measurements, - const vector& dataTypes, - const vector& encodings, - const vector& compressors) { - TSAppendSchemaTemplateReq req; - req.__set_name(template_name); - req.__set_measurements(measurements); - req.__set_isAligned(false); - - vector dataTypesOrdinal; - dataTypesOrdinal.reserve(dataTypes.size()); - for (TSDataType::TSDataType dataType : dataTypes) { - dataTypesOrdinal.push_back(dataType); - } - req.__set_dataTypes(dataTypesOrdinal); - - vector encodingsOrdinal; - encodingsOrdinal.reserve(encodings.size()); - for (TSEncoding::TSEncoding encoding : encodings) { - encodingsOrdinal.push_back(encoding); - } - req.__set_encodings(encodingsOrdinal); - - vector compressorsOrdinal; - compressorsOrdinal.reserve(compressors.size()); - for (CompressionType::CompressionType compressor : compressors) { - compressorsOrdinal.push_back(compressor); - } - req.__set_compressors(compressorsOrdinal); - - defaultSessionConnection_->appendSchemaTemplate(req); -} - -void Session::addUnalignedMeasurementsInTemplate(const string& template_name, const string& measurement, - TSDataType::TSDataType dataType, TSEncoding::TSEncoding encoding, + vector measurements(1, measurement); + vector dataTypes(1, dataType); + vector encodings(1, encoding); + vector compressors(1, compressor); + addAlignedMeasurementsInTemplate(template_name, measurements, dataTypes, encodings, compressors); +} + +void Session::addUnalignedMeasurementsInTemplate( + const string& template_name, const vector& measurements, + const vector& dataTypes, + const vector& encodings, + const vector& compressors) { + TSAppendSchemaTemplateReq req; + req.__set_name(template_name); + req.__set_measurements(measurements); + req.__set_isAligned(false); + + vector dataTypesOrdinal; + dataTypesOrdinal.reserve(dataTypes.size()); + for (TSDataType::TSDataType dataType : dataTypes) { + dataTypesOrdinal.push_back(dataType); + } + req.__set_dataTypes(dataTypesOrdinal); + + vector encodingsOrdinal; + encodingsOrdinal.reserve(encodings.size()); + for (TSEncoding::TSEncoding encoding : encodings) { + encodingsOrdinal.push_back(encoding); + } + req.__set_encodings(encodingsOrdinal); + + vector compressorsOrdinal; + compressorsOrdinal.reserve(compressors.size()); + for (CompressionType::CompressionType compressor : compressors) { + compressorsOrdinal.push_back(compressor); + } + req.__set_compressors(compressorsOrdinal); + + defaultSessionConnection_->appendSchemaTemplate(req); +} + +void Session::addUnalignedMeasurementsInTemplate(const string& template_name, + const string& measurement, + TSDataType::TSDataType dataType, + TSEncoding::TSEncoding encoding, CompressionType::CompressionType compressor) { - vector measurements(1, measurement); - vector dataTypes(1, dataType); - vector encodings(1, encoding); - vector compressors(1, compressor); - addUnalignedMeasurementsInTemplate(template_name, measurements, dataTypes, encodings, compressors); + vector measurements(1, measurement); + vector dataTypes(1, dataType); + vector encodings(1, encoding); + vector compressors(1, compressor); + addUnalignedMeasurementsInTemplate(template_name, measurements, dataTypes, encodings, + compressors); } void Session::deleteNodeInTemplate(const string& template_name, const string& path) { - TSPruneSchemaTemplateReq req; - req.__set_name(template_name); - req.__set_path(path); - defaultSessionConnection_->pruneSchemaTemplate(req); + TSPruneSchemaTemplateReq req; + req.__set_name(template_name); + req.__set_path(path); + defaultSessionConnection_->pruneSchemaTemplate(req); } int Session::countMeasurementsInTemplate(const string& template_name) { - TSQueryTemplateReq req; - req.__set_name(template_name); - req.__set_queryType(TemplateQueryType::COUNT_MEASUREMENTS); - TSQueryTemplateResp resp = defaultSessionConnection_->querySchemaTemplate(req); - return resp.count; + TSQueryTemplateReq req; + req.__set_name(template_name); + req.__set_queryType(TemplateQueryType::COUNT_MEASUREMENTS); + TSQueryTemplateResp resp = defaultSessionConnection_->querySchemaTemplate(req); + return resp.count; } bool Session::isMeasurementInTemplate(const string& template_name, const string& path) { - TSQueryTemplateReq req; - req.__set_name(template_name); - req.__set_measurement(path); - req.__set_queryType(TemplateQueryType::IS_MEASUREMENT); - TSQueryTemplateResp resp = defaultSessionConnection_->querySchemaTemplate(req); - return resp.result; + TSQueryTemplateReq req; + req.__set_name(template_name); + req.__set_measurement(path); + req.__set_queryType(TemplateQueryType::IS_MEASUREMENT); + TSQueryTemplateResp resp = defaultSessionConnection_->querySchemaTemplate(req); + return resp.result; } bool Session::isPathExistInTemplate(const string& template_name, const string& path) { - TSQueryTemplateReq req; - req.__set_name(template_name); - req.__set_measurement(path); - req.__set_queryType(TemplateQueryType::PATH_EXIST); - TSQueryTemplateResp resp = defaultSessionConnection_->querySchemaTemplate(req); - return resp.result; + TSQueryTemplateReq req; + req.__set_name(template_name); + req.__set_measurement(path); + req.__set_queryType(TemplateQueryType::PATH_EXIST); + TSQueryTemplateResp resp = defaultSessionConnection_->querySchemaTemplate(req); + return resp.result; } std::vector Session::showMeasurementsInTemplate(const string& template_name) { - TSQueryTemplateReq req; - req.__set_name(template_name); - req.__set_measurement(""); - req.__set_queryType(TemplateQueryType::SHOW_MEASUREMENTS); - TSQueryTemplateResp resp = defaultSessionConnection_->querySchemaTemplate(req); - return resp.measurements; + TSQueryTemplateReq req; + req.__set_name(template_name); + req.__set_measurement(""); + req.__set_queryType(TemplateQueryType::SHOW_MEASUREMENTS); + TSQueryTemplateResp resp = defaultSessionConnection_->querySchemaTemplate(req); + return resp.measurements; } -std::vector Session::showMeasurementsInTemplate(const string& template_name, const string& pattern) { - TSQueryTemplateReq req; - req.__set_name(template_name); - req.__set_measurement(pattern); - req.__set_queryType(TemplateQueryType::SHOW_MEASUREMENTS); - TSQueryTemplateResp resp = defaultSessionConnection_->querySchemaTemplate(req); - return resp.measurements; +std::vector Session::showMeasurementsInTemplate(const string& template_name, + const string& pattern) { + TSQueryTemplateReq req; + req.__set_name(template_name); + req.__set_measurement(pattern); + req.__set_queryType(TemplateQueryType::SHOW_MEASUREMENTS); + TSQueryTemplateResp resp = defaultSessionConnection_->querySchemaTemplate(req); + return resp.measurements; } bool Session::checkTemplateExists(const string& template_name) { - try { - std::unique_ptr dataset = executeQueryStatement( - "SHOW NODES IN DEVICE TEMPLATE " + template_name); - bool isExisted = dataset->hasNext(); - dataset->closeOperationHandle(); - return isExisted; - } - catch (const exception& e) { - if (strstr(e.what(), "does not exist") != NULL) { - return false; - } - log_debug(e.what()); - throw IoTDBException(e.what()); - } + try { + std::unique_ptr dataset = + executeQueryStatement("SHOW NODES IN DEVICE TEMPLATE " + template_name); + bool isExisted = dataset->hasNext(); + dataset->closeOperationHandle(); + return isExisted; + } catch (const exception& e) { + if (strstr(e.what(), "does not exist") != NULL) { + return false; + } + log_debug(e.what()); + throw IoTDBException(e.what()); + } } diff --git a/iotdb-client/client-cpp/src/main/Session.h b/iotdb-client/client-cpp/src/main/Session.h index 15d0afe4d5f67..30ba60d6cad7c 100644 --- a/iotdb-client/client-cpp/src/main/Session.h +++ b/iotdb-client/client-cpp/src/main/Session.h @@ -53,19 +53,17 @@ using namespace std; +using ::apache::thrift::TException; using ::apache::thrift::protocol::TBinaryProtocol; using ::apache::thrift::protocol::TCompactProtocol; +using ::apache::thrift::transport::TBufferedTransport; +using ::apache::thrift::transport::TFramedTransport; using ::apache::thrift::transport::TSocket; using ::apache::thrift::transport::TTransport; using ::apache::thrift::transport::TTransportException; -using ::apache::thrift::transport::TBufferedTransport; -using ::apache::thrift::transport::TFramedTransport; -using ::apache::thrift::TException; - -template -void safe_cast(const T& value, Target& target) { - /* +template void safe_cast(const T& value, Target& target) { + /* Target Allowed Source Types BOOLEAN BOOLEAN INT32 INT32 @@ -74,39 +72,32 @@ void safe_cast(const T& value, Target& target) { DOUBLE INT32 INT64 FLOAT DOUBLE TEXT TEXT */ - if (std::is_same::value) { - target = *(Target*)&value; - } - else if (std::is_same::value && std::is_array::value && std::is_same< - char, typename std::remove_extent::type>::value) { - string tmp((const char*)&value); - target = *(Target*)&tmp; - } - else if (std::is_same::value && std::is_same::value) { - int64_t tmp = *(int32_t*)&value; - target = *(Target*)&tmp; - } - else if (std::is_same::value && std::is_same::value) { - float tmp = *(int32_t*)&value; - target = *(Target*)&tmp; - } - else if (std::is_same::value && std::is_same::value) { - double tmp = *(int32_t*)&value; - target = *(Target*)&tmp; - } - else if (std::is_same::value && std::is_same::value) { - double tmp = *(int64_t*)&value; - target = *(Target*)&tmp; - } - else if (std::is_same::value && std::is_same::value) { - double tmp = *(float*)&value; - target = *(Target*)&tmp; - } - else { - throw UnSupportedDataTypeException("Error: Parameter type " + - std::string(typeid(T).name()) + " cannot be converted to DataType" + - std::string(typeid(Target).name())); - } + if (std::is_same::value) { + target = *(Target*)&value; + } else if (std::is_same::value && std::is_array::value && + std::is_same::type>::value) { + string tmp((const char*)&value); + target = *(Target*)&tmp; + } else if (std::is_same::value && std::is_same::value) { + int64_t tmp = *(int32_t*)&value; + target = *(Target*)&tmp; + } else if (std::is_same::value && std::is_same::value) { + float tmp = *(int32_t*)&value; + target = *(Target*)&tmp; + } else if (std::is_same::value && std::is_same::value) { + double tmp = *(int32_t*)&value; + target = *(Target*)&tmp; + } else if (std::is_same::value && std::is_same::value) { + double tmp = *(int64_t*)&value; + target = *(Target*)&tmp; + } else if (std::is_same::value && std::is_same::value) { + double tmp = *(float*)&value; + target = *(Target*)&tmp; + } else { + throw UnSupportedDataTypeException("Error: Parameter type " + std::string(typeid(T).name()) + + " cannot be converted to DataType" + + std::string(typeid(Target).name())); + } } /* @@ -125,46 +116,46 @@ void safe_cast(const T& value, Target& target) { */ class Tablet { private: - static const int DEFAULT_ROW_SIZE = 1024; + static const int DEFAULT_ROW_SIZE = 1024; - void createColumns(); - void deleteColumns(); + void createColumns(); + void deleteColumns(); public: - std::string deviceId; // deviceId of this tablet - std::vector> schemas; - // the list of measurement schemas for creating the tablet - std::map schemaNameIndex; // the map of schema name to index - std::vector columnTypes; // the list of column types (used in table model) - std::vector timestamps; // timestamps in this tablet - std::vector values; // each object is a primitive type array, which represents values of one measurement - std::vector bitMaps; // each bitmap represents the existence of each value in the current column - size_t rowSize; //the number of rows to include in this tablet - size_t maxRowNumber; // the maximum number of rows for this tablet - bool isAligned; // whether this tablet store data of aligned timeseries or not - std::vector idColumnIndexes; - - Tablet() = default; - - /** + std::string deviceId; // deviceId of this tablet + std::vector> schemas; + // the list of measurement schemas for creating the tablet + std::map schemaNameIndex; // the map of schema name to index + std::vector columnTypes; // the list of column types (used in table model) + std::vector timestamps; // timestamps in this tablet + std::vector + values; // each object is a primitive type array, which represents values of one measurement + std::vector + bitMaps; // each bitmap represents the existence of each value in the current column + size_t rowSize; //the number of rows to include in this tablet + size_t maxRowNumber; // the maximum number of rows for this tablet + bool isAligned; // whether this tablet store data of aligned timeseries or not + std::vector idColumnIndexes; + + Tablet() = default; + + /** * Return a tablet with default specified row number. This is the standard * constructor (all Tablet should be the same size). * * @param deviceId the name of the device specified to be written in * @param timeseries the list of measurement schemas for creating the tablet */ - Tablet(const std::string& deviceId, - const std::vector>& timeseries) - : Tablet(deviceId, timeseries, DEFAULT_ROW_SIZE) { - } + Tablet(const std::string& deviceId, + const std::vector>& timeseries) + : Tablet(deviceId, timeseries, DEFAULT_ROW_SIZE) {} - Tablet(const std::string& deviceId, - const std::vector>& timeseries, - const std::vector& columnTypes) - : Tablet(deviceId, timeseries, columnTypes, DEFAULT_ROW_SIZE) { - } + Tablet(const std::string& deviceId, + const std::vector>& timeseries, + const std::vector& 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 * always be default size. @@ -175,918 +166,921 @@ class Tablet { * @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>& schemas, - int maxRowNumber) - : Tablet(deviceId, schemas, std::vector(schemas.size(), ColumnCategory::FIELD), maxRowNumber) { + Tablet(const std::string& deviceId, + const std::vector>& schemas, + int maxRowNumber) + : Tablet(deviceId, schemas, + std::vector(schemas.size(), ColumnCategory::FIELD), maxRowNumber) {} + + Tablet(const std::string& deviceId, + const std::vector>& schemas, + const std::vector columnTypes, size_t maxRowNumber, + bool _isAligned = false) + : deviceId(deviceId), schemas(schemas), columnTypes(columnTypes), maxRowNumber(maxRowNumber), + isAligned(_isAligned) { + // create timestamp column + timestamps.resize(maxRowNumber); + // create value columns + values.resize(schemas.size()); + createColumns(); + // init idColumnIndexs + for (size_t i = 0; i < this->columnTypes.size(); i++) { + if (this->columnTypes[i] == ColumnCategory::TAG) { + idColumnIndexes.push_back(i); + } } - - Tablet(const std::string& deviceId, const std::vector>& schemas, - const std::vector columnTypes, - size_t maxRowNumber, bool _isAligned = false) : deviceId(deviceId), schemas(schemas), - columnTypes(columnTypes), - maxRowNumber(maxRowNumber), isAligned(_isAligned) { - // create timestamp column - timestamps.resize(maxRowNumber); - // create value columns - values.resize(schemas.size()); - createColumns(); - // init idColumnIndexs - for (size_t i = 0; i < this->columnTypes.size(); i++) { - if (this->columnTypes[i] == ColumnCategory::TAG) { - idColumnIndexes.push_back(i); - } - } - // create bitMaps - bitMaps.resize(schemas.size()); - for (size_t i = 0; i < schemas.size(); i++) { - bitMaps[i].resize(maxRowNumber); - } - // create schemaNameIndex - for (size_t i = 0; i < schemas.size(); i++) { - schemaNameIndex[schemas[i].first] = i; - } - this->rowSize = 0; + // create bitMaps + bitMaps.resize(schemas.size()); + for (size_t i = 0; i < schemas.size(); i++) { + bitMaps[i].resize(maxRowNumber); } - - Tablet(const Tablet& other) - : deviceId(other.deviceId), - schemas(other.schemas), - schemaNameIndex(other.schemaNameIndex), - columnTypes(other.columnTypes), - timestamps(other.timestamps), - maxRowNumber(other.maxRowNumber), - bitMaps(other.bitMaps), - rowSize(other.rowSize), - isAligned(other.isAligned), - idColumnIndexes(other.idColumnIndexes) { - values.resize(other.values.size()); - for (size_t i = 0; i < other.values.size(); ++i) { - if (!other.values[i]) continue; - TSDataType::TSDataType type = schemas[i].second; - deepCopyTabletColValue(&(other.values[i]), &values[i], type, maxRowNumber); - } + // create schemaNameIndex + for (size_t i = 0; i < schemas.size(); i++) { + schemaNameIndex[schemas[i].first] = i; } - - Tablet& operator=(const Tablet& other) { - if (this != &other) { - deleteColumns(); - deviceId = other.deviceId; - schemas = other.schemas; - schemaNameIndex = other.schemaNameIndex; - columnTypes = other.columnTypes; - timestamps = other.timestamps; - maxRowNumber = other.maxRowNumber; - rowSize = other.rowSize; - isAligned = other.isAligned; - idColumnIndexes = other.idColumnIndexes; - bitMaps = other.bitMaps; - values.resize(other.values.size()); - for (size_t i = 0; i < other.values.size(); ++i) { - if (!other.values[i]) continue; - TSDataType::TSDataType type = schemas[i].second; - deepCopyTabletColValue(&(other.values[i]), &values[i], type, maxRowNumber); - } - } - return *this; + this->rowSize = 0; + } + + Tablet(const Tablet& other) + : deviceId(other.deviceId), schemas(other.schemas), schemaNameIndex(other.schemaNameIndex), + columnTypes(other.columnTypes), timestamps(other.timestamps), + maxRowNumber(other.maxRowNumber), bitMaps(other.bitMaps), rowSize(other.rowSize), + isAligned(other.isAligned), idColumnIndexes(other.idColumnIndexes) { + values.resize(other.values.size()); + for (size_t i = 0; i < other.values.size(); ++i) { + if (!other.values[i]) + continue; + TSDataType::TSDataType type = schemas[i].second; + deepCopyTabletColValue(&(other.values[i]), &values[i], type, maxRowNumber); } - - ~Tablet() { - try { - deleteColumns(); - } - catch (exception& e) { - log_debug(string("Tablet::~Tablet(), ") + e.what()); - } + } + + Tablet& operator=(const Tablet& other) { + if (this != &other) { + deleteColumns(); + deviceId = other.deviceId; + schemas = other.schemas; + schemaNameIndex = other.schemaNameIndex; + columnTypes = other.columnTypes; + timestamps = other.timestamps; + maxRowNumber = other.maxRowNumber; + rowSize = other.rowSize; + isAligned = other.isAligned; + idColumnIndexes = other.idColumnIndexes; + bitMaps = other.bitMaps; + values.resize(other.values.size()); + for (size_t i = 0; i < other.values.size(); ++i) { + if (!other.values[i]) + continue; + TSDataType::TSDataType type = schemas[i].second; + deepCopyTabletColValue(&(other.values[i]), &values[i], type, maxRowNumber); + } } + return *this; + } - void addTimestamp(size_t rowIndex, int64_t timestamp) { - timestamps[rowIndex] = timestamp; - rowSize = max(rowSize, rowIndex + 1); + ~Tablet() { + try { + deleteColumns(); + } catch (exception& e) { + log_debug(string("Tablet::~Tablet(), ") + e.what()); + } + } + + void addTimestamp(size_t rowIndex, int64_t timestamp) { + timestamps[rowIndex] = timestamp; + rowSize = max(rowSize, rowIndex + 1); + } + + static void deepCopyTabletColValue(void* const* srcPtr, void** destPtr, + TSDataType::TSDataType type, int maxRowNumber); + + template void addValue(size_t schemaId, size_t rowIndex, const T& value) { + if (schemaId >= schemas.size()) { + char tmpStr[100]; + sprintf(tmpStr, + "Tablet::addValue(), schemaId >= schemas.size(). schemaId=%ld, schemas.size()=%ld.", + schemaId, schemas.size()); + throw std::out_of_range(tmpStr); } - static void deepCopyTabletColValue(void* const* srcPtr, void** destPtr, - TSDataType::TSDataType type, int maxRowNumber); - - template - void addValue(size_t schemaId, size_t rowIndex, const T& value) { - if (schemaId >= schemas.size()) { - char tmpStr[100]; - sprintf(tmpStr, "Tablet::addValue(), schemaId >= schemas.size(). schemaId=%ld, schemas.size()=%ld.", - schemaId, schemas.size()); - throw std::out_of_range(tmpStr); - } - - if (rowIndex >= rowSize) { - char tmpStr[100]; - sprintf(tmpStr, "Tablet::addValue(), rowIndex >= rowSize. rowIndex=%ld, rowSize.size()=%ld.", rowIndex, - rowSize); - throw std::out_of_range(tmpStr); - } + if (rowIndex >= rowSize) { + char tmpStr[100]; + sprintf(tmpStr, "Tablet::addValue(), rowIndex >= rowSize. rowIndex=%ld, rowSize.size()=%ld.", + rowIndex, rowSize); + throw std::out_of_range(tmpStr); + } - TSDataType::TSDataType dataType = schemas[schemaId].second; - switch (dataType) { - case TSDataType::BOOLEAN: { - safe_cast(value, ((bool*)values[schemaId])[rowIndex]); - break; - } - case TSDataType::INT32: { - safe_cast(value, ((int*)values[schemaId])[rowIndex]); - break; - } - case TSDataType::DATE: { - safe_cast(value, ((boost::gregorian::date*)values[schemaId])[rowIndex]); - break; - } - case TSDataType::TIMESTAMP: - case TSDataType::INT64: { - safe_cast(value, ((int64_t*)values[schemaId])[rowIndex]); - break; - } - case TSDataType::FLOAT: { - safe_cast(value, ((float*)values[schemaId])[rowIndex]); - break; - } - case TSDataType::DOUBLE: { - safe_cast(value, ((double*)values[schemaId])[rowIndex]); - break; - } - case TSDataType::BLOB: - case TSDataType::STRING: - case TSDataType::TEXT: { - safe_cast(value, ((string*)values[schemaId])[rowIndex]); - break; - } - default: - throw UnSupportedDataTypeException(string("Data type ") + to_string(dataType) + " is not supported."); - } + TSDataType::TSDataType dataType = schemas[schemaId].second; + switch (dataType) { + case TSDataType::BOOLEAN: { + safe_cast(value, ((bool*)values[schemaId])[rowIndex]); + break; + } + case TSDataType::INT32: { + safe_cast(value, ((int*)values[schemaId])[rowIndex]); + break; + } + case TSDataType::DATE: { + safe_cast(value, + ((boost::gregorian::date*)values[schemaId])[rowIndex]); + break; + } + case TSDataType::TIMESTAMP: + case TSDataType::INT64: { + safe_cast(value, ((int64_t*)values[schemaId])[rowIndex]); + break; + } + case TSDataType::FLOAT: { + safe_cast(value, ((float*)values[schemaId])[rowIndex]); + break; + } + case TSDataType::DOUBLE: { + safe_cast(value, ((double*)values[schemaId])[rowIndex]); + break; + } + case TSDataType::BLOB: + case TSDataType::STRING: + case TSDataType::TEXT: { + safe_cast(value, ((string*)values[schemaId])[rowIndex]); + break; + } + default: + throw UnSupportedDataTypeException(string("Data type ") + to_string(dataType) + + " is not supported."); + } + } + + // Add a Binary value with extra metadata: [isEOF (1 byte)] + [offset (8 bytes)] + [actual content] + void addValue(size_t schemaId, size_t rowIndex, bool isEOF, int64_t offset, + const std::vector& content) { + // Check schemaId bounds + if (schemaId >= schemas.size()) { + char tmpStr[100]; + sprintf(tmpStr, + "Tablet::addBinaryValueWithMeta(), schemaId >= schemas.size(). schemaId=%ld, " + "schemas.size()=%ld.", + schemaId, schemas.size()); + throw std::out_of_range(tmpStr); } - // Add a Binary value with extra metadata: [isEOF (1 byte)] + [offset (8 bytes)] + [actual content] - void addValue(size_t schemaId, size_t rowIndex, bool isEOF, int64_t offset, const std::vector& content) { - // Check schemaId bounds - if (schemaId >= schemas.size()) { - char tmpStr[100]; - sprintf(tmpStr, - "Tablet::addBinaryValueWithMeta(), schemaId >= schemas.size(). schemaId=%ld, schemas.size()=%ld.", - schemaId, schemas.size()); - throw std::out_of_range(tmpStr); - } + // Check rowIndex bounds + if (rowIndex >= rowSize) { + char tmpStr[100]; + sprintf(tmpStr, + "Tablet::addBinaryValueWithMeta(), rowIndex >= rowSize. rowIndex=%ld, rowSize=%ld.", + rowIndex, rowSize); + throw std::out_of_range(tmpStr); + } - // Check rowIndex bounds - if (rowIndex >= rowSize) { - char tmpStr[100]; - sprintf(tmpStr, "Tablet::addBinaryValueWithMeta(), rowIndex >= rowSize. rowIndex=%ld, rowSize=%ld.", - rowIndex, rowSize); - throw std::out_of_range(tmpStr); - } + TSDataType::TSDataType dataType = schemas[schemaId].second; + if (dataType != TSDataType::OBJECT) { + throw std::invalid_argument("The data type of schemaId " + std::to_string(schemaId) + + " is not OBJECT."); + } - TSDataType::TSDataType dataType = schemas[schemaId].second; - if (dataType != TSDataType::OBJECT) { - throw std::invalid_argument("The data type of schemaId " + std::to_string(schemaId) + " is not OBJECT."); - } + // Create a byte array of size [1 (isEOF) + 8 (offset) + content size] + std::vector val(content.size() + 9); - // Create a byte array of size [1 (isEOF) + 8 (offset) + content size] - std::vector val(content.size() + 9); + // Write the isEOF flag (1 byte) + val[0] = isEOF ? 1 : 0; - // Write the isEOF flag (1 byte) - val[0] = isEOF ? 1 : 0; + // Write the 8-byte offset in big-endian order + for (int i = 0; i < 8; ++i) { + val[1 + i] = static_cast((offset >> (56 - i * 8)) & 0xFF); + } - // Write the 8-byte offset in big-endian order - for (int i = 0; i < 8; ++i) { - val[1 + i] = static_cast((offset >> (56 - i * 8)) & 0xFF); - } + // Append the content bytes + std::copy(content.begin(), content.end(), val.begin() + 9); - // Append the content bytes - std::copy(content.begin(), content.end(), val.begin() + 9); + // Cast the value array and assign the Binary data (stored as string) + std::string valEncoded = std::string(reinterpret_cast(val.data()), val.size()); + safe_cast(valEncoded, ((string*)values[schemaId])[rowIndex]); + } - // Cast the value array and assign the Binary data (stored as string) - std::string valEncoded = std::string(reinterpret_cast(val.data()), val.size()); - safe_cast(valEncoded, ((string*)values[schemaId])[rowIndex]); + void addValue(const string& schemaName, size_t rowIndex, bool isEOF, int64_t offset, + const std::vector& content) { + if (schemaNameIndex.find(schemaName) == schemaNameIndex.end()) { + throw SchemaNotFoundException(string("Schema ") + schemaName + " not found."); } + size_t schemaId = schemaNameIndex[schemaName]; + addValue(schemaId, rowIndex, isEOF, offset, content); + } - void addValue(const string& schemaName, size_t rowIndex, bool isEOF, int64_t offset, - const std::vector& content) { - if (schemaNameIndex.find(schemaName) == schemaNameIndex.end()) { - throw SchemaNotFoundException(string("Schema ") + schemaName + " not found."); - } - size_t schemaId = schemaNameIndex[schemaName]; - addValue(schemaId, rowIndex, isEOF, offset, content); + template void addValue(const string& schemaName, size_t rowIndex, const T& value) { + if (schemaNameIndex.find(schemaName) == schemaNameIndex.end()) { + throw SchemaNotFoundException(string("Schema ") + schemaName + " not found."); } - - template - void addValue(const string& schemaName, size_t rowIndex, const T& value) { - if (schemaNameIndex.find(schemaName) == schemaNameIndex.end()) { - throw SchemaNotFoundException(string("Schema ") + schemaName + " not found."); - } - size_t schemaId = schemaNameIndex[schemaName]; - addValue(schemaId, rowIndex, value); + size_t schemaId = schemaNameIndex[schemaName]; + addValue(schemaId, rowIndex, value); + } + + void* getValue(size_t schemaId, size_t rowIndex, TSDataType::TSDataType dataType) { + if (schemaId >= schemas.size()) { + throw std::out_of_range("Tablet::getValue schemaId out of range: " + + std::to_string(schemaId)); + } + if (rowIndex >= rowSize) { + throw std::out_of_range("Tablet::getValue rowIndex out of range: " + + std::to_string(rowIndex)); } - void* getValue(size_t schemaId, size_t rowIndex, TSDataType::TSDataType dataType) { - if (schemaId >= schemas.size()) { - throw std::out_of_range("Tablet::getValue schemaId out of range: " - + std::to_string(schemaId)); - } - if (rowIndex >= rowSize) { - throw std::out_of_range("Tablet::getValue rowIndex out of range: " - + std::to_string(rowIndex)); - } - - switch (dataType) { - case TSDataType::BOOLEAN: - return &(reinterpret_cast(values[schemaId])[rowIndex]); - case TSDataType::INT32: - return &(reinterpret_cast(values[schemaId])[rowIndex]); - case TSDataType::DATE: - return &(reinterpret_cast(values[schemaId])[rowIndex]); - case TSDataType::TIMESTAMP: - case TSDataType::INT64: - return &(reinterpret_cast(values[schemaId])[rowIndex]); - case TSDataType::FLOAT: - return &(reinterpret_cast(values[schemaId])[rowIndex]); - case TSDataType::DOUBLE: - return &(reinterpret_cast(values[schemaId])[rowIndex]); - case TSDataType::BLOB: - case TSDataType::STRING: - case TSDataType::OBJECT: - case TSDataType::TEXT: - return &(reinterpret_cast(values[schemaId])[rowIndex]); - default: - throw UnSupportedDataTypeException("Unsupported data type: " - + std::to_string(dataType)); - } + switch (dataType) { + case TSDataType::BOOLEAN: + return &(reinterpret_cast(values[schemaId])[rowIndex]); + case TSDataType::INT32: + return &(reinterpret_cast(values[schemaId])[rowIndex]); + case TSDataType::DATE: + return &(reinterpret_cast(values[schemaId])[rowIndex]); + case TSDataType::TIMESTAMP: + case TSDataType::INT64: + return &(reinterpret_cast(values[schemaId])[rowIndex]); + case TSDataType::FLOAT: + return &(reinterpret_cast(values[schemaId])[rowIndex]); + case TSDataType::DOUBLE: + return &(reinterpret_cast(values[schemaId])[rowIndex]); + case TSDataType::BLOB: + case TSDataType::STRING: + case TSDataType::OBJECT: + case TSDataType::TEXT: + return &(reinterpret_cast(values[schemaId])[rowIndex]); + default: + throw UnSupportedDataTypeException("Unsupported data type: " + std::to_string(dataType)); } + } - std::shared_ptr getDeviceID(int i); + std::shared_ptr getDeviceID(int i); - std::vector> getSchemas() const { - return schemas; - } + std::vector> getSchemas() const { + return schemas; + } - void reset(); // Reset Tablet to the default state - set the rowSize to 0 + void reset(); // Reset Tablet to the default state - set the rowSize to 0 - size_t getTimeBytesSize(); + size_t getTimeBytesSize(); - size_t getValueByteSize(); // total byte size that values occupies + size_t getValueByteSize(); // total byte size that values occupies - void setAligned(bool isAligned); + void setAligned(bool isAligned); }; class SessionUtils { public: - static std::string getTime(const Tablet& tablet); + static std::string getTime(const Tablet& tablet); - static std::string getValue(const Tablet& tablet); + static std::string getValue(const Tablet& tablet); - static bool isTabletContainsSingleDevice(Tablet tablet); + static bool isTabletContainsSingleDevice(Tablet tablet); }; class TemplateNode { public: - explicit TemplateNode(const std::string& name) : name_(name) { - } + explicit TemplateNode(const std::string& name) : name_(name) {} - const std::string& getName() const { - return name_; - } + const std::string& getName() const { + return name_; + } - virtual const std::unordered_map>& getChildren() const { - throw BatchExecutionException("Should call exact sub class!"); - } + virtual const std::unordered_map>& + getChildren() const { + throw BatchExecutionException("Should call exact sub class!"); + } - virtual bool isMeasurement() = 0; + virtual bool isMeasurement() = 0; - virtual bool isAligned() { - throw BatchExecutionException("Should call exact sub class!"); - } + virtual bool isAligned() { + throw BatchExecutionException("Should call exact sub class!"); + } - virtual std::string serialize() const { - throw BatchExecutionException("Should call exact sub class!"); - } + virtual std::string serialize() const { + throw BatchExecutionException("Should call exact sub class!"); + } private: - std::string name_; + std::string name_; }; class MeasurementNode : public TemplateNode { public: - MeasurementNode(const std::string& name_, TSDataType::TSDataType data_type_, TSEncoding::TSEncoding encoding_, - CompressionType::CompressionType compression_type_) : TemplateNode(name_) { - this->data_type_ = data_type_; - this->encoding_ = encoding_; - this->compression_type_ = compression_type_; - } + MeasurementNode(const std::string& name_, TSDataType::TSDataType data_type_, + TSEncoding::TSEncoding encoding_, + CompressionType::CompressionType compression_type_) + : TemplateNode(name_) { + this->data_type_ = data_type_; + this->encoding_ = encoding_; + this->compression_type_ = compression_type_; + } - TSDataType::TSDataType getDataType() const { - return data_type_; - } + TSDataType::TSDataType getDataType() const { + return data_type_; + } - TSEncoding::TSEncoding getEncoding() const { - return encoding_; - } + TSEncoding::TSEncoding getEncoding() const { + return encoding_; + } - CompressionType::CompressionType getCompressionType() const { - return compression_type_; - } + CompressionType::CompressionType getCompressionType() const { + return compression_type_; + } - bool isMeasurement() override { - return true; - } + bool isMeasurement() override { + return true; + } - std::string serialize() const override; + std::string serialize() const override; private: - TSDataType::TSDataType data_type_; - TSEncoding::TSEncoding encoding_; - CompressionType::CompressionType compression_type_; + TSDataType::TSDataType data_type_; + TSEncoding::TSEncoding encoding_; + CompressionType::CompressionType compression_type_; }; class InternalNode : public TemplateNode { public: - InternalNode(const std::string& name, bool is_aligned) : TemplateNode(name), is_aligned_(is_aligned) { - } + InternalNode(const std::string& name, bool is_aligned) + : TemplateNode(name), is_aligned_(is_aligned) {} - void addChild(const InternalNode& node) { - if (this->children_.count(node.getName())) { - throw BatchExecutionException("Duplicated child of node in template."); - } - this->children_[node.getName()] = std::make_shared(node); + void addChild(const InternalNode& node) { + if (this->children_.count(node.getName())) { + throw BatchExecutionException("Duplicated child of node in template."); } + this->children_[node.getName()] = std::make_shared(node); + } - void addChild(const MeasurementNode& node) { - if (this->children_.count(node.getName())) { - throw BatchExecutionException("Duplicated child of node in template."); - } - this->children_[node.getName()] = std::make_shared(node); + void addChild(const MeasurementNode& node) { + if (this->children_.count(node.getName())) { + throw BatchExecutionException("Duplicated child of node in template."); } + this->children_[node.getName()] = std::make_shared(node); + } - void deleteChild(const TemplateNode& node) { - this->children_.erase(node.getName()); - } + void deleteChild(const TemplateNode& node) { + this->children_.erase(node.getName()); + } - const std::unordered_map>& getChildren() const override { - return children_; - } + const std::unordered_map>& + getChildren() const override { + return children_; + } - bool isMeasurement() override { - return false; - } + bool isMeasurement() override { + return false; + } - bool isAligned() override { - return is_aligned_; - } + bool isAligned() override { + return is_aligned_; + } private: - std::unordered_map> children_; - bool is_aligned_; + std::unordered_map> children_; + bool is_aligned_; }; namespace TemplateQueryType { -enum TemplateQueryType { - COUNT_MEASUREMENTS, IS_MEASUREMENT, PATH_EXIST, SHOW_MEASUREMENTS -}; +enum TemplateQueryType { COUNT_MEASUREMENTS, IS_MEASUREMENT, PATH_EXIST, SHOW_MEASUREMENTS }; } class Template { public: - Template(const std::string& name, bool is_aligned) : name_(name), is_aligned_(is_aligned) { - } + Template(const std::string& name, bool is_aligned) : name_(name), is_aligned_(is_aligned) {} - const std::string& getName() const { - return name_; - } + const std::string& getName() const { + return name_; + } - bool isAligned() const { - return is_aligned_; - } + bool isAligned() const { + return is_aligned_; + } - void addToTemplate(const InternalNode& child) { - if (this->children_.count(child.getName())) { - throw BatchExecutionException("Duplicated child of node in template."); - } - this->children_[child.getName()] = std::make_shared(child); + void addToTemplate(const InternalNode& child) { + if (this->children_.count(child.getName())) { + throw BatchExecutionException("Duplicated child of node in template."); } + this->children_[child.getName()] = std::make_shared(child); + } - void addToTemplate(const MeasurementNode& child) { - if (this->children_.count(child.getName())) { - throw BatchExecutionException("Duplicated child of node in template."); - } - this->children_[child.getName()] = std::make_shared(child); + void addToTemplate(const MeasurementNode& child) { + if (this->children_.count(child.getName())) { + throw BatchExecutionException("Duplicated child of node in template."); } + this->children_[child.getName()] = std::make_shared(child); + } - std::string serialize() const; + std::string serialize() const; private: - std::string name_; - std::unordered_map> children_; - bool is_aligned_; + std::string name_; + std::unordered_map> children_; + bool is_aligned_; }; class Session { private: - std::string host_; - int rpcPort_; - bool useSSL_ = false; - std::string trustCertFilePath_; - std::vector nodeUrls_; - std::string username_; - std::string password_; - const TSProtocolVersion::type protocolVersion_ = TSProtocolVersion::IOTDB_SERVICE_PROTOCOL_V3; - bool isClosed_ = true; - std::string zoneId_; - int fetchSize_; - const static int DEFAULT_FETCH_SIZE = 10000; - const static int DEFAULT_TIMEOUT_MS = 0; - int connectTimeoutMs_; - Version::Version version; - std::string sqlDialect_ = "tree"; // default sql dialect - std::string database_; - bool enableAutoFetch_ = true; - bool enableRedirection_ = true; - std::shared_ptr nodesSupplier_; - friend class SessionConnection; - friend class TableSession; - std::shared_ptr defaultSessionConnection_; - - TEndPoint defaultEndPoint_; - - struct TEndPointHash { - size_t operator()(const TEndPoint& endpoint) const { - return std::hash()(endpoint.ip) ^ std::hash()(endpoint.port); - } - }; + std::string host_; + int rpcPort_; + bool useSSL_ = false; + std::string trustCertFilePath_; + std::vector nodeUrls_; + std::string username_; + std::string password_; + const TSProtocolVersion::type protocolVersion_ = TSProtocolVersion::IOTDB_SERVICE_PROTOCOL_V3; + bool isClosed_ = true; + std::string zoneId_; + int fetchSize_; + const static int DEFAULT_FETCH_SIZE = 10000; + const static int DEFAULT_TIMEOUT_MS = 0; + int connectTimeoutMs_; + Version::Version version; + std::string sqlDialect_ = "tree"; // default sql dialect + std::string database_; + bool enableAutoFetch_ = true; + bool enableRedirection_ = true; + std::shared_ptr nodesSupplier_; + friend class SessionConnection; + friend class TableSession; + std::shared_ptr defaultSessionConnection_; + + TEndPoint defaultEndPoint_; + + struct TEndPointHash { + size_t operator()(const TEndPoint& endpoint) const { + return std::hash()(endpoint.ip) ^ std::hash()(endpoint.port); + } + }; - struct TEndPointEqual { - bool operator()(const TEndPoint& lhs, const TEndPoint& rhs) const { - return lhs.ip == rhs.ip && lhs.port == rhs.port; - } - }; + struct TEndPointEqual { + bool operator()(const TEndPoint& lhs, const TEndPoint& rhs) const { + return lhs.ip == rhs.ip && lhs.port == rhs.port; + } + }; - using EndPointSessionMap = std::unordered_map< - TEndPoint, shared_ptr, TEndPointHash, TEndPointEqual>; - EndPointSessionMap endPointToSessionConnection; - std::unordered_map deviceIdToEndpoint; - std::unordered_map, TEndPoint> tableModelDeviceIdToEndpoint; + using EndPointSessionMap = + std::unordered_map, TEndPointHash, TEndPointEqual>; + EndPointSessionMap endPointToSessionConnection; + std::unordered_map deviceIdToEndpoint; + std::unordered_map, TEndPoint> tableModelDeviceIdToEndpoint; private: - void removeBrokenSessionConnection(shared_ptr sessionConnection); + void removeBrokenSessionConnection(shared_ptr sessionConnection); - static bool checkSorted(const Tablet& tablet); + static bool checkSorted(const Tablet& tablet); - static bool checkSorted(const std::vector& times); + static bool checkSorted(const std::vector& times); - static void sortTablet(Tablet& tablet); + static void sortTablet(Tablet& tablet); - static void sortIndexByTimestamp(int* index, std::vector& timestamps, int length); + static void sortIndexByTimestamp(int* index, std::vector& timestamps, int length); - void appendValues(std::string& buffer, const char* value, int size); + void appendValues(std::string& buffer, const char* value, int size); - void - putValuesIntoBuffer(const std::vector& types, const std::vector& values, - std::string& buf); + void putValuesIntoBuffer(const std::vector& types, + const std::vector& values, std::string& buf); - int8_t getDataTypeNumber(TSDataType::TSDataType type); + int8_t getDataTypeNumber(TSDataType::TSDataType type); - struct TsCompare { - std::vector& timestamps; + struct TsCompare { + std::vector& timestamps; - explicit TsCompare(std::vector& inTimestamps) : timestamps(inTimestamps) { - }; + explicit TsCompare(std::vector& inTimestamps) : timestamps(inTimestamps){}; - bool operator()(int i, int j) { return (timestamps[i] < timestamps[j]); }; + bool operator()(int i, int j) { + return (timestamps[i] < timestamps[j]); }; + }; - std::string getVersionString(Version::Version version); + std::string getVersionString(Version::Version version); - void initZoneId(); + void initZoneId(); - void initNodesSupplier(const std::vector& nodeUrls = std::vector()); + void initNodesSupplier(const std::vector& nodeUrls = std::vector()); - void initDefaultSessionConnection(); + void initDefaultSessionConnection(); - template - void insertByGroup(std::unordered_map, T>& insertGroup, - InsertConsumer insertConsumer); + template + void insertByGroup(std::unordered_map, T>& insertGroup, + InsertConsumer insertConsumer); - template - void insertOnce(std::unordered_map, T>& insertGroup, - InsertConsumer insertConsumer); + template + void insertOnce(std::unordered_map, T>& insertGroup, + InsertConsumer insertConsumer); - void insertStringRecordsWithLeaderCache(vector deviceIds, vector times, - vector> measurementsList, vector> valuesList, - bool isAligned); + void insertStringRecordsWithLeaderCache(vector deviceIds, vector times, + vector> measurementsList, + vector> valuesList, bool isAligned); - void insertRecordsWithLeaderCache(vector deviceIds, vector times, - vector> measurementsList, - const vector>& typesList, - vector> valuesList, bool isAligned); + void insertRecordsWithLeaderCache(vector deviceIds, vector times, + vector> measurementsList, + const vector>& typesList, + vector> valuesList, bool isAligned); - void insertTabletsWithLeaderCache(unordered_map tablets, bool sorted, bool isAligned); + void insertTabletsWithLeaderCache(unordered_map tablets, bool sorted, + bool isAligned); - shared_ptr getQuerySessionConnection(); + shared_ptr getQuerySessionConnection(); - shared_ptr getSessionConnection(std::string deviceId); + shared_ptr getSessionConnection(std::string deviceId); - shared_ptr getSessionConnection(std::shared_ptr deviceId); + shared_ptr getSessionConnection(std::shared_ptr deviceId); - void handleQueryRedirection(TEndPoint endPoint); + void handleQueryRedirection(TEndPoint endPoint); - void handleRedirection(const std::string& deviceId, TEndPoint endPoint); + void handleRedirection(const std::string& deviceId, TEndPoint endPoint); - void handleRedirection(const std::shared_ptr& deviceId, TEndPoint endPoint); + void handleRedirection(const std::shared_ptr& deviceId, TEndPoint endPoint); - void setSqlDialect(const std::string& dialect) { - this->sqlDialect_ = dialect; - } + void setSqlDialect(const std::string& dialect) { + this->sqlDialect_ = dialect; + } - void setDatabase(const std::string& database) { - this->database_ = database; - } + void setDatabase(const std::string& database) { + this->database_ = database; + } - string getDatabase() { - return database_; - } + string getDatabase() { + return database_; + } - void changeDatabase(string database) { - this->database_ = database; - } + void changeDatabase(string database) { + this->database_ = database; + } public: - Session(const std::string& host, int rpcPort) : username_("root"), password_("root"), version(Version::V_1_0) { - this->host_ = host; - this->rpcPort_ = rpcPort; - initZoneId(); - initNodesSupplier(); - } - - Session(const std::vector& nodeUrls, const std::string& username, const std::string& password) - : nodeUrls_(nodeUrls), username_(username), password_(password), version(Version::V_1_0) { - initZoneId(); - initNodesSupplier(this->nodeUrls_); - } + Session(const std::string& host, int rpcPort) + : username_("root"), password_("root"), version(Version::V_1_0) { + this->host_ = host; + this->rpcPort_ = rpcPort; + initZoneId(); + initNodesSupplier(); + } + + Session(const std::vector& nodeUrls, const std::string& username, + const std::string& password) + : nodeUrls_(nodeUrls), username_(username), password_(password), version(Version::V_1_0) { + initZoneId(); + initNodesSupplier(this->nodeUrls_); + } + + Session(const std::string& host, int rpcPort, const std::string& username, + const std::string& password) + : fetchSize_(DEFAULT_FETCH_SIZE) { + this->host_ = host; + this->rpcPort_ = rpcPort; + this->username_ = username; + this->password_ = password; + this->version = Version::V_1_0; + initZoneId(); + initNodesSupplier(); + } + + Session(const std::string& host, int rpcPort, const std::string& username, + const std::string& password, const std::string& zoneId, + int fetchSize = DEFAULT_FETCH_SIZE) { + this->host_ = host; + this->rpcPort_ = rpcPort; + this->username_ = username; + this->password_ = password; + this->zoneId_ = zoneId; + this->fetchSize_ = fetchSize; + this->version = Version::V_1_0; + initZoneId(); + initNodesSupplier(); + } + + Session(const std::string& host, const std::string& rpcPort, const std::string& username = "user", + const std::string& password = "password", const std::string& zoneId = "", + int fetchSize = DEFAULT_FETCH_SIZE) { + this->host_ = host; + this->rpcPort_ = stoi(rpcPort); + this->username_ = username; + this->password_ = password; + this->zoneId_ = zoneId; + this->fetchSize_ = fetchSize; + this->version = Version::V_1_0; + initZoneId(); + initNodesSupplier(); + } + + Session(AbstractSessionBuilder* builder) { + this->host_ = builder->host; + this->rpcPort_ = builder->rpcPort; + this->username_ = builder->username; + this->password_ = builder->password; + this->zoneId_ = builder->zoneId; + this->fetchSize_ = builder->fetchSize; + this->version = Version::V_1_0; + this->sqlDialect_ = builder->sqlDialect; + this->database_ = builder->database; + this->enableAutoFetch_ = builder->enableAutoFetch; + this->enableRedirection_ = builder->enableRedirections; + this->connectTimeoutMs_ = builder->connectTimeoutMs; + this->nodeUrls_ = builder->nodeUrls; + this->useSSL_ = builder->useSSL; + this->trustCertFilePath_ = builder->trustCertFilePath; + initZoneId(); + initNodesSupplier(this->nodeUrls_); + } + + ~Session(); + + void open(); + + void open(bool enableRPCCompression); + + void open(bool enableRPCCompression, int connectionTimeoutInMs); + + void close(); + + void setTimeZone(const std::string& zoneId); + + std::string getTimeZone(); + + void insertRecord(const std::string& deviceId, int64_t time, + const std::vector& measurements, + const std::vector& values); + + void insertRecord(const std::string& deviceId, int64_t time, + const std::vector& measurements, + const std::vector& types, + const std::vector& values); + + void insertAlignedRecord(const std::string& deviceId, int64_t time, + const std::vector& measurements, + const std::vector& values); + + void insertAlignedRecord(const std::string& deviceId, int64_t time, + const std::vector& measurements, + const std::vector& types, + const std::vector& values); - Session(const std::string& host, int rpcPort, const std::string& username, const std::string& password) - : fetchSize_(DEFAULT_FETCH_SIZE) { - this->host_ = host; - this->rpcPort_ = rpcPort; - this->username_ = username; - this->password_ = password; - this->version = Version::V_1_0; - initZoneId(); - initNodesSupplier(); - } - - Session(const std::string& host, int rpcPort, const std::string& username, const std::string& password, - const std::string& zoneId, int fetchSize = DEFAULT_FETCH_SIZE) { - this->host_ = host; - this->rpcPort_ = rpcPort; - this->username_ = username; - this->password_ = password; - this->zoneId_ = zoneId; - this->fetchSize_ = fetchSize; - this->version = Version::V_1_0; - initZoneId(); - initNodesSupplier(); - } - - Session(const std::string& host, const std::string& rpcPort, const std::string& username = "user", - const std::string& password = "password", const std::string& zoneId = "", - int fetchSize = DEFAULT_FETCH_SIZE) { - this->host_ = host; - this->rpcPort_ = stoi(rpcPort); - this->username_ = username; - this->password_ = password; - this->zoneId_ = zoneId; - this->fetchSize_ = fetchSize; - this->version = Version::V_1_0; - initZoneId(); - initNodesSupplier(); - } - - Session(AbstractSessionBuilder* builder) { - this->host_ = builder->host; - this->rpcPort_ = builder->rpcPort; - this->username_ = builder->username; - this->password_ = builder->password; - this->zoneId_ = builder->zoneId; - this->fetchSize_ = builder->fetchSize; - this->version = Version::V_1_0; - this->sqlDialect_ = builder->sqlDialect; - this->database_ = builder->database; - this->enableAutoFetch_ = builder->enableAutoFetch; - this->enableRedirection_ = builder->enableRedirections; - this->connectTimeoutMs_ = builder->connectTimeoutMs; - this->nodeUrls_ = builder->nodeUrls; - this->useSSL_ = builder->useSSL; - this->trustCertFilePath_ = builder->trustCertFilePath; - initZoneId(); - initNodesSupplier(this->nodeUrls_); - } - - ~Session(); - - void open(); - - void open(bool enableRPCCompression); - - void open(bool enableRPCCompression, int connectionTimeoutInMs); - - void close(); - - void setTimeZone(const std::string& zoneId); - - std::string getTimeZone(); - - void insertRecord(const std::string& deviceId, int64_t time, const std::vector& measurements, - const std::vector& values); - - void insertRecord(const std::string& deviceId, int64_t time, const std::vector& measurements, - const std::vector& types, const std::vector& values); - - void insertAlignedRecord(const std::string& deviceId, int64_t time, const std::vector& measurements, - const std::vector& values); + void insertRecords(const std::vector& deviceIds, const std::vector& times, + const std::vector>& measurementsList, + const std::vector>& valuesList); - void insertAlignedRecord(const std::string& deviceId, int64_t time, const std::vector& measurements, - const std::vector& types, const std::vector& values); + void insertRecords(const std::vector& deviceIds, const std::vector& times, + const std::vector>& measurementsList, + const std::vector>& typesList, + const std::vector>& valuesList); - void insertRecords(const std::vector& deviceIds, - const std::vector& times, - const std::vector>& measurementsList, - const std::vector>& valuesList); + void insertAlignedRecords(const std::vector& deviceIds, + const std::vector& times, + const std::vector>& measurementsList, + const std::vector>& valuesList); - void insertRecords(const std::vector& deviceIds, - const std::vector& times, - const std::vector>& measurementsList, - const std::vector>& typesList, - const std::vector>& valuesList); + void insertAlignedRecords(const std::vector& deviceIds, + const std::vector& times, + const std::vector>& measurementsList, + const std::vector>& typesList, + const std::vector>& valuesList); - void insertAlignedRecords(const std::vector& deviceIds, - const std::vector& times, - const std::vector>& measurementsList, - const std::vector>& valuesList); + void insertRecordsOfOneDevice(const std::string& deviceId, std::vector& times, + std::vector>& measurementsList, + std::vector>& typesList, + std::vector>& valuesList); - void insertAlignedRecords(const std::vector& deviceIds, - const std::vector& times, - const std::vector>& measurementsList, - const std::vector>& typesList, - const std::vector>& valuesList); + void insertRecordsOfOneDevice(const std::string& deviceId, std::vector& times, + std::vector>& measurementsList, + std::vector>& typesList, + std::vector>& valuesList, bool sorted); - void insertRecordsOfOneDevice(const std::string& deviceId, - std::vector& times, - std::vector>& measurementsList, - std::vector>& typesList, - std::vector>& valuesList); + void insertAlignedRecordsOfOneDevice(const std::string& deviceId, std::vector& times, + std::vector>& measurementsList, + std::vector>& typesList, + std::vector>& valuesList); - void insertRecordsOfOneDevice(const std::string& deviceId, - std::vector& times, - std::vector>& measurementsList, - std::vector>& typesList, - std::vector>& valuesList, - bool sorted); + void insertAlignedRecordsOfOneDevice(const std::string& deviceId, std::vector& times, + std::vector>& measurementsList, + std::vector>& typesList, + std::vector>& valuesList, bool sorted); - void insertAlignedRecordsOfOneDevice(const std::string& deviceId, - std::vector& times, - std::vector>& measurementsList, - std::vector>& typesList, - std::vector>& valuesList); + void insertTablet(Tablet& tablet); - void insertAlignedRecordsOfOneDevice(const std::string& deviceId, - std::vector& times, - std::vector>& measurementsList, - std::vector>& typesList, - std::vector>& valuesList, - bool sorted); + void insertTablet(Tablet& tablet, bool sorted); - void insertTablet(Tablet& tablet); + void insertRelationalTablet(Tablet& tablet); - void insertTablet(Tablet& tablet, bool sorted); + void insertRelationalTabletOnce( + const std::unordered_map, Tablet>& relationalTabletGroup, + bool sorted); - void insertRelationalTablet(Tablet& tablet); + void insertRelationalTabletByGroup( + const std::unordered_map, Tablet>& relationalTabletGroup, + bool sorted); - void insertRelationalTabletOnce( - const std::unordered_map, Tablet>& relationalTabletGroup, - bool sorted); + void insertRelationalTablet(Tablet& tablet, bool sorted); - void insertRelationalTabletByGroup( - const std::unordered_map, Tablet>& relationalTabletGroup, - bool sorted); + static void buildInsertTabletReq(TSInsertTabletReq& request, Tablet& tablet, bool sorted); - void insertRelationalTablet(Tablet& tablet, bool sorted); + void insertTablet(TSInsertTabletReq request); - static void buildInsertTabletReq(TSInsertTabletReq& request, Tablet& tablet, bool sorted); + void insertAlignedTablet(Tablet& tablet); - void insertTablet(TSInsertTabletReq request); + void insertAlignedTablet(Tablet& tablet, bool sorted); - void insertAlignedTablet(Tablet& tablet); + void insertTablets(std::unordered_map& tablets); - void insertAlignedTablet(Tablet& tablet, bool sorted); + void insertTablets(std::unordered_map& tablets, bool sorted); - void insertTablets(std::unordered_map& tablets); + void insertAlignedTablets(std::unordered_map& tablets, bool sorted = false); - void insertTablets(std::unordered_map& tablets, bool sorted); + void testInsertRecord(const std::string& deviceId, int64_t time, + const std::vector& measurements, + const std::vector& values); - void insertAlignedTablets(std::unordered_map& tablets, bool sorted = false); + void testInsertTablet(const Tablet& tablet); - void testInsertRecord(const std::string& deviceId, int64_t time, - const std::vector& measurements, - const std::vector& values); + void testInsertRecords(const std::vector& deviceIds, + const std::vector& times, + const std::vector>& measurementsList, + const std::vector>& valuesList); - void testInsertTablet(const Tablet& tablet); + void deleteTimeseries(const std::string& path); - void testInsertRecords(const std::vector& deviceIds, - const std::vector& times, - const std::vector>& measurementsList, - const std::vector>& valuesList); + void deleteTimeseries(const std::vector& paths); - void deleteTimeseries(const std::string& path); + void deleteData(const std::string& path, int64_t endTime); - void deleteTimeseries(const std::vector& paths); + void deleteData(const std::vector& paths, int64_t endTime); - void deleteData(const std::string& path, int64_t endTime); + void deleteData(const std::vector& paths, int64_t startTime, int64_t endTime); - void deleteData(const std::vector& paths, int64_t endTime); + void setStorageGroup(const std::string& storageGroupId); - void deleteData(const std::vector& paths, int64_t startTime, int64_t endTime); + void deleteStorageGroup(const std::string& storageGroup); - void setStorageGroup(const std::string& storageGroupId); + void deleteStorageGroups(const std::vector& storageGroups); - void deleteStorageGroup(const std::string& storageGroup); + void createDatabase(const std::string& database); - void deleteStorageGroups(const std::vector& storageGroups); + void deleteDatabase(const std::string& database); - void createDatabase(const std::string& database); + void deleteDatabases(const std::vector& databases); - void deleteDatabase(const std::string& database); + void createTimeseries(const std::string& path, TSDataType::TSDataType dataType, + TSEncoding::TSEncoding encoding, + CompressionType::CompressionType compressor); - void deleteDatabases(const std::vector& databases); + void createTimeseries(const std::string& path, TSDataType::TSDataType dataType, + TSEncoding::TSEncoding encoding, + CompressionType::CompressionType compressor, + std::map* props, + std::map* tags, + std::map* attributes, + const std::string& measurementAlias); - void createTimeseries(const std::string& path, TSDataType::TSDataType dataType, TSEncoding::TSEncoding encoding, - CompressionType::CompressionType compressor); + void createMultiTimeseries(const std::vector& paths, + const std::vector& dataTypes, + const std::vector& encodings, + const std::vector& compressors, + std::vector>* propsList, + std::vector>* tagsList, + std::vector>* attributesList, + std::vector* measurementAliasList); - void createTimeseries(const std::string& path, TSDataType::TSDataType dataType, TSEncoding::TSEncoding encoding, - CompressionType::CompressionType compressor, - std::map* props, std::map* tags, - std::map* attributes, - const std::string& measurementAlias); - - void createMultiTimeseries(const std::vector& paths, + void createAlignedTimeseries(const std::string& deviceId, + const std::vector& measurements, const std::vector& dataTypes, const std::vector& encodings, - const std::vector& compressors, - std::vector>* propsList, - std::vector>* tagsList, - std::vector>* attributesList, - std::vector* measurementAliasList); + const std::vector& compressors); + + bool checkTimeseriesExists(const std::string& path); - void createAlignedTimeseries(const std::string& deviceId, - const std::vector& measurements, - const std::vector& dataTypes, - const std::vector& encodings, - const std::vector& compressors); + std::unique_ptr executeQueryStatement(const std::string& sql); - bool checkTimeseriesExists(const std::string& path); + std::unique_ptr executeQueryStatement(const std::string& sql, + int64_t timeoutInMs); - std::unique_ptr executeQueryStatement(const std::string& sql); + std::unique_ptr executeQueryStatementMayRedirect(const std::string& sql, + int64_t timeoutInMs); - std::unique_ptr executeQueryStatement(const std::string& sql, int64_t timeoutInMs); + void executeNonQueryStatement(const std::string& sql); - std::unique_ptr executeQueryStatementMayRedirect(const std::string& sql, int64_t timeoutInMs); + std::unique_ptr executeRawDataQuery(const std::vector& paths, + int64_t startTime, int64_t endTime); - void executeNonQueryStatement(const std::string& sql); + std::unique_ptr executeLastDataQuery(const std::vector& paths); - std::unique_ptr executeRawDataQuery(const std::vector& paths, int64_t startTime, - int64_t endTime); + std::unique_ptr executeLastDataQuery(const std::vector& paths, + int64_t lastTime); - std::unique_ptr executeLastDataQuery(const std::vector& paths); + void createSchemaTemplate(const Template& templ); - std::unique_ptr executeLastDataQuery(const std::vector& paths, int64_t lastTime); + void setSchemaTemplate(const std::string& template_name, const std::string& prefix_path); - void createSchemaTemplate(const Template& templ); + void unsetSchemaTemplate(const std::string& prefix_path, const std::string& template_name); - void setSchemaTemplate(const std::string& template_name, const std::string& prefix_path); + void addAlignedMeasurementsInTemplate( + const std::string& template_name, const std::vector& measurements, + const std::vector& dataTypes, + const std::vector& encodings, + const std::vector& compressors); - void unsetSchemaTemplate(const std::string& prefix_path, const std::string& template_name); + void addAlignedMeasurementsInTemplate(const std::string& template_name, + const std::string& measurement, + TSDataType::TSDataType dataType, + TSEncoding::TSEncoding encoding, + CompressionType::CompressionType compressor); - void addAlignedMeasurementsInTemplate(const std::string& template_name, - const std::vector& measurements, - const std::vector& dataTypes, - const std::vector& encodings, - const std::vector& compressors); + void addUnalignedMeasurementsInTemplate( + const std::string& template_name, const std::vector& measurements, + const std::vector& dataTypes, + const std::vector& encodings, + const std::vector& compressors); - void addAlignedMeasurementsInTemplate(const std::string& template_name, + void addUnalignedMeasurementsInTemplate(const std::string& template_name, const std::string& measurement, TSDataType::TSDataType dataType, TSEncoding::TSEncoding encoding, CompressionType::CompressionType compressor); - void addUnalignedMeasurementsInTemplate(const std::string& template_name, - const std::vector& measurements, - const std::vector& dataTypes, - const std::vector& encodings, - const std::vector& compressors); - - void addUnalignedMeasurementsInTemplate(const std::string& template_name, - const std::string& measurement, - TSDataType::TSDataType dataType, - TSEncoding::TSEncoding encoding, - CompressionType::CompressionType compressor); + void deleteNodeInTemplate(const std::string& template_name, const std::string& path); - void deleteNodeInTemplate(const std::string& template_name, const std::string& path); + int countMeasurementsInTemplate(const std::string& template_name); - int countMeasurementsInTemplate(const std::string& template_name); + bool isMeasurementInTemplate(const std::string& template_name, const std::string& path); - bool isMeasurementInTemplate(const std::string& template_name, const std::string& path); + bool isPathExistInTemplate(const std::string& template_name, const std::string& path); - bool isPathExistInTemplate(const std::string& template_name, const std::string& path); + std::vector showMeasurementsInTemplate(const std::string& template_name); - std::vector showMeasurementsInTemplate(const std::string& template_name); + std::vector showMeasurementsInTemplate(const std::string& template_name, + const std::string& pattern); - std::vector showMeasurementsInTemplate(const std::string& template_name, const std::string& pattern); - - bool checkTemplateExists(const std::string& template_name); + bool checkTemplateExists(const std::string& template_name); }; template void Session::insertByGroup(std::unordered_map, T>& insertGroup, InsertConsumer insertConsumer) { - std::vector> futures; - - for (auto& entry : insertGroup) { - auto connection = entry.first; - auto& req = entry.second; - futures.emplace_back(std::async(std::launch::async, [=, &req]() mutable { - try { - insertConsumer(connection, req); - } - catch (const RedirectException& e) { - for (const auto& deviceEndPoint : e.deviceEndPointMap) { - handleRedirection(deviceEndPoint.first, deviceEndPoint.second); - } - } catch (const IoTDBConnectionException& e) { - if (endPointToSessionConnection.size() > 1) { - removeBrokenSessionConnection(connection); - try { - insertConsumer(defaultSessionConnection_, req); - } - catch (const RedirectException&) { - } - } - else { - throw; - } - } catch (const std::exception& e) { - log_debug(e.what()); - throw IoTDBException(e.what()); - } - })); - } + std::vector> futures; - std::string errorMessages; - for (auto& f : futures) { - try { - f.get(); + for (auto& entry : insertGroup) { + auto connection = entry.first; + auto& req = entry.second; + futures.emplace_back(std::async(std::launch::async, [=, &req]() mutable { + try { + insertConsumer(connection, req); + } catch (const RedirectException& e) { + for (const auto& deviceEndPoint : e.deviceEndPointMap) { + handleRedirection(deviceEndPoint.first, deviceEndPoint.second); } - catch (const IoTDBConnectionException& e) { - throw; - } catch (const std::exception& e) { - if (!errorMessages.empty()) { - errorMessages += ";"; - } - errorMessages += e.what(); + } catch (const IoTDBConnectionException& e) { + if (endPointToSessionConnection.size() > 1) { + removeBrokenSessionConnection(connection); + try { + insertConsumer(defaultSessionConnection_, req); + } catch (const RedirectException&) { + } + } else { + throw; } + } catch (const std::exception& e) { + log_debug(e.what()); + throw IoTDBException(e.what()); + } + })); + } + + std::string errorMessages; + for (auto& f : futures) { + try { + f.get(); + } catch (const IoTDBConnectionException& e) { + throw; + } catch (const std::exception& e) { + if (!errorMessages.empty()) { + errorMessages += ";"; + } + errorMessages += e.what(); } + } - if (!errorMessages.empty()) { - throw StatementExecutionException(errorMessages); - } + if (!errorMessages.empty()) { + throw StatementExecutionException(errorMessages); + } } template void Session::insertOnce(std::unordered_map, T>& insertGroup, InsertConsumer insertConsumer) { - auto connection = insertGroup.begin()->first; - auto req = insertGroup.begin()->second; - try { - insertConsumer(connection, req); + auto connection = insertGroup.begin()->first; + auto req = insertGroup.begin()->second; + try { + insertConsumer(connection, req); + } catch (RedirectException e) { + for (const auto& deviceEndPoint : e.deviceEndPointMap) { + handleRedirection(deviceEndPoint.first, deviceEndPoint.second); } - catch (RedirectException e) { - for (const auto& deviceEndPoint : e.deviceEndPointMap) { - handleRedirection(deviceEndPoint.first, deviceEndPoint.second); - } - } catch (IoTDBConnectionException e) { - if (endPointToSessionConnection.size() > 1) { - removeBrokenSessionConnection(connection); - try { - insertConsumer(defaultSessionConnection_, req); - } - catch (RedirectException e) { - } - } - else { - throw e; - } + } catch (IoTDBConnectionException e) { + if (endPointToSessionConnection.size() > 1) { + removeBrokenSessionConnection(connection); + try { + insertConsumer(defaultSessionConnection_, req); + } catch (RedirectException e) { + } + } else { + throw e; } + } } #endif // IOTDB_SESSION_H diff --git a/iotdb-client/client-cpp/src/main/SessionBuilder.h b/iotdb-client/client-cpp/src/main/SessionBuilder.h index 02d31042491d9..14342697eb5d9 100644 --- a/iotdb-client/client-cpp/src/main/SessionBuilder.h +++ b/iotdb-client/client-cpp/src/main/SessionBuilder.h @@ -24,82 +24,83 @@ class SessionBuilder : public AbstractSessionBuilder { public: - SessionBuilder* host(const std::string &host) { - AbstractSessionBuilder::host = host; - return this; + SessionBuilder* host(const std::string& host) { + AbstractSessionBuilder::host = host; + return this; + } + + SessionBuilder* rpcPort(int rpcPort) { + AbstractSessionBuilder::rpcPort = rpcPort; + return this; + } + + SessionBuilder* useSSL(bool useSSL) { + AbstractSessionBuilder::useSSL = useSSL; + return this; + } + + SessionBuilder* trustCertFilePath(const std::string& trustCertFilePath) { + AbstractSessionBuilder::trustCertFilePath = trustCertFilePath; + return this; + } + + SessionBuilder* username(const std::string& username) { + AbstractSessionBuilder::username = username; + return this; + } + + SessionBuilder* password(const std::string& password) { + AbstractSessionBuilder::password = password; + return this; + } + + SessionBuilder* zoneId(const std::string& zoneId) { + AbstractSessionBuilder::zoneId = zoneId; + return this; + } + + SessionBuilder* fetchSize(int fetchSize) { + AbstractSessionBuilder::fetchSize = fetchSize; + return this; + } + + SessionBuilder* database(const std::string& database) { + AbstractSessionBuilder::database = database; + return this; + } + + SessionBuilder* nodeUrls(const std::vector& nodeUrls) { + AbstractSessionBuilder::nodeUrls = nodeUrls; + return this; + } + + SessionBuilder* enableAutoFetch(bool enableAutoFetch) { + AbstractSessionBuilder::enableAutoFetch = enableAutoFetch; + return this; + } + + SessionBuilder* enableRedirections(bool enableRedirections) { + AbstractSessionBuilder::enableRedirections = enableRedirections; + return this; + } + + SessionBuilder* enableRPCCompression(bool enableRPCCompression) { + AbstractSessionBuilder::enableRPCCompression = enableRPCCompression; + return this; + } + + std::shared_ptr build() { + if (!AbstractSessionBuilder::nodeUrls.empty() && + (AbstractSessionBuilder::host != DEFAULT_HOST || + AbstractSessionBuilder::rpcPort != DEFAULT_RPC_PORT)) { + throw IoTDBException( + "Session builder does not support setting node urls and host/rpcPort at the same time."); } - - SessionBuilder* rpcPort(int rpcPort) { - AbstractSessionBuilder::rpcPort = rpcPort; - return this; - } - - SessionBuilder* useSSL(bool useSSL) { - AbstractSessionBuilder::useSSL = useSSL; - return this; - } - - SessionBuilder* trustCertFilePath(const std::string &trustCertFilePath) { - AbstractSessionBuilder::trustCertFilePath = trustCertFilePath; - return this; - } - - SessionBuilder* username(const std::string &username) { - AbstractSessionBuilder::username = username; - return this; - } - - SessionBuilder* password(const std::string &password) { - AbstractSessionBuilder::password = password; - return this; - } - - SessionBuilder* zoneId(const std::string &zoneId) { - AbstractSessionBuilder::zoneId = zoneId; - return this; - } - - SessionBuilder* fetchSize(int fetchSize) { - AbstractSessionBuilder::fetchSize = fetchSize; - return this; - } - - SessionBuilder* database(const std::string &database) { - AbstractSessionBuilder::database = database; - return this; - } - - SessionBuilder* nodeUrls(const std::vector& nodeUrls) { - AbstractSessionBuilder::nodeUrls = nodeUrls; - return this; - } - - SessionBuilder* enableAutoFetch(bool enableAutoFetch) { - AbstractSessionBuilder::enableAutoFetch = enableAutoFetch; - return this; - } - - SessionBuilder* enableRedirections(bool enableRedirections) { - AbstractSessionBuilder::enableRedirections = enableRedirections; - return this; - } - - SessionBuilder* enableRPCCompression(bool enableRPCCompression) { - AbstractSessionBuilder::enableRPCCompression = enableRPCCompression; - return this; - } - - std::shared_ptr build() { - if (!AbstractSessionBuilder::nodeUrls.empty() && - (AbstractSessionBuilder::host != DEFAULT_HOST || - AbstractSessionBuilder::rpcPort != DEFAULT_RPC_PORT)) { - throw IoTDBException("Session builder does not support setting node urls and host/rpcPort at the same time."); - } - sqlDialect = "tree"; - auto newSession = std::make_shared(this); - newSession->open(false); - return newSession; - } + sqlDialect = "tree"; + auto newSession = std::make_shared(this); + newSession->open(false); + return newSession; + } }; #endif // IOTDB_SESSION_BUILDER_H \ No newline at end of file diff --git a/iotdb-client/client-cpp/src/main/SessionConnection.cpp b/iotdb-client/client-cpp/src/main/SessionConnection.cpp index e4d369374c84e..a7fa3beb6ffc4 100644 --- a/iotdb-client/client-cpp/src/main/SessionConnection.cpp +++ b/iotdb-client/client-cpp/src/main/SessionConnection.cpp @@ -31,666 +31,617 @@ using namespace apache::thrift::transport; SessionConnection::SessionConnection(Session* session_ptr, const TEndPoint& endpoint, const std::string& zoneId, - std::shared_ptr nodeSupplier, - int fetchSize, - int maxRetries, - int64_t retryInterval, - int64_t connectionTimeout, - std::string dialect, - std::string db) - : session(session_ptr), - zoneId(zoneId), - endPoint(endpoint), - availableNodes(std::move(nodeSupplier)), - fetchSize(fetchSize), - maxRetryCount(maxRetries), - retryIntervalMs(retryInterval), - connectionTimeoutInMs(connectionTimeout), - sqlDialect(std::move(dialect)), - database(std::move(db)) { - this->zoneId = zoneId.empty() ? getSystemDefaultZoneId() : zoneId; - endPointList.push_back(endpoint); - init(endPoint, session->useSSL_, session->trustCertFilePath_); + std::shared_ptr nodeSupplier, int fetchSize, + int maxRetries, int64_t retryInterval, + int64_t connectionTimeout, std::string dialect, std::string db) + : session(session_ptr), zoneId(zoneId), endPoint(endpoint), + availableNodes(std::move(nodeSupplier)), fetchSize(fetchSize), maxRetryCount(maxRetries), + retryIntervalMs(retryInterval), connectionTimeoutInMs(connectionTimeout), + sqlDialect(std::move(dialect)), database(std::move(db)) { + this->zoneId = zoneId.empty() ? getSystemDefaultZoneId() : zoneId; + endPointList.push_back(endpoint); + init(endPoint, session->useSSL_, session->trustCertFilePath_); } void SessionConnection::close() { - bool needThrowException = false; - string errMsg; - session = nullptr; - try { - TSCloseSessionReq req; - req.__set_sessionId(sessionId); - TSStatus tsStatus; - client->closeSession(tsStatus, req); - } - catch (const TTransportException& e) { - log_debug(e.what()); - throw IoTDBConnectionException(e.what()); - } catch (const exception& e) { - log_debug(e.what()); - errMsg = errMsg + "Session::close() client->closeSession() error, maybe remote server is down. " + e.what() + - "\n"; - needThrowException = true; - } - - try { - if (transport->isOpen()) { - transport->close(); - } - } - catch (const exception& e) { - log_debug(e.what()); - errMsg = errMsg + "Session::close() transport->close() error. " + e.what() + "\n"; - needThrowException = true; + bool needThrowException = false; + string errMsg; + session = nullptr; + try { + TSCloseSessionReq req; + req.__set_sessionId(sessionId); + TSStatus tsStatus; + client->closeSession(tsStatus, req); + } catch (const TTransportException& e) { + log_debug(e.what()); + throw IoTDBConnectionException(e.what()); + } catch (const exception& e) { + log_debug(e.what()); + errMsg = errMsg + + "Session::close() client->closeSession() error, maybe remote server is down. " + + e.what() + "\n"; + needThrowException = true; + } + + try { + if (transport->isOpen()) { + transport->close(); } + } catch (const exception& e) { + log_debug(e.what()); + errMsg = errMsg + "Session::close() transport->close() error. " + e.what() + "\n"; + needThrowException = true; + } - if (needThrowException) { - throw IoTDBException(errMsg); - } + if (needThrowException) { + throw IoTDBException(errMsg); + } } SessionConnection::~SessionConnection() { - try { - close(); - } - catch (const exception& e) { - log_debug(e.what()); - } + try { + close(); + } catch (const exception& e) { + log_debug(e.what()); + } } -void SessionConnection::init(const TEndPoint& endpoint, bool useSSL, const std::string& trustCertFilePath) { - if (useSSL) { +void SessionConnection::init(const TEndPoint& endpoint, bool useSSL, + const std::string& trustCertFilePath) { + if (useSSL) { #if WITH_SSL - socketFactory_->loadTrustedCertificates(trustCertFilePath.c_str()); - socketFactory_->authenticate(false); - auto sslSocket = socketFactory_->createSocket(endPoint.ip, endPoint.port); - sslSocket->setConnTimeout(connectionTimeoutInMs); - transport = std::make_shared(sslSocket); + socketFactory_->loadTrustedCertificates(trustCertFilePath.c_str()); + socketFactory_->authenticate(false); + auto sslSocket = socketFactory_->createSocket(endPoint.ip, endPoint.port); + sslSocket->setConnTimeout(connectionTimeoutInMs); + transport = std::make_shared(sslSocket); #else - throw IoTDBException("SSL/TLS support is not enabled in this build. " - "Please rebuild with -DWITH_SSL=ON flag " - "or use non-SSL connection."); + throw IoTDBException("SSL/TLS support is not enabled in this build. " + "Please rebuild with -DWITH_SSL=ON flag " + "or use non-SSL connection."); #endif - } else { - auto socket = std::make_shared(endPoint.ip, endPoint.port); - socket->setConnTimeout(connectionTimeoutInMs); - transport = std::make_shared(socket); - } - if (!transport->isOpen()) { - try { - transport->open(); - } - catch (TTransportException& e) { - log_debug(e.what()); - throw IoTDBConnectionException(e.what()); - } - } - if (enableRPCCompression) { - shared_ptr protocol(new TCompactProtocol(transport)); - client = std::make_shared(protocol); - } - else { - shared_ptr protocol(new TBinaryProtocol(transport)); - client = std::make_shared(protocol); - } - - std::map configuration; - configuration["version"] = session->getVersionString(session->version); - configuration["sql_dialect"] = sqlDialect; - if (database != "") { - configuration["db"] = database; - } - TSOpenSessionReq openReq; - openReq.__set_username(session->username_); - openReq.__set_password(session->password_); - openReq.__set_zoneId(zoneId); - openReq.__set_configuration(configuration); + } else { + auto socket = std::make_shared(endPoint.ip, endPoint.port); + socket->setConnTimeout(connectionTimeoutInMs); + transport = std::make_shared(socket); + } + if (!transport->isOpen()) { try { - TSOpenSessionResp openResp; - client->openSession(openResp, openReq); - RpcUtils::verifySuccess(openResp.status); - if (session->protocolVersion_ != openResp.serverProtocolVersion) { - if (openResp.serverProtocolVersion == 0) { - // less than 0.10 - throw logic_error(string("Protocol not supported, Client version is ") + - to_string(session->protocolVersion_) + - ", but Server version is " + to_string(openResp.serverProtocolVersion)); - } - } - - sessionId = openResp.sessionId; - statementId = client->requestStatementId(sessionId); - - if (!zoneId.empty()) { - setTimeZone(zoneId); - } + transport->open(); + } catch (TTransportException& e) { + log_debug(e.what()); + throw IoTDBConnectionException(e.what()); } - catch (const TTransportException& e) { - log_debug(e.what()); - transport->close(); - throw IoTDBConnectionException(e.what()); - } catch (const IoTDBException& e) { - log_debug(e.what()); - transport->close(); - throw; - } catch (const exception& e) { - log_debug(e.what()); - transport->close(); - throw; + } + if (enableRPCCompression) { + shared_ptr protocol(new TCompactProtocol(transport)); + client = std::make_shared(protocol); + } else { + shared_ptr protocol(new TBinaryProtocol(transport)); + client = std::make_shared(protocol); + } + + std::map configuration; + configuration["version"] = session->getVersionString(session->version); + configuration["sql_dialect"] = sqlDialect; + if (database != "") { + configuration["db"] = database; + } + TSOpenSessionReq openReq; + openReq.__set_username(session->username_); + openReq.__set_password(session->password_); + openReq.__set_zoneId(zoneId); + openReq.__set_configuration(configuration); + try { + TSOpenSessionResp openResp; + client->openSession(openResp, openReq); + RpcUtils::verifySuccess(openResp.status); + if (session->protocolVersion_ != openResp.serverProtocolVersion) { + if (openResp.serverProtocolVersion == 0) { + // less than 0.10 + throw logic_error(string("Protocol not supported, Client version is ") + + to_string(session->protocolVersion_) + ", but Server version is " + + to_string(openResp.serverProtocolVersion)); + } } -} -std::unique_ptr SessionConnection::executeQueryStatement(const std::string& sql, int64_t timeoutInMs) { - TSExecuteStatementReq req; - req.__set_sessionId(sessionId); - req.__set_statementId(statementId); - req.__set_statement(sql); - req.__set_timeout(timeoutInMs); - req.__set_enableRedirectQuery(true); - - auto result = callWithRetryAndReconnect( - [this, &req]() { - TSExecuteStatementResp resp; - client->executeQueryStatementV2(resp, req); - return resp; - }, - [](const TSExecuteStatementResp& resp) { - return resp.status; - } - ); - TSExecuteStatementResp resp = result.getResult(); - if (result.getRetryAttempts() == 0) { - RpcUtils::verifySuccessWithRedirection(resp.status); - } - else { - RpcUtils::verifySuccess(resp.status); - } + sessionId = openResp.sessionId; + statementId = client->requestStatementId(sessionId); - return std::unique_ptr(new SessionDataSet(sql, resp.columns, resp.dataTypeList, - resp.columnNameIndexMap, resp.queryId, statementId, - client, sessionId, resp.queryResult, resp.ignoreTimeStamp, - timeoutInMs, resp.moreData, fetchSize, zoneId, - timeFactor, resp.columnIndex2TsBlockColumnIndexList)); -} - -std::unique_ptr SessionConnection::executeRawDataQuery(const std::vector& paths, - int64_t startTime, int64_t endTime) { - TSRawDataQueryReq req; - req.__set_sessionId(sessionId); - req.__set_statementId(statementId); - req.__set_fetchSize(fetchSize); - req.__set_paths(paths); - req.__set_startTime(startTime); - req.__set_endTime(endTime); - auto result = callWithRetryAndReconnect( - [this, &req]() { - TSExecuteStatementResp resp; - client->executeRawDataQueryV2(resp, req); - return resp; - }, - [](const TSExecuteStatementResp& resp) { - return resp.status; - } - ); - TSExecuteStatementResp resp = result.getResult(); - if (result.getRetryAttempts() == 0) { - RpcUtils::verifySuccessWithRedirection(resp.status); + if (!zoneId.empty()) { + setTimeZone(zoneId); } - else { - RpcUtils::verifySuccess(resp.status); - } - return std::unique_ptr(new SessionDataSet("", resp.columns, resp.dataTypeList, - resp.columnNameIndexMap, resp.queryId, statementId, - client, sessionId, resp.queryResult, resp.ignoreTimeStamp, - connectionTimeoutInMs, resp.moreData, fetchSize, zoneId, - timeFactor, resp.columnIndex2TsBlockColumnIndexList)); -} - -std::unique_ptr SessionConnection::executeLastDataQuery(const std::vector& paths, - int64_t lastTime) { - TSLastDataQueryReq req; - req.__set_sessionId(sessionId); - req.__set_statementId(statementId); - req.__set_fetchSize(fetchSize); - req.__set_paths(paths); - req.__set_time(lastTime); - - auto result = callWithRetryAndReconnect( - [this, &req]() { - TSExecuteStatementResp resp; - client->executeLastDataQuery(resp, req); - return resp; - }, - [](const TSExecuteStatementResp& resp) { - return resp.status; - } - ); - TSExecuteStatementResp resp = result.getResult(); - if (result.getRetryAttempts() == 0) { - RpcUtils::verifySuccessWithRedirection(resp.status); - } - else { - RpcUtils::verifySuccess(resp.status); - } - return std::unique_ptr(new SessionDataSet("", resp.columns, resp.dataTypeList, - resp.columnNameIndexMap, resp.queryId, statementId, - client, sessionId, resp.queryResult, resp.ignoreTimeStamp, - connectionTimeoutInMs, resp.moreData, fetchSize, zoneId, - timeFactor, resp.columnIndex2TsBlockColumnIndexList)); + } catch (const TTransportException& e) { + log_debug(e.what()); + transport->close(); + throw IoTDBConnectionException(e.what()); + } catch (const IoTDBException& e) { + log_debug(e.what()); + transport->close(); + throw; + } catch (const exception& e) { + log_debug(e.what()); + transport->close(); + throw; + } +} + +std::unique_ptr SessionConnection::executeQueryStatement(const std::string& sql, + int64_t timeoutInMs) { + TSExecuteStatementReq req; + req.__set_sessionId(sessionId); + req.__set_statementId(statementId); + req.__set_statement(sql); + req.__set_timeout(timeoutInMs); + req.__set_enableRedirectQuery(true); + + auto result = callWithRetryAndReconnect( + [this, &req]() { + TSExecuteStatementResp resp; + client->executeQueryStatementV2(resp, req); + return resp; + }, + [](const TSExecuteStatementResp& resp) { return resp.status; }); + TSExecuteStatementResp resp = result.getResult(); + if (result.getRetryAttempts() == 0) { + RpcUtils::verifySuccessWithRedirection(resp.status); + } else { + RpcUtils::verifySuccess(resp.status); + } + + return std::unique_ptr(new SessionDataSet( + sql, resp.columns, resp.dataTypeList, resp.columnNameIndexMap, resp.queryId, statementId, + client, sessionId, resp.queryResult, resp.ignoreTimeStamp, timeoutInMs, resp.moreData, + fetchSize, zoneId, timeFactor, resp.columnIndex2TsBlockColumnIndexList)); +} + +std::unique_ptr +SessionConnection::executeRawDataQuery(const std::vector& paths, int64_t startTime, + int64_t endTime) { + TSRawDataQueryReq req; + req.__set_sessionId(sessionId); + req.__set_statementId(statementId); + req.__set_fetchSize(fetchSize); + req.__set_paths(paths); + req.__set_startTime(startTime); + req.__set_endTime(endTime); + auto result = callWithRetryAndReconnect( + [this, &req]() { + TSExecuteStatementResp resp; + client->executeRawDataQueryV2(resp, req); + return resp; + }, + [](const TSExecuteStatementResp& resp) { return resp.status; }); + TSExecuteStatementResp resp = result.getResult(); + if (result.getRetryAttempts() == 0) { + RpcUtils::verifySuccessWithRedirection(resp.status); + } else { + RpcUtils::verifySuccess(resp.status); + } + return std::unique_ptr(new SessionDataSet( + "", resp.columns, resp.dataTypeList, resp.columnNameIndexMap, resp.queryId, statementId, + client, sessionId, resp.queryResult, resp.ignoreTimeStamp, connectionTimeoutInMs, + resp.moreData, fetchSize, zoneId, timeFactor, resp.columnIndex2TsBlockColumnIndexList)); +} + +std::unique_ptr +SessionConnection::executeLastDataQuery(const std::vector& paths, int64_t lastTime) { + TSLastDataQueryReq req; + req.__set_sessionId(sessionId); + req.__set_statementId(statementId); + req.__set_fetchSize(fetchSize); + req.__set_paths(paths); + req.__set_time(lastTime); + + auto result = callWithRetryAndReconnect( + [this, &req]() { + TSExecuteStatementResp resp; + client->executeLastDataQuery(resp, req); + return resp; + }, + [](const TSExecuteStatementResp& resp) { return resp.status; }); + TSExecuteStatementResp resp = result.getResult(); + if (result.getRetryAttempts() == 0) { + RpcUtils::verifySuccessWithRedirection(resp.status); + } else { + RpcUtils::verifySuccess(resp.status); + } + return std::unique_ptr(new SessionDataSet( + "", resp.columns, resp.dataTypeList, resp.columnNameIndexMap, resp.queryId, statementId, + client, sessionId, resp.queryResult, resp.ignoreTimeStamp, connectionTimeoutInMs, + resp.moreData, fetchSize, zoneId, timeFactor, resp.columnIndex2TsBlockColumnIndexList)); } void SessionConnection::executeNonQueryStatement(const string& sql) { - TSExecuteStatementReq req; - req.__set_sessionId(sessionId); - req.__set_statementId(statementId); - req.__set_statement(sql); - req.__set_timeout(0); //0 means no timeout. This value keep consistent to JAVA SDK. - TSExecuteStatementResp resp; - try { - client->executeUpdateStatementV2(resp, req); - if (resp.database != "") { - database = resp.database; - session->database_ = database; - } - RpcUtils::verifySuccess(resp.status); - } - 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) { - throw IoTDBException(e.what()); + TSExecuteStatementReq req; + req.__set_sessionId(sessionId); + req.__set_statementId(statementId); + req.__set_statement(sql); + req.__set_timeout(0); //0 means no timeout. This value keep consistent to JAVA SDK. + TSExecuteStatementResp resp; + try { + client->executeUpdateStatementV2(resp, req); + if (resp.database != "") { + database = resp.database; + session->database_ = database; } + RpcUtils::verifySuccess(resp.status); + } 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) { + throw IoTDBException(e.what()); + } } const TEndPoint& SessionConnection::getEndPoint() { - return endPoint; + return endPoint; } void SessionConnection::setTimeZone(const std::string& newZoneId) { - TSSetTimeZoneReq req; - req.__set_sessionId(sessionId); - req.__set_timeZone(newZoneId); + TSSetTimeZoneReq req; + req.__set_sessionId(sessionId); + req.__set_timeZone(newZoneId); - try { - TSStatus tsStatus; - client->setTimeZone(tsStatus, req); - zoneId = newZoneId; - } - catch (const TException& e) { - throw IoTDBConnectionException(e.what()); - } + try { + TSStatus tsStatus; + client->setTimeZone(tsStatus, req); + zoneId = newZoneId; + } catch (const TException& e) { + throw IoTDBConnectionException(e.what()); + } } std::string SessionConnection::getSystemDefaultZoneId() { - time_t ts = 0; - struct tm tmv{}; -#if defined(_WIN64) || defined (WIN32) || defined (_WIN32) - localtime_s(&tmv, &ts); + time_t ts = 0; + struct tm tmv {}; +#if defined(_WIN64) || defined(WIN32) || defined(_WIN32) + localtime_s(&tmv, &ts); #else - localtime_r(&ts, &tmv); + localtime_r(&ts, &tmv); #endif - char zoneStr[32]; - strftime(zoneStr, sizeof(zoneStr), "%z", &tmv); - return zoneStr; + char zoneStr[32]; + strftime(zoneStr, sizeof(zoneStr), "%z", &tmv); + return zoneStr; } bool SessionConnection::reconnect() { - bool reconnect = false; - for (int i = 1; i <= 3; i++) { - if (transport != nullptr) { - transport->close(); - endPointList = std::move(availableNodes->getEndPointList()); - int currHostIndex = rand() % endPointList.size(); - int tryHostNum = 0; - for (int j = currHostIndex; j < endPointList.size(); j++) { - if (tryHostNum == endPointList.size()) { - break; - } - this->endPoint = endPointList[j]; - if (j == endPointList.size() - 1) { - j = -1; - } - tryHostNum++; - try { - init(this->endPoint, this->session->useSSL_, this->session->trustCertFilePath_); - reconnect = true; - } - catch (const IoTDBConnectionException& e) { - log_warn("The current node may have been down, connection exception: %s", e.what()); - continue; - } catch (exception& e) { - log_warn("login in failed, because %s", e.what()); - } - break; - } + bool reconnect = false; + for (int i = 1; i <= 3; i++) { + if (transport != nullptr) { + transport->close(); + endPointList = std::move(availableNodes->getEndPointList()); + int currHostIndex = rand() % endPointList.size(); + int tryHostNum = 0; + for (int j = currHostIndex; j < endPointList.size(); j++) { + if (tryHostNum == endPointList.size()) { + break; } - if (reconnect) { - session->removeBrokenSessionConnection(shared_from_this()); - session->defaultEndPoint_ = this->endPoint; - session->defaultSessionConnection_ = shared_from_this(); - session->endPointToSessionConnection.insert(make_pair(this->endPoint, shared_from_this())); + this->endPoint = endPointList[j]; + if (j == endPointList.size() - 1) { + j = -1; } + tryHostNum++; + try { + init(this->endPoint, this->session->useSSL_, this->session->trustCertFilePath_); + reconnect = true; + } catch (const IoTDBConnectionException& e) { + log_warn("The current node may have been down, connection exception: %s", e.what()); + continue; + } catch (exception& e) { + log_warn("login in failed, because %s", e.what()); + } + break; + } } - return reconnect; + if (reconnect) { + session->removeBrokenSessionConnection(shared_from_this()); + session->defaultEndPoint_ = this->endPoint; + session->defaultSessionConnection_ = shared_from_this(); + session->endPointToSessionConnection.insert(make_pair(this->endPoint, shared_from_this())); + } + } + return reconnect; } void SessionConnection::insertStringRecord(const TSInsertStringRecordReq& request) { - auto rpc = [this, request]() { - return this->insertStringRecordInternal(request); - }; - callWithRetryAndVerifyWithRedirection(rpc); + auto rpc = [this, request]() { return this->insertStringRecordInternal(request); }; + callWithRetryAndVerifyWithRedirection(rpc); } void SessionConnection::insertRecord(const TSInsertRecordReq& request) { - auto rpc = [this, request]() { - return this->insertRecordInternal(request); - }; - callWithRetryAndVerifyWithRedirection(rpc); + auto rpc = [this, request]() { return this->insertRecordInternal(request); }; + callWithRetryAndVerifyWithRedirection(rpc); } void SessionConnection::insertStringRecords(const TSInsertStringRecordsReq& request) { - auto rpc = [this, request]() { - return this->insertStringRecordsInternal(request); - }; - callWithRetryAndVerifyWithRedirection(rpc); + auto rpc = [this, request]() { return this->insertStringRecordsInternal(request); }; + callWithRetryAndVerifyWithRedirection(rpc); } void SessionConnection::insertRecords(const TSInsertRecordsReq& request) { - auto rpc = [this, request]() { - return this->insertRecordsInternal(request); - }; - callWithRetryAndVerifyWithRedirection(rpc); + auto rpc = [this, request]() { return this->insertRecordsInternal(request); }; + callWithRetryAndVerifyWithRedirection(rpc); } void SessionConnection::insertRecordsOfOneDevice(TSInsertRecordsOfOneDeviceReq request) { - auto rpc = [this, request]() { - return this->insertRecordsOfOneDeviceInternal(request); - }; - callWithRetryAndVerifyWithRedirection(rpc); + auto rpc = [this, request]() { return this->insertRecordsOfOneDeviceInternal(request); }; + callWithRetryAndVerifyWithRedirection(rpc); } -void SessionConnection::insertStringRecordsOfOneDevice(TSInsertStringRecordsOfOneDeviceReq request) { - auto rpc = [this, request]() { - return this->insertStringRecordsOfOneDeviceInternal(request); - }; - callWithRetryAndVerifyWithRedirection(rpc); +void SessionConnection::insertStringRecordsOfOneDevice( + TSInsertStringRecordsOfOneDeviceReq request) { + auto rpc = [this, request]() { return this->insertStringRecordsOfOneDeviceInternal(request); }; + callWithRetryAndVerifyWithRedirection(rpc); } void SessionConnection::insertTablet(TSInsertTabletReq request) { - auto rpc = [this, request]() { - return this->insertTabletInternal(request); - }; - callWithRetryAndVerifyWithRedirection(rpc); + auto rpc = [this, request]() { return this->insertTabletInternal(request); }; + callWithRetryAndVerifyWithRedirection(rpc); } void SessionConnection::insertTablets(TSInsertTabletsReq request) { - auto rpc = [this, request]() { - return this->insertTabletsInternal(request); - }; - callWithRetryAndVerifyWithRedirection(rpc); + auto rpc = [this, request]() { return this->insertTabletsInternal(request); }; + callWithRetryAndVerifyWithRedirection(rpc); } void SessionConnection::testInsertStringRecord(TSInsertStringRecordReq& request) { - auto rpc = [this, &request]() { - request.sessionId = sessionId; - TSStatus ret; - client->testInsertStringRecord(ret, request); - return ret; - }; - auto status = callWithRetryAndReconnect(rpc).getResult(); - RpcUtils::verifySuccess(status); + auto rpc = [this, &request]() { + request.sessionId = sessionId; + TSStatus ret; + client->testInsertStringRecord(ret, request); + return ret; + }; + auto status = callWithRetryAndReconnect(rpc).getResult(); + RpcUtils::verifySuccess(status); } void SessionConnection::testInsertTablet(TSInsertTabletReq& request) { - auto rpc = [this, &request]() { - request.sessionId = sessionId; - TSStatus ret; - client->testInsertTablet(ret, request); - return ret; - }; - auto status = callWithRetryAndReconnect(rpc).getResult(); - RpcUtils::verifySuccess(status); + auto rpc = [this, &request]() { + request.sessionId = sessionId; + TSStatus ret; + client->testInsertTablet(ret, request); + return ret; + }; + auto status = callWithRetryAndReconnect(rpc).getResult(); + RpcUtils::verifySuccess(status); } void SessionConnection::testInsertRecords(TSInsertRecordsReq& request) { - auto rpc = [this, &request]() { - request.sessionId = sessionId; - TSStatus ret; - client->testInsertRecords(ret, request); - return ret; - }; - auto status = callWithRetryAndReconnect(rpc).getResult(); - RpcUtils::verifySuccess(status); + auto rpc = [this, &request]() { + request.sessionId = sessionId; + TSStatus ret; + client->testInsertRecords(ret, request); + return ret; + }; + auto status = callWithRetryAndReconnect(rpc).getResult(); + RpcUtils::verifySuccess(status); } void SessionConnection::deleteTimeseries(const vector& paths) { - auto rpc = [this, &paths]() { - TSStatus ret; - client->deleteTimeseries(ret, sessionId, paths); - return ret; - }; - callWithRetryAndVerify(rpc); + auto rpc = [this, &paths]() { + TSStatus ret; + client->deleteTimeseries(ret, sessionId, paths); + return ret; + }; + callWithRetryAndVerify(rpc); } void SessionConnection::deleteData(const TSDeleteDataReq& request) { - auto rpc = [this, request]() { - return this->deleteDataInternal(request); - }; - callWithRetryAndVerify(rpc); + auto rpc = [this, request]() { return this->deleteDataInternal(request); }; + callWithRetryAndVerify(rpc); } void SessionConnection::setStorageGroup(const string& storageGroupId) { - auto rpc = [this, &storageGroupId]() { - TSStatus ret; - client->setStorageGroup(ret, sessionId, storageGroupId); - return ret; - }; - auto ret = callWithRetryAndReconnect(rpc); - RpcUtils::verifySuccess(ret.getResult()); + auto rpc = [this, &storageGroupId]() { + TSStatus ret; + client->setStorageGroup(ret, sessionId, storageGroupId); + return ret; + }; + auto ret = callWithRetryAndReconnect(rpc); + RpcUtils::verifySuccess(ret.getResult()); } void SessionConnection::deleteStorageGroups(const vector& storageGroups) { - auto rpc = [this, &storageGroups]() { - TSStatus ret; - client->deleteStorageGroups(ret, sessionId, storageGroups); - return ret; - }; - auto ret = callWithRetryAndReconnect(rpc); - RpcUtils::verifySuccess(ret.getResult()); + auto rpc = [this, &storageGroups]() { + TSStatus ret; + client->deleteStorageGroups(ret, sessionId, storageGroups); + return ret; + }; + auto ret = callWithRetryAndReconnect(rpc); + RpcUtils::verifySuccess(ret.getResult()); } void SessionConnection::createTimeseries(TSCreateTimeseriesReq& req) { - auto rpc = [this, &req]() { - TSStatus ret; - req.sessionId = sessionId; - client->createTimeseries(ret, req); - return ret; - }; - auto ret = callWithRetryAndReconnect(rpc); - RpcUtils::verifySuccess(ret.getResult()); + auto rpc = [this, &req]() { + TSStatus ret; + req.sessionId = sessionId; + client->createTimeseries(ret, req); + return ret; + }; + auto ret = callWithRetryAndReconnect(rpc); + RpcUtils::verifySuccess(ret.getResult()); } void SessionConnection::createMultiTimeseries(TSCreateMultiTimeseriesReq& req) { - auto rpc = [this, &req]() { - TSStatus ret; - req.sessionId = sessionId; - client->createMultiTimeseries(ret, req); - return ret; - }; - auto ret = callWithRetryAndReconnect(rpc); - RpcUtils::verifySuccess(ret.getResult()); + auto rpc = [this, &req]() { + TSStatus ret; + req.sessionId = sessionId; + client->createMultiTimeseries(ret, req); + return ret; + }; + auto ret = callWithRetryAndReconnect(rpc); + RpcUtils::verifySuccess(ret.getResult()); } void SessionConnection::createAlignedTimeseries(TSCreateAlignedTimeseriesReq& req) { - auto rpc = [this, &req]() { - TSStatus ret; - req.sessionId = sessionId; - client->createAlignedTimeseries(ret, req); - return ret; - }; - auto ret = callWithRetryAndReconnect(rpc); - RpcUtils::verifySuccess(ret.getResult()); + auto rpc = [this, &req]() { + TSStatus ret; + req.sessionId = sessionId; + client->createAlignedTimeseries(ret, req); + return ret; + }; + auto ret = callWithRetryAndReconnect(rpc); + RpcUtils::verifySuccess(ret.getResult()); } TSGetTimeZoneResp SessionConnection::getTimeZone() { - auto rpc = [this]() { - TSGetTimeZoneResp resp; - client->getTimeZone(resp, sessionId); - zoneId = resp.timeZone; - return resp; - }; - auto ret = callWithRetryAndReconnect(rpc, - [](const TSGetTimeZoneResp& resp) { - return resp.status; - }); - RpcUtils::verifySuccess(ret.getResult().status); - return ret.result; + auto rpc = [this]() { + TSGetTimeZoneResp resp; + client->getTimeZone(resp, sessionId); + zoneId = resp.timeZone; + return resp; + }; + auto ret = callWithRetryAndReconnect( + rpc, [](const TSGetTimeZoneResp& resp) { return resp.status; }); + RpcUtils::verifySuccess(ret.getResult().status); + return ret.result; } void SessionConnection::setTimeZone(TSSetTimeZoneReq& req) { - auto rpc = [this, &req]() { - TSStatus ret; - req.sessionId = sessionId; - client->setTimeZone(ret, req); - zoneId = req.timeZone; - return ret; - }; - auto ret = callWithRetryAndReconnect(rpc); - RpcUtils::verifySuccess(ret.getResult()); + auto rpc = [this, &req]() { + TSStatus ret; + req.sessionId = sessionId; + client->setTimeZone(ret, req); + zoneId = req.timeZone; + return ret; + }; + auto ret = callWithRetryAndReconnect(rpc); + RpcUtils::verifySuccess(ret.getResult()); } void SessionConnection::createSchemaTemplate(TSCreateSchemaTemplateReq req) { - auto rpc = [this, &req]() { - TSStatus ret; - req.sessionId = sessionId; - client->createSchemaTemplate(ret, req); - return ret; - }; - auto ret = callWithRetryAndReconnect(rpc); - RpcUtils::verifySuccess(ret.getResult()); + auto rpc = [this, &req]() { + TSStatus ret; + req.sessionId = sessionId; + client->createSchemaTemplate(ret, req); + return ret; + }; + auto ret = callWithRetryAndReconnect(rpc); + RpcUtils::verifySuccess(ret.getResult()); } void SessionConnection::setSchemaTemplate(TSSetSchemaTemplateReq req) { - auto rpc = [this, &req]() { - TSStatus ret; - req.sessionId = sessionId; - client->setSchemaTemplate(ret, req); - return ret; - }; - auto ret = callWithRetryAndReconnect(rpc); - RpcUtils::verifySuccess(ret.getResult()); + auto rpc = [this, &req]() { + TSStatus ret; + req.sessionId = sessionId; + client->setSchemaTemplate(ret, req); + return ret; + }; + auto ret = callWithRetryAndReconnect(rpc); + RpcUtils::verifySuccess(ret.getResult()); } void SessionConnection::unsetSchemaTemplate(TSUnsetSchemaTemplateReq req) { - auto rpc = [this, &req]() { - TSStatus ret; - req.sessionId = sessionId; - client->unsetSchemaTemplate(ret, req); - return ret; - }; - auto ret = callWithRetryAndReconnect(rpc); - RpcUtils::verifySuccess(ret.getResult()); + auto rpc = [this, &req]() { + TSStatus ret; + req.sessionId = sessionId; + client->unsetSchemaTemplate(ret, req); + return ret; + }; + auto ret = callWithRetryAndReconnect(rpc); + RpcUtils::verifySuccess(ret.getResult()); } void SessionConnection::appendSchemaTemplate(TSAppendSchemaTemplateReq req) { - auto rpc = [this, &req]() { - TSStatus ret; - req.sessionId = sessionId; - client->appendSchemaTemplate(ret, req); - return ret; - }; - auto ret = callWithRetryAndReconnect(rpc); - RpcUtils::verifySuccess(ret.getResult()); + auto rpc = [this, &req]() { + TSStatus ret; + req.sessionId = sessionId; + client->appendSchemaTemplate(ret, req); + return ret; + }; + auto ret = callWithRetryAndReconnect(rpc); + RpcUtils::verifySuccess(ret.getResult()); } void SessionConnection::pruneSchemaTemplate(TSPruneSchemaTemplateReq req) { - auto rpc = [this, &req]() { - TSStatus ret; - req.sessionId = sessionId; - client->pruneSchemaTemplate(ret, req); - return ret; - }; - auto ret = callWithRetryAndReconnect(rpc); - RpcUtils::verifySuccess(ret.getResult()); + auto rpc = [this, &req]() { + TSStatus ret; + req.sessionId = sessionId; + client->pruneSchemaTemplate(ret, req); + return ret; + }; + auto ret = callWithRetryAndReconnect(rpc); + RpcUtils::verifySuccess(ret.getResult()); } TSQueryTemplateResp SessionConnection::querySchemaTemplate(TSQueryTemplateReq req) { - auto rpc = [this, &req]() { - TSQueryTemplateResp ret; - req.sessionId = sessionId; - client->querySchemaTemplate(ret, req); - return ret; - }; - auto ret = callWithRetryAndReconnect(rpc, - [](const TSQueryTemplateResp& resp) { - return resp.status; - }); - RpcUtils::verifySuccess(ret.getResult().status); - return ret.getResult(); + auto rpc = [this, &req]() { + TSQueryTemplateResp ret; + req.sessionId = sessionId; + client->querySchemaTemplate(ret, req); + return ret; + }; + auto ret = callWithRetryAndReconnect( + rpc, [](const TSQueryTemplateResp& resp) { return resp.status; }); + RpcUtils::verifySuccess(ret.getResult().status); + return ret.getResult(); } TSStatus SessionConnection::insertStringRecordInternal(TSInsertStringRecordReq request) { - request.sessionId = sessionId; - TSStatus ret; - client->insertStringRecord(ret, request); - return ret; + request.sessionId = sessionId; + TSStatus ret; + client->insertStringRecord(ret, request); + return ret; } TSStatus SessionConnection::insertRecordInternal(TSInsertRecordReq request) { - request.sessionId = sessionId; - TSStatus ret; - client->insertRecord(ret, request); - return ret; + request.sessionId = sessionId; + TSStatus ret; + client->insertRecord(ret, request); + return ret; } TSStatus SessionConnection::insertStringRecordsInternal(TSInsertStringRecordsReq request) { - request.sessionId = sessionId; - TSStatus ret; - client->insertStringRecords(ret, request); - return ret; + request.sessionId = sessionId; + TSStatus ret; + client->insertStringRecords(ret, request); + return ret; } TSStatus SessionConnection::insertRecordsInternal(TSInsertRecordsReq request) { - request.sessionId = sessionId; - TSStatus ret; - client->insertRecords(ret, request); - return ret; + request.sessionId = sessionId; + TSStatus ret; + client->insertRecords(ret, request); + return ret; } -TSStatus SessionConnection::insertRecordsOfOneDeviceInternal(TSInsertRecordsOfOneDeviceReq request) { - request.sessionId = sessionId; - TSStatus ret; - client->insertRecordsOfOneDevice(ret, request); - return ret; +TSStatus +SessionConnection::insertRecordsOfOneDeviceInternal(TSInsertRecordsOfOneDeviceReq request) { + request.sessionId = sessionId; + TSStatus ret; + client->insertRecordsOfOneDevice(ret, request); + return ret; } -TSStatus SessionConnection::insertStringRecordsOfOneDeviceInternal(TSInsertStringRecordsOfOneDeviceReq request) { - request.sessionId = sessionId; - TSStatus ret; - client->insertStringRecordsOfOneDevice(ret, request); - return ret; +TSStatus SessionConnection::insertStringRecordsOfOneDeviceInternal( + TSInsertStringRecordsOfOneDeviceReq request) { + request.sessionId = sessionId; + TSStatus ret; + client->insertStringRecordsOfOneDevice(ret, request); + return ret; } TSStatus SessionConnection::insertTabletInternal(TSInsertTabletReq request) { - request.sessionId = sessionId; - TSStatus ret; - client->insertTablet(ret, request); - return ret; + request.sessionId = sessionId; + TSStatus ret; + client->insertTablet(ret, request); + return ret; } TSStatus SessionConnection::insertTabletsInternal(TSInsertTabletsReq request) { - request.sessionId = sessionId; - TSStatus ret; - client->insertTablets(ret, request); - return ret; + request.sessionId = sessionId; + TSStatus ret; + client->insertTablets(ret, request); + return ret; } TSStatus SessionConnection::deleteDataInternal(TSDeleteDataReq request) { - request.sessionId = sessionId; - TSStatus ret; - client->deleteData(ret, request); - return ret; + request.sessionId = sessionId; + TSStatus ret; + client->deleteData(ret, request); + return ret; } diff --git a/iotdb-client/client-cpp/src/main/SessionConnection.h b/iotdb-client/client-cpp/src/main/SessionConnection.h index a4ef7a7d64c48..031b7d3a8926f 100644 --- a/iotdb-client/client-cpp/src/main/SessionConnection.h +++ b/iotdb-client/client-cpp/src/main/SessionConnection.h @@ -37,336 +37,320 @@ class Session; class SessionConnection : public std::enable_shared_from_this { public: - SessionConnection(Session* session_ptr, const TEndPoint& endpoint, - const std::string& zoneId, - std::shared_ptr nodeSupplier, - int fetchSize = 10000, - int maxRetries = 3, - int64_t retryInterval = 500, - int64_t connectionTimeoutMs = 3 * 1000, - std::string dialect = "tree", - std::string db = ""); + SessionConnection(Session* session_ptr, const TEndPoint& endpoint, const std::string& zoneId, + std::shared_ptr nodeSupplier, int fetchSize = 10000, + int maxRetries = 3, int64_t retryInterval = 500, + int64_t connectionTimeoutMs = 3 * 1000, std::string dialect = "tree", + std::string db = ""); - ~SessionConnection(); + ~SessionConnection(); - void setTimeZone(const std::string& newZoneId); + void setTimeZone(const std::string& newZoneId); + const TEndPoint& getEndPoint(); - const TEndPoint& getEndPoint(); + void init(const TEndPoint& endpoint, bool useSSL, const std::string& trustCertFilePath); - void init(const TEndPoint& endpoint, bool useSSL, const std::string& trustCertFilePath); + void insertStringRecord(const TSInsertStringRecordReq& request); - void insertStringRecord(const TSInsertStringRecordReq& request); + void insertRecord(const TSInsertRecordReq& request); - void insertRecord(const TSInsertRecordReq& request); + void insertStringRecords(const TSInsertStringRecordsReq& request); - void insertStringRecords(const TSInsertStringRecordsReq& request); + void insertRecords(const TSInsertRecordsReq& request); - void insertRecords(const TSInsertRecordsReq& request); + void insertRecordsOfOneDevice(TSInsertRecordsOfOneDeviceReq request); - void insertRecordsOfOneDevice(TSInsertRecordsOfOneDeviceReq request); + void insertStringRecordsOfOneDevice(TSInsertStringRecordsOfOneDeviceReq request); - void insertStringRecordsOfOneDevice(TSInsertStringRecordsOfOneDeviceReq request); + void insertTablet(TSInsertTabletReq request); - void insertTablet(TSInsertTabletReq request); + void insertTablets(TSInsertTabletsReq request); - void insertTablets(TSInsertTabletsReq request); + void testInsertStringRecord(TSInsertStringRecordReq& request); - void testInsertStringRecord(TSInsertStringRecordReq& request); + void testInsertTablet(TSInsertTabletReq& request); - void testInsertTablet(TSInsertTabletReq& request); + void testInsertRecords(TSInsertRecordsReq& request); - void testInsertRecords(TSInsertRecordsReq& request); + void deleteTimeseries(const vector& paths); - void deleteTimeseries(const vector& paths); + void deleteData(const TSDeleteDataReq& request); - void deleteData(const TSDeleteDataReq& request); + void setStorageGroup(const string& storageGroupId); - void setStorageGroup(const string& storageGroupId); + void deleteStorageGroups(const vector& storageGroups); - void deleteStorageGroups(const vector& storageGroups); + void createTimeseries(TSCreateTimeseriesReq& req); - void createTimeseries(TSCreateTimeseriesReq& req); + void createMultiTimeseries(TSCreateMultiTimeseriesReq& req); - void createMultiTimeseries(TSCreateMultiTimeseriesReq& req); + void createAlignedTimeseries(TSCreateAlignedTimeseriesReq& req); - void createAlignedTimeseries(TSCreateAlignedTimeseriesReq& req); + TSGetTimeZoneResp getTimeZone(); - TSGetTimeZoneResp getTimeZone(); + void setTimeZone(TSSetTimeZoneReq& req); - void setTimeZone(TSSetTimeZoneReq& req); + void createSchemaTemplate(TSCreateSchemaTemplateReq req); - void createSchemaTemplate(TSCreateSchemaTemplateReq req); + void setSchemaTemplate(TSSetSchemaTemplateReq req); - void setSchemaTemplate(TSSetSchemaTemplateReq req); + void unsetSchemaTemplate(TSUnsetSchemaTemplateReq req); - void unsetSchemaTemplate(TSUnsetSchemaTemplateReq req); + void appendSchemaTemplate(TSAppendSchemaTemplateReq req); - void appendSchemaTemplate(TSAppendSchemaTemplateReq req); + void pruneSchemaTemplate(TSPruneSchemaTemplateReq req); - void pruneSchemaTemplate(TSPruneSchemaTemplateReq req); + TSQueryTemplateResp querySchemaTemplate(TSQueryTemplateReq req); - TSQueryTemplateResp querySchemaTemplate(TSQueryTemplateReq req); + std::unique_ptr executeRawDataQuery(const std::vector& paths, + int64_t startTime, int64_t endTime); - std::unique_ptr executeRawDataQuery(const std::vector& paths, int64_t startTime, - int64_t endTime); + std::unique_ptr executeLastDataQuery(const std::vector& paths, + int64_t lastTime); - std::unique_ptr executeLastDataQuery(const std::vector& paths, int64_t lastTime); + void executeNonQueryStatement(const std::string& sql); - void executeNonQueryStatement(const std::string& sql); + std::unique_ptr executeQueryStatement(const std::string& sql, + int64_t timeoutInMs = -1); - std::unique_ptr executeQueryStatement(const std::string& sql, int64_t timeoutInMs = -1); + std::shared_ptr getSessionClient() { + return client; + } - std::shared_ptr getSessionClient() { - return client; - } - - friend class Session; + friend class Session; private: - void close(); - std::string getSystemDefaultZoneId(); - bool reconnect(); + void close(); + std::string getSystemDefaultZoneId(); + bool reconnect(); - template - struct RetryResult { - T result; - std::exception_ptr exception; - int retryAttempts; + template struct RetryResult { + T result; + std::exception_ptr exception; + int retryAttempts; - RetryResult(T r, std::exception_ptr e, int a) - : result(r), exception(e), retryAttempts(a) { - } + RetryResult(T r, std::exception_ptr e, int a) : result(r), exception(e), retryAttempts(a) {} - int getRetryAttempts() const { return retryAttempts; } - T getResult() const { return result; } - std::exception_ptr getException() const { return exception; } - }; + int getRetryAttempts() const { + return retryAttempts; + } + T getResult() const { + return result; + } + std::exception_ptr getException() const { + return exception; + } + }; - template - void callWithRetryAndVerifyWithRedirection(std::function rpc); + template void callWithRetryAndVerifyWithRedirection(std::function rpc); - template - void callWithRetryAndVerifyWithRedirectionForMultipleDevices( - std::function rpc, const vector& deviceIds); + template + void callWithRetryAndVerifyWithRedirectionForMultipleDevices(std::function rpc, + const vector& deviceIds); - template - RetryResult callWithRetryAndVerify(std::function rpc); + template RetryResult callWithRetryAndVerify(std::function rpc); - template - RetryResult callWithRetry(std::function rpc); + template RetryResult callWithRetry(std::function rpc); - template - RetryResult callWithRetryAndReconnect(RpcFunc rpc); + template RetryResult callWithRetryAndReconnect(RpcFunc rpc); - template - RetryResult callWithRetryAndReconnect(RpcFunc rpc, StatusGetter statusGetter); + template + RetryResult callWithRetryAndReconnect(RpcFunc rpc, StatusGetter statusGetter); - template - RetryResult callWithRetryAndReconnect(RpcFunc rpc, ShouldRetry shouldRetry, ForceReconnect forceReconnect); + template + RetryResult callWithRetryAndReconnect(RpcFunc rpc, ShouldRetry shouldRetry, + ForceReconnect forceReconnect); - TSStatus insertStringRecordInternal(TSInsertStringRecordReq request); + TSStatus insertStringRecordInternal(TSInsertStringRecordReq request); - TSStatus insertRecordInternal(TSInsertRecordReq request); + TSStatus insertRecordInternal(TSInsertRecordReq request); - TSStatus insertStringRecordsInternal(TSInsertStringRecordsReq request); + TSStatus insertStringRecordsInternal(TSInsertStringRecordsReq request); - TSStatus insertRecordsInternal(TSInsertRecordsReq request); + TSStatus insertRecordsInternal(TSInsertRecordsReq request); - TSStatus insertRecordsOfOneDeviceInternal(TSInsertRecordsOfOneDeviceReq request); + TSStatus insertRecordsOfOneDeviceInternal(TSInsertRecordsOfOneDeviceReq request); - TSStatus insertStringRecordsOfOneDeviceInternal(TSInsertStringRecordsOfOneDeviceReq request); + TSStatus insertStringRecordsOfOneDeviceInternal(TSInsertStringRecordsOfOneDeviceReq request); - TSStatus insertTabletInternal(TSInsertTabletReq request); + TSStatus insertTabletInternal(TSInsertTabletReq request); - TSStatus insertTabletsInternal(TSInsertTabletsReq request); + TSStatus insertTabletsInternal(TSInsertTabletsReq request); - TSStatus deleteDataInternal(TSDeleteDataReq request); + TSStatus deleteDataInternal(TSDeleteDataReq request); #if WITH_SSL - std::shared_ptr socketFactory_ = - std::make_shared(); + std::shared_ptr socketFactory_ = + std::make_shared(); #endif - std::shared_ptr transport; - std::shared_ptr client; - Session* session; - int64_t sessionId; - int64_t statementId; - int64_t connectionTimeoutInMs; - bool enableRPCCompression = false; - std::string zoneId; - TEndPoint endPoint; - std::vector endPointList; - std::shared_ptr availableNodes; - int fetchSize; - int maxRetryCount; - int64_t retryIntervalMs; - std::string sqlDialect; - std::string database; - int timeFactor = 1000; + std::shared_ptr transport; + std::shared_ptr client; + Session* session; + int64_t sessionId; + int64_t statementId; + int64_t connectionTimeoutInMs; + bool enableRPCCompression = false; + std::string zoneId; + TEndPoint endPoint; + std::vector endPointList; + std::shared_ptr availableNodes; + int fetchSize; + int maxRetryCount; + int64_t retryIntervalMs; + std::string sqlDialect; + std::string database; + int timeFactor = 1000; }; template SessionConnection::RetryResult SessionConnection::callWithRetry(std::function rpc) { - std::exception_ptr lastException = nullptr; - TSStatus status; - int i; - for (i = 0; i <= maxRetryCount; i++) { - if (i > 0) { - lastException = nullptr; - status = TSStatus(); - try { - std::this_thread::sleep_for( - std::chrono::milliseconds(retryIntervalMs)); - } - catch (const std::exception& e) { - break; - } - if (!reconnect()) { - continue; - } - } - - try { - status = rpc(); - if (status.__isset.needRetry && status.needRetry) { - continue; - } - break; - } - catch (...) { - lastException = std::current_exception(); - } + std::exception_ptr lastException = nullptr; + TSStatus status; + int i; + for (i = 0; i <= maxRetryCount; i++) { + if (i > 0) { + lastException = nullptr; + status = TSStatus(); + try { + std::this_thread::sleep_for(std::chrono::milliseconds(retryIntervalMs)); + } catch (const std::exception& e) { + break; + } + if (!reconnect()) { + continue; + } } - return {status, lastException, i}; + + try { + status = rpc(); + if (status.__isset.needRetry && status.needRetry) { + continue; + } + break; + } catch (...) { + lastException = std::current_exception(); + } + } + return {status, lastException, i}; } template void SessionConnection::callWithRetryAndVerifyWithRedirection(std::function rpc) { - auto result = callWithRetry(rpc); - - auto status = result.getResult(); - if (result.getRetryAttempts() == 0) { - RpcUtils::verifySuccessWithRedirection(status); - } - else { - RpcUtils::verifySuccess(status); - } - - if (result.getException()) { - try { - std::rethrow_exception(result.getException()); - } - catch (const std::exception& e) { - throw IoTDBConnectionException(e.what()); - } + auto result = callWithRetry(rpc); + + auto status = result.getResult(); + if (result.getRetryAttempts() == 0) { + RpcUtils::verifySuccessWithRedirection(status); + } else { + RpcUtils::verifySuccess(status); + } + + if (result.getException()) { + try { + std::rethrow_exception(result.getException()); + } catch (const std::exception& e) { + throw IoTDBConnectionException(e.what()); } + } } template void SessionConnection::callWithRetryAndVerifyWithRedirectionForMultipleDevices( std::function rpc, const vector& deviceIds) { - auto result = callWithRetry(rpc); - auto status = result.getResult(); - if (result.getRetryAttempts() == 0) { - RpcUtils::verifySuccessWithRedirectionForMultiDevices(status, deviceIds); - } - else { - RpcUtils::verifySuccess(status); - } - if (result.getException()) { - try { - std::rethrow_exception(result.getException()); - } - catch (const std::exception& e) { - throw IoTDBConnectionException(e.what()); - } + auto result = callWithRetry(rpc); + auto status = result.getResult(); + if (result.getRetryAttempts() == 0) { + RpcUtils::verifySuccessWithRedirectionForMultiDevices(status, deviceIds); + } else { + RpcUtils::verifySuccess(status); + } + if (result.getException()) { + try { + std::rethrow_exception(result.getException()); + } catch (const std::exception& e) { + throw IoTDBConnectionException(e.what()); } - result.exception = nullptr; + } + result.exception = nullptr; } template -SessionConnection::RetryResult SessionConnection::callWithRetryAndVerify(std::function rpc) { - auto result = callWithRetry(rpc); - RpcUtils::verifySuccess(result.getResult()); - if (result.getException()) { - try { - std::rethrow_exception(result.getException()); - } - catch (const std::exception& e) { - throw IoTDBConnectionException(e.what()); - } +SessionConnection::RetryResult +SessionConnection::callWithRetryAndVerify(std::function rpc) { + auto result = callWithRetry(rpc); + RpcUtils::verifySuccess(result.getResult()); + if (result.getException()) { + try { + std::rethrow_exception(result.getException()); + } catch (const std::exception& e) { + throw IoTDBConnectionException(e.what()); } - return result; + } + return result; } template SessionConnection::RetryResult SessionConnection::callWithRetryAndReconnect(RpcFunc rpc) { - return callWithRetryAndReconnect(rpc, - [](const TSStatus& status) { - return status.__isset.needRetry && status.needRetry; - }, - [](const TSStatus& status) { - return status.code == TSStatusCode::PLAN_FAILED_NETWORK_PARTITION; - } - ); + return callWithRetryAndReconnect( + rpc, [](const TSStatus& status) { return status.__isset.needRetry && status.needRetry; }, + [](const TSStatus& status) { + return status.code == TSStatusCode::PLAN_FAILED_NETWORK_PARTITION; + }); } template -SessionConnection::RetryResult SessionConnection::callWithRetryAndReconnect(RpcFunc rpc, StatusGetter statusGetter) { - auto shouldRetry = [&statusGetter](const T& t) { - auto status = statusGetter(t); - return status.__isset.needRetry && status.needRetry; - }; - auto forceReconnect = [&statusGetter](const T& t) { - auto status = statusGetter(t); - return status.code == TSStatusCode::PLAN_FAILED_NETWORK_PARTITION;; - }; - return callWithRetryAndReconnect(rpc, shouldRetry, forceReconnect); +SessionConnection::RetryResult +SessionConnection::callWithRetryAndReconnect(RpcFunc rpc, StatusGetter statusGetter) { + auto shouldRetry = [&statusGetter](const T& t) { + auto status = statusGetter(t); + return status.__isset.needRetry && status.needRetry; + }; + auto forceReconnect = [&statusGetter](const T& t) { + auto status = statusGetter(t); + return status.code == TSStatusCode::PLAN_FAILED_NETWORK_PARTITION; + ; + }; + return callWithRetryAndReconnect(rpc, shouldRetry, forceReconnect); } - template -SessionConnection::RetryResult SessionConnection::callWithRetryAndReconnect(RpcFunc rpc, - ShouldRetry shouldRetry, - ForceReconnect forceReconnect) { - std::exception_ptr lastException = nullptr; - T result; - int retryAttempt; - for (retryAttempt = 0; retryAttempt <= maxRetryCount; retryAttempt++) { - try { - result = rpc(); - lastException = nullptr; - } - catch (...) { - result = T(); - lastException = std::current_exception(); - } - - if (!shouldRetry(result)) { - return {result, lastException, retryAttempt}; - } - - if (lastException != nullptr || - std::find(availableNodes->getEndPointList().begin(), availableNodes->getEndPointList().end(), - this->endPoint) == availableNodes->getEndPointList().end() || - forceReconnect(result)) { - reconnect(); - } - - try { - std::this_thread::sleep_for(std::chrono::milliseconds(retryIntervalMs)); - } - catch (const std::exception& e) { - log_debug("Thread was interrupted during retry " + - std::to_string(retryAttempt) + - " with wait time " + - std::to_string(retryIntervalMs) + - " ms. Exiting retry loop."); - break; - } +SessionConnection::RetryResult +SessionConnection::callWithRetryAndReconnect(RpcFunc rpc, ShouldRetry shouldRetry, + ForceReconnect forceReconnect) { + std::exception_ptr lastException = nullptr; + T result; + int retryAttempt; + for (retryAttempt = 0; retryAttempt <= maxRetryCount; retryAttempt++) { + try { + result = rpc(); + lastException = nullptr; + } catch (...) { + result = T(); + lastException = std::current_exception(); + } + + if (!shouldRetry(result)) { + return {result, lastException, retryAttempt}; + } + + if (lastException != nullptr || + std::find(availableNodes->getEndPointList().begin(), + availableNodes->getEndPointList().end(), + this->endPoint) == availableNodes->getEndPointList().end() || + forceReconnect(result)) { + reconnect(); + } + + try { + std::this_thread::sleep_for(std::chrono::milliseconds(retryIntervalMs)); + } catch (const std::exception& e) { + log_debug("Thread was interrupted during retry " + std::to_string(retryAttempt) + + " with wait time " + std::to_string(retryIntervalMs) + " ms. Exiting retry loop."); + break; } + } - return {result, lastException, retryAttempt}; + return {result, lastException, retryAttempt}; } #endif diff --git a/iotdb-client/client-cpp/src/main/SessionDataSet.cpp b/iotdb-client/client-cpp/src/main/SessionDataSet.cpp index 0cdd13402e4dc..d291ce375bbc8 100644 --- a/iotdb-client/client-cpp/src/main/SessionDataSet.cpp +++ b/iotdb-client/client-cpp/src/main/SessionDataSet.cpp @@ -22,261 +22,262 @@ #include RowRecord::RowRecord(int64_t timestamp) { - this->timestamp = timestamp; + this->timestamp = timestamp; } RowRecord::RowRecord(int64_t timestamp, const std::vector& fields) - : timestamp(timestamp), fields(fields) { -} + : timestamp(timestamp), fields(fields) {} -RowRecord::RowRecord(const std::vector& fields) - : timestamp(-1), fields(fields) { -} +RowRecord::RowRecord(const std::vector& fields) : timestamp(-1), fields(fields) {} RowRecord::RowRecord() { - this->timestamp = -1; + this->timestamp = -1; } void RowRecord::addField(const Field& f) { - this->fields.push_back(f); + this->fields.push_back(f); } std::string RowRecord::toString() { - std::string ret; - if (this->timestamp != -1) { - ret.append(std::to_string(timestamp)); - ret.append("\t"); + std::string ret; + if (this->timestamp != -1) { + ret.append(std::to_string(timestamp)); + ret.append("\t"); + } + for (size_t i = 0; i < fields.size(); i++) { + if (i != 0) { + ret.append("\t"); } - for (size_t i = 0; i < fields.size(); i++) { - if (i != 0) { - ret.append("\t"); - } - TSDataType::TSDataType dataType = fields[i].dataType; - switch (dataType) { - case TSDataType::BOOLEAN: - if (!fields[i].boolV.is_initialized()) { - ret.append("null"); - } else { - ret.append(fields[i].boolV.value() ? "true" : "false"); - } - break; - case TSDataType::INT32: - if (!fields[i].intV.is_initialized()) { - ret.append("null"); - } else { - ret.append(std::to_string(fields[i].intV.value())); - } - break; - case TSDataType::DATE: - if (!fields[i].dateV.is_initialized()) { - ret.append("null"); - } else { - ret.append(boost::gregorian::to_iso_extended_string(fields[i].dateV.value())); - } - break; - case TSDataType::TIMESTAMP: - case TSDataType::INT64: - if (!fields[i].longV.is_initialized()) { - ret.append("null"); - } else { - ret.append(std::to_string(fields[i].longV.value())); - } - break; - case TSDataType::FLOAT: - if (!fields[i].floatV.is_initialized()) { - ret.append("null"); - } else { - ret.append(std::to_string(fields[i].floatV.value())); - } - break; - case TSDataType::DOUBLE: - if (!fields[i].doubleV.is_initialized()) { - ret.append("null"); - } else { - ret.append(std::to_string(fields[i].doubleV.value())); - } - break; - case TSDataType::BLOB: - case TSDataType::STRING: - case TSDataType::OBJECT: - case TSDataType::TEXT: - if (!fields[i].stringV.is_initialized()) { - ret.append("null"); - } else { - ret.append(fields[i].stringV.value()); - } - break; - default: - break; - } + TSDataType::TSDataType dataType = fields[i].dataType; + switch (dataType) { + case TSDataType::BOOLEAN: + if (!fields[i].boolV.is_initialized()) { + ret.append("null"); + } else { + ret.append(fields[i].boolV.value() ? "true" : "false"); + } + break; + case TSDataType::INT32: + if (!fields[i].intV.is_initialized()) { + ret.append("null"); + } else { + ret.append(std::to_string(fields[i].intV.value())); + } + break; + case TSDataType::DATE: + if (!fields[i].dateV.is_initialized()) { + ret.append("null"); + } else { + ret.append(boost::gregorian::to_iso_extended_string(fields[i].dateV.value())); + } + break; + case TSDataType::TIMESTAMP: + case TSDataType::INT64: + if (!fields[i].longV.is_initialized()) { + ret.append("null"); + } else { + ret.append(std::to_string(fields[i].longV.value())); + } + break; + case TSDataType::FLOAT: + if (!fields[i].floatV.is_initialized()) { + ret.append("null"); + } else { + ret.append(std::to_string(fields[i].floatV.value())); + } + break; + case TSDataType::DOUBLE: + if (!fields[i].doubleV.is_initialized()) { + ret.append("null"); + } else { + ret.append(std::to_string(fields[i].doubleV.value())); + } + break; + case TSDataType::BLOB: + case TSDataType::STRING: + case TSDataType::OBJECT: + case TSDataType::TEXT: + if (!fields[i].stringV.is_initialized()) { + ret.append("null"); + } else { + ret.append(fields[i].stringV.value()); + } + break; + default: + break; } - ret.append("\n"); - return ret; + } + ret.append("\n"); + return ret; } bool SessionDataSet::hasNext() { - if (iotdbRpcDataSet_->hasCachedRecord()) { - return true; - } - return iotdbRpcDataSet_->next(); + if (iotdbRpcDataSet_->hasCachedRecord()) { + return true; + } + return iotdbRpcDataSet_->next(); } shared_ptr SessionDataSet::next() { - if (!iotdbRpcDataSet_->hasCachedRecord() && !hasNext()) { - return nullptr; - } - iotdbRpcDataSet_->setHasCachedRecord(false); + if (!iotdbRpcDataSet_->hasCachedRecord() && !hasNext()) { + return nullptr; + } + iotdbRpcDataSet_->setHasCachedRecord(false); - return constructRowRecordFromValueArray(); + return constructRowRecordFromValueArray(); } int SessionDataSet::getFetchSize() { - return iotdbRpcDataSet_->getFetchSize(); + return iotdbRpcDataSet_->getFetchSize(); } void SessionDataSet::setFetchSize(int fetchSize) { - return iotdbRpcDataSet_->setFetchSize(fetchSize); + return iotdbRpcDataSet_->setFetchSize(fetchSize); } const std::vector& SessionDataSet::getColumnNames() const { - return iotdbRpcDataSet_->getColumnNameList(); + return iotdbRpcDataSet_->getColumnNameList(); } const std::vector& SessionDataSet::getColumnTypeList() const { - return iotdbRpcDataSet_->getColumnTypeList(); + return iotdbRpcDataSet_->getColumnTypeList(); } void SessionDataSet::closeOperationHandle(bool forceClose) { - iotdbRpcDataSet_->close(forceClose); + iotdbRpcDataSet_->close(forceClose); } bool SessionDataSet::DataIterator::next() { - return iotdbRpcDataSet_->next(); + return iotdbRpcDataSet_->next(); } bool SessionDataSet::DataIterator::isNull(const std::string& columnName) { - return iotdbRpcDataSet_->isNullByColumnName(columnName); + return iotdbRpcDataSet_->isNullByColumnName(columnName); } bool SessionDataSet::DataIterator::isNullByIndex(int32_t columnIndex) { - return iotdbRpcDataSet_->isNullByIndex(columnIndex); + return iotdbRpcDataSet_->isNullByIndex(columnIndex); } boost::optional SessionDataSet::DataIterator::getBooleanByIndex(int32_t columnIndex) { - return iotdbRpcDataSet_->getBooleanByIndex(columnIndex); + return iotdbRpcDataSet_->getBooleanByIndex(columnIndex); } boost::optional SessionDataSet::DataIterator::getBoolean(const std::string& columnName) { - return iotdbRpcDataSet_->getBoolean(columnName); + return iotdbRpcDataSet_->getBoolean(columnName); } boost::optional SessionDataSet::DataIterator::getDoubleByIndex(int32_t columnIndex) { - return iotdbRpcDataSet_->getDoubleByIndex(columnIndex); + return iotdbRpcDataSet_->getDoubleByIndex(columnIndex); } boost::optional SessionDataSet::DataIterator::getDouble(const std::string& columnName) { - return iotdbRpcDataSet_->getDouble(columnName); + return iotdbRpcDataSet_->getDouble(columnName); } boost::optional SessionDataSet::DataIterator::getFloatByIndex(int32_t columnIndex) { - return iotdbRpcDataSet_->getFloatByIndex(columnIndex); + return iotdbRpcDataSet_->getFloatByIndex(columnIndex); } boost::optional SessionDataSet::DataIterator::getFloat(const std::string& columnName) { - return iotdbRpcDataSet_->getFloat(columnName); + return iotdbRpcDataSet_->getFloat(columnName); } boost::optional SessionDataSet::DataIterator::getIntByIndex(int32_t columnIndex) { - return iotdbRpcDataSet_->getIntByIndex(columnIndex); + return iotdbRpcDataSet_->getIntByIndex(columnIndex); } boost::optional SessionDataSet::DataIterator::getInt(const std::string& columnName) { - return iotdbRpcDataSet_->getInt(columnName); + return iotdbRpcDataSet_->getInt(columnName); } boost::optional SessionDataSet::DataIterator::getLongByIndex(int32_t columnIndex) { - return iotdbRpcDataSet_->getLongByIndex(columnIndex); + return iotdbRpcDataSet_->getLongByIndex(columnIndex); } boost::optional SessionDataSet::DataIterator::getLong(const std::string& columnName) { - return iotdbRpcDataSet_->getLong(columnName); + return iotdbRpcDataSet_->getLong(columnName); } boost::optional SessionDataSet::DataIterator::getStringByIndex(int32_t columnIndex) { - return iotdbRpcDataSet_->getStringByIndex(columnIndex); + return iotdbRpcDataSet_->getStringByIndex(columnIndex); } -boost::optional SessionDataSet::DataIterator::getString(const std::string& columnName) { - return iotdbRpcDataSet_->getString(columnName); +boost::optional +SessionDataSet::DataIterator::getString(const std::string& columnName) { + return iotdbRpcDataSet_->getString(columnName); } boost::optional SessionDataSet::DataIterator::getTimestampByIndex(int32_t columnIndex) { - return iotdbRpcDataSet_->getTimestampByIndex(columnIndex); + return iotdbRpcDataSet_->getTimestampByIndex(columnIndex); } boost::optional SessionDataSet::DataIterator::getTimestamp(const std::string& columnName) { - return iotdbRpcDataSet_->getTimestamp(columnName); + return iotdbRpcDataSet_->getTimestamp(columnName); } -boost::optional SessionDataSet::DataIterator::getDateByIndex(int32_t columnIndex) { - return iotdbRpcDataSet_->getDateByIndex(columnIndex); +boost::optional +SessionDataSet::DataIterator::getDateByIndex(int32_t columnIndex) { + return iotdbRpcDataSet_->getDateByIndex(columnIndex); } -boost::optional SessionDataSet::DataIterator::getDate(const std::string& columnName) { - return iotdbRpcDataSet_->getDate(columnName); +boost::optional +SessionDataSet::DataIterator::getDate(const std::string& columnName) { + return iotdbRpcDataSet_->getDate(columnName); } int32_t SessionDataSet::DataIterator::findColumn(const std::string& columnName) { - return iotdbRpcDataSet_->findColumn(columnName); + return iotdbRpcDataSet_->findColumn(columnName); } const std::vector& SessionDataSet::DataIterator::getColumnNames() const { - return iotdbRpcDataSet_->getColumnNameList(); + return iotdbRpcDataSet_->getColumnNameList(); } const std::vector& SessionDataSet::DataIterator::getColumnTypeList() const { - return iotdbRpcDataSet_->getColumnTypeList(); + return iotdbRpcDataSet_->getColumnTypeList(); } shared_ptr SessionDataSet::constructRowRecordFromValueArray() { - std::vector outFields; - for (int i = iotdbRpcDataSet_->getValueColumnStartIndex(); i < iotdbRpcDataSet_->getColumnSize(); i++) { - Field field; - std::string columnName = iotdbRpcDataSet_->getColumnNameList().at(i); - if (!iotdbRpcDataSet_->isNullByColumnName(columnName)) { - TSDataType::TSDataType dataType = iotdbRpcDataSet_->getDataType(columnName); - field.dataType = dataType; - switch (dataType) { - case TSDataType::BOOLEAN: - field.boolV = iotdbRpcDataSet_->getBoolean(columnName); - break; - case TSDataType::INT32: - field.intV = iotdbRpcDataSet_->getInt(columnName); - break; - case TSDataType::DATE: - field.dateV = iotdbRpcDataSet_->getDate(columnName); - break; - case TSDataType::INT64: - case TSDataType::TIMESTAMP: - field.longV = iotdbRpcDataSet_->getLong(columnName); - break; - case TSDataType::FLOAT: - field.floatV = iotdbRpcDataSet_->getFloat(columnName); - break; - case TSDataType::DOUBLE: - field.doubleV = iotdbRpcDataSet_->getDouble(columnName); - break; - case TSDataType::TEXT: - case TSDataType::BLOB: - case TSDataType::STRING: - case TSDataType::OBJECT: - field.stringV = iotdbRpcDataSet_->getBinary(columnName)->getStringValue(); - break; - default: - throw UnSupportedDataTypeException("Data type %s is not supported." + dataType); - } - } - outFields.emplace_back(field); + std::vector outFields; + for (int i = iotdbRpcDataSet_->getValueColumnStartIndex(); i < iotdbRpcDataSet_->getColumnSize(); + i++) { + Field field; + std::string columnName = iotdbRpcDataSet_->getColumnNameList().at(i); + if (!iotdbRpcDataSet_->isNullByColumnName(columnName)) { + TSDataType::TSDataType dataType = iotdbRpcDataSet_->getDataType(columnName); + field.dataType = dataType; + switch (dataType) { + case TSDataType::BOOLEAN: + field.boolV = iotdbRpcDataSet_->getBoolean(columnName); + break; + case TSDataType::INT32: + field.intV = iotdbRpcDataSet_->getInt(columnName); + break; + case TSDataType::DATE: + field.dateV = iotdbRpcDataSet_->getDate(columnName); + break; + case TSDataType::INT64: + case TSDataType::TIMESTAMP: + field.longV = iotdbRpcDataSet_->getLong(columnName); + break; + case TSDataType::FLOAT: + field.floatV = iotdbRpcDataSet_->getFloat(columnName); + break; + case TSDataType::DOUBLE: + field.doubleV = iotdbRpcDataSet_->getDouble(columnName); + break; + case TSDataType::TEXT: + case TSDataType::BLOB: + case TSDataType::STRING: + case TSDataType::OBJECT: + field.stringV = iotdbRpcDataSet_->getBinary(columnName)->getStringValue(); + break; + default: + throw UnSupportedDataTypeException("Data type %s is not supported." + dataType); + } } - return std::make_shared(iotdbRpcDataSet_->getCurrentRowTime(), outFields); + outFields.emplace_back(field); + } + return std::make_shared(iotdbRpcDataSet_->getCurrentRowTime(), outFields); } diff --git a/iotdb-client/client-cpp/src/main/SessionDataSet.h b/iotdb-client/client-cpp/src/main/SessionDataSet.h index c796c5758e757..0f2c018d25661 100644 --- a/iotdb-client/client-cpp/src/main/SessionDataSet.h +++ b/iotdb-client/client-cpp/src/main/SessionDataSet.h @@ -30,117 +30,95 @@ class RowRecord { public: - int64_t timestamp; - std::vector fields; + int64_t timestamp; + std::vector fields; - explicit RowRecord(int64_t timestamp); - RowRecord(int64_t timestamp, const std::vector& fields); - explicit RowRecord(const std::vector& fields); - RowRecord(); + explicit RowRecord(int64_t timestamp); + RowRecord(int64_t timestamp, const std::vector& fields); + explicit RowRecord(const std::vector& fields); + RowRecord(); - void addField(const Field& f); - std::string toString(); + void addField(const Field& f); + std::string toString(); }; class SessionDataSet { public: - SessionDataSet(const std::string& sql, - const std::vector& columnNameList, - const std::vector& columnTypeList, - const std::map& columnNameIndex, - int64_t queryId, - int64_t statementId, - std::shared_ptr client, - int64_t sessionId, - const std::vector& queryResult, - bool ignoreTimestamp, - int64_t timeout, - bool moreData, - int32_t fetchSize, - const std::string& zoneId, - int32_t timeFactor, - std::vector& columnIndex2TsBlockColumnIndexList) { - iotdbRpcDataSet_ = std::make_shared(sql, - columnNameList, - columnTypeList, - columnNameIndex, - ignoreTimestamp, - moreData, - queryId, - statementId, - client, - sessionId, - queryResult, - fetchSize, - timeout, - zoneId, - IoTDBRpcDataSet::DEFAULT_TIME_FORMAT, - timeFactor, - columnIndex2TsBlockColumnIndexList); - } - - ~SessionDataSet() = default; - - bool hasNext(); - shared_ptr next(); - - int getFetchSize(); - void setFetchSize(int fetchSize); - - const std::vector& getColumnNames() const; - const std::vector& getColumnTypeList() const; - void closeOperationHandle(bool forceClose = false); - - class DataIterator { - std::shared_ptr iotdbRpcDataSet_; + SessionDataSet(const std::string& sql, const std::vector& columnNameList, + const std::vector& columnTypeList, + const std::map& columnNameIndex, int64_t queryId, + int64_t statementId, std::shared_ptr client, + int64_t sessionId, const std::vector& queryResult, + bool ignoreTimestamp, int64_t timeout, bool moreData, int32_t fetchSize, + const std::string& zoneId, int32_t timeFactor, + std::vector& columnIndex2TsBlockColumnIndexList) { + iotdbRpcDataSet_ = std::make_shared( + sql, columnNameList, columnTypeList, columnNameIndex, ignoreTimestamp, moreData, queryId, + statementId, client, sessionId, queryResult, fetchSize, timeout, zoneId, + IoTDBRpcDataSet::DEFAULT_TIME_FORMAT, timeFactor, columnIndex2TsBlockColumnIndexList); + } + + ~SessionDataSet() = default; + + bool hasNext(); + shared_ptr next(); + + int getFetchSize(); + void setFetchSize(int fetchSize); + + const std::vector& getColumnNames() const; + const std::vector& getColumnTypeList() const; + void closeOperationHandle(bool forceClose = false); + + class DataIterator { + std::shared_ptr iotdbRpcDataSet_; - public: - DataIterator(std::shared_ptr iotdbRpcDataSet) : - iotdbRpcDataSet_(iotdbRpcDataSet) { - } + public: + DataIterator(std::shared_ptr iotdbRpcDataSet) + : iotdbRpcDataSet_(iotdbRpcDataSet) {} - bool next(); + bool next(); - bool isNull(const std::string& columnName); - bool isNullByIndex(int32_t columnIndex); + bool isNull(const std::string& columnName); + bool isNullByIndex(int32_t columnIndex); - boost::optional getBooleanByIndex(int32_t columnIndex); - boost::optional getBoolean(const std::string& columnName); + boost::optional getBooleanByIndex(int32_t columnIndex); + boost::optional getBoolean(const std::string& columnName); - boost::optional getDoubleByIndex(int32_t columnIndex); - boost::optional getDouble(const std::string& columnName); + boost::optional getDoubleByIndex(int32_t columnIndex); + boost::optional getDouble(const std::string& columnName); - boost::optional getFloatByIndex(int32_t columnIndex); - boost::optional getFloat(const std::string& columnName); + boost::optional getFloatByIndex(int32_t columnIndex); + boost::optional getFloat(const std::string& columnName); - boost::optional getIntByIndex(int32_t columnIndex); - boost::optional getInt(const std::string& columnName); + boost::optional getIntByIndex(int32_t columnIndex); + boost::optional getInt(const std::string& columnName); - boost::optional getLongByIndex(int32_t columnIndex); - boost::optional getLong(const std::string& columnName); + boost::optional getLongByIndex(int32_t columnIndex); + boost::optional getLong(const std::string& columnName); - boost::optional getStringByIndex(int32_t columnIndex); - boost::optional getString(const std::string& columnName); + boost::optional getStringByIndex(int32_t columnIndex); + boost::optional getString(const std::string& columnName); - boost::optional getTimestampByIndex(int32_t columnIndex); - boost::optional getTimestamp(const std::string& columnName); + boost::optional getTimestampByIndex(int32_t columnIndex); + boost::optional getTimestamp(const std::string& columnName); - boost::optional getDateByIndex(int32_t columnIndex); - boost::optional getDate(const std::string& columnName); + boost::optional getDateByIndex(int32_t columnIndex); + boost::optional getDate(const std::string& columnName); - int32_t findColumn(const std::string& columnName); - const std::vector& getColumnNames() const; - const std::vector& getColumnTypeList() const; - }; + int32_t findColumn(const std::string& columnName); + const std::vector& getColumnNames() const; + const std::vector& getColumnTypeList() const; + }; - DataIterator getIterator() { - return {iotdbRpcDataSet_}; - }; + DataIterator getIterator() { + return {iotdbRpcDataSet_}; + }; private: - std::shared_ptr constructRowRecordFromValueArray(); + std::shared_ptr constructRowRecordFromValueArray(); - std::shared_ptr iotdbRpcDataSet_; + std::shared_ptr iotdbRpcDataSet_; }; #endif diff --git a/iotdb-client/client-cpp/src/main/TableSession.cpp b/iotdb-client/client-cpp/src/main/TableSession.cpp index 35613cff6dab9..9cd80b7dd7897 100644 --- a/iotdb-client/client-cpp/src/main/TableSession.cpp +++ b/iotdb-client/client-cpp/src/main/TableSession.cpp @@ -22,24 +22,25 @@ #include "TableSession.h" #include "SessionDataSet.h" -void TableSession::insert(Tablet &tablet, bool sorted) { - session_->insertRelationalTablet(tablet, sorted); +void TableSession::insert(Tablet& tablet, bool sorted) { + session_->insertRelationalTablet(tablet, sorted); } -void TableSession::executeNonQueryStatement(const string &sql) { - session_->executeNonQueryStatement(sql); +void TableSession::executeNonQueryStatement(const string& sql) { + session_->executeNonQueryStatement(sql); } -unique_ptr TableSession::executeQueryStatement(const string &sql) { - return session_->executeQueryStatement(sql); +unique_ptr TableSession::executeQueryStatement(const string& sql) { + return session_->executeQueryStatement(sql); } -unique_ptr TableSession::executeQueryStatement(const string &sql, int64_t timeoutInMs) { - return session_->executeQueryStatement(sql, timeoutInMs); +unique_ptr TableSession::executeQueryStatement(const string& sql, + int64_t timeoutInMs) { + return session_->executeQueryStatement(sql, timeoutInMs); } string TableSession::getDatabase() { - return session_->getDatabase(); + return session_->getDatabase(); } void TableSession::open(bool enableRPCCompression) { - session_->open(enableRPCCompression); + session_->open(enableRPCCompression); } void TableSession::close() { - session_->close(); + session_->close(); } \ No newline at end of file diff --git a/iotdb-client/client-cpp/src/main/TableSession.h b/iotdb-client/client-cpp/src/main/TableSession.h index 09768439e83ea..d1eecfeeabaea 100644 --- a/iotdb-client/client-cpp/src/main/TableSession.h +++ b/iotdb-client/client-cpp/src/main/TableSession.h @@ -26,20 +26,21 @@ class TableSession { private: - std::shared_ptr session_; - string getDatabase(); + std::shared_ptr session_; + string getDatabase(); + public: - TableSession(std::shared_ptr session) { - this->session_ = session; - } - ~TableSession() {} + TableSession(std::shared_ptr session) { + this->session_ = session; + } + ~TableSession() {} - void insert(Tablet& tablet, bool sorted = false); - void executeNonQueryStatement(const std::string& sql); - unique_ptr executeQueryStatement(const std::string& sql); - unique_ptr executeQueryStatement(const std::string& sql, int64_t timeoutInMs); - void open(bool enableRPCCompression = false); - void close(); + void insert(Tablet& tablet, bool sorted = false); + void executeNonQueryStatement(const std::string& sql); + unique_ptr executeQueryStatement(const std::string& sql); + unique_ptr executeQueryStatement(const std::string& sql, int64_t timeoutInMs); + void open(bool enableRPCCompression = false); + void close(); }; #endif // IOTDB_TABLESESSION_H \ No newline at end of file diff --git a/iotdb-client/client-cpp/src/main/TableSessionBuilder.h b/iotdb-client/client-cpp/src/main/TableSessionBuilder.h index 9f76b84815ba6..3c9739ecc8edb 100644 --- a/iotdb-client/client-cpp/src/main/TableSessionBuilder.h +++ b/iotdb-client/client-cpp/src/main/TableSessionBuilder.h @@ -26,7 +26,7 @@ #include "AbstractSessionBuilder.h" class TableSessionBuilder : public AbstractSessionBuilder { - /* + /* std::string host; int rpcPort; std::string username; @@ -37,59 +37,60 @@ class TableSessionBuilder : public AbstractSessionBuilder { std::string database; */ public: - TableSessionBuilder* host(const std::string &host) { - AbstractSessionBuilder::host = host; - return this; - } - TableSessionBuilder* rpcPort(int rpcPort) { - AbstractSessionBuilder::rpcPort = rpcPort; - return this; - } - TableSessionBuilder* useSSL(bool useSSL) { - AbstractSessionBuilder::useSSL = useSSL; - return this; - } + TableSessionBuilder* host(const std::string& host) { + AbstractSessionBuilder::host = host; + return this; + } + TableSessionBuilder* rpcPort(int rpcPort) { + AbstractSessionBuilder::rpcPort = rpcPort; + return this; + } + TableSessionBuilder* useSSL(bool useSSL) { + AbstractSessionBuilder::useSSL = useSSL; + return this; + } - TableSessionBuilder* trustCertFilePath(const std::string &trustCertFilePath) { - AbstractSessionBuilder::trustCertFilePath = trustCertFilePath; - return this; - } + TableSessionBuilder* trustCertFilePath(const std::string& trustCertFilePath) { + AbstractSessionBuilder::trustCertFilePath = trustCertFilePath; + return this; + } - TableSessionBuilder* username(const std::string &username) { - AbstractSessionBuilder::username = username; - return this; - } - TableSessionBuilder* password(const std::string &password) { - AbstractSessionBuilder::password = password; - return this; - } - TableSessionBuilder* zoneId(const std::string &zoneId) { - AbstractSessionBuilder::zoneId = zoneId; - return this; - } - TableSessionBuilder* fetchSize(int fetchSize) { - AbstractSessionBuilder::fetchSize = fetchSize; - return this; - } - TableSessionBuilder* database(const std::string &database) { - AbstractSessionBuilder::database = database; - return this; - } - TableSessionBuilder* nodeUrls(const std::vector& nodeUrls) { - AbstractSessionBuilder::nodeUrls = nodeUrls; - return this; - } - std::shared_ptr build() { - if (!AbstractSessionBuilder::nodeUrls.empty() && - (AbstractSessionBuilder::host != DEFAULT_HOST || - AbstractSessionBuilder::rpcPort != DEFAULT_RPC_PORT)) { - throw IoTDBException("Session builder does not support setting node urls and host/rpcPort at the same time."); - } - sqlDialect = "table"; - auto newSession = std::make_shared(this); - newSession->open(false); - return std::make_shared(newSession); + TableSessionBuilder* username(const std::string& username) { + AbstractSessionBuilder::username = username; + return this; + } + TableSessionBuilder* password(const std::string& password) { + AbstractSessionBuilder::password = password; + return this; + } + TableSessionBuilder* zoneId(const std::string& zoneId) { + AbstractSessionBuilder::zoneId = zoneId; + return this; + } + TableSessionBuilder* fetchSize(int fetchSize) { + AbstractSessionBuilder::fetchSize = fetchSize; + return this; + } + TableSessionBuilder* database(const std::string& database) { + AbstractSessionBuilder::database = database; + return this; + } + TableSessionBuilder* nodeUrls(const std::vector& nodeUrls) { + AbstractSessionBuilder::nodeUrls = nodeUrls; + return this; + } + std::shared_ptr build() { + if (!AbstractSessionBuilder::nodeUrls.empty() && + (AbstractSessionBuilder::host != DEFAULT_HOST || + AbstractSessionBuilder::rpcPort != DEFAULT_RPC_PORT)) { + throw IoTDBException( + "Session builder does not support setting node urls and host/rpcPort at the same time."); } + sqlDialect = "table"; + auto newSession = std::make_shared(this); + newSession->open(false); + return std::make_shared(newSession); + } }; #endif // IOTDB_TABLESESSIONBUILDER_H \ No newline at end of file diff --git a/iotdb-client/client-cpp/src/main/ThriftConnection.cpp b/iotdb-client/client-cpp/src/main/ThriftConnection.cpp index 3a7989a5e7654..b02cb03c45402 100644 --- a/iotdb-client/client-cpp/src/main/ThriftConnection.cpp +++ b/iotdb-client/client-cpp/src/main/ThriftConnection.cpp @@ -29,159 +29,143 @@ const int ThriftConnection::THRIFT_MAX_FRAME_SIZE = 1048576; const int ThriftConnection::CONNECTION_TIMEOUT_IN_MS = 1000; const int ThriftConnection::DEFAULT_FETCH_SIZE = 10000; -ThriftConnection::ThriftConnection(const TEndPoint& endPoint, - int thriftDefaultBufferSize, - int thriftMaxFrameSize, - int connectionTimeoutInMs, - int fetchSize) - : endPoint_(endPoint), - thriftDefaultBufferSize_(thriftDefaultBufferSize), - thriftMaxFrameSize_(thriftMaxFrameSize), - connectionTimeoutInMs_(connectionTimeoutInMs), - fetchSize_(fetchSize){ -} +ThriftConnection::ThriftConnection(const TEndPoint& endPoint, int thriftDefaultBufferSize, + int thriftMaxFrameSize, int connectionTimeoutInMs, int fetchSize) + : endPoint_(endPoint), thriftDefaultBufferSize_(thriftDefaultBufferSize), + thriftMaxFrameSize_(thriftMaxFrameSize), connectionTimeoutInMs_(connectionTimeoutInMs), + fetchSize_(fetchSize) {} ThriftConnection::~ThriftConnection() = default; void ThriftConnection::initZoneId() { - if (!zoneId_.empty()) { - return; - } + if (!zoneId_.empty()) { + return; + } - time_t ts = 0; - struct tm tmv{}; -#if defined(_WIN64) || defined (WIN32) || defined (_WIN32) - localtime_s(&tmv, &ts); + time_t ts = 0; + struct tm tmv {}; +#if defined(_WIN64) || defined(WIN32) || defined(_WIN32) + localtime_s(&tmv, &ts); #else - localtime_r(&ts, &tmv); + localtime_r(&ts, &tmv); #endif - char zoneStr[32]; - strftime(zoneStr, sizeof(zoneStr), "%z", &tmv); - zoneId_ = zoneStr; + char zoneStr[32]; + strftime(zoneStr, sizeof(zoneStr), "%z", &tmv); + zoneId_ = zoneStr; } -void ThriftConnection::init(const std::string& username, - const std::string& password, - bool enableRPCCompression, - bool useSSL, - const std::string& trustCertFilePath, - const std::string& zoneId, +void ThriftConnection::init(const std::string& username, const std::string& password, + bool enableRPCCompression, bool useSSL, + const std::string& trustCertFilePath, const std::string& zoneId, const std::string& version) { - if (useSSL) { + if (useSSL) { #if WITH_SSL - socketFactory_->loadTrustedCertificates(trustCertFilePath.c_str()); - socketFactory_->authenticate(false); - auto sslSocket = socketFactory_->createSocket(endPoint_.ip, endPoint_.port); - sslSocket->setConnTimeout(connectionTimeoutInMs_); - transport_ = std::make_shared(sslSocket); + socketFactory_->loadTrustedCertificates(trustCertFilePath.c_str()); + socketFactory_->authenticate(false); + auto sslSocket = socketFactory_->createSocket(endPoint_.ip, endPoint_.port); + sslSocket->setConnTimeout(connectionTimeoutInMs_); + transport_ = std::make_shared(sslSocket); #else - throw IoTDBException("SSL/TLS support is not enabled in this build. " - "Please rebuild with -DWITH_SSL=ON flag " - "or use non-SSL connection."); + throw IoTDBException("SSL/TLS support is not enabled in this build. " + "Please rebuild with -DWITH_SSL=ON flag " + "or use non-SSL connection."); #endif - } else { - auto socket = std::make_shared(endPoint_.ip, endPoint_.port); - socket->setConnTimeout(connectionTimeoutInMs_); - transport_ = std::make_shared(socket); - } - if (!transport_->isOpen()) { - try { - transport_->open(); - } - catch (TTransportException& e) { - throw IoTDBConnectionException(e.what()); - } - } - if (zoneId.empty()) { - initZoneId(); - } - else { - this->zoneId_ = zoneId; + } else { + auto socket = std::make_shared(endPoint_.ip, endPoint_.port); + socket->setConnTimeout(connectionTimeoutInMs_); + transport_ = std::make_shared(socket); + } + if (!transport_->isOpen()) { + try { + transport_->open(); + } catch (TTransportException& e) { + throw IoTDBConnectionException(e.what()); } + } + if (zoneId.empty()) { + initZoneId(); + } else { + this->zoneId_ = zoneId; + } - if (enableRPCCompression) { - std::shared_ptr protocol(new TCompactProtocol(transport_)); - client_ = std::make_shared(protocol); - } - else { - std::shared_ptr protocol(new TBinaryProtocol(transport_)); - client_ = std::make_shared(protocol); - } + if (enableRPCCompression) { + std::shared_ptr protocol(new TCompactProtocol(transport_)); + client_ = std::make_shared(protocol); + } else { + std::shared_ptr protocol(new TBinaryProtocol(transport_)); + client_ = std::make_shared(protocol); + } - std::map configuration; - configuration["version"] = version; - TSOpenSessionReq openReq; - openReq.__set_username(username); - openReq.__set_password(password); - openReq.__set_zoneId(this->zoneId_); - openReq.__set_configuration(configuration); - try { - TSOpenSessionResp openResp; - client_->openSession(openResp, openReq); - RpcUtils::verifySuccess(openResp.status); - sessionId_ = openResp.sessionId; - statementId_ = client_->requestStatementId(sessionId_); - } - catch (const TTransportException& e) { - transport_->close(); - throw IoTDBConnectionException(e.what()); - } catch (const IoTDBException& e) { - transport_->close(); - throw IoTDBException(e.what()); - } catch (const std::exception& e) { - transport_->close(); - throw IoTDBException(e.what()); - } + std::map configuration; + configuration["version"] = version; + TSOpenSessionReq openReq; + openReq.__set_username(username); + openReq.__set_password(password); + openReq.__set_zoneId(this->zoneId_); + openReq.__set_configuration(configuration); + try { + TSOpenSessionResp openResp; + client_->openSession(openResp, openReq); + RpcUtils::verifySuccess(openResp.status); + sessionId_ = openResp.sessionId; + statementId_ = client_->requestStatementId(sessionId_); + } catch (const TTransportException& e) { + transport_->close(); + throw IoTDBConnectionException(e.what()); + } catch (const IoTDBException& e) { + transport_->close(); + throw IoTDBException(e.what()); + } catch (const std::exception& e) { + transport_->close(); + throw IoTDBException(e.what()); + } } -std::unique_ptr ThriftConnection::executeQueryStatement(const std::string& sql, int64_t timeoutInMs) { - TSExecuteStatementReq req; - req.__set_sessionId(sessionId_); - req.__set_statementId(statementId_); - req.__set_statement(sql); - req.__set_timeout(timeoutInMs); - TSExecuteStatementResp resp; - try { - client_->executeQueryStatementV2(resp, req); - RpcUtils::verifySuccess(resp.status); - } - catch (const TTransportException& e) { - throw IoTDBConnectionException(e.what()); - } catch (const IoTDBException& e) { - throw IoTDBConnectionException(e.what()); - } catch (const std::exception& e) { - throw IoTDBException(e.what()); - } - std::shared_ptr queryDataSet(new TSQueryDataSet(resp.queryDataSet)); - return std::unique_ptr(new SessionDataSet("", resp.columns, resp.dataTypeList, - resp.columnNameIndexMap, resp.queryId, statementId_, - client_, sessionId_, resp.queryResult, resp.ignoreTimeStamp, - connectionTimeoutInMs_, resp.moreData, fetchSize_, zoneId_, - timeFactor_, resp.columnIndex2TsBlockColumnIndexList)); +std::unique_ptr ThriftConnection::executeQueryStatement(const std::string& sql, + int64_t timeoutInMs) { + TSExecuteStatementReq req; + req.__set_sessionId(sessionId_); + req.__set_statementId(statementId_); + req.__set_statement(sql); + req.__set_timeout(timeoutInMs); + TSExecuteStatementResp resp; + try { + client_->executeQueryStatementV2(resp, req); + RpcUtils::verifySuccess(resp.status); + } catch (const TTransportException& e) { + throw IoTDBConnectionException(e.what()); + } catch (const IoTDBException& e) { + throw IoTDBConnectionException(e.what()); + } catch (const std::exception& e) { + throw IoTDBException(e.what()); + } + std::shared_ptr queryDataSet(new TSQueryDataSet(resp.queryDataSet)); + return std::unique_ptr(new SessionDataSet( + "", resp.columns, resp.dataTypeList, resp.columnNameIndexMap, resp.queryId, statementId_, + client_, sessionId_, resp.queryResult, resp.ignoreTimeStamp, connectionTimeoutInMs_, + resp.moreData, fetchSize_, zoneId_, timeFactor_, resp.columnIndex2TsBlockColumnIndexList)); } void ThriftConnection::close() { - try { - if (client_) { - TSCloseSessionReq req; - req.__set_sessionId(sessionId_); - TSStatus tsStatus; - client_->closeSession(tsStatus, req); - } - } - catch (const TTransportException& e) { - throw IoTDBConnectionException(e.what()); - } catch (const std::exception& e) { - throw IoTDBConnectionException(e.what()); + try { + if (client_) { + TSCloseSessionReq req; + req.__set_sessionId(sessionId_); + TSStatus tsStatus; + client_->closeSession(tsStatus, req); } + } catch (const TTransportException& e) { + throw IoTDBConnectionException(e.what()); + } catch (const std::exception& e) { + throw IoTDBConnectionException(e.what()); + } - try { - if (transport_->isOpen()) { - transport_->close(); - } - } - catch (const std::exception& e) { - throw IoTDBConnectionException(e.what()); + try { + if (transport_->isOpen()) { + transport_->close(); } + } catch (const std::exception& e) { + throw IoTDBConnectionException(e.what()); + } } diff --git a/iotdb-client/client-cpp/src/main/ThriftConnection.h b/iotdb-client/client-cpp/src/main/ThriftConnection.h index 6dae68357fdbc..ab094c6608c52 100644 --- a/iotdb-client/client-cpp/src/main/ThriftConnection.h +++ b/iotdb-client/client-cpp/src/main/ThriftConnection.h @@ -29,51 +29,49 @@ class SessionDataSet; class ThriftConnection { public: - static const int THRIFT_DEFAULT_BUFFER_SIZE; - static const int THRIFT_MAX_FRAME_SIZE; - static const int CONNECTION_TIMEOUT_IN_MS; - static const int DEFAULT_FETCH_SIZE; + static const int THRIFT_DEFAULT_BUFFER_SIZE; + static const int THRIFT_MAX_FRAME_SIZE; + static const int CONNECTION_TIMEOUT_IN_MS; + static const int DEFAULT_FETCH_SIZE; - explicit ThriftConnection(const TEndPoint& endPoint, - int thriftDefaultBufferSize = THRIFT_DEFAULT_BUFFER_SIZE, - int thriftMaxFrameSize = THRIFT_MAX_FRAME_SIZE, - int connectionTimeoutInMs = CONNECTION_TIMEOUT_IN_MS, - int fetchSize = DEFAULT_FETCH_SIZE); + explicit ThriftConnection(const TEndPoint& endPoint, + int thriftDefaultBufferSize = THRIFT_DEFAULT_BUFFER_SIZE, + int thriftMaxFrameSize = THRIFT_MAX_FRAME_SIZE, + int connectionTimeoutInMs = CONNECTION_TIMEOUT_IN_MS, + int fetchSize = DEFAULT_FETCH_SIZE); - ~ThriftConnection(); + ~ThriftConnection(); - void init(const std::string& username, - const std::string& password, - bool enableRPCCompression = false, - bool useSSL = false, - const std::string& trustCertFilePath = "", - const std::string& zoneId = std::string(), - const std::string& version = "V_1_0"); + void init(const std::string& username, const std::string& password, + bool enableRPCCompression = false, bool useSSL = false, + const std::string& trustCertFilePath = "", const std::string& zoneId = std::string(), + const std::string& version = "V_1_0"); - std::unique_ptr executeQueryStatement(const std::string& sql, int64_t timeoutInMs = -1); + std::unique_ptr executeQueryStatement(const std::string& sql, + int64_t timeoutInMs = -1); - void close(); + void close(); private: - TEndPoint endPoint_; + TEndPoint endPoint_; - int thriftDefaultBufferSize_; - int thriftMaxFrameSize_; - int connectionTimeoutInMs_; - int fetchSize_; + int thriftDefaultBufferSize_; + int thriftMaxFrameSize_; + int connectionTimeoutInMs_; + int fetchSize_; #if WITH_SSL - std::shared_ptr socketFactory_ = - std::make_shared(); + std::shared_ptr socketFactory_ = + std::make_shared(); #endif - std::shared_ptr transport_; - std::shared_ptr client_; - int64_t sessionId_{}; - int64_t statementId_{}; - std::string zoneId_; - int timeFactor_{}; + std::shared_ptr transport_; + std::shared_ptr client_; + int64_t sessionId_{}; + int64_t statementId_{}; + std::string zoneId_; + int timeFactor_{}; - void initZoneId(); + void initZoneId(); }; #endif diff --git a/iotdb-client/client-cpp/src/main/TsBlock.cpp b/iotdb-client/client-cpp/src/main/TsBlock.cpp index 92afbef3f270f..e1c5840900cbe 100644 --- a/iotdb-client/client-cpp/src/main/TsBlock.cpp +++ b/iotdb-client/client-cpp/src/main/TsBlock.cpp @@ -21,101 +21,98 @@ #include #include "TsBlock.h" -std::shared_ptr TsBlock::create(int32_t positionCount, - std::shared_ptr timeColumn, +std::shared_ptr TsBlock::create(int32_t positionCount, std::shared_ptr timeColumn, std::vector> valueColumns) { - if (valueColumns.empty()) { - throw std::invalid_argument("valueColumns cannot be empty"); - } - return std::shared_ptr(new TsBlock(positionCount, std::move(timeColumn), std::move(valueColumns))); + if (valueColumns.empty()) { + throw std::invalid_argument("valueColumns cannot be empty"); + } + return std::shared_ptr( + new TsBlock(positionCount, std::move(timeColumn), std::move(valueColumns))); } std::shared_ptr TsBlock::deserialize(const std::string& data) { - MyStringBuffer buffer(data); - - // Read value column count - int32_t valueColumnCount = buffer.getInt(); - if (valueColumnCount < 0) { - throw IoTDBException("TsBlock::deserialize: negative valueColumnCount"); - } - const int64_t minHeaderBytes = - 9LL + 2LL * static_cast(valueColumnCount); - if (minHeaderBytes > static_cast(data.size())) { - throw IoTDBException("TsBlock::deserialize: truncated header"); - } - - // Read value column data types - std::vector valueColumnDataTypes(valueColumnCount); - for (int32_t i = 0; i < valueColumnCount; i++) { - valueColumnDataTypes[i] = static_cast(buffer.getChar()); - } - - // Read position count - int32_t positionCount = buffer.getInt(); - if (positionCount < 0) { - throw IoTDBException("TsBlock::deserialize: negative positionCount"); - } - - // Read column encodings - std::vector columnEncodings(valueColumnCount + 1); - for (int32_t i = 0; i < valueColumnCount + 1; i++) { - columnEncodings[i] = static_cast(buffer.getChar()); - } - - // Read time column - auto timeColumnDecoder = getColumnDecoder(columnEncodings[0]); - auto timeColumn = timeColumnDecoder->readColumn(buffer, TSDataType::INT64, positionCount); - - // Read value columns - std::vector> valueColumns(valueColumnCount); - for (int32_t i = 0; i < valueColumnCount; i++) { - auto valueColumnDecoder = getColumnDecoder(columnEncodings[i + 1]); - valueColumns[i] = valueColumnDecoder->readColumn(buffer, valueColumnDataTypes[i], positionCount); - } - - return create(positionCount, std::move(timeColumn), std::move(valueColumns)); + MyStringBuffer buffer(data); + + // Read value column count + int32_t valueColumnCount = buffer.getInt(); + if (valueColumnCount < 0) { + throw IoTDBException("TsBlock::deserialize: negative valueColumnCount"); + } + const int64_t minHeaderBytes = 9LL + 2LL * static_cast(valueColumnCount); + if (minHeaderBytes > static_cast(data.size())) { + throw IoTDBException("TsBlock::deserialize: truncated header"); + } + + // Read value column data types + std::vector valueColumnDataTypes(valueColumnCount); + for (int32_t i = 0; i < valueColumnCount; i++) { + valueColumnDataTypes[i] = static_cast(buffer.getChar()); + } + + // Read position count + int32_t positionCount = buffer.getInt(); + if (positionCount < 0) { + throw IoTDBException("TsBlock::deserialize: negative positionCount"); + } + + // Read column encodings + std::vector columnEncodings(valueColumnCount + 1); + for (int32_t i = 0; i < valueColumnCount + 1; i++) { + columnEncodings[i] = static_cast(buffer.getChar()); + } + + // Read time column + auto timeColumnDecoder = getColumnDecoder(columnEncodings[0]); + auto timeColumn = timeColumnDecoder->readColumn(buffer, TSDataType::INT64, positionCount); + + // Read value columns + std::vector> valueColumns(valueColumnCount); + for (int32_t i = 0; i < valueColumnCount; i++) { + auto valueColumnDecoder = getColumnDecoder(columnEncodings[i + 1]); + valueColumns[i] = + valueColumnDecoder->readColumn(buffer, valueColumnDataTypes[i], positionCount); + } + + return create(positionCount, std::move(timeColumn), std::move(valueColumns)); } -TsBlock::TsBlock(int32_t positionCount, - std::shared_ptr timeColumn, +TsBlock::TsBlock(int32_t positionCount, std::shared_ptr timeColumn, std::vector> valueColumns) - : positionCount_(positionCount), - timeColumn_(std::move(timeColumn)), - valueColumns_(std::move(valueColumns)) { -} + : positionCount_(positionCount), timeColumn_(std::move(timeColumn)), + valueColumns_(std::move(valueColumns)) {} int32_t TsBlock::getPositionCount() const { - return positionCount_; + return positionCount_; } int64_t TsBlock::getStartTime() const { - return timeColumn_->getLong(0); + return timeColumn_->getLong(0); } int64_t TsBlock::getEndTime() const { - return timeColumn_->getLong(positionCount_ - 1); + return timeColumn_->getLong(positionCount_ - 1); } bool TsBlock::isEmpty() const { - return positionCount_ == 0; + return positionCount_ == 0; } int64_t TsBlock::getTimeByIndex(int32_t index) const { - return timeColumn_->getLong(index); + return timeColumn_->getLong(index); } int32_t TsBlock::getValueColumnCount() const { - return static_cast(valueColumns_.size()); + return static_cast(valueColumns_.size()); } const std::shared_ptr TsBlock::getTimeColumn() const { - return timeColumn_; + return timeColumn_; } const std::vector>& TsBlock::getValueColumns() const { - return valueColumns_; + return valueColumns_; } const std::shared_ptr TsBlock::getColumn(int32_t columnIndex) const { - return valueColumns_[columnIndex]; + return valueColumns_[columnIndex]; } diff --git a/iotdb-client/client-cpp/src/main/TsBlock.h b/iotdb-client/client-cpp/src/main/TsBlock.h index 2ca6b0197445d..0ffa8c0af6c1b 100644 --- a/iotdb-client/client-cpp/src/main/TsBlock.h +++ b/iotdb-client/client-cpp/src/main/TsBlock.h @@ -26,30 +26,28 @@ class TsBlock { public: - static std::shared_ptr create(int32_t positionCount, - std::shared_ptr timeColumn, - std::vector> valueColumns); - - static std::shared_ptr deserialize(const std::string& data); - - int32_t getPositionCount() const; - int64_t getStartTime() const; - int64_t getEndTime() const; - bool isEmpty() const; - int64_t getTimeByIndex(int32_t index) const; - int32_t getValueColumnCount() const; - const std::shared_ptr getTimeColumn() const; - const std::vector>& getValueColumns() const; - const std::shared_ptr getColumn(int32_t columnIndex) const; + static std::shared_ptr create(int32_t positionCount, std::shared_ptr timeColumn, + std::vector> valueColumns); + + static std::shared_ptr deserialize(const std::string& data); + + int32_t getPositionCount() const; + int64_t getStartTime() const; + int64_t getEndTime() const; + bool isEmpty() const; + int64_t getTimeByIndex(int32_t index) const; + int32_t getValueColumnCount() const; + const std::shared_ptr getTimeColumn() const; + const std::vector>& getValueColumns() const; + const std::shared_ptr getColumn(int32_t columnIndex) const; private: - TsBlock(int32_t positionCount, - std::shared_ptr timeColumn, - std::vector> valueColumns); + TsBlock(int32_t positionCount, std::shared_ptr timeColumn, + std::vector> valueColumns); - std::shared_ptr timeColumn_; - std::vector> valueColumns_; - int32_t positionCount_; + std::shared_ptr timeColumn_; + std::vector> valueColumns_; + int32_t positionCount_; }; #endif diff --git a/iotdb-client/client-cpp/src/test/catch2/catch.hpp b/iotdb-client/client-cpp/src/test/catch2/catch.hpp index 156988613d2e1..26fba3c742bab 100644 --- a/iotdb-client/client-cpp/src/test/catch2/catch.hpp +++ b/iotdb-client/client-cpp/src/test/catch2/catch.hpp @@ -31,55 +31,54 @@ #define TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED // start catch.hpp - #define CATCH_VERSION_MAJOR 2 #define CATCH_VERSION_MINOR 13 #define CATCH_VERSION_PATCH 7 #ifdef __clang__ -# pragma clang system_header +#pragma clang system_header #elif defined __GNUC__ -# pragma GCC system_header +#pragma GCC system_header #endif // start catch_suppress_warnings.h #ifdef __clang__ -# ifdef __ICC // icpc defines the __clang__ macro -# pragma warning(push) -# pragma warning(disable: 161 1682) -# else // __ICC -# pragma clang diagnostic push -# pragma clang diagnostic ignored "-Wpadded" -# pragma clang diagnostic ignored "-Wswitch-enum" -# pragma clang diagnostic ignored "-Wcovered-switch-default" -# endif +#ifdef __ICC // icpc defines the __clang__ macro +#pragma warning(push) +#pragma warning(disable : 161 1682) +#else // __ICC +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +#pragma clang diagnostic ignored "-Wswitch-enum" +#pragma clang diagnostic ignored "-Wcovered-switch-default" +#endif #elif defined __GNUC__ - // Because REQUIREs trigger GCC's -Wparentheses, and because still - // supported version of g++ have only buggy support for _Pragmas, - // Wparentheses have to be suppressed globally. -# pragma GCC diagnostic ignored "-Wparentheses" // See #674 for details - -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wunused-variable" -# pragma GCC diagnostic ignored "-Wpadded" +// Because REQUIREs trigger GCC's -Wparentheses, and because still +// supported version of g++ have only buggy support for _Pragmas, +// Wparentheses have to be suppressed globally. +#pragma GCC diagnostic ignored "-Wparentheses" // See #674 for details + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-variable" +#pragma GCC diagnostic ignored "-Wpadded" #endif // end catch_suppress_warnings.h #if defined(CATCH_CONFIG_MAIN) || defined(CATCH_CONFIG_RUNNER) -# define CATCH_IMPL -# define CATCH_CONFIG_ALL_PARTS +#define CATCH_IMPL +#define CATCH_CONFIG_ALL_PARTS #endif // In the impl file, we want to have access to all parts of the headers // Can also be used to sanely support PCHs #if defined(CATCH_CONFIG_ALL_PARTS) -# define CATCH_CONFIG_EXTERNAL_INTERFACES -# if defined(CATCH_CONFIG_DISABLE_MATCHERS) -# undef CATCH_CONFIG_DISABLE_MATCHERS -# endif -# if !defined(CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER) -# define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER -# endif +#define CATCH_CONFIG_EXTERNAL_INTERFACES +#if defined(CATCH_CONFIG_DISABLE_MATCHERS) +#undef CATCH_CONFIG_DISABLE_MATCHERS +#endif +#if !defined(CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER) +#define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER +#endif #endif #if !defined(CATCH_CONFIG_IMPL_ONLY) @@ -88,34 +87,34 @@ // See e.g.: // https://opensource.apple.com/source/CarbonHeaders/CarbonHeaders-18.1/TargetConditionals.h.auto.html #ifdef __APPLE__ -# include -# if (defined(TARGET_OS_OSX) && TARGET_OS_OSX == 1) || \ - (defined(TARGET_OS_MAC) && TARGET_OS_MAC == 1) -# define CATCH_PLATFORM_MAC -# elif (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE == 1) -# define CATCH_PLATFORM_IPHONE -# endif +#include +#if (defined(TARGET_OS_OSX) && TARGET_OS_OSX == 1) || (defined(TARGET_OS_MAC) && TARGET_OS_MAC == 1) +#define CATCH_PLATFORM_MAC +#elif (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE == 1) +#define CATCH_PLATFORM_IPHONE +#endif #elif defined(linux) || defined(__linux) || defined(__linux__) -# define CATCH_PLATFORM_LINUX +#define CATCH_PLATFORM_LINUX -#elif defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) || defined(__MINGW32__) -# define CATCH_PLATFORM_WINDOWS +#elif defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) || \ + defined(__MINGW32__) +#define CATCH_PLATFORM_WINDOWS #endif // end catch_platform.h #ifdef CATCH_IMPL -# ifndef CLARA_CONFIG_MAIN -# define CLARA_CONFIG_MAIN_NOT_DEFINED -# define CLARA_CONFIG_MAIN -# endif +#ifndef CLARA_CONFIG_MAIN +#define CLARA_CONFIG_MAIN_NOT_DEFINED +#define CLARA_CONFIG_MAIN +#endif #endif // start catch_user_interfaces.h namespace Catch { - unsigned int rngSeed(); +unsigned int rngSeed(); } // end catch_user_interfaces.h @@ -144,30 +143,31 @@ namespace Catch { #ifdef __cplusplus -# if (__cplusplus >= 201402L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L) -# define CATCH_CPP14_OR_GREATER -# endif +#if (__cplusplus >= 201402L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L) +#define CATCH_CPP14_OR_GREATER +#endif -# if (__cplusplus >= 201703L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) -# define CATCH_CPP17_OR_GREATER -# endif +#if (__cplusplus >= 201703L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) +#define CATCH_CPP17_OR_GREATER +#endif #endif // Only GCC compiler should be used in this block, so other compilers trying to // mask themselves as GCC should be ignored. -#if defined(__GNUC__) && !defined(__clang__) && !defined(__ICC) && !defined(__CUDACC__) && !defined(__LCC__) -# define CATCH_INTERNAL_START_WARNINGS_SUPPRESSION _Pragma( "GCC diagnostic push" ) -# define CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION _Pragma( "GCC diagnostic pop" ) +#if defined(__GNUC__) && !defined(__clang__) && !defined(__ICC) && !defined(__CUDACC__) && \ + !defined(__LCC__) +#define CATCH_INTERNAL_START_WARNINGS_SUPPRESSION _Pragma("GCC diagnostic push") +#define CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION _Pragma("GCC diagnostic pop") -# define CATCH_INTERNAL_IGNORE_BUT_WARN(...) (void)__builtin_constant_p(__VA_ARGS__) +#define CATCH_INTERNAL_IGNORE_BUT_WARN(...) (void)__builtin_constant_p(__VA_ARGS__) #endif #if defined(__clang__) -# define CATCH_INTERNAL_START_WARNINGS_SUPPRESSION _Pragma( "clang diagnostic push" ) -# define CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION _Pragma( "clang diagnostic pop" ) +#define CATCH_INTERNAL_START_WARNINGS_SUPPRESSION _Pragma("clang diagnostic push") +#define CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION _Pragma("clang diagnostic pop") // As of this writing, IBM XL's implementation of __builtin_constant_p has a bug // which results in calls to destructors being emitted for each temporary, @@ -180,62 +180,64 @@ namespace Catch { // ``` // // Therefore, `CATCH_INTERNAL_IGNORE_BUT_WARN` is not implemented. -# if !defined(__ibmxl__) && !defined(__CUDACC__) -# define CATCH_INTERNAL_IGNORE_BUT_WARN(...) (void)__builtin_constant_p(__VA_ARGS__) /* NOLINT(cppcoreguidelines-pro-type-vararg, hicpp-vararg) */ -# endif +#if !defined(__ibmxl__) && !defined(__CUDACC__) +#define CATCH_INTERNAL_IGNORE_BUT_WARN(...) \ + (void)__builtin_constant_p( \ + __VA_ARGS__) /* NOLINT(cppcoreguidelines-pro-type-vararg, hicpp-vararg) */ +#endif -# define CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ - _Pragma( "clang diagnostic ignored \"-Wexit-time-destructors\"" ) \ - _Pragma( "clang diagnostic ignored \"-Wglobal-constructors\"") +#define CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + _Pragma("clang diagnostic ignored \"-Wexit-time-destructors\"") \ + _Pragma("clang diagnostic ignored \"-Wglobal-constructors\"") -# define CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS \ - _Pragma( "clang diagnostic ignored \"-Wparentheses\"" ) +#define CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS \ + _Pragma("clang diagnostic ignored \"-Wparentheses\"") -# define CATCH_INTERNAL_SUPPRESS_UNUSED_WARNINGS \ - _Pragma( "clang diagnostic ignored \"-Wunused-variable\"" ) +#define CATCH_INTERNAL_SUPPRESS_UNUSED_WARNINGS \ + _Pragma("clang diagnostic ignored \"-Wunused-variable\"") -# define CATCH_INTERNAL_SUPPRESS_ZERO_VARIADIC_WARNINGS \ - _Pragma( "clang diagnostic ignored \"-Wgnu-zero-variadic-macro-arguments\"" ) +#define CATCH_INTERNAL_SUPPRESS_ZERO_VARIADIC_WARNINGS \ + _Pragma("clang diagnostic ignored \"-Wgnu-zero-variadic-macro-arguments\"") -# define CATCH_INTERNAL_SUPPRESS_UNUSED_TEMPLATE_WARNINGS \ - _Pragma( "clang diagnostic ignored \"-Wunused-template\"" ) +#define CATCH_INTERNAL_SUPPRESS_UNUSED_TEMPLATE_WARNINGS \ + _Pragma("clang diagnostic ignored \"-Wunused-template\"") #endif // __clang__ //////////////////////////////////////////////////////////////////////////////// // Assume that non-Windows platforms support posix signals by default #if !defined(CATCH_PLATFORM_WINDOWS) - #define CATCH_INTERNAL_CONFIG_POSIX_SIGNALS +#define CATCH_INTERNAL_CONFIG_POSIX_SIGNALS #endif //////////////////////////////////////////////////////////////////////////////// // We know some environments not to support full POSIX signals #if defined(__CYGWIN__) || defined(__QNX__) || defined(__EMSCRIPTEN__) || defined(__DJGPP__) - #define CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS +#define CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS #endif #ifdef __OS400__ -# define CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS -# define CATCH_CONFIG_COLOUR_NONE +#define CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS +#define CATCH_CONFIG_COLOUR_NONE #endif //////////////////////////////////////////////////////////////////////////////// // Android somehow still does not support std::to_string #if defined(__ANDROID__) -# define CATCH_INTERNAL_CONFIG_NO_CPP11_TO_STRING -# define CATCH_INTERNAL_CONFIG_ANDROID_LOGWRITE +#define CATCH_INTERNAL_CONFIG_NO_CPP11_TO_STRING +#define CATCH_INTERNAL_CONFIG_ANDROID_LOGWRITE #endif //////////////////////////////////////////////////////////////////////////////// // Not all Windows environments support SEH properly #if defined(__MINGW32__) -# define CATCH_INTERNAL_CONFIG_NO_WINDOWS_SEH +#define CATCH_INTERNAL_CONFIG_NO_WINDOWS_SEH #endif //////////////////////////////////////////////////////////////////////////////// // PS4 #if defined(__ORBIS__) -# define CATCH_INTERNAL_CONFIG_NO_NEW_CAPTURE +#define CATCH_INTERNAL_CONFIG_NO_NEW_CAPTURE #endif //////////////////////////////////////////////////////////////////////////////// @@ -244,64 +246,64 @@ namespace Catch { // Required for some versions of Cygwin to declare gettimeofday // see: http://stackoverflow.com/questions/36901803/gettimeofday-not-declared-in-this-scope-cygwin -# define _BSD_SOURCE +#define _BSD_SOURCE // some versions of cygwin (most) do not support std::to_string. Use the libstd check. // https://gcc.gnu.org/onlinedocs/gcc-4.8.2/libstdc++/api/a01053_source.html line 2812-2813 -# if !((__cplusplus >= 201103L) && defined(_GLIBCXX_USE_C99) \ - && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF)) +#if !((__cplusplus >= 201103L) && defined(_GLIBCXX_USE_C99) && \ + !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF)) -# define CATCH_INTERNAL_CONFIG_NO_CPP11_TO_STRING +#define CATCH_INTERNAL_CONFIG_NO_CPP11_TO_STRING -# endif +#endif #endif // __CYGWIN__ //////////////////////////////////////////////////////////////////////////////// // Visual C++ #if defined(_MSC_VER) -# define CATCH_INTERNAL_START_WARNINGS_SUPPRESSION __pragma( warning(push) ) -# define CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION __pragma( warning(pop) ) +#define CATCH_INTERNAL_START_WARNINGS_SUPPRESSION __pragma(warning(push)) +#define CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION __pragma(warning(pop)) // Universal Windows platform does not support SEH // Or console colours (or console at all...) -# if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP) -# define CATCH_CONFIG_COLOUR_NONE -# else -# define CATCH_INTERNAL_CONFIG_WINDOWS_SEH -# endif +#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP) +#define CATCH_CONFIG_COLOUR_NONE +#else +#define CATCH_INTERNAL_CONFIG_WINDOWS_SEH +#endif // MSVC traditional preprocessor needs some workaround for __VA_ARGS__ // _MSVC_TRADITIONAL == 0 means new conformant preprocessor // _MSVC_TRADITIONAL == 1 means old traditional non-conformant preprocessor -# if !defined(__clang__) // Handle Clang masquerading for msvc -# if !defined(_MSVC_TRADITIONAL) || (defined(_MSVC_TRADITIONAL) && _MSVC_TRADITIONAL) -# define CATCH_INTERNAL_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR -# endif // MSVC_TRADITIONAL -# endif // __clang__ +#if !defined(__clang__) // Handle Clang masquerading for msvc +#if !defined(_MSVC_TRADITIONAL) || (defined(_MSVC_TRADITIONAL) && _MSVC_TRADITIONAL) +#define CATCH_INTERNAL_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR +#endif // MSVC_TRADITIONAL +#endif // __clang__ #endif // _MSC_VER #if defined(_REENTRANT) || defined(_MSC_VER) // Enable async processing, as -pthread is specified or no additional linking is required -# define CATCH_INTERNAL_CONFIG_USE_ASYNC +#define CATCH_INTERNAL_CONFIG_USE_ASYNC #endif // _MSC_VER //////////////////////////////////////////////////////////////////////////////// // Check if we are compiled with -fno-exceptions or equivalent #if defined(__EXCEPTIONS) || defined(__cpp_exceptions) || defined(_CPPUNWIND) -# define CATCH_INTERNAL_CONFIG_EXCEPTIONS_ENABLED +#define CATCH_INTERNAL_CONFIG_EXCEPTIONS_ENABLED #endif //////////////////////////////////////////////////////////////////////////////// // DJGPP #ifdef __DJGPP__ -# define CATCH_INTERNAL_CONFIG_NO_WCHAR +#define CATCH_INTERNAL_CONFIG_NO_WCHAR #endif // __DJGPP__ //////////////////////////////////////////////////////////////////////////////// // Embarcadero C++Build #if defined(__BORLANDC__) - #define CATCH_INTERNAL_CONFIG_POLYFILL_ISNAN +#define CATCH_INTERNAL_CONFIG_POLYFILL_ISNAN #endif //////////////////////////////////////////////////////////////////////////////// @@ -311,8 +313,8 @@ namespace Catch { // handled by it. // Otherwise all supported compilers support COUNTER macro, // but user still might want to turn it off -#if ( !defined(__JETBRAINS_IDE__) || __JETBRAINS_IDE__ >= 20170300L ) - #define CATCH_INTERNAL_CONFIG_COUNTER +#if (!defined(__JETBRAINS_IDE__) || __JETBRAINS_IDE__ >= 20170300L) +#define CATCH_INTERNAL_CONFIG_COUNTER #endif //////////////////////////////////////////////////////////////////////////////// @@ -321,9 +323,9 @@ namespace Catch { // This means that it is detected as Windows, but does not provide // the same set of capabilities as real Windows does. #if defined(UNDER_RTSS) || defined(RTX64_BUILD) - #define CATCH_INTERNAL_CONFIG_NO_WINDOWS_SEH - #define CATCH_INTERNAL_CONFIG_NO_ASYNC - #define CATCH_CONFIG_COLOUR_NONE +#define CATCH_INTERNAL_CONFIG_NO_WINDOWS_SEH +#define CATCH_INTERNAL_CONFIG_NO_ASYNC +#define CATCH_CONFIG_COLOUR_NONE #endif #if !defined(_GLIBCXX_USE_C99_MATH_TR1) @@ -332,139 +334,155 @@ namespace Catch { // Various stdlib support checks that require __has_include #if defined(__has_include) - // Check if string_view is available and usable - #if __has_include() && defined(CATCH_CPP17_OR_GREATER) - # define CATCH_INTERNAL_CONFIG_CPP17_STRING_VIEW - #endif - - // Check if optional is available and usable - # if __has_include() && defined(CATCH_CPP17_OR_GREATER) - # define CATCH_INTERNAL_CONFIG_CPP17_OPTIONAL - # endif // __has_include() && defined(CATCH_CPP17_OR_GREATER) - - // Check if byte is available and usable - # if __has_include() && defined(CATCH_CPP17_OR_GREATER) - # include - # if defined(__cpp_lib_byte) && (__cpp_lib_byte > 0) - # define CATCH_INTERNAL_CONFIG_CPP17_BYTE - # endif - # endif // __has_include() && defined(CATCH_CPP17_OR_GREATER) - - // Check if variant is available and usable - # if __has_include() && defined(CATCH_CPP17_OR_GREATER) - # if defined(__clang__) && (__clang_major__ < 8) - // work around clang bug with libstdc++ https://bugs.llvm.org/show_bug.cgi?id=31852 - // fix should be in clang 8, workaround in libstdc++ 8.2 - # include - # if defined(__GLIBCXX__) && defined(_GLIBCXX_RELEASE) && (_GLIBCXX_RELEASE < 9) - # define CATCH_CONFIG_NO_CPP17_VARIANT - # else - # define CATCH_INTERNAL_CONFIG_CPP17_VARIANT - # endif // defined(__GLIBCXX__) && defined(_GLIBCXX_RELEASE) && (_GLIBCXX_RELEASE < 9) - # else - # define CATCH_INTERNAL_CONFIG_CPP17_VARIANT - # endif // defined(__clang__) && (__clang_major__ < 8) - # endif // __has_include() && defined(CATCH_CPP17_OR_GREATER) +// Check if string_view is available and usable +#if __has_include() && defined(CATCH_CPP17_OR_GREATER) +#define CATCH_INTERNAL_CONFIG_CPP17_STRING_VIEW +#endif + +// Check if optional is available and usable +#if __has_include() && defined(CATCH_CPP17_OR_GREATER) +#define CATCH_INTERNAL_CONFIG_CPP17_OPTIONAL +#endif // __has_include() && defined(CATCH_CPP17_OR_GREATER) + +// Check if byte is available and usable +#if __has_include() && defined(CATCH_CPP17_OR_GREATER) +#include +#if defined(__cpp_lib_byte) && (__cpp_lib_byte > 0) +#define CATCH_INTERNAL_CONFIG_CPP17_BYTE +#endif +#endif // __has_include() && defined(CATCH_CPP17_OR_GREATER) + +// Check if variant is available and usable +#if __has_include() && defined(CATCH_CPP17_OR_GREATER) +#if defined(__clang__) && (__clang_major__ < 8) +// work around clang bug with libstdc++ https://bugs.llvm.org/show_bug.cgi?id=31852 +// fix should be in clang 8, workaround in libstdc++ 8.2 +#include +#if defined(__GLIBCXX__) && defined(_GLIBCXX_RELEASE) && (_GLIBCXX_RELEASE < 9) +#define CATCH_CONFIG_NO_CPP17_VARIANT +#else +#define CATCH_INTERNAL_CONFIG_CPP17_VARIANT +#endif // defined(__GLIBCXX__) && defined(_GLIBCXX_RELEASE) && (_GLIBCXX_RELEASE < 9) +#else +#define CATCH_INTERNAL_CONFIG_CPP17_VARIANT +#endif // defined(__clang__) && (__clang_major__ < 8) +#endif // __has_include() && defined(CATCH_CPP17_OR_GREATER) #endif // defined(__has_include) -#if defined(CATCH_INTERNAL_CONFIG_COUNTER) && !defined(CATCH_CONFIG_NO_COUNTER) && !defined(CATCH_CONFIG_COUNTER) -# define CATCH_CONFIG_COUNTER +#if defined(CATCH_INTERNAL_CONFIG_COUNTER) && !defined(CATCH_CONFIG_NO_COUNTER) && \ + !defined(CATCH_CONFIG_COUNTER) +#define CATCH_CONFIG_COUNTER #endif -#if defined(CATCH_INTERNAL_CONFIG_WINDOWS_SEH) && !defined(CATCH_CONFIG_NO_WINDOWS_SEH) && !defined(CATCH_CONFIG_WINDOWS_SEH) && !defined(CATCH_INTERNAL_CONFIG_NO_WINDOWS_SEH) -# define CATCH_CONFIG_WINDOWS_SEH +#if defined(CATCH_INTERNAL_CONFIG_WINDOWS_SEH) && !defined(CATCH_CONFIG_NO_WINDOWS_SEH) && \ + !defined(CATCH_CONFIG_WINDOWS_SEH) && !defined(CATCH_INTERNAL_CONFIG_NO_WINDOWS_SEH) +#define CATCH_CONFIG_WINDOWS_SEH #endif // This is set by default, because we assume that unix compilers are posix-signal-compatible by default. -#if defined(CATCH_INTERNAL_CONFIG_POSIX_SIGNALS) && !defined(CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS) && !defined(CATCH_CONFIG_NO_POSIX_SIGNALS) && !defined(CATCH_CONFIG_POSIX_SIGNALS) -# define CATCH_CONFIG_POSIX_SIGNALS +#if defined(CATCH_INTERNAL_CONFIG_POSIX_SIGNALS) && \ + !defined(CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS) && !defined(CATCH_CONFIG_NO_POSIX_SIGNALS) && \ + !defined(CATCH_CONFIG_POSIX_SIGNALS) +#define CATCH_CONFIG_POSIX_SIGNALS #endif // This is set by default, because we assume that compilers with no wchar_t support are just rare exceptions. -#if !defined(CATCH_INTERNAL_CONFIG_NO_WCHAR) && !defined(CATCH_CONFIG_NO_WCHAR) && !defined(CATCH_CONFIG_WCHAR) -# define CATCH_CONFIG_WCHAR +#if !defined(CATCH_INTERNAL_CONFIG_NO_WCHAR) && !defined(CATCH_CONFIG_NO_WCHAR) && \ + !defined(CATCH_CONFIG_WCHAR) +#define CATCH_CONFIG_WCHAR #endif -#if !defined(CATCH_INTERNAL_CONFIG_NO_CPP11_TO_STRING) && !defined(CATCH_CONFIG_NO_CPP11_TO_STRING) && !defined(CATCH_CONFIG_CPP11_TO_STRING) -# define CATCH_CONFIG_CPP11_TO_STRING +#if !defined(CATCH_INTERNAL_CONFIG_NO_CPP11_TO_STRING) && \ + !defined(CATCH_CONFIG_NO_CPP11_TO_STRING) && !defined(CATCH_CONFIG_CPP11_TO_STRING) +#define CATCH_CONFIG_CPP11_TO_STRING #endif -#if defined(CATCH_INTERNAL_CONFIG_CPP17_OPTIONAL) && !defined(CATCH_CONFIG_NO_CPP17_OPTIONAL) && !defined(CATCH_CONFIG_CPP17_OPTIONAL) -# define CATCH_CONFIG_CPP17_OPTIONAL +#if defined(CATCH_INTERNAL_CONFIG_CPP17_OPTIONAL) && !defined(CATCH_CONFIG_NO_CPP17_OPTIONAL) && \ + !defined(CATCH_CONFIG_CPP17_OPTIONAL) +#define CATCH_CONFIG_CPP17_OPTIONAL #endif -#if defined(CATCH_INTERNAL_CONFIG_CPP17_STRING_VIEW) && !defined(CATCH_CONFIG_NO_CPP17_STRING_VIEW) && !defined(CATCH_CONFIG_CPP17_STRING_VIEW) -# define CATCH_CONFIG_CPP17_STRING_VIEW +#if defined(CATCH_INTERNAL_CONFIG_CPP17_STRING_VIEW) && \ + !defined(CATCH_CONFIG_NO_CPP17_STRING_VIEW) && !defined(CATCH_CONFIG_CPP17_STRING_VIEW) +#define CATCH_CONFIG_CPP17_STRING_VIEW #endif -#if defined(CATCH_INTERNAL_CONFIG_CPP17_VARIANT) && !defined(CATCH_CONFIG_NO_CPP17_VARIANT) && !defined(CATCH_CONFIG_CPP17_VARIANT) -# define CATCH_CONFIG_CPP17_VARIANT +#if defined(CATCH_INTERNAL_CONFIG_CPP17_VARIANT) && !defined(CATCH_CONFIG_NO_CPP17_VARIANT) && \ + !defined(CATCH_CONFIG_CPP17_VARIANT) +#define CATCH_CONFIG_CPP17_VARIANT #endif -#if defined(CATCH_INTERNAL_CONFIG_CPP17_BYTE) && !defined(CATCH_CONFIG_NO_CPP17_BYTE) && !defined(CATCH_CONFIG_CPP17_BYTE) -# define CATCH_CONFIG_CPP17_BYTE +#if defined(CATCH_INTERNAL_CONFIG_CPP17_BYTE) && !defined(CATCH_CONFIG_NO_CPP17_BYTE) && \ + !defined(CATCH_CONFIG_CPP17_BYTE) +#define CATCH_CONFIG_CPP17_BYTE #endif #if defined(CATCH_CONFIG_EXPERIMENTAL_REDIRECT) -# define CATCH_INTERNAL_CONFIG_NEW_CAPTURE +#define CATCH_INTERNAL_CONFIG_NEW_CAPTURE #endif -#if defined(CATCH_INTERNAL_CONFIG_NEW_CAPTURE) && !defined(CATCH_INTERNAL_CONFIG_NO_NEW_CAPTURE) && !defined(CATCH_CONFIG_NO_NEW_CAPTURE) && !defined(CATCH_CONFIG_NEW_CAPTURE) -# define CATCH_CONFIG_NEW_CAPTURE +#if defined(CATCH_INTERNAL_CONFIG_NEW_CAPTURE) && \ + !defined(CATCH_INTERNAL_CONFIG_NO_NEW_CAPTURE) && !defined(CATCH_CONFIG_NO_NEW_CAPTURE) && \ + !defined(CATCH_CONFIG_NEW_CAPTURE) +#define CATCH_CONFIG_NEW_CAPTURE #endif #if !defined(CATCH_INTERNAL_CONFIG_EXCEPTIONS_ENABLED) && !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) -# define CATCH_CONFIG_DISABLE_EXCEPTIONS +#define CATCH_CONFIG_DISABLE_EXCEPTIONS #endif -#if defined(CATCH_INTERNAL_CONFIG_POLYFILL_ISNAN) && !defined(CATCH_CONFIG_NO_POLYFILL_ISNAN) && !defined(CATCH_CONFIG_POLYFILL_ISNAN) -# define CATCH_CONFIG_POLYFILL_ISNAN +#if defined(CATCH_INTERNAL_CONFIG_POLYFILL_ISNAN) && !defined(CATCH_CONFIG_NO_POLYFILL_ISNAN) && \ + !defined(CATCH_CONFIG_POLYFILL_ISNAN) +#define CATCH_CONFIG_POLYFILL_ISNAN #endif -#if defined(CATCH_INTERNAL_CONFIG_USE_ASYNC) && !defined(CATCH_INTERNAL_CONFIG_NO_ASYNC) && !defined(CATCH_CONFIG_NO_USE_ASYNC) && !defined(CATCH_CONFIG_USE_ASYNC) -# define CATCH_CONFIG_USE_ASYNC +#if defined(CATCH_INTERNAL_CONFIG_USE_ASYNC) && !defined(CATCH_INTERNAL_CONFIG_NO_ASYNC) && \ + !defined(CATCH_CONFIG_NO_USE_ASYNC) && !defined(CATCH_CONFIG_USE_ASYNC) +#define CATCH_CONFIG_USE_ASYNC #endif -#if defined(CATCH_INTERNAL_CONFIG_ANDROID_LOGWRITE) && !defined(CATCH_CONFIG_NO_ANDROID_LOGWRITE) && !defined(CATCH_CONFIG_ANDROID_LOGWRITE) -# define CATCH_CONFIG_ANDROID_LOGWRITE +#if defined(CATCH_INTERNAL_CONFIG_ANDROID_LOGWRITE) && \ + !defined(CATCH_CONFIG_NO_ANDROID_LOGWRITE) && !defined(CATCH_CONFIG_ANDROID_LOGWRITE) +#define CATCH_CONFIG_ANDROID_LOGWRITE #endif -#if defined(CATCH_INTERNAL_CONFIG_GLOBAL_NEXTAFTER) && !defined(CATCH_CONFIG_NO_GLOBAL_NEXTAFTER) && !defined(CATCH_CONFIG_GLOBAL_NEXTAFTER) -# define CATCH_CONFIG_GLOBAL_NEXTAFTER +#if defined(CATCH_INTERNAL_CONFIG_GLOBAL_NEXTAFTER) && \ + !defined(CATCH_CONFIG_NO_GLOBAL_NEXTAFTER) && !defined(CATCH_CONFIG_GLOBAL_NEXTAFTER) +#define CATCH_CONFIG_GLOBAL_NEXTAFTER #endif // Even if we do not think the compiler has that warning, we still have // to provide a macro that can be used by the code. #if !defined(CATCH_INTERNAL_START_WARNINGS_SUPPRESSION) -# define CATCH_INTERNAL_START_WARNINGS_SUPPRESSION +#define CATCH_INTERNAL_START_WARNINGS_SUPPRESSION #endif #if !defined(CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION) -# define CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION +#define CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION #endif #if !defined(CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS) -# define CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS +#define CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS #endif #if !defined(CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS) -# define CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS +#define CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS #endif #if !defined(CATCH_INTERNAL_SUPPRESS_UNUSED_WARNINGS) -# define CATCH_INTERNAL_SUPPRESS_UNUSED_WARNINGS +#define CATCH_INTERNAL_SUPPRESS_UNUSED_WARNINGS #endif #if !defined(CATCH_INTERNAL_SUPPRESS_ZERO_VARIADIC_WARNINGS) -# define CATCH_INTERNAL_SUPPRESS_ZERO_VARIADIC_WARNINGS +#define CATCH_INTERNAL_SUPPRESS_ZERO_VARIADIC_WARNINGS #endif // The goal of this macro is to avoid evaluation of the arguments, but // still have the compiler warn on problems inside... #if !defined(CATCH_INTERNAL_IGNORE_BUT_WARN) -# define CATCH_INTERNAL_IGNORE_BUT_WARN(...) +#define CATCH_INTERNAL_IGNORE_BUT_WARN(...) #endif #if defined(__APPLE__) && defined(__apple_build_version__) && (__clang_major__ < 10) -# undef CATCH_INTERNAL_SUPPRESS_UNUSED_TEMPLATE_WARNINGS +#undef CATCH_INTERNAL_SUPPRESS_UNUSED_TEMPLATE_WARNINGS #elif defined(__clang__) && (__clang_major__ < 5) -# undef CATCH_INTERNAL_SUPPRESS_UNUSED_TEMPLATE_WARNINGS +#undef CATCH_INTERNAL_SUPPRESS_UNUSED_TEMPLATE_WARNINGS #endif #if !defined(CATCH_INTERNAL_SUPPRESS_UNUSED_TEMPLATE_WARNINGS) -# define CATCH_INTERNAL_SUPPRESS_UNUSED_TEMPLATE_WARNINGS +#define CATCH_INTERNAL_SUPPRESS_UNUSED_TEMPLATE_WARNINGS #endif #if defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) @@ -477,17 +495,19 @@ namespace Catch { #define CATCH_CATCH_ANON(type) catch (type) #endif -#if defined(CATCH_INTERNAL_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR) && !defined(CATCH_CONFIG_NO_TRADITIONAL_MSVC_PREPROCESSOR) && !defined(CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR) +#if defined(CATCH_INTERNAL_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR) && \ + !defined(CATCH_CONFIG_NO_TRADITIONAL_MSVC_PREPROCESSOR) && \ + !defined(CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR) #define CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR #endif // end catch_compiler_capabilities.h -#define INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line ) name##line -#define INTERNAL_CATCH_UNIQUE_NAME_LINE( name, line ) INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line ) +#define INTERNAL_CATCH_UNIQUE_NAME_LINE2(name, line) name##line +#define INTERNAL_CATCH_UNIQUE_NAME_LINE(name, line) INTERNAL_CATCH_UNIQUE_NAME_LINE2(name, line) #ifdef CATCH_CONFIG_COUNTER -# define INTERNAL_CATCH_UNIQUE_NAME( name ) INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __COUNTER__ ) +#define INTERNAL_CATCH_UNIQUE_NAME(name) INTERNAL_CATCH_UNIQUE_NAME_LINE(name, __COUNTER__) #else -# define INTERNAL_CATCH_UNIQUE_NAME( name ) INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __LINE__ ) +#define INTERNAL_CATCH_UNIQUE_NAME(name) INTERNAL_CATCH_UNIQUE_NAME_LINE(name, __LINE__) #endif #include @@ -500,80 +520,80 @@ std::ostream& operator<<(std::ostream&, Catch_global_namespace_dummy); namespace Catch { - struct CaseSensitive { enum Choice { - Yes, - No - }; }; +struct CaseSensitive { + enum Choice { Yes, No }; +}; - class NonCopyable { - NonCopyable( NonCopyable const& ) = delete; - NonCopyable( NonCopyable && ) = delete; - NonCopyable& operator = ( NonCopyable const& ) = delete; - NonCopyable& operator = ( NonCopyable && ) = delete; +class NonCopyable { + NonCopyable(NonCopyable const&) = delete; + NonCopyable(NonCopyable&&) = delete; + NonCopyable& operator=(NonCopyable const&) = delete; + NonCopyable& operator=(NonCopyable&&) = delete; - protected: - NonCopyable(); - virtual ~NonCopyable(); - }; +protected: + NonCopyable(); + virtual ~NonCopyable(); +}; - struct SourceLineInfo { +struct SourceLineInfo { - SourceLineInfo() = delete; - SourceLineInfo( char const* _file, std::size_t _line ) noexcept - : file( _file ), - line( _line ) - {} + SourceLineInfo() = delete; + SourceLineInfo(char const* _file, std::size_t _line) noexcept : file(_file), line(_line) {} - SourceLineInfo( SourceLineInfo const& other ) = default; - SourceLineInfo& operator = ( SourceLineInfo const& ) = default; - SourceLineInfo( SourceLineInfo&& ) noexcept = default; - SourceLineInfo& operator = ( SourceLineInfo&& ) noexcept = default; + SourceLineInfo(SourceLineInfo const& other) = default; + SourceLineInfo& operator=(SourceLineInfo const&) = default; + SourceLineInfo(SourceLineInfo&&) noexcept = default; + SourceLineInfo& operator=(SourceLineInfo&&) noexcept = default; - bool empty() const noexcept { return file[0] == '\0'; } - bool operator == ( SourceLineInfo const& other ) const noexcept; - bool operator < ( SourceLineInfo const& other ) const noexcept; + bool empty() const noexcept { + return file[0] == '\0'; + } + bool operator==(SourceLineInfo const& other) const noexcept; + bool operator<(SourceLineInfo const& other) const noexcept; - char const* file; - std::size_t line; - }; + char const* file; + std::size_t line; +}; - std::ostream& operator << ( std::ostream& os, SourceLineInfo const& info ); +std::ostream& operator<<(std::ostream& os, SourceLineInfo const& info); - // Bring in operator<< from global namespace into Catch namespace - // This is necessary because the overload of operator<< above makes - // lookup stop at namespace Catch - using ::operator<<; +// Bring in operator<< from global namespace into Catch namespace +// This is necessary because the overload of operator<< above makes +// lookup stop at namespace Catch +using ::operator<<; - // Use this in variadic streaming macros to allow - // >> +StreamEndStop - // as well as - // >> stuff +StreamEndStop - struct StreamEndStop { - std::string operator+() const; - }; - template - T const& operator + ( T const& value, StreamEndStop ) { - return value; - } +// Use this in variadic streaming macros to allow +// >> +StreamEndStop +// as well as +// >> stuff +StreamEndStop +struct StreamEndStop { + std::string operator+() const; +}; +template T const& operator+(T const& value, StreamEndStop) { + return value; } +} // namespace Catch -#define CATCH_INTERNAL_LINEINFO \ - ::Catch::SourceLineInfo( __FILE__, static_cast( __LINE__ ) ) +#define CATCH_INTERNAL_LINEINFO \ + ::Catch::SourceLineInfo(__FILE__, static_cast(__LINE__)) // end catch_common.h namespace Catch { - struct RegistrarForTagAliases { - RegistrarForTagAliases( char const* alias, char const* tag, SourceLineInfo const& lineInfo ); - }; +struct RegistrarForTagAliases { + RegistrarForTagAliases(char const* alias, char const* tag, SourceLineInfo const& lineInfo); +}; } // end namespace Catch -#define CATCH_REGISTER_TAG_ALIAS( alias, spec ) \ - CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \ - CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ - namespace{ Catch::RegistrarForTagAliases INTERNAL_CATCH_UNIQUE_NAME( AutoRegisterTagAlias )( alias, spec, CATCH_INTERNAL_LINEINFO ); } \ - CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION +#define CATCH_REGISTER_TAG_ALIAS(alias, spec) \ + CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \ + CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ + namespace { \ + Catch::RegistrarForTagAliases \ + INTERNAL_CATCH_UNIQUE_NAME(AutoRegisterTagAlias)(alias, spec, CATCH_INTERNAL_LINEINFO); \ + } \ + CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION // end catch_tag_alias_autoregistrar.h // start catch_test_registry.h @@ -584,28 +604,29 @@ namespace Catch { namespace Catch { - class TestSpec; +class TestSpec; - struct ITestInvoker { - virtual void invoke () const = 0; - virtual ~ITestInvoker(); - }; +struct ITestInvoker { + virtual void invoke() const = 0; + virtual ~ITestInvoker(); +}; - class TestCase; - struct IConfig; +class TestCase; +struct IConfig; - struct ITestCaseRegistry { - virtual ~ITestCaseRegistry(); - virtual std::vector const& getAllTests() const = 0; - virtual std::vector const& getAllTestsSorted( IConfig const& config ) const = 0; - }; +struct ITestCaseRegistry { + virtual ~ITestCaseRegistry(); + virtual std::vector const& getAllTests() const = 0; + virtual std::vector const& getAllTestsSorted(IConfig const& config) const = 0; +}; - bool isThrowSafe( TestCase const& testCase, IConfig const& config ); - bool matchTest( TestCase const& testCase, TestSpec const& testSpec, IConfig const& config ); - std::vector filterTests( std::vector const& testCases, TestSpec const& testSpec, IConfig const& config ); - std::vector const& getAllTestCasesSorted( IConfig const& config ); +bool isThrowSafe(TestCase const& testCase, IConfig const& config); +bool matchTest(TestCase const& testCase, TestSpec const& testSpec, IConfig const& config); +std::vector filterTests(std::vector const& testCases, TestSpec const& testSpec, + IConfig const& config); +std::vector const& getAllTestCasesSorted(IConfig const& config); -} +} // namespace Catch // end catch_interfaces_testcase.h // start catch_stringref.h @@ -617,110 +638,116 @@ namespace Catch { namespace Catch { - /// A non-owning string class (similar to the forthcoming std::string_view) - /// Note that, because a StringRef may be a substring of another string, - /// it may not be null terminated. - class StringRef { - public: - using size_type = std::size_t; - using const_iterator = const char*; +/// A non-owning string class (similar to the forthcoming std::string_view) +/// Note that, because a StringRef may be a substring of another string, +/// it may not be null terminated. +class StringRef { +public: + using size_type = std::size_t; + using const_iterator = const char*; - private: - static constexpr char const* const s_empty = ""; +private: + static constexpr char const* const s_empty = ""; - char const* m_start = s_empty; - size_type m_size = 0; + char const* m_start = s_empty; + size_type m_size = 0; - public: // construction - constexpr StringRef() noexcept = default; +public: // construction + constexpr StringRef() noexcept = default; - StringRef( char const* rawChars ) noexcept; + StringRef(char const* rawChars) noexcept; - constexpr StringRef( char const* rawChars, size_type size ) noexcept - : m_start( rawChars ), - m_size( size ) - {} + constexpr StringRef(char const* rawChars, size_type size) noexcept + : m_start(rawChars), m_size(size) {} - StringRef( std::string const& stdString ) noexcept - : m_start( stdString.c_str() ), - m_size( stdString.size() ) - {} + StringRef(std::string const& stdString) noexcept + : m_start(stdString.c_str()), m_size(stdString.size()) {} - explicit operator std::string() const { - return std::string(m_start, m_size); - } + explicit operator std::string() const { + return std::string(m_start, m_size); + } - public: // operators - auto operator == ( StringRef const& other ) const noexcept -> bool; - auto operator != (StringRef const& other) const noexcept -> bool { - return !(*this == other); - } +public: // operators + auto operator==(StringRef const& other) const noexcept -> bool; + auto operator!=(StringRef const& other) const noexcept -> bool { + return !(*this == other); + } - auto operator[] ( size_type index ) const noexcept -> char { - assert(index < m_size); - return m_start[index]; - } + auto operator[](size_type index) const noexcept -> char { + assert(index < m_size); + return m_start[index]; + } - public: // named queries - constexpr auto empty() const noexcept -> bool { - return m_size == 0; - } - constexpr auto size() const noexcept -> size_type { - return m_size; - } +public: // named queries + constexpr auto empty() const noexcept -> bool { + return m_size == 0; + } + constexpr auto size() const noexcept -> size_type { + return m_size; + } - // Returns the current start pointer. If the StringRef is not - // null-terminated, throws std::domain_exception - auto c_str() const -> char const*; + // Returns the current start pointer. If the StringRef is not + // null-terminated, throws std::domain_exception + auto c_str() const -> char const*; - public: // substrings and searches - // Returns a substring of [start, start + length). - // If start + length > size(), then the substring is [start, size()). - // If start > size(), then the substring is empty. - auto substr( size_type start, size_type length ) const noexcept -> StringRef; +public: // substrings and searches + // Returns a substring of [start, start + length). + // If start + length > size(), then the substring is [start, size()). + // If start > size(), then the substring is empty. + auto substr(size_type start, size_type length) const noexcept -> StringRef; - // Returns the current start pointer. May not be null-terminated. - auto data() const noexcept -> char const*; + // Returns the current start pointer. May not be null-terminated. + auto data() const noexcept -> char const*; - constexpr auto isNullTerminated() const noexcept -> bool { - return m_start[m_size] == '\0'; - } + constexpr auto isNullTerminated() const noexcept -> bool { + return m_start[m_size] == '\0'; + } - public: // iterators - constexpr const_iterator begin() const { return m_start; } - constexpr const_iterator end() const { return m_start + m_size; } - }; +public: // iterators + constexpr const_iterator begin() const { + return m_start; + } + constexpr const_iterator end() const { + return m_start + m_size; + } +}; - auto operator += ( std::string& lhs, StringRef const& sr ) -> std::string&; - auto operator << ( std::ostream& os, StringRef const& sr ) -> std::ostream&; +auto operator+=(std::string& lhs, StringRef const& sr) -> std::string&; +auto operator<<(std::ostream& os, StringRef const& sr) -> std::ostream&; - constexpr auto operator "" _sr( char const* rawChars, std::size_t size ) noexcept -> StringRef { - return StringRef( rawChars, size ); - } +constexpr auto operator"" _sr(char const* rawChars, std::size_t size) noexcept -> StringRef { + return StringRef(rawChars, size); +} } // namespace Catch -constexpr auto operator "" _catch_sr( char const* rawChars, std::size_t size ) noexcept -> Catch::StringRef { - return Catch::StringRef( rawChars, size ); +constexpr auto operator"" _catch_sr(char const* rawChars, std::size_t size) noexcept + -> Catch::StringRef { + return Catch::StringRef(rawChars, size); } // end catch_stringref.h // start catch_preprocessor.hpp - #define CATCH_RECURSION_LEVEL0(...) __VA_ARGS__ -#define CATCH_RECURSION_LEVEL1(...) CATCH_RECURSION_LEVEL0(CATCH_RECURSION_LEVEL0(CATCH_RECURSION_LEVEL0(__VA_ARGS__))) -#define CATCH_RECURSION_LEVEL2(...) CATCH_RECURSION_LEVEL1(CATCH_RECURSION_LEVEL1(CATCH_RECURSION_LEVEL1(__VA_ARGS__))) -#define CATCH_RECURSION_LEVEL3(...) CATCH_RECURSION_LEVEL2(CATCH_RECURSION_LEVEL2(CATCH_RECURSION_LEVEL2(__VA_ARGS__))) -#define CATCH_RECURSION_LEVEL4(...) CATCH_RECURSION_LEVEL3(CATCH_RECURSION_LEVEL3(CATCH_RECURSION_LEVEL3(__VA_ARGS__))) -#define CATCH_RECURSION_LEVEL5(...) CATCH_RECURSION_LEVEL4(CATCH_RECURSION_LEVEL4(CATCH_RECURSION_LEVEL4(__VA_ARGS__))) +#define CATCH_RECURSION_LEVEL1(...) \ + CATCH_RECURSION_LEVEL0(CATCH_RECURSION_LEVEL0(CATCH_RECURSION_LEVEL0(__VA_ARGS__))) +#define CATCH_RECURSION_LEVEL2(...) \ + CATCH_RECURSION_LEVEL1(CATCH_RECURSION_LEVEL1(CATCH_RECURSION_LEVEL1(__VA_ARGS__))) +#define CATCH_RECURSION_LEVEL3(...) \ + CATCH_RECURSION_LEVEL2(CATCH_RECURSION_LEVEL2(CATCH_RECURSION_LEVEL2(__VA_ARGS__))) +#define CATCH_RECURSION_LEVEL4(...) \ + CATCH_RECURSION_LEVEL3(CATCH_RECURSION_LEVEL3(CATCH_RECURSION_LEVEL3(__VA_ARGS__))) +#define CATCH_RECURSION_LEVEL5(...) \ + CATCH_RECURSION_LEVEL4(CATCH_RECURSION_LEVEL4(CATCH_RECURSION_LEVEL4(__VA_ARGS__))) #ifdef CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR #define INTERNAL_CATCH_EXPAND_VARGS(...) __VA_ARGS__ // MSVC needs more evaluations -#define CATCH_RECURSION_LEVEL6(...) CATCH_RECURSION_LEVEL5(CATCH_RECURSION_LEVEL5(CATCH_RECURSION_LEVEL5(__VA_ARGS__))) -#define CATCH_RECURSE(...) CATCH_RECURSION_LEVEL6(CATCH_RECURSION_LEVEL6(__VA_ARGS__)) +#define CATCH_RECURSION_LEVEL6(...) \ + CATCH_RECURSION_LEVEL5(CATCH_RECURSION_LEVEL5(CATCH_RECURSION_LEVEL5(__VA_ARGS__))) +#define CATCH_RECURSE(...) CATCH_RECURSION_LEVEL6(CATCH_RECURSION_LEVEL6(__VA_ARGS__)) #else -#define CATCH_RECURSE(...) CATCH_RECURSION_LEVEL5(__VA_ARGS__) +#define CATCH_RECURSE(...) CATCH_RECURSION_LEVEL5(__VA_ARGS__) #endif #define CATCH_REC_END(...) @@ -733,37 +760,50 @@ constexpr auto operator "" _catch_sr( char const* rawChars, std::size_t size ) n #define CATCH_REC_GET_END1(...) CATCH_REC_GET_END2 #define CATCH_REC_GET_END(...) CATCH_REC_GET_END1 #define CATCH_REC_NEXT0(test, next, ...) next CATCH_REC_OUT -#define CATCH_REC_NEXT1(test, next) CATCH_DEFER ( CATCH_REC_NEXT0 ) ( test, next, 0) -#define CATCH_REC_NEXT(test, next) CATCH_REC_NEXT1(CATCH_REC_GET_END test, next) - -#define CATCH_REC_LIST0(f, x, peek, ...) , f(x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST1) ) ( f, peek, __VA_ARGS__ ) -#define CATCH_REC_LIST1(f, x, peek, ...) , f(x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST0) ) ( f, peek, __VA_ARGS__ ) -#define CATCH_REC_LIST2(f, x, peek, ...) f(x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST1) ) ( f, peek, __VA_ARGS__ ) - -#define CATCH_REC_LIST0_UD(f, userdata, x, peek, ...) , f(userdata, x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST1_UD) ) ( f, userdata, peek, __VA_ARGS__ ) -#define CATCH_REC_LIST1_UD(f, userdata, x, peek, ...) , f(userdata, x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST0_UD) ) ( f, userdata, peek, __VA_ARGS__ ) -#define CATCH_REC_LIST2_UD(f, userdata, x, peek, ...) f(userdata, x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST1_UD) ) ( f, userdata, peek, __VA_ARGS__ ) +#define CATCH_REC_NEXT1(test, next) CATCH_DEFER(CATCH_REC_NEXT0)(test, next, 0) +#define CATCH_REC_NEXT(test, next) CATCH_REC_NEXT1(CATCH_REC_GET_END test, next) + +#define CATCH_REC_LIST0(f, x, peek, ...) \ + , f(x) CATCH_DEFER(CATCH_REC_NEXT(peek, CATCH_REC_LIST1))(f, peek, __VA_ARGS__) +#define CATCH_REC_LIST1(f, x, peek, ...) \ + , f(x) CATCH_DEFER(CATCH_REC_NEXT(peek, CATCH_REC_LIST0))(f, peek, __VA_ARGS__) +#define CATCH_REC_LIST2(f, x, peek, ...) \ + f(x) CATCH_DEFER(CATCH_REC_NEXT(peek, CATCH_REC_LIST1))(f, peek, __VA_ARGS__) + +#define CATCH_REC_LIST0_UD(f, userdata, x, peek, ...) \ + , f(userdata, x) \ + CATCH_DEFER(CATCH_REC_NEXT(peek, CATCH_REC_LIST1_UD))(f, userdata, peek, __VA_ARGS__) +#define CATCH_REC_LIST1_UD(f, userdata, x, peek, ...) \ + , f(userdata, x) \ + CATCH_DEFER(CATCH_REC_NEXT(peek, CATCH_REC_LIST0_UD))(f, userdata, peek, __VA_ARGS__) +#define CATCH_REC_LIST2_UD(f, userdata, x, peek, ...) \ + f(userdata, x) \ + CATCH_DEFER(CATCH_REC_NEXT(peek, CATCH_REC_LIST1_UD))(f, userdata, peek, __VA_ARGS__) // Applies the function macro `f` to each of the remaining parameters, inserts commas between the results, // and passes userdata as the first parameter to each invocation, // e.g. CATCH_REC_LIST_UD(f, x, a, b, c) evaluates to f(x, a), f(x, b), f(x, c) -#define CATCH_REC_LIST_UD(f, userdata, ...) CATCH_RECURSE(CATCH_REC_LIST2_UD(f, userdata, __VA_ARGS__, ()()(), ()()(), ()()(), 0)) +#define CATCH_REC_LIST_UD(f, userdata, ...) \ + CATCH_RECURSE(CATCH_REC_LIST2_UD(f, userdata, __VA_ARGS__, ()()(), ()()(), ()()(), 0)) -#define CATCH_REC_LIST(f, ...) CATCH_RECURSE(CATCH_REC_LIST2(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0)) +#define CATCH_REC_LIST(f, ...) \ + CATCH_RECURSE(CATCH_REC_LIST2(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0)) #define INTERNAL_CATCH_EXPAND1(param) INTERNAL_CATCH_EXPAND2(param) -#define INTERNAL_CATCH_EXPAND2(...) INTERNAL_CATCH_NO## __VA_ARGS__ +#define INTERNAL_CATCH_EXPAND2(...) INTERNAL_CATCH_NO##__VA_ARGS__ #define INTERNAL_CATCH_DEF(...) INTERNAL_CATCH_DEF __VA_ARGS__ #define INTERNAL_CATCH_NOINTERNAL_CATCH_DEF #define INTERNAL_CATCH_STRINGIZE(...) INTERNAL_CATCH_STRINGIZE2(__VA_ARGS__) #ifndef CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR #define INTERNAL_CATCH_STRINGIZE2(...) #__VA_ARGS__ -#define INTERNAL_CATCH_STRINGIZE_WITHOUT_PARENS(param) INTERNAL_CATCH_STRINGIZE(INTERNAL_CATCH_REMOVE_PARENS(param)) +#define INTERNAL_CATCH_STRINGIZE_WITHOUT_PARENS(param) \ + INTERNAL_CATCH_STRINGIZE(INTERNAL_CATCH_REMOVE_PARENS(param)) #else // MSVC is adding extra space and needs another indirection to expand INTERNAL_CATCH_NOINTERNAL_CATCH_DEF #define INTERNAL_CATCH_STRINGIZE2(...) INTERNAL_CATCH_STRINGIZE3(__VA_ARGS__) #define INTERNAL_CATCH_STRINGIZE3(...) #__VA_ARGS__ -#define INTERNAL_CATCH_STRINGIZE_WITHOUT_PARENS(param) (INTERNAL_CATCH_STRINGIZE(INTERNAL_CATCH_REMOVE_PARENS(param)) + 1) +#define INTERNAL_CATCH_STRINGIZE_WITHOUT_PARENS(param) \ + (INTERNAL_CATCH_STRINGIZE(INTERNAL_CATCH_REMOVE_PARENS(param)) + 1) #endif #define INTERNAL_CATCH_MAKE_NAMESPACE2(...) ns_##__VA_ARGS__ @@ -772,588 +812,899 @@ constexpr auto operator "" _catch_sr( char const* rawChars, std::size_t size ) n #define INTERNAL_CATCH_REMOVE_PARENS(...) INTERNAL_CATCH_EXPAND1(INTERNAL_CATCH_DEF __VA_ARGS__) #ifndef CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR -#define INTERNAL_CATCH_MAKE_TYPE_LIST2(...) decltype(get_wrapper()) -#define INTERNAL_CATCH_MAKE_TYPE_LIST(...) INTERNAL_CATCH_MAKE_TYPE_LIST2(INTERNAL_CATCH_REMOVE_PARENS(__VA_ARGS__)) +#define INTERNAL_CATCH_MAKE_TYPE_LIST2(...) \ + decltype(get_wrapper()) +#define INTERNAL_CATCH_MAKE_TYPE_LIST(...) \ + INTERNAL_CATCH_MAKE_TYPE_LIST2(INTERNAL_CATCH_REMOVE_PARENS(__VA_ARGS__)) #else -#define INTERNAL_CATCH_MAKE_TYPE_LIST2(...) INTERNAL_CATCH_EXPAND_VARGS(decltype(get_wrapper())) -#define INTERNAL_CATCH_MAKE_TYPE_LIST(...) INTERNAL_CATCH_EXPAND_VARGS(INTERNAL_CATCH_MAKE_TYPE_LIST2(INTERNAL_CATCH_REMOVE_PARENS(__VA_ARGS__))) +#define INTERNAL_CATCH_MAKE_TYPE_LIST2(...) \ + INTERNAL_CATCH_EXPAND_VARGS( \ + decltype(get_wrapper())) +#define INTERNAL_CATCH_MAKE_TYPE_LIST(...) \ + INTERNAL_CATCH_EXPAND_VARGS( \ + INTERNAL_CATCH_MAKE_TYPE_LIST2(INTERNAL_CATCH_REMOVE_PARENS(__VA_ARGS__))) #endif -#define INTERNAL_CATCH_MAKE_TYPE_LISTS_FROM_TYPES(...)\ - CATCH_REC_LIST(INTERNAL_CATCH_MAKE_TYPE_LIST,__VA_ARGS__) +#define INTERNAL_CATCH_MAKE_TYPE_LISTS_FROM_TYPES(...) \ + CATCH_REC_LIST(INTERNAL_CATCH_MAKE_TYPE_LIST, __VA_ARGS__) #define INTERNAL_CATCH_REMOVE_PARENS_1_ARG(_0) INTERNAL_CATCH_REMOVE_PARENS(_0) -#define INTERNAL_CATCH_REMOVE_PARENS_2_ARG(_0, _1) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_1_ARG(_1) -#define INTERNAL_CATCH_REMOVE_PARENS_3_ARG(_0, _1, _2) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_2_ARG(_1, _2) -#define INTERNAL_CATCH_REMOVE_PARENS_4_ARG(_0, _1, _2, _3) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_3_ARG(_1, _2, _3) -#define INTERNAL_CATCH_REMOVE_PARENS_5_ARG(_0, _1, _2, _3, _4) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_4_ARG(_1, _2, _3, _4) -#define INTERNAL_CATCH_REMOVE_PARENS_6_ARG(_0, _1, _2, _3, _4, _5) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_5_ARG(_1, _2, _3, _4, _5) -#define INTERNAL_CATCH_REMOVE_PARENS_7_ARG(_0, _1, _2, _3, _4, _5, _6) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_6_ARG(_1, _2, _3, _4, _5, _6) -#define INTERNAL_CATCH_REMOVE_PARENS_8_ARG(_0, _1, _2, _3, _4, _5, _6, _7) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_7_ARG(_1, _2, _3, _4, _5, _6, _7) -#define INTERNAL_CATCH_REMOVE_PARENS_9_ARG(_0, _1, _2, _3, _4, _5, _6, _7, _8) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_8_ARG(_1, _2, _3, _4, _5, _6, _7, _8) -#define INTERNAL_CATCH_REMOVE_PARENS_10_ARG(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_9_ARG(_1, _2, _3, _4, _5, _6, _7, _8, _9) -#define INTERNAL_CATCH_REMOVE_PARENS_11_ARG(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_10_ARG(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10) +#define INTERNAL_CATCH_REMOVE_PARENS_2_ARG(_0, _1) \ + INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_1_ARG(_1) +#define INTERNAL_CATCH_REMOVE_PARENS_3_ARG(_0, _1, _2) \ + INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_2_ARG(_1, _2) +#define INTERNAL_CATCH_REMOVE_PARENS_4_ARG(_0, _1, _2, _3) \ + INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_3_ARG(_1, _2, _3) +#define INTERNAL_CATCH_REMOVE_PARENS_5_ARG(_0, _1, _2, _3, _4) \ + INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_4_ARG(_1, _2, _3, _4) +#define INTERNAL_CATCH_REMOVE_PARENS_6_ARG(_0, _1, _2, _3, _4, _5) \ + INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_5_ARG(_1, _2, _3, _4, _5) +#define INTERNAL_CATCH_REMOVE_PARENS_7_ARG(_0, _1, _2, _3, _4, _5, _6) \ + INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_6_ARG(_1, _2, _3, _4, _5, _6) +#define INTERNAL_CATCH_REMOVE_PARENS_8_ARG(_0, _1, _2, _3, _4, _5, _6, _7) \ + INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_7_ARG(_1, _2, _3, _4, _5, _6, _7) +#define INTERNAL_CATCH_REMOVE_PARENS_9_ARG(_0, _1, _2, _3, _4, _5, _6, _7, _8) \ + INTERNAL_CATCH_REMOVE_PARENS(_0), \ + INTERNAL_CATCH_REMOVE_PARENS_8_ARG(_1, _2, _3, _4, _5, _6, _7, _8) +#define INTERNAL_CATCH_REMOVE_PARENS_10_ARG(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9) \ + INTERNAL_CATCH_REMOVE_PARENS(_0), \ + INTERNAL_CATCH_REMOVE_PARENS_9_ARG(_1, _2, _3, _4, _5, _6, _7, _8, _9) +#define INTERNAL_CATCH_REMOVE_PARENS_11_ARG(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10) \ + INTERNAL_CATCH_REMOVE_PARENS(_0), \ + INTERNAL_CATCH_REMOVE_PARENS_10_ARG(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10) #define INTERNAL_CATCH_VA_NARGS_IMPL(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N -#define INTERNAL_CATCH_TYPE_GEN\ - template struct TypeList {};\ - template\ - constexpr auto get_wrapper() noexcept -> TypeList { return {}; }\ - template class...> struct TemplateTypeList{};\ - template class...Cs>\ - constexpr auto get_wrapper() noexcept -> TemplateTypeList { return {}; }\ - template\ - struct append;\ - template\ - struct rewrap;\ - template class, typename...>\ - struct create;\ - template class, typename>\ - struct convert;\ - \ - template \ - struct append { using type = T; };\ - template< template class L1, typename...E1, template class L2, typename...E2, typename...Rest>\ - struct append, L2, Rest...> { using type = typename append, Rest...>::type; };\ - template< template class L1, typename...E1, typename...Rest>\ - struct append, TypeList, Rest...> { using type = L1; };\ - \ - template< template class Container, template class List, typename...elems>\ - struct rewrap, List> { using type = TypeList>; };\ - template< template class Container, template class List, class...Elems, typename...Elements>\ - struct rewrap, List, Elements...> { using type = typename append>, typename rewrap, Elements...>::type>::type; };\ - \ - template