Skip to content

Commit eaa9173

Browse files
committed
Use Collections.unmodifiableMap
1 parent 63eb118 commit eaa9173

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

core/src/main/java/com/datastax/oss/driver/api/core/tracker/RequestIdGenerator.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919

2020
import com.datastax.oss.driver.api.core.cql.Statement;
2121
import com.datastax.oss.driver.api.core.session.Request;
22-
import com.datastax.oss.protocol.internal.util.collection.NullAllowingImmutableMap;
2322
import edu.umd.cs.findbugs.annotations.NonNull;
2423
import java.nio.ByteBuffer;
2524
import java.nio.charset.StandardCharsets;
25+
import java.util.Collections;
26+
import java.util.HashMap;
2627
import java.util.Map;
2728

2829
/**
@@ -68,26 +69,14 @@ default String getCustomPayloadKey() {
6869
default Statement<?> getDecoratedStatement(
6970
@NonNull Statement<?> statement, @NonNull String requestId) {
7071

71-
// local copy
72-
Map<String, ByteBuffer> existing = statement.getCustomPayload();
72+
Map<String, ByteBuffer> existing = new HashMap<>(statement.getCustomPayload());
7373
String key = getCustomPayloadKey();
7474

75-
NullAllowingImmutableMap.Builder<String, ByteBuffer> builder =
76-
NullAllowingImmutableMap.builder();
75+
// Add or overwrite
76+
existing.put(key, ByteBuffer.wrap(requestId.getBytes(StandardCharsets.UTF_8)));
7777

78-
// Copy all existing entries except the one we’re overwriting
79-
if (existing != null && !existing.isEmpty()) {
80-
for (Map.Entry<String, ByteBuffer> e : existing.entrySet()) {
81-
if (!e.getKey().equals(key)) {
82-
builder.put(e.getKey(), e.getValue());
83-
}
84-
}
85-
}
78+
Map<String, ByteBuffer> unmodifiableMap = Collections.unmodifiableMap(existing);
8679

87-
// Put (or overwrite) the target key
88-
builder.put(key, ByteBuffer.wrap(requestId.getBytes(StandardCharsets.UTF_8)));
89-
90-
Map<String, ByteBuffer> newPayload = builder.build();
91-
return statement.setCustomPayload(newPayload);
80+
return statement.setCustomPayload(unmodifiableMap);
9281
}
9382
}

integration-tests/src/test/java/com/datastax/oss/driver/core/tracker/RequestIdGeneratorIT.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
*/
1818
package com.datastax.oss.driver.core.tracker;
1919

20+
import static com.datastax.oss.driver.Assertions.assertThatStage;
2021
import static org.assertj.core.api.Assertions.assertThat;
2122

2223
import com.datastax.oss.driver.api.core.CqlSession;
2324
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
2425
import com.datastax.oss.driver.api.core.config.DriverConfigLoader;
2526
import com.datastax.oss.driver.api.core.cql.ResultSet;
27+
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
2628
import com.datastax.oss.driver.api.core.cql.Statement;
2729
import com.datastax.oss.driver.api.core.session.Request;
2830
import com.datastax.oss.driver.api.core.tracker.RequestIdGenerator;
@@ -122,4 +124,23 @@ public void should_not_write_id_to_custom_payload_when_key_is_not_set() {
122124
assertThat(rs.getExecutionInfo().getRequest().getCustomPayload().get("trace_key")).isNull();
123125
}
124126
}
127+
128+
@Test
129+
public void should_succeed_with_null_value_in_custom_payload() {
130+
DriverConfigLoader loader =
131+
SessionUtils.configLoaderBuilder()
132+
.withString(
133+
DefaultDriverOption.REQUEST_ID_GENERATOR_CLASS, "W3CContextRequestIdGenerator")
134+
.build();
135+
try (CqlSession session = SessionUtils.newSession(ccmRule, loader)) {
136+
String query = "SELECT * FROM system.local";
137+
Map<String, ByteBuffer> customPayload =
138+
new NullAllowingImmutableMap.Builder<String, ByteBuffer>(1).put("my_key", null).build();
139+
SimpleStatement statement =
140+
SimpleStatement.newInstance(query).setCustomPayload(customPayload);
141+
assertThatStage(session.executeAsync(statement)).isSuccess();
142+
ResultSet rs = session.execute(query);
143+
assertThat(rs.getExecutionInfo().getRequest().getCustomPayload().get("trace_key")).isNull();
144+
}
145+
}
125146
}

0 commit comments

Comments
 (0)