Skip to content

Commit 4ccee4d

Browse files
committed
Avoid dealing with the command base handler when we can, since we want to remove it.
1 parent d7f63b2 commit 4ccee4d

File tree

7 files changed

+25
-38
lines changed

7 files changed

+25
-38
lines changed

vertx-mssql-client/src/main/java/io/vertx/mssqlclient/impl/codec/TdsMessageCodec.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ private void fail(Throwable cause) {
6565
}
6666

6767
private void fail(MSSQLCommandCodec<?, ?> codec, Throwable cause) {
68-
Completable<?> handler = codec.cmd.handler;
69-
if (handler != null) {
70-
handler.complete(null, cause);
71-
}
68+
codec.cmd.fail(cause);
7269
}
7370

7471
@Override

vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/MySQLCodec.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@ private void clearInflightCommands(Throwable cause) {
8181
private void fail(CommandCodec<?, ?> codec, Throwable cause) {
8282
if (failure == null) {
8383
failure = cause;
84-
Completable<?> handler = codec.cmd.handler;
85-
if (handler != null) {
86-
handler.complete(null, cause);
87-
}
84+
codec.cmd.fail(cause);
8885
}
8986
}
9087

vertx-pg-client/src/main/java/io/vertx/pgclient/impl/codec/PgCodec.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@ private void fail(Throwable cause) {
8383
}
8484

8585
private void fail(PgCommandCodec<?, ?> codec, Throwable cause) {
86-
Completable<?> handler = codec.cmd.handler;
87-
if (handler != null) {
88-
handler.complete(null, cause);
89-
}
86+
codec.cmd.fail(cause);
9087
}
9188

9289
@Override

vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/SocketConnectionBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ protected <R> void doSchedule(CommandBase<R> cmd, Completable<R> handler) {
213213
CompositeCommand composite = (CompositeCommand) cmd;
214214
List<CommandBase<?>> commands = composite.commands();
215215
pending.addAll(commands);
216-
composite.handler.succeed();
216+
composite.succeed();
217217
} else {
218218
pending.add(cmd);
219219
}

vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/TransactionImpl.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,15 @@ public TransactionImpl(ContextInternal context, Handler<Void> endHandler, Connec
4646
public Future<Transaction> begin() {
4747
PromiseInternal<Transaction> promise = context.promise();
4848
TxCommand<Transaction> begin = new TxCommand<>(TxCommand.Kind.BEGIN, this);
49-
begin.handler = wrap(begin, promise);
50-
schedule(begin);
49+
scheduleInternal(begin, wrap(begin, promise));
5150
return promise.future();
5251
}
5352

5453
public void fail() {
5554
failed = true;
5655
}
5756

58-
private <R> void execute(CommandBase<R> cmd) {
59-
Completable<R> handler = cmd.handler;
57+
private <R> void execute(CommandBase<R> cmd, Completable<R> handler) {
6058
connection.schedule(cmd, handler);
6159
}
6260

@@ -71,35 +69,42 @@ private <T> Completable<T> wrap(CommandBase<?> cmd, Completable<T> handler) {
7169
}
7270

7371
public <R> void schedule(CommandBase<R> cmd, Completable<R> handler) {
74-
cmd.handler = wrap(cmd, handler);
75-
if (!schedule(cmd)) {
72+
if (!scheduleInternal(cmd, wrap(cmd, handler))) {
7673
handler.fail("Transaction already completed");
7774
}
7875
}
7976

80-
public <R> boolean schedule(CommandBase<R> b) {
77+
public <R> boolean scheduleInternal(CommandBase<R> b, Completable<R> handler) {
8178
synchronized (this) {
8279
if (ended) {
8380
return false;
8481
}
8582
pendingQueries++;
8683
}
87-
execute(b);
84+
execute(b, handler);
8885
return true;
8986
}
9087

9188
private void checkEnd() {
92-
TxCommand<?> cmd;
89+
TxCommand<Void> cmd;
90+
Completable<Void> handler;
9391
synchronized (this) {
9492
if (pendingQueries > 0 || !ended || endCommand != null) {
9593
return;
9694
}
9795
TxCommand.Kind kind = failed ? TxCommand.Kind.ROLLBACK : TxCommand.Kind.COMMIT;
98-
endCommand = txCommand(kind);
99-
cmd = endCommand;
96+
cmd = new TxCommand<>(kind, null);
97+
handler = (res, err) -> {
98+
if (err == null) {
99+
completion.complete(kind);
100+
} else {
101+
completion.fail(err);
102+
}
103+
};
104+
endCommand = cmd;
100105
}
101106
endHandler.handle(null);
102-
execute(cmd);
107+
execute(cmd, handler);
103108
}
104109

105110
private Future<TxCommand.Kind> end(boolean rollback) {
@@ -144,18 +149,6 @@ public void rollback(Handler<AsyncResult<Void>> handler) {
144149
}
145150
}
146151

147-
private TxCommand<Void> txCommand(TxCommand.Kind kind) {
148-
TxCommand<Void> cmd = new TxCommand<>(kind, null);
149-
cmd.handler = (res, err) -> {
150-
if (err == null) {
151-
completion.complete(kind);
152-
} else {
153-
completion.fail(err);
154-
}
155-
};
156-
return cmd;
157-
}
158-
159152
@Override
160153
public Future<Void> completion() {
161154
return completion.future().flatMap(k -> {

vertx-sql-client/src/main/java/io/vertx/sqlclient/internal/command/CommandBase.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
public abstract class CommandBase<R> {
3030

31-
// Todo : hide this
3231
public Completable<R> handler;
3332

3433
public final void fail(Throwable err) {

vertx-sql-client/src/main/java/io/vertx/sqlclient/internal/command/CompositeCommand.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ public <R> void add(CommandBase<R> cmd, Completable<R> handler) {
2020
public List<CommandBase<?>> commands() {
2121
return commands;
2222
}
23+
24+
public final void succeed() {
25+
handler.succeed();
26+
}
2327
}

0 commit comments

Comments
 (0)