|
30 | 30 | import io.quarkus.redis.datasource.transactions.OptimisticLockingTransactionResult;
|
31 | 31 | import io.quarkus.redis.datasource.transactions.ReactiveTransactionalRedisDataSource;
|
32 | 32 | import io.quarkus.redis.datasource.transactions.TransactionResult;
|
33 |
| -import io.quarkus.redis.datasource.transactions.TransactionalRedisDataSource; |
34 | 33 | import io.quarkus.redis.datasource.value.ReactiveValueCommands;
|
35 | 34 | import io.smallrye.common.annotation.Experimental;
|
36 | 35 | import io.smallrye.mutiny.Uni;
|
|
50 | 49 | public interface ReactiveRedisDataSource {
|
51 | 50 |
|
52 | 51 | /**
|
53 |
| - * Retrieves a {@link ReactiveRedisDataSource} using a single connection with the Redis Server. |
54 |
| - * The connection is acquired from the pool and released when the {@code Uni} returned by {@code function} produces |
55 |
| - * a {@code null} item or a failure. |
| 52 | + * Obtains a {@link ReactiveRedisDataSource} that uses a single connection to the Redis server |
| 53 | + * and passes it to the given {@code function}. The connection is acquired from the pool |
| 54 | + * and released when the {@code Uni} returned by {@code function} produces an item or a failure. |
56 | 55 | *
|
57 |
| - * @param function the function receiving the single-connection data source and producing {@code null} when the |
| 56 | + * @param function the function receiving the connection and producing {@code null} when the |
58 | 57 | * connection can be released.
|
59 | 58 | */
|
60 | 59 | Uni<Void> withConnection(Function<ReactiveRedisDataSource, Uni<Void>> function);
|
61 | 60 |
|
62 | 61 | /**
|
63 |
| - * Retrieves a {@link RedisDataSource} enqueuing commands in a Redis Transaction ({@code MULTI}). |
64 |
| - * Note that transaction acquires a single connection, and all the commands are enqueued in this connection. |
65 |
| - * The commands are only executed when the passed block emits the {@code null} item. |
| 62 | + * Obtains a {@link ReactiveRedisDataSource} that enqueues commands in a Redis Transaction ({@code MULTI}) |
| 63 | + * and passes it to the given {@code tx} block. Note that the transaction acquires a single connection |
| 64 | + * and all the commands are enqueued on this connection. The commands are only executed when the {@code Uni} |
| 65 | + * returned by {@code tx} produces an item. |
66 | 66 | * <p>
|
67 |
| - * The results of the commands are retrieved using the produced {@link TransactionResult}. |
| 67 | + * The results of the commands can be obtained from the returned {@link TransactionResult}. |
| 68 | + * Errors are represented as {@code Throwable}s in the {@code TransactionResult}. |
68 | 69 | * <p>
|
69 |
| - * The user can discard a transaction using the {@link TransactionalRedisDataSource#discard()} method. |
70 |
| - * In this case, the produced {@link TransactionResult} will be empty. |
| 70 | + * The user can discard a transaction using the {@link ReactiveTransactionalRedisDataSource#discard()} method. |
| 71 | + * In this case, the produced {@link TransactionResult} is empty. |
| 72 | + * If the {@code tx} block completes with a failure, the transaction is discarded automatically and |
| 73 | + * the resulting {@code Uni} completes with the same failure. |
71 | 74 | *
|
72 |
| - * @param tx the consumer receiving the transactional redis data source. The enqueued commands are only executed |
73 |
| - * at the end of the block. |
| 75 | + * @param tx the consumer receiving the transactional Redis data source. The enqueued commands are only executed |
| 76 | + * when this block completes with an item. |
74 | 77 | */
|
75 | 78 | Uni<TransactionResult> withTransaction(Function<ReactiveTransactionalRedisDataSource, Uni<Void>> tx);
|
76 | 79 |
|
77 | 80 | /**
|
78 |
| - * Retrieves a {@link RedisDataSource} enqueuing commands in a Redis Transaction ({@code MULTI}). |
79 |
| - * Note that transaction acquires a single connection, and all the commands are enqueued in this connection. |
80 |
| - * The commands are only executed when the passed block emits the {@code null} item. |
| 81 | + * Obtains a {@link ReactiveRedisDataSource} that enqueues commands in a Redis Transaction ({@code WATCH} & {@code MULTI}), |
| 82 | + * starts watching the given {@code watchedKeys}, and passes it to the given {@code tx} block. |
| 83 | + * Note that the transaction acquires a single connection and all the commands are enqueued on this connection. |
| 84 | + * The commands are only executed when the {@code Uni} returned by {@code tx} produces an item. |
81 | 85 | * <p>
|
82 |
| - * The results of the commands are retrieved using the produced {@link TransactionResult}. |
| 86 | + * The results of the commands can be obtained from the returned {@link TransactionResult}. |
| 87 | + * Errors are represented as {@code Throwable}s in the {@code TransactionResult}. |
83 | 88 | * <p>
|
84 |
| - * The user can discard a transaction using the {@link TransactionalRedisDataSource#discard()} method. |
85 |
| - * In this case, the produced {@link TransactionResult} will be empty. |
| 89 | + * The user can discard a transaction using the {@link ReactiveTransactionalRedisDataSource#discard()} method. |
| 90 | + * In this case, the produced {@link TransactionResult} is empty. |
| 91 | + * If the {@code tx} block completes with a failure, the transaction is discarded automatically and |
| 92 | + * the resulting {@code Uni} completes with the same failure. |
86 | 93 | *
|
87 |
| - * @param tx the consumer receiving the transactional redis data source. The enqueued commands are only executed |
88 |
| - * at the end of the block. |
| 94 | + * @param tx the consumer receiving the transactional Redis data source. The enqueued commands are only executed |
| 95 | + * when this block completes with an item. |
89 | 96 | * @param watchedKeys the keys to watch during the execution of the transaction. If one of these key is modified before
|
90 | 97 | * the completion of the transaction, the transaction is discarded.
|
91 | 98 | */
|
92 | 99 | Uni<TransactionResult> withTransaction(Function<ReactiveTransactionalRedisDataSource, Uni<Void>> tx, String... watchedKeys);
|
93 | 100 |
|
94 | 101 | /**
|
95 |
| - * Retrieves a {@link RedisDataSource} enqueuing commands in a Redis Transaction ({@code MULTI}). |
96 |
| - * Note that transaction acquires a single connection, and all the commands are enqueued in this connection. |
97 |
| - * The commands are only executed when the passed block emits the {@code null} item. |
| 102 | + * Obtains a {@link ReactiveRedisDataSource} that enqueues commands in a Redis Transaction ({@code WATCH} & {@code MULTI}), |
| 103 | + * starts watching the given {@code watchedKeys}, passes it to the given {@code preTx} block and if that |
| 104 | + * doesn't fail, passes it again to the given {@code tx} block. Note that the transaction acquires a single |
| 105 | + * connection and all the commands are enqueued on this connection. The commands are only executed when |
| 106 | + * the {@code Uni} returned by {@code tx} produces an item. |
98 | 107 | * <p>
|
99 |
| - * This variant also allows executing code before the transaction gets started but after the key being watched: |
| 108 | + * This variant also allows executing code before the transaction gets started but after the keys are watched: |
100 | 109 | *
|
101 | 110 | * <pre>
|
102 |
| - * WATCH key |
103 |
| - * // preTxBlock |
| 111 | + * WATCH key |
| 112 | + * // preTx |
104 | 113 | * element = ZRANGE k 0 0
|
105 |
| - * // TxBlock |
106 |
| - * MULTI |
107 |
| - * ZREM k element |
108 |
| - * EXEC |
| 114 | + * MULTI |
| 115 | + * // tx |
| 116 | + * ZREM k element |
| 117 | + * EXEC |
109 | 118 | * </pre>
|
110 | 119 | * <p>
|
111 | 120 | * The {@code preTxBlock} returns a {@link Uni Uni<I>}. The produced value is received by the {@code tx} block,
|
112 | 121 | * which can use that value to execute the appropriate operation in the transaction. The produced value can also be
|
113 |
| - * retrieved from the produced {@link OptimisticLockingTransactionResult}. Commands issued in the {@code preTxBlock } |
114 |
| - * must used the passed (single-connection) {@link ReactiveRedisDataSource} instance. |
| 122 | + * obtained from the returned {@link OptimisticLockingTransactionResult}. Commands issued in the {@code preTx} |
| 123 | + * block must use the passed (single-connection) {@link ReactiveRedisDataSource} instance. |
115 | 124 | * <p>
|
116 |
| - * If the {@code preTxBlock} throws an exception or emits a failure, the transaction is not executed, and the returned |
117 |
| - * {@link OptimisticLockingTransactionResult} is empty. |
| 125 | + * If the {@code preTx} block completes with a failure, all watched keys are {@code UNWATCH}ed and the returned |
| 126 | + * {@code Uni} completes with the same failure. |
118 | 127 | * <p>
|
119 | 128 | * This construct allows implementing operation relying on optimistic locking.
|
120 |
| - * The results of the commands are retrieved using the produced {@link OptimisticLockingTransactionResult}. |
| 129 | + * The results of the commands can be obtained from the returned {@link OptimisticLockingTransactionResult}. |
| 130 | + * Errors are represented as {@code Throwable}s in the {@code OptimisticLockingTransactionResult}. |
121 | 131 | * <p>
|
122 |
| - * The user can discard a transaction using the {@link TransactionalRedisDataSource#discard()} method. |
123 |
| - * In this case, the produced {@link OptimisticLockingTransactionResult} will be empty. |
124 |
| - * |
125 |
| - * @param tx the consumer receiving the transactional redis data source. The enqueued commands are only executed |
126 |
| - * at the end of the block. |
| 132 | + * The user can discard a transaction using the {@link ReactiveTransactionalRedisDataSource#discard()} method. |
| 133 | + * In this case, the produced {@link OptimisticLockingTransactionResult} is empty. |
| 134 | + * If the {@code tx} block completes with a failure, the transaction is discarded automatically and |
| 135 | + * the resulting {@code Uni} completes with the same failure. |
| 136 | + * |
| 137 | + * @param preTx the consumer receiving the Redis data source before the transaction is started but after |
| 138 | + * the {@code watchedKeys} are watched. |
| 139 | + * @param tx the consumer receiving the transactional Redis data source after the transaction is started. |
| 140 | + * The enqueued commands are only executed when this block completes with an item. |
127 | 141 | * @param watchedKeys the keys to watch during the execution of the transaction. If one of these key is modified before
|
128 | 142 | * the completion of the transaction, the transaction is discarded.
|
129 | 143 | */
|
130 |
| - <I> Uni<OptimisticLockingTransactionResult<I>> withTransaction(Function<ReactiveRedisDataSource, Uni<I>> preTxBlock, |
| 144 | + <I> Uni<OptimisticLockingTransactionResult<I>> withTransaction(Function<ReactiveRedisDataSource, Uni<I>> preTx, |
131 | 145 | BiFunction<I, ReactiveTransactionalRedisDataSource, Uni<Void>> tx,
|
132 | 146 | String... watchedKeys);
|
133 | 147 |
|
|
0 commit comments