|
| 1 | + |
| 2 | +package io.helidon.benchmark.nima.models; |
| 3 | + |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.concurrent.locks.ReentrantLock; |
| 7 | + |
| 8 | +import io.vertx.core.Vertx; |
| 9 | +import io.vertx.pgclient.PgConnectOptions; |
| 10 | +import io.vertx.pgclient.PgConnection; |
| 11 | +import io.vertx.sqlclient.PreparedQuery; |
| 12 | +import io.vertx.sqlclient.Row; |
| 13 | +import io.vertx.sqlclient.RowSet; |
| 14 | + |
| 15 | +class PgClientConnectionPool implements AutoCloseable { |
| 16 | + |
| 17 | + private final Vertx vertx; |
| 18 | + private final PgConnectOptions options; |
| 19 | + private final ReentrantLock lock = new ReentrantLock(); |
| 20 | + private final Map<String, PgClientConnection> connectionMap = new HashMap<>(); |
| 21 | + |
| 22 | + public PgClientConnectionPool(Vertx vertx, PgConnectOptions options) { |
| 23 | + this.vertx = vertx; |
| 24 | + this.options = options; |
| 25 | + } |
| 26 | + |
| 27 | + public PgClientConnection clientConnection() { |
| 28 | + String carrierThread = carrierThread(); |
| 29 | + PgClientConnection connection = connectionMap.get(carrierThread); |
| 30 | + if (connection == null) { |
| 31 | + try { |
| 32 | + lock.lock(); |
| 33 | + connection = connectionMap.get(carrierThread); |
| 34 | + if (connection == null) { |
| 35 | + connection = newConnection(); |
| 36 | + connectionMap.put(carrierThread, connection); |
| 37 | + } |
| 38 | + } finally { |
| 39 | + lock.unlock(); |
| 40 | + } |
| 41 | + } |
| 42 | + return connection; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void close() { |
| 47 | + try { |
| 48 | + for (PgClientConnection connection : connectionMap.values()) { |
| 49 | + connection.close(); |
| 50 | + } |
| 51 | + } catch (Exception e) { |
| 52 | + throw new RuntimeException(e); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private PgClientConnection newConnection() { |
| 57 | + try { |
| 58 | + PgConnection conn = PgConnection.connect(vertx, options) |
| 59 | + .toCompletionStage().toCompletableFuture().get(); |
| 60 | + PgClientConnection clientConn = new PgClientConnection(conn); |
| 61 | + clientConn.prepare(); |
| 62 | + return clientConn; |
| 63 | + } catch (Exception e) { |
| 64 | + throw new RuntimeException(e); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + static String carrierThread() { |
| 69 | + String threadName = Thread.currentThread().toString(); |
| 70 | + return threadName.substring(threadName.indexOf('@') + 1); |
| 71 | + } |
| 72 | + |
| 73 | + public static class PgClientConnection implements AutoCloseable { |
| 74 | + static final int UPDATE_QUERIES = 500; |
| 75 | + private static String SELECT_WORLD = "SELECT id, randomnumber from WORLD where id=$1"; |
| 76 | + private static String SELECT_FORTUNE = "SELECT * from FORTUNE"; |
| 77 | + |
| 78 | + private PreparedQuery<RowSet<Row>> worldQuery; |
| 79 | + private PreparedQuery<RowSet<Row>> fortuneQuery; |
| 80 | + private PreparedQuery<RowSet<Row>>[] updateQuery; |
| 81 | + |
| 82 | + private final PgConnection conn; |
| 83 | + |
| 84 | + PgClientConnection(PgConnection conn) { |
| 85 | + this.conn = conn; |
| 86 | + } |
| 87 | + |
| 88 | + public PgConnection pgConnection() { |
| 89 | + return conn; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void close() { |
| 94 | + conn.close(); |
| 95 | + } |
| 96 | + |
| 97 | + public PreparedQuery<RowSet<Row>> worldQuery() { |
| 98 | + return worldQuery; |
| 99 | + } |
| 100 | + |
| 101 | + public PreparedQuery<RowSet<Row>> fortuneQuery() { |
| 102 | + return fortuneQuery; |
| 103 | + } |
| 104 | + |
| 105 | + public PreparedQuery<RowSet<Row>> updateQuery(int queryCount) { |
| 106 | + return updateQuery[queryCount - 1]; |
| 107 | + } |
| 108 | + |
| 109 | + @SuppressWarnings("unchecked") |
| 110 | + void prepare() { |
| 111 | + try { |
| 112 | + worldQuery = conn.prepare(SELECT_WORLD) |
| 113 | + .toCompletionStage().toCompletableFuture().get().query(); |
| 114 | + fortuneQuery = conn.prepare(SELECT_FORTUNE) |
| 115 | + .toCompletionStage().toCompletableFuture().get().query(); |
| 116 | + updateQuery = (PreparedQuery<RowSet<Row>>[]) new PreparedQuery<?>[UPDATE_QUERIES]; |
| 117 | + for (int i = 0; i < UPDATE_QUERIES; i++) { |
| 118 | + updateQuery[i] = conn.prepare(singleUpdate(i + 1)) |
| 119 | + .toCompletionStage().toCompletableFuture().get().query(); |
| 120 | + } |
| 121 | + } catch (Exception e) { |
| 122 | + throw new RuntimeException(e); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private static String singleUpdate(int count) { |
| 127 | + StringBuilder sql = new StringBuilder(); |
| 128 | + sql.append("UPDATE WORLD SET RANDOMNUMBER = CASE ID"); |
| 129 | + for (int i = 0; i < count; i++) { |
| 130 | + int k = i * 2 + 1; |
| 131 | + sql.append(" WHEN $").append(k).append(" THEN $").append(k + 1); |
| 132 | + } |
| 133 | + sql.append(" ELSE RANDOMNUMBER"); |
| 134 | + sql.append(" END WHERE ID IN ($1"); |
| 135 | + for (int i = 1; i < count; i++) { |
| 136 | + int k = i * 2 + 1; |
| 137 | + sql.append(",$").append(k); |
| 138 | + } |
| 139 | + sql.append(")"); |
| 140 | + return sql.toString(); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments