2
2
3
3
import java .util .ArrayList ;
4
4
import java .util .List ;
5
- import java .util .concurrent .ExecutionException ;
6
5
import java .util .logging .Logger ;
7
6
8
7
import io .helidon .config .Config ;
9
8
import io .vertx .core .Future ;
10
9
import io .vertx .core .Vertx ;
11
10
import io .vertx .core .VertxOptions ;
12
11
import io .vertx .pgclient .PgConnectOptions ;
13
- import io .vertx .pgclient .PgPool ;
14
- import io .vertx .sqlclient .PoolOptions ;
15
12
import io .vertx .sqlclient .PreparedQuery ;
16
13
import io .vertx .sqlclient .Row ;
17
14
import io .vertx .sqlclient .RowSet ;
18
- import io .vertx .sqlclient .SqlClient ;
19
15
import io .vertx .sqlclient .Tuple ;
20
16
21
17
import static io .helidon .benchmark .nima .models .DbRepository .randomWorldNumber ;
22
18
23
19
public class PgClientRepository implements DbRepository {
24
20
private static final Logger LOGGER = Logger .getLogger (PgClientRepository .class .getName ());
25
- private static final int UPDATE_QUERIES = 500 ;
26
21
27
- private final SqlClient updatePool ;
28
-
29
- private final PreparedQuery <RowSet <Row >> getFortuneQuery ;
30
- private final PreparedQuery <RowSet <Row >> getWorldQuery ;
31
- private final PreparedQuery <RowSet <Row >>[] updateWorldSingleQuery ;
22
+ private final PgClientConnectionPool connectionPool ;
32
23
33
24
@ SuppressWarnings ("unchecked" )
34
25
public PgClientRepository (Config config ) {
@@ -41,27 +32,16 @@ public PgClientRepository(Config config) {
41
32
.setUser (config .get ("username" ).asString ().orElse ("benchmarkdbuser" ))
42
33
.setPassword (config .get ("password" ).asString ().orElse ("benchmarkdbpass" ))
43
34
.setPipeliningLimit (100000 );
44
-
45
- int sqlPoolSize = config .get ("sql-pool-size" ).asInt ().orElse (64 );
46
- PoolOptions clientOptions = new PoolOptions ().setMaxSize (sqlPoolSize );
47
- LOGGER .info ("sql-pool-size is " + sqlPoolSize );
48
-
49
- SqlClient queryPool = PgPool .client (vertx , connectOptions , clientOptions );
50
- updatePool = PgPool .client (vertx , connectOptions , clientOptions );
51
-
52
- getWorldQuery = queryPool .preparedQuery ("SELECT id, randomnumber FROM world WHERE id = $1" );
53
- getFortuneQuery = queryPool .preparedQuery ("SELECT id, message FROM fortune" );
54
-
55
- updateWorldSingleQuery = new PreparedQuery [UPDATE_QUERIES ];
56
- for (int i = 0 ; i < UPDATE_QUERIES ; i ++) {
57
- updateWorldSingleQuery [i ] = queryPool .preparedQuery (singleUpdate (i + 1 ));
58
- }
35
+ int sqlPoolSize = config .get ("sql-pool-size" ).asInt ().orElse (Runtime .getRuntime ().availableProcessors ());
36
+ connectionPool = new PgClientConnectionPool (vertx , sqlPoolSize , connectOptions );
37
+ connectionPool .connect ();
59
38
}
60
39
61
40
@ Override
62
41
public World getWorld (int id ) {
63
42
try {
64
- return getWorldQuery .execute (Tuple .of (id ))
43
+ PreparedQuery <RowSet <Row >> worldQuery = connectionPool .clientConnection ().worldQuery ();
44
+ return worldQuery .execute (Tuple .of (id ))
65
45
.map (rows -> {
66
46
Row r = rows .iterator ().next ();
67
47
return new World (r .getInteger (0 ), r .getInteger (1 ));
@@ -74,13 +54,14 @@ public World getWorld(int id) {
74
54
@ Override
75
55
public List <World > getWorlds (int count ) {
76
56
try {
57
+ PreparedQuery <RowSet <Row >> worldQuery = connectionPool .clientConnection ().worldQuery ();
77
58
List <Future <?>> futures = new ArrayList <>();
78
59
for (int i = 0 ; i < count ; i ++) {
79
- futures .add (getWorldQuery .execute (Tuple .of (randomWorldNumber ()))
80
- .map (rows -> {
81
- Row r = rows .iterator ().next ();
82
- return new World (r .getInteger (0 ), r .getInteger (1 ));
83
- }));
60
+ futures .add (worldQuery .execute (Tuple .of (randomWorldNumber ()))
61
+ .map (rows -> {
62
+ Row r = rows .iterator ().next ();
63
+ return new World (r .getInteger (0 ), r .getInteger (1 ));
64
+ }));
84
65
}
85
66
return Future .all (futures ).toCompletionStage ().toCompletableFuture ().get ().list ();
86
67
} catch (Exception e ) {
@@ -92,7 +73,18 @@ public List<World> getWorlds(int count) {
92
73
public List <World > updateWorlds (int count ) {
93
74
List <World > worlds = getWorlds (count );
94
75
try {
95
- return updateWorlds (worlds , count , updatePool );
76
+ PreparedQuery <RowSet <Row >> updateQuery = connectionPool .clientConnection ().updateQuery (count );
77
+ List <Integer > updateParams = new ArrayList <>(count * 2 );
78
+ for (World world : worlds ) {
79
+ updateParams .add (world .id );
80
+ world .randomNumber = randomWorldNumber ();
81
+ updateParams .add (world .randomNumber );
82
+ }
83
+ return updateQuery .execute (Tuple .wrap (updateParams ))
84
+ .toCompletionStage ()
85
+ .thenApply (rows -> worlds )
86
+ .toCompletableFuture ()
87
+ .get ();
96
88
} catch (Exception e ) {
97
89
throw new RuntimeException (e );
98
90
}
@@ -101,7 +93,8 @@ public List<World> updateWorlds(int count) {
101
93
@ Override
102
94
public List <Fortune > getFortunes () {
103
95
try {
104
- return getFortuneQuery .execute ()
96
+ PreparedQuery <RowSet <Row >> fortuneQuery = connectionPool .clientConnection ().fortuneQuery ();
97
+ return fortuneQuery .execute ()
105
98
.map (rows -> {
106
99
List <Fortune > fortunes = new ArrayList <>(rows .size () + 1 );
107
100
for (Row r : rows ) {
@@ -113,37 +106,4 @@ public List<Fortune> getFortunes() {
113
106
throw new RuntimeException (e );
114
107
}
115
108
}
116
-
117
- private List <World > updateWorlds (List <World > worlds , int count , SqlClient pool )
118
- throws ExecutionException , InterruptedException {
119
- int size = worlds .size ();
120
- List <Integer > updateParams = new ArrayList <>(size * 2 );
121
- for (World world : worlds ) {
122
- updateParams .add (world .id );
123
- world .randomNumber = randomWorldNumber ();
124
- updateParams .add (world .randomNumber );
125
- }
126
- return updateWorldSingleQuery [count - 1 ].execute (Tuple .wrap (updateParams ))
127
- .toCompletionStage ()
128
- .thenApply (rows -> worlds )
129
- .toCompletableFuture ()
130
- .get ();
131
- }
132
-
133
- private static String singleUpdate (int count ) {
134
- StringBuilder sql = new StringBuilder ();
135
- sql .append ("UPDATE WORLD SET RANDOMNUMBER = CASE ID" );
136
- for (int i = 0 ; i < count ; i ++) {
137
- int k = i * 2 + 1 ;
138
- sql .append (" WHEN $" ).append (k ).append (" THEN $" ).append (k + 1 );
139
- }
140
- sql .append (" ELSE RANDOMNUMBER" );
141
- sql .append (" END WHERE ID IN ($1" );
142
- for (int i = 1 ; i < count ; i ++) {
143
- int k = i * 2 + 1 ;
144
- sql .append (",$" ).append (k );
145
- }
146
- sql .append (")" );
147
- return sql .toString ();
148
- }
149
109
}
0 commit comments