Skip to content

Commit f621373

Browse files
authored
fix cpp device_id parser (#759)
1 parent c67a199 commit f621373

5 files changed

Lines changed: 86 additions & 45 deletions

File tree

cpp/src/common/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ add_library(common_obj OBJECT ${common_SRC_LIST}
3333
${common_mutex_SRC_LIST}
3434
${common_datatype_SRC_LIST})
3535

36+
if (ENABLE_ANTLR4)
37+
target_compile_definitions(common_obj PRIVATE ENABLE_ANTLR4)
38+
endif()
39+
3640
# install header files recursively
3741
file(GLOB_RECURSE HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
3842
copy_to_dir(${HEADERS} "common_obj")

cpp/src/common/device_id.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ std::vector<std::string> StringArrayDeviceID::split_device_id_string(
211211
auto splits = storage::PathNodesGenerator::invokeParser(device_id_string);
212212
return split_device_id_string(splits);
213213
#else
214-
return split_string(device_id_string, '.');
214+
auto splits = split_string(device_id_string, '.');
215+
return split_device_id_string(splits);
215216
#endif
216217
}
217218

cpp/src/common/path.cc

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* License); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#include "common/path.h"
21+
22+
#ifdef ENABLE_ANTLR4
23+
#include "parser/path_nodes_generator.h"
24+
#endif
25+
26+
namespace storage {
27+
28+
Path::Path() = default;
29+
30+
Path::Path(std::string& device, std::string& measurement)
31+
: measurement_(measurement),
32+
device_id_(std::make_shared<StringArrayDeviceID>(device)) {
33+
full_path_ = device + "." + measurement;
34+
}
35+
36+
Path::Path(const std::string& path_sc, bool if_split) {
37+
if (!path_sc.empty()) {
38+
if (!if_split) {
39+
full_path_ = path_sc;
40+
device_id_ = std::make_shared<StringArrayDeviceID>(path_sc);
41+
} else {
42+
#ifdef ENABLE_ANTLR4
43+
std::vector<std::string> nodes =
44+
PathNodesGenerator::invokeParser(path_sc);
45+
#else
46+
std::vector<std::string> nodes =
47+
IDeviceID::split_string(path_sc, '.');
48+
#endif
49+
if (nodes.size() > 1) {
50+
device_id_ = std::make_shared<StringArrayDeviceID>(
51+
std::vector<std::string>(nodes.begin(), nodes.end() - 1));
52+
measurement_ = nodes[nodes.size() - 1];
53+
full_path_ = device_id_->get_device_name() + "." + measurement_;
54+
} else {
55+
full_path_ = path_sc;
56+
device_id_ = std::make_shared<StringArrayDeviceID>();
57+
measurement_ = path_sc;
58+
}
59+
}
60+
} else {
61+
full_path_ = "";
62+
device_id_ = std::make_shared<StringArrayDeviceID>();
63+
measurement_ = "";
64+
}
65+
}
66+
67+
} // namespace storage

cpp/src/common/path.h

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222
#include <string>
2323

2424
#include "common/device_id.h"
25-
#ifdef ENABLE_ANTLR4
26-
#include "parser/generated/PathParser.h"
27-
#include "parser/path_nodes_generator.h"
28-
#endif
2925
#include "utils/errno_define.h"
3026

3127
namespace storage {
@@ -35,46 +31,9 @@ struct Path {
3531
std::shared_ptr<IDeviceID> device_id_;
3632
std::string full_path_;
3733

38-
Path() {}
39-
40-
Path(std::string& device, std::string& measurement)
41-
: measurement_(measurement),
42-
device_id_(std::make_shared<StringArrayDeviceID>(device)) {
43-
full_path_ = device + "." + measurement;
44-
}
45-
46-
Path(const std::string& path_sc, bool if_split = true) {
47-
if (!path_sc.empty()) {
48-
if (!if_split) {
49-
full_path_ = path_sc;
50-
device_id_ = std::make_shared<StringArrayDeviceID>(path_sc);
51-
} else {
52-
#ifdef ENABLE_ANTLR4
53-
std::vector<std::string> nodes =
54-
PathNodesGenerator::invokeParser(path_sc);
55-
#else
56-
std::vector<std::string> nodes =
57-
IDeviceID::split_string(path_sc, '.');
58-
#endif
59-
if (nodes.size() > 1) {
60-
device_id_ = std::make_shared<StringArrayDeviceID>(
61-
std::vector<std::string>(nodes.begin(),
62-
nodes.end() - 1));
63-
measurement_ = nodes[nodes.size() - 1];
64-
full_path_ =
65-
device_id_->get_device_name() + "." + measurement_;
66-
} else {
67-
full_path_ = path_sc;
68-
device_id_ = std::make_shared<StringArrayDeviceID>();
69-
measurement_ = path_sc;
70-
}
71-
}
72-
} else {
73-
full_path_ = "";
74-
device_id_ = std::make_shared<StringArrayDeviceID>();
75-
measurement_ = "";
76-
}
77-
}
34+
Path();
35+
Path(std::string& device, std::string& measurement);
36+
Path(const std::string& path_sc, bool if_split = true);
7837

7938
bool operator==(const Path& path) {
8039
if (measurement_.compare(path.measurement_) == 0 &&

cpp/test/common/device_id_test.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ TEST(DeviceIdTest, NormalTest) {
3131
ASSERT_EQ("root.db.tb.device1", device_id.get_device_name());
3232
}
3333

34+
TEST(DeviceIdTest, DeviceIdStringFallbackSemantic) {
35+
std::string device_id_string = "root.sg1.FeederA";
36+
StringArrayDeviceID device_id = StringArrayDeviceID(device_id_string);
37+
38+
// For a 3-level identifier, table name should be merged as "root.sg1".
39+
ASSERT_EQ("root.sg1", device_id.get_table_name());
40+
ASSERT_EQ(2, device_id.segment_num());
41+
ASSERT_EQ("root.sg1.FeederA", device_id.get_device_name());
42+
}
43+
3444
TEST(DeviceIdTest, TabletDeviceId) {
3545
std::vector<TSDataType> measurement_types{
3646
TSDataType::STRING, TSDataType::STRING, TSDataType::STRING,

0 commit comments

Comments
 (0)