-
Notifications
You must be signed in to change notification settings - Fork 17
/
NameUtils.java
195 lines (165 loc) · 4.82 KB
/
NameUtils.java
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
package com.demandware.carbonj.service.db.index;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.python.google.common.base.Preconditions;
import com.demandware.carbonj.service.db.util.Quota;
public class NameUtils
{
private static final Logger log = LoggerFactory.getLogger( NameUtils.class );
final private String rootKey;
final private String rootKeyPrefix;
final private Quota nameErrLogQuota;
NameUtils( String rootKey, Quota nameErrLogQuota )
{
this.rootKey = rootKey;
this.rootKeyPrefix = rootKey + ".";
this.nameErrLogQuota = Preconditions.checkNotNull( nameErrLogQuota );
}
public NameUtils()
{
this( InternalConfig.getRootEntryKey() );
}
public NameUtils( Quota nameErrLogQuota )
{
this( InternalConfig.getRootEntryKey(), nameErrLogQuota );
}
NameUtils( String rootKey )
{
this( rootKey, new Quota( 0, 0 )
{
// TBD - just a simple hack for now - by default not log errors
@Override
public boolean allow()
{
return false;
}
} );
}
public boolean isValid( String name )
{
return isValid( name, nameErrLogQuota.allow() );
}
public boolean isValid( String name, boolean verbose )
{
if ( name == null )
{
if ( verbose )
{
log.warn( "Invalid metric name - [null]." );
}
return false;
}
if ( name.length() == 0 )
{
if ( verbose )
{
log.warn( "Invalid metric name - [\"\"]." );
}
return false;
}
if ( name.startsWith( rootKeyPrefix ) )
{
if ( verbose )
{
log.warn( String.format( "Metric name [%s] is invalid because it starts with reserved prefix [%s]",
name, rootKeyPrefix ) );
}
return false;
}
boolean valid = true;
char prev;
char ch = '.';
for ( int i = 0, n = name.length(); i < n; i++ )
{
prev = ch;
ch = name.charAt( i );
// a-z
if ( ch >= 0x61 && ch <= 0x7A )
{
continue;
}
// A-Z
if ( ch >= 0x41 && ch <= 0x5A )
{
continue;
}
// 0-9
if ( ch >= 0x30 && ch <= 0x39 )
{
continue;
}
// doesn't start with '.', and doesn't have '..' in the middle
if ( ch == '.' && prev != '.' )
{
continue;
}
if ( ch == '_' || ch == '-' || ch == ':' || ch == '=' || ch == '%' )
{
continue;
}
// invalid character
if ( verbose )
{
log.warn( String.format( "Metric name [%s] contains invalid character [%s] in pos [%s]", name, ch, i ) );
}
valid = false;
break;
}
// name ends with '.'
if ( valid && ch == '.' )
{
if ( verbose )
{
log.warn( String.format( "Metric name [%s] is invalid because it ends with '.'", name ) );
}
valid = false;
}
return valid;
}
public boolean isTopLevel( String name )
{
return name.indexOf( '.' ) == -1;
}
public String firstSegment( String name )
{
int i = name.indexOf( '.' );
return i < 0 ? name : name.substring( 0, i );
}
public Optional<String> parentName( String name )
{
int i = name.lastIndexOf( '.' );
return i < 0 ? Optional.empty() : Optional.of( name.substring( 0, i ) );
}
public String[] metricNameHierarchy( String name )
{
// brute force
String[] parts = name.split( "\\." );
String[] paths = new String[parts.length];
for ( int i = 0; i < paths.length; i++ )
{
if ( i > 0 )
{
paths[i] = paths[i - 1] + "." + parts[i];
}
else
{
paths[i] = parts[i];
}
}
return paths;
}
/**
* Assembles metric name from (parent, child) segments and skips parent if it is a root key.
*/
public String toMetricName( String parentPath, String childPath )
{
return rootKey.equals( parentPath ) ? childPath : parentPath + "." + childPath;
}
}