forked from apache/rocketmq-client-cpp
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNamespaceUtil.cpp
More file actions
119 lines (96 loc) · 4.19 KB
/
Copy pathNamespaceUtil.cpp
File metadata and controls
119 lines (96 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* 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 "NamespaceUtil.h"
#include "Logging.h"
#include "UtilAll.h"
namespace rocketmq {
static const char NAMESPACE_SEPARATOR = '%';
static const std::string STRING_BLANK = "";
static const size_t RETRY_PREFIX_LENGTH = RETRY_GROUP_TOPIC_PREFIX.length();
static const size_t DLQ_PREFIX_LENGTH = DLQ_GROUP_TOPIC_PREFIX.length();
static const std::string ENDPOINT_PREFIX = "http://";
static const size_t ENDPOINT_PREFIX_LENGTH = ENDPOINT_PREFIX.length();
std::string NamespaceUtil::withoutNamespace(const std::string& resourceWithNamespace) {
if (resourceWithNamespace.empty() || isSystemResource(resourceWithNamespace)) {
return resourceWithNamespace;
}
auto resourceWithoutRetryAndDLQ = withoutRetryAndDLQ(resourceWithNamespace);
auto index = resourceWithoutRetryAndDLQ.find(NAMESPACE_SEPARATOR);
if (index > 0) {
auto resourceWithoutNamespace = resourceWithoutRetryAndDLQ.substr(index + 1);
if (UtilAll::isRetryTopic(resourceWithNamespace)) {
return UtilAll::getRetryTopic(resourceWithoutNamespace);
} else if (UtilAll::isDLQTopic(resourceWithNamespace)) {
return UtilAll::getDLQTopic(resourceWithoutNamespace);
} else {
return resourceWithoutNamespace;
}
}
return resourceWithNamespace;
}
std::string NamespaceUtil::withoutNamespace(const std::string& resourceWithNamespace, const std::string& name_space) {
if (resourceWithNamespace.empty() || name_space.empty()) {
return resourceWithNamespace;
}
auto resourceWithoutRetryAndDLQ = withoutRetryAndDLQ(resourceWithNamespace);
if (resourceWithoutRetryAndDLQ.find(name_space + NAMESPACE_SEPARATOR) == 0) {
return withoutNamespace(resourceWithNamespace);
}
return resourceWithNamespace;
}
std::string NamespaceUtil::wrapNamespace(const std::string& name_space, const std::string& resourceWithoutNamespace) {
if (name_space.empty() || resourceWithoutNamespace.empty()) {
return resourceWithoutNamespace;
}
// if (isSystemResource(resourceWithoutNamespace) || isAlreadyWithNamespace(resourceWithoutNamespace, namespace)) {
// return resourceWithoutNamespace;
// }
auto resourceWithoutRetryAndDLQ = withoutRetryAndDLQ(resourceWithoutNamespace);
std::string resourceWithNamespace;
if (UtilAll::isRetryTopic(resourceWithoutNamespace)) {
resourceWithNamespace.append(RETRY_GROUP_TOPIC_PREFIX);
}
if (UtilAll::isDLQTopic(resourceWithoutNamespace)) {
resourceWithNamespace.append(DLQ_GROUP_TOPIC_PREFIX);
}
resourceWithNamespace.append(name_space);
resourceWithNamespace.push_back(NAMESPACE_SEPARATOR);
resourceWithNamespace.append(resourceWithoutRetryAndDLQ);
return resourceWithNamespace;
}
std::string NamespaceUtil::withoutRetryAndDLQ(const std::string& originalResource) {
if (UtilAll::isRetryTopic(originalResource)) {
return originalResource.substr(RETRY_PREFIX_LENGTH);
} else if (UtilAll::isDLQTopic(originalResource)) {
return originalResource.substr(DLQ_PREFIX_LENGTH);
} else {
return originalResource;
}
}
bool NamespaceUtil::isSystemResource(const std::string& resource) {
return false;
}
bool NamespaceUtil::isEndPointURL(const std::string& nameServerAddr) {
return nameServerAddr.find(ENDPOINT_PREFIX) == 0;
}
std::string NamespaceUtil::formatNameServerURL(const std::string& nameServerAddr) {
if (nameServerAddr.find(ENDPOINT_PREFIX) == 0) {
return nameServerAddr.substr(ENDPOINT_PREFIX_LENGTH);
}
return nameServerAddr;
}
} // namespace rocketmq