Skip to content

Commit a395a5d

Browse files
committed
Changing TagMap.Builder -> TagMap.Ledger
Renamed TagMap.Builder -> TagMap.Ledger to make the concept a bit more clear Also renamed Builder.put methods to Ledger.set to be more consistent with TagMap Update users of TagMap.Builder as needed
1 parent f6a5794 commit a395a5d

File tree

6 files changed

+137
-139
lines changed

6 files changed

+137
-139
lines changed

dd-trace-core/src/main/java/datadog/trace/core/CoreTracer.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,7 +1380,7 @@ public static class CoreSpanBuilder implements AgentTracer.SpanBuilder {
13801380
private final CoreTracer tracer;
13811381

13821382
// Builder attributes
1383-
private TagMap.Builder tagBuilder;
1383+
private TagMap.Ledger tagLedger;
13841384
private long timestampMicro;
13851385
private AgentSpanContext parent;
13861386
private String serviceName;
@@ -1513,21 +1513,21 @@ public CoreSpanBuilder withTag(final String tag, final Object value) {
15131513
if (tag == null) {
15141514
return this;
15151515
}
1516-
TagMap.Builder tagBuilder = this.tagBuilder;
1517-
if (tagBuilder == null) {
1516+
TagMap.Ledger tagLedger = this.tagLedger;
1517+
if (tagLedger == null) {
15181518
// Insertion order is important, so using TagBuilder which builds up a set
15191519
// of Entry modifications in order
1520-
this.tagBuilder = tagBuilder = TagMap.builder();
1520+
this.tagLedger = tagLedger = TagMap.ledger();
15211521
}
15221522
if (value == null) {
15231523
// DQH - Use of smartRemove is important to avoid clobbering entries added by another map
15241524
// smartRemove only records the removal if a prior matching put has already occurred in the
15251525
// builder
15261526
// smartRemove is O(n) but since removes are rare, this is preferable to a more complicated
15271527
// implementation in setAll
1528-
tagBuilder.smartRemove(tag);
1528+
tagLedger.smartRemove(tag);
15291529
} else {
1530-
tagBuilder.put(tag, value);
1530+
tagLedger.set(tag, value);
15311531
}
15321532
return this;
15331533
}
@@ -1815,7 +1815,7 @@ private DDSpanContext buildSpanContext() {
18151815
// the builder. This is the order that the tags were added previously, but maybe the `tags`
18161816
// set in the builder should come last, so that they override other tags.
18171817
context.setAllTags(mergedTracerTags, mergedTracerTagsNeedsIntercept);
1818-
context.setAllTags(tagBuilder);
1818+
context.setAllTags(tagLedger);
18191819
context.setAllTags(coreTags, coreTagsNeedsIntercept);
18201820
context.setAllTags(rootSpanTags, rootSpanTagsNeedsIntercept);
18211821
context.setAllTags(contextualTags);

dd-trace-core/src/main/java/datadog/trace/core/DDSpanContext.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -779,14 +779,14 @@ void setAllTags(final TagMap map, boolean needsIntercept) {
779779
}
780780
}
781781

782-
void setAllTags(final TagMap.Builder builder) {
783-
if (builder == null) {
782+
void setAllTags(final TagMap.Ledger ledger) {
783+
if (ledger == null) {
784784
return;
785785
}
786786

787787
TagInterceptor tagInterceptor = traceCollector.getTracer().getTagInterceptor();
788788
synchronized (unsafeTags) {
789-
for (final TagMap.EntryChange entryChange : builder) {
789+
for (final TagMap.EntryChange entryChange : ledger) {
790790
if (entryChange.isRemoval()) {
791791
unsafeTags.removeEntry(entryChange.tag());
792792
} else {

dd-trace-core/src/main/java/datadog/trace/core/propagation/B3HttpCodec.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public B3BaseContextInterpreter(Config config) {
185185

186186
protected void setSpanId(final String sId) {
187187
spanId = DDSpanId.fromHex(sId);
188-
tagBuilder().put(B3_SPAN_ID, sId);
188+
tagLedger().set(B3_SPAN_ID, sId);
189189
}
190190

191191
protected boolean setTraceId(final String tId) {
@@ -198,7 +198,7 @@ protected boolean setTraceId(final String tId) {
198198
B3TraceId b3TraceId = B3TraceId.fromHex(tId);
199199
traceId = b3TraceId.toLong() == 0 ? DDTraceId.ZERO : b3TraceId;
200200
}
201-
tagBuilder().put(B3_TRACE_ID, tId);
201+
tagLedger().set(B3_TRACE_ID, tId);
202202
return true;
203203
}
204204
}

dd-trace-core/src/main/java/datadog/trace/core/propagation/ContextInterpreter.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public abstract class ContextInterpreter implements AgentPropagation.KeyClassifi
4545
protected DDTraceId traceId;
4646
protected long spanId;
4747
protected int samplingPriority;
48-
protected TagMap.Builder tagBuilder;
48+
protected TagMap.Ledger tagLedger;
4949
protected Map<String, String> baggage;
5050

5151
protected CharSequence lastParentId;
@@ -78,11 +78,11 @@ protected ContextInterpreter(Config config) {
7878
this.requestHeaderTagsCommaAllowed = config.isRequestHeaderTagsCommaAllowed();
7979
}
8080

81-
final TagMap.Builder tagBuilder() {
82-
if (tagBuilder == null) {
83-
tagBuilder = TagMap.builder();
81+
final TagMap.Ledger tagLedger() {
82+
if (tagLedger == null) {
83+
tagLedger = TagMap.ledger();
8484
}
85-
return tagBuilder;
85+
return tagLedger;
8686
}
8787

8888
/**
@@ -197,8 +197,8 @@ protected final boolean handleTags(String key, String value) {
197197
final String lowerCaseKey = toLowerCase(key);
198198
final String mappedKey = headerTags.get(lowerCaseKey);
199199
if (null != mappedKey) {
200-
tagBuilder()
201-
.put(
200+
tagLedger()
201+
.set(
202202
mappedKey,
203203
HttpCodec.decode(
204204
requestHeaderTagsCommaAllowed ? value : HttpCodec.firstHeaderValue(value)));
@@ -230,7 +230,7 @@ public ContextInterpreter reset(TraceConfig traceConfig) {
230230
samplingPriority = PrioritySampling.UNSET;
231231
origin = null;
232232
endToEndStartTime = 0;
233-
if (tagBuilder != null) tagBuilder.reset();
233+
if (tagLedger != null) tagLedger.reset();
234234
baggage = Collections.emptyMap();
235235
valid = true;
236236
fullContext = true;
@@ -258,19 +258,19 @@ protected TagContext build() {
258258
origin,
259259
endToEndStartTime,
260260
baggage,
261-
tagBuilder == null ? null : tagBuilder.build(),
261+
tagLedger == null ? null : tagLedger.build(),
262262
httpHeaders,
263263
propagationTags,
264264
traceConfig,
265265
style());
266266
} else if (origin != null
267-
|| (tagBuilder != null && !tagBuilder.isDefinitelyEmpty())
267+
|| (tagLedger != null && !tagLedger.isDefinitelyEmpty())
268268
|| httpHeaders != null
269269
|| !baggage.isEmpty()
270270
|| samplingPriority != PrioritySampling.UNSET) {
271271
return new TagContext(
272272
origin,
273-
tagBuilder == null ? null : tagBuilder.build(),
273+
tagLedger == null ? null : tagLedger.build(),
274274
httpHeaders,
275275
baggage,
276276
samplingPriorityOrDefault(traceId, samplingPriority),

internal-api/src/main/java/datadog/trace/api/TagMap.java

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* <ul>
2222
* <li>fast copy from one map to another
23-
* <li>compatibility with Builder idioms
23+
* <li>compatibility with builder idioms
2424
* <li>building small maps as fast as possible
2525
* <li>storing primitives without boxing
2626
* <li>minimal memory footprint
@@ -71,14 +71,14 @@ private static final TagMap createEmpty() {
7171
return new TagMap(new Object[1], 0);
7272
}
7373

74-
/** Creates a new TagMap.Builder */
75-
public static final Builder builder() {
76-
return new Builder();
74+
/** Creates a new TagMap.Ledger */
75+
public static final Ledger ledger() {
76+
return new Ledger();
7777
}
7878

79-
/** Creates a new TagMap.Builder which handles <code>size</code> modifications before expansion */
80-
public static final Builder builder(int size) {
81-
return new Builder(size);
79+
/** Creates a new TagMap.Ledger which handles <code>size</code> modifications before expansion */
80+
public static final Ledger ledger(int size) {
81+
return new Ledger(size);
8282
}
8383

8484
/** Creates a new mutable TagMap that contains the contents of <code>map</code> */
@@ -337,8 +337,8 @@ public final void putAll(Iterable<? extends Entry> entries) {
337337
}
338338
}
339339

340-
public final void putAll(TagMap.Builder builder) {
341-
putAll(builder.entryChanges, builder.nextPos);
340+
public final void putAll(TagMap.Ledger ledger) {
341+
putAll(ledger.entryChanges, ledger.nextPos);
342342
}
343343

344344
private final void putAll(EntryChange[] entryChanges, int size) {
@@ -830,19 +830,19 @@ public final String toString() {
830830
final String toPrettyString() {
831831
boolean first = true;
832832

833-
StringBuilder builder = new StringBuilder(128);
834-
builder.append('{');
833+
StringBuilder ledger = new StringBuilder(128);
834+
ledger.append('{');
835835
for (Entry entry : this) {
836836
if (first) {
837837
first = false;
838838
} else {
839-
builder.append(", ");
839+
ledger.append(", ");
840840
}
841841

842-
builder.append(entry.tag).append('=').append(entry.stringValue());
842+
ledger.append(entry.tag).append('=').append(entry.stringValue());
843843
}
844-
builder.append('}');
845-
return builder.toString();
844+
ledger.append('}');
845+
return ledger.toString();
846846
}
847847

848848
/**
@@ -852,25 +852,25 @@ final String toPrettyString() {
852852
final String toInternalString() {
853853
Object[] thisBuckets = this.buckets;
854854

855-
StringBuilder builder = new StringBuilder(128);
855+
StringBuilder ledger = new StringBuilder(128);
856856
for (int i = 0; i < thisBuckets.length; ++i) {
857-
builder.append('[').append(i).append("] = ");
857+
ledger.append('[').append(i).append("] = ");
858858

859859
Object thisBucket = thisBuckets[i];
860860
if (thisBucket == null) {
861-
builder.append("null");
861+
ledger.append("null");
862862
} else if (thisBucket instanceof Entry) {
863-
builder.append('{').append(thisBucket).append('}');
863+
ledger.append('{').append(thisBucket).append('}');
864864
} else if (thisBucket instanceof BucketGroup) {
865865
for (BucketGroup curGroup = (BucketGroup) thisBucket;
866866
curGroup != null;
867867
curGroup = curGroup.prev) {
868-
builder.append(curGroup).append(" -> ");
868+
ledger.append(curGroup).append(" -> ");
869869
}
870870
}
871-
builder.append('\n');
871+
ledger.append('\n');
872872
}
873-
return builder.toString();
873+
return ledger.toString();
874874
}
875875

876876
static final int _hash(String tag) {
@@ -1439,16 +1439,20 @@ private static final double prim2Double(long prim) {
14391439
}
14401440
}
14411441

1442-
public static final class Builder implements Iterable<EntryChange> {
1442+
/*
1443+
* An in-order ledger of changes to be made to a TagMap.
1444+
* Ledger can also serves as a builder for TagMap-s via build & buildImmutable.
1445+
*/
1446+
public static final class Ledger implements Iterable<EntryChange> {
14431447
private EntryChange[] entryChanges;
14441448
private int nextPos = 0;
14451449
private boolean containsRemovals = false;
14461450

1447-
private Builder() {
1451+
private Ledger() {
14481452
this(8);
14491453
}
14501454

1451-
private Builder(int size) {
1455+
private Ledger(int size) {
14521456
this.entryChanges = new EntryChange[size];
14531457
}
14541458

@@ -1457,7 +1461,7 @@ public final boolean isDefinitelyEmpty() {
14571461
}
14581462

14591463
/**
1460-
* Provides the estimated size of the map created by the builder Doesn't account for overwritten
1464+
* Provides the estimated size of the map created by the ledger Doesn't account for overwritten
14611465
* entries or entry removal
14621466
*
14631467
* @return
@@ -1470,54 +1474,48 @@ public final boolean containsRemovals() {
14701474
return this.containsRemovals;
14711475
}
14721476

1473-
public final Builder put(String tag, Object value) {
1477+
public final Ledger set(String tag, Object value) {
14741478
return this.recordEntry(Entry.newAnyEntry(tag, value));
14751479
}
14761480

1477-
public final Builder put(String tag, CharSequence value) {
1481+
public final Ledger set(String tag, CharSequence value) {
14781482
return this.recordEntry(Entry.newObjectEntry(tag, value));
14791483
}
14801484

1481-
public final Builder put(String tag, boolean value) {
1485+
public final Ledger set(String tag, boolean value) {
14821486
return this.recordEntry(Entry.newBooleanEntry(tag, value));
14831487
}
14841488

1485-
public final Builder put(String tag, int value) {
1489+
public final Ledger set(String tag, int value) {
14861490
return this.recordEntry(Entry.newIntEntry(tag, value));
14871491
}
14881492

1489-
public final Builder put(String tag, long value) {
1493+
public final Ledger set(String tag, long value) {
14901494
return this.recordEntry(Entry.newLongEntry(tag, value));
14911495
}
14921496

1493-
public final Builder put(String tag, float value) {
1497+
public final Ledger set(String tag, float value) {
14941498
return this.recordEntry(Entry.newFloatEntry(tag, value));
14951499
}
14961500

1497-
public final Builder put(String tag, double value) {
1501+
public final Ledger set(String tag, double value) {
14981502
return this.recordEntry(Entry.newDoubleEntry(tag, value));
14991503
}
15001504

1501-
public final Builder uncheckedPut(Entry entry) {
1505+
public final Ledger set(Entry entry) {
15021506
return this.recordEntry(entry);
15031507
}
15041508

1505-
public final Builder put(Entry entry) {
1506-
this.recordChange(entry);
1507-
this.containsRemovals |= entry.isRemoval();
1508-
return this;
1509-
}
1510-
1511-
public final Builder remove(String tag) {
1509+
public final Ledger remove(String tag) {
15121510
return this.recordRemoval(EntryChange.newRemoval(tag));
15131511
}
15141512

1515-
private final Builder recordEntry(Entry entry) {
1513+
private final Ledger recordEntry(Entry entry) {
15161514
this.recordChange(entry);
15171515
return this;
15181516
}
15191517

1520-
private final Builder recordRemoval(EntryRemoval entry) {
1518+
private final Ledger recordRemoval(EntryRemoval entry) {
15211519
this.recordChange(entry);
15221520
this.containsRemovals = true;
15231521

@@ -1532,7 +1530,7 @@ private final void recordChange(EntryChange entryChange) {
15321530
this.entryChanges[this.nextPos++] = entryChange;
15331531
}
15341532

1535-
public final Builder smartRemove(String tag) {
1533+
public final Ledger smartRemove(String tag) {
15361534
if (this.contains(tag)) {
15371535
this.remove(tag);
15381536
}
@@ -2153,15 +2151,15 @@ BucketGroup _cloneEntries() {
21532151

21542152
@Override
21552153
public String toString() {
2156-
StringBuilder builder = new StringBuilder(32);
2157-
builder.append('[');
2154+
StringBuilder ledger = new StringBuilder(32);
2155+
ledger.append('[');
21582156
for (int i = 0; i < BucketGroup.LEN; ++i) {
2159-
if (i != 0) builder.append(", ");
2157+
if (i != 0) ledger.append(", ");
21602158

2161-
builder.append(this._entryAt(i));
2159+
ledger.append(this._entryAt(i));
21622160
}
2163-
builder.append(']');
2164-
return builder.toString();
2161+
ledger.append(']');
2162+
return ledger.toString();
21652163
}
21662164
}
21672165

0 commit comments

Comments
 (0)