-
Notifications
You must be signed in to change notification settings - Fork 161
/
ArrayTagSet.java
494 lines (448 loc) · 14 KB
/
ArrayTagSet.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
* Copyright 2014-2020 Netflix, Inc.
*
* Licensed 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.
*/
package com.netflix.spectator.api;
import com.netflix.spectator.impl.Preconditions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.concurrent.ConcurrentMap;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
/**
* An immutable set of tags sorted by the tag key.
*/
final class ArrayTagSet implements TagList {
/** Empty tag set. */
static final ArrayTagSet EMPTY = new ArrayTagSet(new String[0]);
/** Create a new tag set. */
static ArrayTagSet create(String... tags) {
return EMPTY.addAll(tags);
}
/** Create a new tag set. */
static ArrayTagSet create(Tag... tags) {
return EMPTY.addAll(tags);
}
/** Create a new tag set. */
static ArrayTagSet create(Iterable<Tag> tags) {
return (tags instanceof ArrayTagSet) ? (ArrayTagSet) tags : EMPTY.addAll(tags);
}
/** Create a new tag set. */
static ArrayTagSet create(Map<String, String> tags) {
return EMPTY.addAll(tags);
}
/**
* This method can be used to get better performance for some critical use-cases, but also
* has increased risk. If there is any doubt, then use {@link #create(Tag...)} instead.
*
* <p>Create a new tag set based on the provided array. The provided array will be used
* so it should not be modified after. Caller must ensure that:</p>
*
* <ul>
* <li>Length of tags array is even.</li>
* <li>There are no null values for the first length entries in the array.</li>
* </ul>
*/
static ArrayTagSet unsafeCreate(String[] tags, int length) {
insertionSort(tags, length);
int len = dedupInPlace(tags, length);
return new ArrayTagSet(tags, len);
}
private final String[] tags;
private final int length;
private int cachedHashCode;
private ArrayTagSet(String[] tags) {
this(tags, tags.length);
}
private ArrayTagSet(String[] tags, int length) {
if (tags.length % 2 != 0) {
throw new IllegalArgumentException("length of tags array must be even");
}
if (length > tags.length) {
throw new IllegalArgumentException("length cannot be larger than tags array");
}
this.tags = tags;
this.length = length;
this.cachedHashCode = 0;
}
/** Check if this set is empty. */
boolean isEmpty() {
return length == 0;
}
/** Add a new tag to the set. */
@SuppressWarnings("PMD.AvoidArrayLoops")
ArrayTagSet add(String k, String v) {
Preconditions.checkNotNull(k, "key");
Preconditions.checkNotNull(v, "value");
if (length == 0) {
return new ArrayTagSet(new String[] {k, v});
} else {
String[] newTags = new String[length + 2];
int i = 0;
for (; i < length && tags[i].compareTo(k) < 0; i += 2) {
newTags[i] = tags[i];
newTags[i + 1] = tags[i + 1];
}
if (i < length && tags[i].equals(k)) {
// Override
newTags[i++] = k;
newTags[i++] = v;
System.arraycopy(tags, i, newTags, i, length - i);
i = length;
} else {
// Insert
newTags[i] = k;
newTags[i + 1] = v;
System.arraycopy(tags, i, newTags, i + 2, length - i);
i = newTags.length;
}
return new ArrayTagSet(newTags, i);
}
}
/** Add a new tag to the set. */
ArrayTagSet add(Tag tag) {
return add(tag.key(), tag.value());
}
/** Add a collection of tags to the set. */
ArrayTagSet addAll(Iterable<Tag> ts) {
if (ts instanceof Collection) {
Collection<Tag> data = (Collection<Tag>) ts;
if (data.isEmpty()) {
return this;
} else {
String[] newTags = new String[2 * data.size()];
int i = 0;
for (Tag t : data) {
newTags[i++] = t.key();
newTags[i++] = t.value();
}
checkForNullValues(newTags);
return addAll(newTags, newTags.length);
}
} else if (ts instanceof TagList) {
if (ts instanceof ArrayTagSet) {
return addAll((ArrayTagSet) ts);
}
return addAll((TagList) ts);
} else {
List<Tag> data = new ArrayList<>();
for (Tag t : ts) {
data.add(t);
}
return addAll(data);
}
}
/** Add a collection of tags to the set. */
ArrayTagSet addAll(Map<String, String> ts) {
if (ts.isEmpty()) {
return this;
} else if (ts instanceof ConcurrentMap) {
// Special case ConcurrentMaps to avoid propagating errors if there is a bug in the caller
// and the map is mutated while being added:
// https://github.com/Netflix/spectator/issues/733
List<Tag> data = new ArrayList<>(ts.size());
for (Map.Entry<String, String> entry : ts.entrySet()) {
data.add(new BasicTag(entry.getKey(), entry.getValue()));
}
return addAll(data);
} else {
String[] newTags = new String[2 * ts.size()];
int i = 0;
for (Map.Entry<String, String> entry : ts.entrySet()) {
newTags[i++] = entry.getKey();
newTags[i++] = entry.getValue();
}
checkForNullValues(newTags);
return addAll(newTags, newTags.length);
}
}
/** Add a collection of tags to the set. */
ArrayTagSet addAll(String[] ts) {
if (ts.length % 2 != 0) {
throw new IllegalArgumentException("array length must be even, (length=" + ts.length + ")");
}
checkForNullValues(ts);
String[] copy = Arrays.copyOf(ts, ts.length);
return addAll(copy, copy.length);
}
/** Add a collection of tags to the set. */
private ArrayTagSet addAll(String[] ts, int tsLength) {
if (tsLength == 0) {
return this;
} else if (length == 0) {
insertionSort(ts, tsLength);
int len = dedup(ts, 0, ts, 0, tsLength);
return new ArrayTagSet(ts, len);
} else {
String[] newTags = new String[length + tsLength];
insertionSort(ts, tsLength);
int newLength = merge(newTags, tags, length, ts, tsLength);
return new ArrayTagSet(newTags, newLength);
}
}
private ArrayTagSet addAll(ArrayTagSet ts) {
if (ts == this || ts.isEmpty()) {
return this;
}
if (isEmpty()) {
return ts;
}
String[] newTags = new String[length + ts.length];
int newLength = merge(newTags, tags, length, ts.tags, ts.length);
return new ArrayTagSet(newTags, newLength);
}
private ArrayTagSet addAll(TagList ts) {
int size = ts.size();
if (size == 0) {
return this;
}
String[] newTags = new String[size * 2];
int j = 0;
for (int i = 0; i < size; i++) {
newTags[j++] = Objects.requireNonNull(ts.getKey(i), "tag keys cannot be null");
newTags[j++] = Objects.requireNonNull(ts.getValue(i), "tag values cannot be null");
}
return addAll(newTags, newTags.length);
}
/** Add a collection of tags to the set. */
ArrayTagSet addAll(Tag[] ts) {
return addAll(ts, ts.length);
}
/** Add a collection of tags to the set. */
ArrayTagSet addAll(Tag[] ts, int tsLength) {
if (tsLength == 0) {
return this;
} else {
String[] newTags = toStringArray(ts, tsLength);
checkForNullValues(newTags);
return addAll(newTags, newTags.length);
}
}
private String[] toStringArray(Tag[] ts, int length) {
String[] strs = new String[length * 2];
for (int i = 0; i < length; ++i) {
strs[2 * i] = ts[i].key();
strs[2 * i + 1] = ts[i].value();
}
return strs;
}
private void checkForNullValues(String[] ts) {
for (String s : ts) {
if (s == null) {
throw new NullPointerException("tag keys and values cannot be null");
}
}
}
/**
* Sort a string array that consists of tag key/value pairs by key. The array will be
* sorted in-place. Tag lists are supposed to be fairly small, typically less than 20
* tags. With the small size a simple insertion sort works well.
*/
private static void insertionSort(String[] ts, int length) {
if (length == 4) {
// Two key/value pairs, swap if needed
if (ts[0].compareTo(ts[2]) > 0) {
// Swap key
String tmp = ts[0];
ts[0] = ts[2];
ts[2] = tmp;
// Swap value
tmp = ts[1];
ts[1] = ts[3];
ts[3] = tmp;
}
} else if (length > 4) {
// One entry is already sorted. Two entries handled above, for larger arrays
// use insertion sort.
for (int i = 2; i < length; i += 2) {
String k = ts[i];
String v = ts[i + 1];
int j = i - 2;
for (; j >= 0 && ts[j].compareTo(k) > 0; j -= 2) {
ts[j + 2] = ts[j];
ts[j + 3] = ts[j + 1];
}
ts[j + 2] = k;
ts[j + 3] = v;
}
}
}
/**
* Merge and dedup any entries in {@code ts} that have the same key. The last entry
* with a given key will get selected.
*/
private static int merge(String[] dst, String[] srcA, int lengthA, String[] srcB, int lengthB) {
int i = 0;
int ai = 0;
int bi = 0;
while (ai < lengthA && bi < lengthB) {
final String ak = srcA[ai];
final String av = srcA[ai + 1];
String bk = srcB[bi];
String bv = srcB[bi + 1];
int cmp = ak.compareTo(bk);
if (cmp < 0) {
dst[i++] = ak;
dst[i++] = av;
ai += 2;
} else if (cmp > 0) {
dst[i++] = bk;
dst[i++] = bv;
bi += 2;
} else {
// Newer tags should override, use source B if there are duplicate keys.
// If source B has duplicates, then use the last value for the given key.
int j = bi + 2;
for (; j < lengthB && ak.equals(srcB[j]); j += 2) {
bk = srcB[j];
bv = srcB[j + 1];
}
dst[i++] = bk;
dst[i++] = bv;
bi = j;
ai += 2; // Ignore
}
}
if (ai < lengthA) {
System.arraycopy(srcA, ai, dst, i, lengthA - ai);
i += lengthA - ai;
} else if (bi < lengthB) {
i = dedup(srcB, bi, dst, i, lengthB - bi);
}
return i;
}
/**
* Dedup any entries in {@code ts} that have the same key. The last entry with a given
* key will get selected. Input data must already be sorted by the tag key. Returns the
* length of the overall deduped array.
*/
private static int dedup(String[] src, int ss, String[] dst, int ds, int len) {
if (len == 0) {
return ds;
} else {
String k = src[ss];
dst[ds] = k;
dst[ds + 1] = src[ss + 1];
int j = ds;
final int e = ss + len;
for (int i = ss + 2; i < e; i += 2) {
if (k.equals(src[i])) {
dst[j] = src[i];
dst[j + 1] = src[i + 1];
} else {
j += 2; // Not deduping, skip over previous entry
k = src[i];
dst[j] = k;
dst[j + 1] = src[i + 1];
}
}
return j + 2;
}
}
/**
* Dedup any entries in {@code data} that have the same key. The last entry with a given
* key will get selected. Input data must already be sorted by the tag key. Returns the
* length of the overall deduped array.
*/
private static int dedupInPlace(String[] data, int len) {
if (len == 0) {
return 0;
} else {
String k = data[0];
int j = 0;
for (int i = 2; i < len; i += 2) {
if (k.equals(data[i])) {
data[j] = data[i];
data[j + 1] = data[i + 1];
} else {
j += 2; // Not deduping, skip over previous entry
k = data[i];
if (i > j) {
data[j] = k;
data[j + 1] = data[i + 1];
}
}
}
return j + 2;
}
}
/** Return the key at the specified position. */
@Override public String getKey(int i) {
return tags[i * 2];
}
/** Return the value at the specified position. */
@Override public String getValue(int i) {
return tags[i * 2 + 1];
}
/** Return the current size of this tag set. */
@Override public int size() {
return length / 2;
}
@Override public ArrayTagSet filter(BiPredicate<String, String> predicate) {
final int n = size();
String[] result = new String[2 * n];
int pos = 0;
for (int i = 0; i < n; ++i) {
final String k = getKey(i);
final String v = getValue(i);
if (predicate.test(k, v)) {
result[pos++] = k;
result[pos++] = v;
}
}
return new ArrayTagSet(result, pos);
}
@Override public ArrayTagSet filterByKey(Predicate<String> predicate) {
return filter((k, v) -> predicate.test(k));
}
@Override public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ArrayTagSet other = (ArrayTagSet) o;
if (length != other.length) return false;
for (int i = 0; i < length; ++i) {
if (!tags[i].equals(other.tags[i])) return false;
}
return true;
}
@Override public int hashCode() {
if (cachedHashCode == 0) {
int result = length;
for (int i = 0; i < length; ++i) {
result = 31 * result + tags[i].hashCode();
}
cachedHashCode = result;
}
return cachedHashCode;
}
@Override public String toString() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < length; i += 2) {
builder.append(':').append(tags[i]).append('=').append(tags[i + 1]);
}
return builder.toString();
}
@Override
public Spliterator<Tag> spliterator() {
return Spliterators.spliterator(iterator(), size(),
(Spliterator.ORDERED | Spliterator.SORTED | Spliterator.NONNULL
| Spliterator.DISTINCT | Spliterator.IMMUTABLE));
}
}