29
29
import io .opentelemetry .api .common .Attributes ;
30
30
import java .util .ArrayList ;
31
31
import java .util .Arrays ;
32
- import java .util .Objects ;
32
+ import java .util .HashMap ;
33
+ import java .util .Map ;
33
34
import java .util .concurrent .ExecutionException ;
34
35
import java .util .concurrent .Future ;
35
36
import java .util .concurrent .TimeUnit ;
@@ -51,6 +52,7 @@ class DatabaseClientImpl implements DatabaseClient {
51
52
@ VisibleForTesting final boolean useMultiplexedSessionForRW ;
52
53
private final int dbId ;
53
54
private final AtomicInteger nthRequest ;
55
+ private final Map <String , Integer > clientIdToOrdinalMap ;
54
56
55
57
final boolean useMultiplexedSessionBlindWrite ;
56
58
@@ -98,18 +100,19 @@ class DatabaseClientImpl implements DatabaseClient {
98
100
this .useMultiplexedSessionForRW = useMultiplexedSessionForRW ;
99
101
this .commonAttributes = commonAttributes ;
100
102
103
+ this .clientIdToOrdinalMap = new HashMap <String , Integer >();
101
104
this .dbId = this .dbIdFromClientId (this .clientId );
102
105
this .nthRequest = new AtomicInteger (0 );
103
106
}
104
107
105
108
@ VisibleForTesting
106
- int dbIdFromClientId (String clientId ) {
107
- int i = clientId . indexOf ( "-" );
108
- String strWithValue = clientId . substring ( i + 1 );
109
- if ( Objects . equals ( strWithValue , "" )) {
110
- strWithValue = "0" ;
109
+ synchronized int dbIdFromClientId (String clientId ) {
110
+ Integer id = this . clientIdToOrdinalMap . get ( clientId );
111
+ if ( id == null ) {
112
+ id = this . clientIdToOrdinalMap . size () + 1 ;
113
+ this . clientIdToOrdinalMap . put ( clientId , id ) ;
111
114
}
112
- return Integer . parseInt ( strWithValue ) ;
115
+ return id ;
113
116
}
114
117
115
118
@ VisibleForTesting
@@ -423,9 +426,13 @@ private UpdateOption[] withReqId(
423
426
if (reqId == null ) {
424
427
return options ;
425
428
}
426
- ArrayList <UpdateOption > allOptions = new ArrayList (Arrays .asList (options ));
427
- allOptions .add (new Options .RequestIdOption (reqId ));
428
- return allOptions .toArray (new UpdateOption [0 ]);
429
+ if (options == null || options .length == 0 ) {
430
+ return new UpdateOption [] {new Options .RequestIdOption (reqId )};
431
+ }
432
+ UpdateOption [] allOptions = new UpdateOption [options .length + 1 ];
433
+ System .arraycopy (options , 0 , allOptions , 0 , options .length );
434
+ allOptions [options .length ] = new Options .RequestIdOption (reqId );
435
+ return allOptions ;
429
436
}
430
437
431
438
private TransactionOption [] withReqId (
0 commit comments