diff --git a/cpp/src/common/CMakeLists.txt b/cpp/src/common/CMakeLists.txt index 7ac55ab5c..4406cb219 100644 --- a/cpp/src/common/CMakeLists.txt +++ b/cpp/src/common/CMakeLists.txt @@ -33,6 +33,10 @@ add_library(common_obj OBJECT ${common_SRC_LIST} ${common_mutex_SRC_LIST} ${common_datatype_SRC_LIST}) +if (ENABLE_ANTLR4) + target_compile_definitions(common_obj PRIVATE ENABLE_ANTLR4) +endif() + # install header files recursively file(GLOB_RECURSE HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/*.h") copy_to_dir(${HEADERS} "common_obj") \ No newline at end of file diff --git a/cpp/src/common/device_id.cc b/cpp/src/common/device_id.cc index 263cb675c..02cd32510 100644 --- a/cpp/src/common/device_id.cc +++ b/cpp/src/common/device_id.cc @@ -211,7 +211,8 @@ std::vector StringArrayDeviceID::split_device_id_string( auto splits = storage::PathNodesGenerator::invokeParser(device_id_string); return split_device_id_string(splits); #else - return split_string(device_id_string, '.'); + auto splits = split_string(device_id_string, '.'); + return split_device_id_string(splits); #endif } diff --git a/cpp/src/common/path.cc b/cpp/src/common/path.cc new file mode 100644 index 000000000..fae3bff26 --- /dev/null +++ b/cpp/src/common/path.cc @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * License); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "common/path.h" + +#ifdef ENABLE_ANTLR4 +#include "parser/path_nodes_generator.h" +#endif + +namespace storage { + +Path::Path() = default; + +Path::Path(std::string& device, std::string& measurement) + : measurement_(measurement), + device_id_(std::make_shared(device)) { + full_path_ = device + "." + measurement; +} + +Path::Path(const std::string& path_sc, bool if_split) { + if (!path_sc.empty()) { + if (!if_split) { + full_path_ = path_sc; + device_id_ = std::make_shared(path_sc); + } else { +#ifdef ENABLE_ANTLR4 + std::vector nodes = + PathNodesGenerator::invokeParser(path_sc); +#else + std::vector nodes = + IDeviceID::split_string(path_sc, '.'); +#endif + if (nodes.size() > 1) { + device_id_ = std::make_shared( + std::vector(nodes.begin(), nodes.end() - 1)); + measurement_ = nodes[nodes.size() - 1]; + full_path_ = device_id_->get_device_name() + "." + measurement_; + } else { + full_path_ = path_sc; + device_id_ = std::make_shared(); + measurement_ = path_sc; + } + } + } else { + full_path_ = ""; + device_id_ = std::make_shared(); + measurement_ = ""; + } +} + +} // namespace storage diff --git a/cpp/src/common/path.h b/cpp/src/common/path.h index 55abf810d..3896b2715 100644 --- a/cpp/src/common/path.h +++ b/cpp/src/common/path.h @@ -22,10 +22,6 @@ #include #include "common/device_id.h" -#ifdef ENABLE_ANTLR4 -#include "parser/generated/PathParser.h" -#include "parser/path_nodes_generator.h" -#endif #include "utils/errno_define.h" namespace storage { @@ -35,46 +31,9 @@ struct Path { std::shared_ptr device_id_; std::string full_path_; - Path() {} - - Path(std::string& device, std::string& measurement) - : measurement_(measurement), - device_id_(std::make_shared(device)) { - full_path_ = device + "." + measurement; - } - - Path(const std::string& path_sc, bool if_split = true) { - if (!path_sc.empty()) { - if (!if_split) { - full_path_ = path_sc; - device_id_ = std::make_shared(path_sc); - } else { -#ifdef ENABLE_ANTLR4 - std::vector nodes = - PathNodesGenerator::invokeParser(path_sc); -#else - std::vector nodes = - IDeviceID::split_string(path_sc, '.'); -#endif - if (nodes.size() > 1) { - device_id_ = std::make_shared( - std::vector(nodes.begin(), - nodes.end() - 1)); - measurement_ = nodes[nodes.size() - 1]; - full_path_ = - device_id_->get_device_name() + "." + measurement_; - } else { - full_path_ = path_sc; - device_id_ = std::make_shared(); - measurement_ = path_sc; - } - } - } else { - full_path_ = ""; - device_id_ = std::make_shared(); - measurement_ = ""; - } - } + Path(); + Path(std::string& device, std::string& measurement); + Path(const std::string& path_sc, bool if_split = true); bool operator==(const Path& path) { if (measurement_.compare(path.measurement_) == 0 && diff --git a/cpp/test/common/device_id_test.cc b/cpp/test/common/device_id_test.cc index a72bd2889..f3877c278 100644 --- a/cpp/test/common/device_id_test.cc +++ b/cpp/test/common/device_id_test.cc @@ -31,6 +31,16 @@ TEST(DeviceIdTest, NormalTest) { ASSERT_EQ("root.db.tb.device1", device_id.get_device_name()); } +TEST(DeviceIdTest, DeviceIdStringFallbackSemantic) { + std::string device_id_string = "root.sg1.FeederA"; + StringArrayDeviceID device_id = StringArrayDeviceID(device_id_string); + + // For a 3-level identifier, table name should be merged as "root.sg1". + ASSERT_EQ("root.sg1", device_id.get_table_name()); + ASSERT_EQ(2, device_id.segment_num()); + ASSERT_EQ("root.sg1.FeederA", device_id.get_device_name()); +} + TEST(DeviceIdTest, TabletDeviceId) { std::vector measurement_types{ TSDataType::STRING, TSDataType::STRING, TSDataType::STRING,