|
| 1 | +/* |
| 2 | + * Copyright 2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.r2dbc.postgresql; |
| 18 | + |
| 19 | +import io.r2dbc.postgresql.client.Binding; |
| 20 | +import io.r2dbc.postgresql.client.Client; |
| 21 | +import io.r2dbc.postgresql.client.ExtendedQueryMessageFlow; |
| 22 | +import io.r2dbc.postgresql.util.Assert; |
| 23 | +import reactor.core.publisher.Mono; |
| 24 | +import reactor.util.annotation.Nullable; |
| 25 | +import reactor.util.function.Tuple2; |
| 26 | +import reactor.util.function.Tuples; |
| 27 | + |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Collection; |
| 30 | +import java.util.Iterator; |
| 31 | +import java.util.LinkedHashMap; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.concurrent.atomic.AtomicInteger; |
| 35 | + |
| 36 | +/** |
| 37 | + * Bounded (size-limited) {@link StatementCache}. |
| 38 | + */ |
| 39 | +final class BoundedStatementCache implements StatementCache { |
| 40 | + |
| 41 | + private final Map<Tuple2<String, List<Integer>>, String> cache = new LinkedHashMap<>(16, 0.75f, true); |
| 42 | + |
| 43 | + private final Client client; |
| 44 | + |
| 45 | + private final AtomicInteger counter = new AtomicInteger(); |
| 46 | + |
| 47 | + private final int limit; |
| 48 | + |
| 49 | + public BoundedStatementCache(Client client, int limit) { |
| 50 | + this.client = Assert.requireNonNull(client, "client must not be null"); |
| 51 | + if (limit <= 0) { |
| 52 | + throw new IllegalArgumentException("statement cache limit must be greater than zero"); |
| 53 | + } |
| 54 | + this.limit = limit; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public Mono<String> getName(Binding binding, String sql) { |
| 59 | + Assert.requireNonNull(binding, "binding must not be null"); |
| 60 | + Assert.requireNonNull(sql, "sql must not be null"); |
| 61 | + Tuple2<String, List<Integer>> key = Tuples.of(sql, binding.getParameterTypes()); |
| 62 | + String name = get(key); |
| 63 | + if (name != null) { |
| 64 | + return Mono.just(name); |
| 65 | + } |
| 66 | + |
| 67 | + Mono<Void> closeLastStatement = Mono.defer(() -> { |
| 68 | + if (getCacheSize() < this.limit) { |
| 69 | + return Mono.empty(); |
| 70 | + } |
| 71 | + String lastAccessedStatementName = getAndRemoveEldest(); |
| 72 | + ExceptionFactory factory = ExceptionFactory.withSql(lastAccessedStatementName); |
| 73 | + return ExtendedQueryMessageFlow |
| 74 | + .closeStatement(this.client, lastAccessedStatementName) |
| 75 | + .handle(factory::handleErrorResponse) |
| 76 | + .then(); |
| 77 | + }); |
| 78 | + |
| 79 | + return closeLastStatement.then(this.parse(sql, binding.getParameterTypes())) |
| 80 | + .doOnNext(preparedName -> put(key, preparedName)); |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + /** |
| 85 | + * Synchronized cache access: Return all statement names. |
| 86 | + * |
| 87 | + * @return statement names. |
| 88 | + */ |
| 89 | + Collection<String> getCachedStatementNames() { |
| 90 | + synchronized (this.cache) { |
| 91 | + List<String> names = new ArrayList<>(this.cache.size()); |
| 92 | + names.addAll(cache.values()); |
| 93 | + return names; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Synchronized cache access: Retrieve statement name by key. |
| 99 | + * |
| 100 | + * @param key |
| 101 | + * @return statement name by key |
| 102 | + */ |
| 103 | + @Nullable |
| 104 | + private String get(Tuple2<String, List<Integer>> key) { |
| 105 | + synchronized (this.cache) { |
| 106 | + return this.cache.get(key); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Synchronized cache access: Least recently used entry. |
| 112 | + * |
| 113 | + * @return least recently used entry |
| 114 | + */ |
| 115 | + private String getAndRemoveEldest() { |
| 116 | + synchronized (this.cache) { |
| 117 | + Iterator<Map.Entry<Tuple2<String, List<Integer>>, String>> iterator = this.cache.entrySet().iterator(); |
| 118 | + String entry = iterator.next().getValue(); |
| 119 | + iterator.remove(); |
| 120 | + return entry; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Synchronized cache access: Store prepared statement. |
| 126 | + */ |
| 127 | + private void put(Tuple2<String, List<Integer>> key, String preparedName) { |
| 128 | + synchronized (this.cache) { |
| 129 | + this.cache.put(key, preparedName); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Synchronized cache access: Return the cache size. |
| 135 | + * |
| 136 | + * @return the cache size. |
| 137 | + */ |
| 138 | + private int getCacheSize() { |
| 139 | + synchronized (this.cache) { |
| 140 | + return this.cache.size(); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public String toString() { |
| 146 | + return "LimitedStatementCache{" + |
| 147 | + "cache=" + this.cache + |
| 148 | + ", counter=" + this.counter + |
| 149 | + ", client=" + this.client + |
| 150 | + ", limit=" + this.limit + |
| 151 | + '}'; |
| 152 | + } |
| 153 | + |
| 154 | + private Mono<String> parse(String sql, List<Integer> types) { |
| 155 | + String name = String.format("S_%d", this.counter.getAndIncrement()); |
| 156 | + |
| 157 | + ExceptionFactory factory = ExceptionFactory.withSql(name); |
| 158 | + return ExtendedQueryMessageFlow |
| 159 | + .parse(this.client, name, sql, types) |
| 160 | + .handle(factory::handleErrorResponse) |
| 161 | + .then(Mono.just(name)) |
| 162 | + .cache(); |
| 163 | + } |
| 164 | +} |
0 commit comments