22
22
import io .r2dbc .postgresql .util .Assert ;
23
23
import reactor .core .publisher .Mono ;
24
24
import reactor .util .annotation .Nullable ;
25
- import reactor .util .function .Tuple2 ;
26
- import reactor .util .function .Tuples ;
27
25
28
26
import java .util .ArrayList ;
27
+ import java .util .Arrays ;
29
28
import java .util .Collection ;
30
29
import java .util .Iterator ;
31
30
import java .util .LinkedHashMap ;
38
37
*/
39
38
final class BoundedStatementCache implements StatementCache {
40
39
41
- private final Map <Tuple2 < String , List < Integer >> , String > cache = new LinkedHashMap <>(16 , 0.75f , true );
40
+ private final Map <CacheKey , String > cache = new LinkedHashMap <>(16 , 0.75f , true );
42
41
43
42
private final Client client ;
44
43
@@ -58,7 +57,7 @@ public BoundedStatementCache(Client client, int limit) {
58
57
public Mono <String > getName (Binding binding , String sql ) {
59
58
Assert .requireNonNull (binding , "binding must not be null" );
60
59
Assert .requireNonNull (sql , "sql must not be null" );
61
- Tuple2 < String , List < Integer >> key = Tuples . of (sql , binding .getParameterTypes ());
60
+ CacheKey key = new CacheKey (sql , binding .getParameterTypes ());
62
61
String name = get (key );
63
62
if (name != null ) {
64
63
return Mono .just (name );
@@ -76,7 +75,7 @@ public Mono<String> getName(Binding binding, String sql) {
76
75
.then ();
77
76
});
78
77
79
- return closeLastStatement .then (this . parse (sql , binding .getParameterTypes ()))
78
+ return closeLastStatement .then (parse (sql , binding .getParameterTypes ()))
80
79
.doOnNext (preparedName -> put (key , preparedName ));
81
80
}
82
81
@@ -101,7 +100,7 @@ Collection<String> getCachedStatementNames() {
101
100
* @return statement name by key
102
101
*/
103
102
@ Nullable
104
- private String get (Tuple2 < String , List < Integer >> key ) {
103
+ private String get (CacheKey key ) {
105
104
synchronized (this .cache ) {
106
105
return this .cache .get (key );
107
106
}
@@ -114,7 +113,7 @@ private String get(Tuple2<String, List<Integer>> key) {
114
113
*/
115
114
private String getAndRemoveEldest () {
116
115
synchronized (this .cache ) {
117
- Iterator <Map .Entry <Tuple2 < String , List < Integer >> , String >> iterator = this .cache .entrySet ().iterator ();
116
+ Iterator <Map .Entry <CacheKey , String >> iterator = this .cache .entrySet ().iterator ();
118
117
String entry = iterator .next ().getValue ();
119
118
iterator .remove ();
120
119
return entry ;
@@ -124,7 +123,7 @@ private String getAndRemoveEldest() {
124
123
/**
125
124
* Synchronized cache access: Store prepared statement.
126
125
*/
127
- private void put (Tuple2 < String , List < Integer >> key , String preparedName ) {
126
+ private void put (CacheKey key , String preparedName ) {
128
127
synchronized (this .cache ) {
129
128
this .cache .put (key , preparedName );
130
129
}
@@ -151,8 +150,8 @@ public String toString() {
151
150
'}' ;
152
151
}
153
152
154
- private Mono <String > parse (String sql , List < Integer > types ) {
155
- String name = String . format ( "S_%d" , this .counter .getAndIncrement () );
153
+ private Mono <String > parse (String sql , int [] types ) {
154
+ String name = "S_" + this .counter .getAndIncrement ();
156
155
157
156
ExceptionFactory factory = ExceptionFactory .withSql (name );
158
157
return ExtendedQueryMessageFlow
@@ -161,4 +160,40 @@ private Mono<String> parse(String sql, List<Integer> types) {
161
160
.then (Mono .just (name ))
162
161
.cache ();
163
162
}
163
+
164
+ static class CacheKey {
165
+
166
+ String sql ;
167
+
168
+ int [] parameterTypes ;
169
+
170
+ public CacheKey (String sql , int [] parameterTypes ) {
171
+ this .sql = sql ;
172
+ this .parameterTypes = parameterTypes ;
173
+ }
174
+
175
+ @ Override
176
+ public boolean equals (Object o ) {
177
+ if (this == o ) {
178
+ return true ;
179
+ }
180
+ if (!(o instanceof CacheKey )) {
181
+ return false ;
182
+ }
183
+
184
+ CacheKey cacheKey = (CacheKey ) o ;
185
+
186
+ if (this .sql != null ? !this .sql .equals (cacheKey .sql ) : cacheKey .sql != null ) {
187
+ return false ;
188
+ }
189
+ return Arrays .equals (this .parameterTypes , cacheKey .parameterTypes );
190
+ }
191
+
192
+ @ Override
193
+ public int hashCode () {
194
+ int result = this .sql != null ? this .sql .hashCode () : 0 ;
195
+ result = 31 * result + Arrays .hashCode (this .parameterTypes );
196
+ return result ;
197
+ }
198
+ }
164
199
}
0 commit comments