22
22
import java .time .temporal .ChronoUnit ;
23
23
import java .util .Optional ;
24
24
import java .util .UUID ;
25
+ import java .util .function .Consumer ;
26
+ import java .util .logging .Logger ;
25
27
26
28
import com .couchbase .client .core .annotation .Stability ;
27
29
import com .couchbase .client .core .transaction .CoreTransactionAttemptContext ;
33
35
import com .couchbase .client .core .transaction .support .AttemptState ;
34
36
import com .couchbase .client .java .codec .JsonSerializer ;
35
37
import reactor .core .publisher .Mono ;
38
+ import reactor .util .annotation .Nullable ;
36
39
37
40
/**
38
41
* To access the ReactiveTransactionAttemptContext held by TransactionAttemptContext
@@ -66,13 +69,7 @@ public static ReactiveTransactionAttemptContext reactive(TransactionAttemptConte
66
69
} catch (Throwable err ) {
67
70
throw new RuntimeException (err );
68
71
}
69
- try {
70
- Field field = TransactionAttemptContext .class .getDeclaredField ("internal" );
71
- field .setAccessible (true );
72
- return new ReactiveTransactionAttemptContext ((CoreTransactionAttemptContext ) field .get (atr ), serializer );
73
- } catch (Throwable err ) {
74
- throw new RuntimeException (err );
75
- }
72
+ return new ReactiveTransactionAttemptContext (getCore (atr ), serializer );
76
73
}
77
74
78
75
public static TransactionAttemptContext blocking (ReactiveTransactionAttemptContext atr ) {
@@ -84,35 +81,22 @@ public static TransactionAttemptContext blocking(ReactiveTransactionAttemptConte
84
81
} catch (Throwable err ) {
85
82
throw new RuntimeException (err );
86
83
}
87
- try {
88
- Field field = ReactiveTransactionAttemptContext .class .getDeclaredField ("internal" );
89
- field .setAccessible (true );
90
- return new TransactionAttemptContext ((CoreTransactionAttemptContext ) field .get (atr ), serializer );
91
- } catch (Throwable err ) {
92
- throw new RuntimeException (err );
93
- }
84
+ return new TransactionAttemptContext (getCore (atr ), serializer );
94
85
}
95
86
96
87
public static CoreTransactionLogger getLogger (ReactiveTransactionAttemptContext attemptContextReactive ) {
97
- // todo gp needed?
98
- return null ;
99
- // return attemptContextReactive;
88
+ return attemptContextReactive .logger ();
89
+ }
90
+
91
+ public static CoreTransactionLogger getLogger (TransactionAttemptContext attemptContextReactive ) {
92
+ return attemptContextReactive .logger ();
100
93
}
101
94
102
95
// todo gp needed?
103
96
@ Stability .Internal
104
97
public static CoreTransactionAttemptContext newCoreTranactionAttemptContext (ReactiveTransactions transactions ) {
105
- // PerTransactionConfig perConfig = PerTransactionConfigBuilder.create().build();
106
- // MergedTransactionConfig merged = new MergedTransactionConfig(transactions.config(), Optional.of(perConfig));
107
- //
108
- // TransactionContext overall = new TransactionContext(
109
- // transactions.cleanup().clusterData().cluster().environment().requestTracer(),
110
- // transactions.cleanup().clusterData().cluster().environment().eventBus(),
111
- // UUID.randomUUID().toString(), now(), Duration.ZERO, merged);
112
98
113
99
String txnId = UUID .randomUUID ().toString ();
114
- // overall.LOGGER.info(configDebug(transactions.config(), perConfig));
115
-
116
100
CoreTransactionsReactive coreTransactionsReactive ;
117
101
try {
118
102
Field field = ReactiveTransactions .class .getDeclaredField ("internal" );
@@ -122,27 +106,19 @@ public static CoreTransactionAttemptContext newCoreTranactionAttemptContext(Reac
122
106
throw new RuntimeException (err );
123
107
}
124
108
125
- CoreTransactionOptions perConfig = new CoreTransactionOptions (Optional .empty (),
126
- Optional .empty (),
127
- Optional .empty (),
128
- Optional .of (Duration .ofMinutes (10 )),
129
- Optional .empty (),
130
- Optional .empty ());
109
+ CoreTransactionOptions perConfig = new CoreTransactionOptions (Optional .empty (), Optional .empty (), Optional .empty (),
110
+ Optional .of (Duration .ofMinutes (10 )), Optional .empty (), Optional .empty ());
131
111
132
112
CoreMergedTransactionConfig merged = new CoreMergedTransactionConfig (coreTransactionsReactive .config (),
133
113
Optional .ofNullable (perConfig ));
134
114
CoreTransactionContext overall = new CoreTransactionContext (
135
115
coreTransactionsReactive .core ().context ().environment ().requestTracer (),
136
116
coreTransactionsReactive .core ().context ().environment ().eventBus (), UUID .randomUUID ().toString (), merged ,
137
117
coreTransactionsReactive .core ().transactionsCleanup ());
138
- // overall.LOGGER.info(configDebug(config, perConfig, cleanup.clusterData().cluster().core()));
139
118
140
119
CoreTransactionAttemptContext coreTransactionAttemptContext = coreTransactionsReactive .createAttemptContext (overall ,
141
120
merged , txnId );
142
121
return coreTransactionAttemptContext ;
143
- // ReactiveTransactionAttemptContext reactiveTransactionAttemptContext = new ReactiveTransactionAttemptContext(
144
- // coreTransactionAttemptContext, null);
145
- // return reactiveTransactionAttemptContext;
146
122
}
147
123
148
124
private static Duration now () {
@@ -168,15 +144,13 @@ public static CoreTransactionAttemptContext getCore(ReactiveTransactionAttemptCo
168
144
}
169
145
170
146
public static CoreTransactionAttemptContext getCore (TransactionAttemptContext atr ) {
171
- CoreTransactionAttemptContext coreTransactionsReactive ;
172
147
try {
173
148
Field field = TransactionAttemptContext .class .getDeclaredField ("internal" );
174
149
field .setAccessible (true );
175
- coreTransactionsReactive = (CoreTransactionAttemptContext ) field .get (atr );
150
+ return (CoreTransactionAttemptContext ) field .get (atr );
176
151
} catch (Throwable err ) {
177
152
throw new RuntimeException (err );
178
153
}
179
- return coreTransactionsReactive ;
180
154
}
181
155
182
156
public static Mono <Void > implicitCommit (ReactiveTransactionAttemptContext atr , boolean b ) {
@@ -186,17 +160,17 @@ public static Mono<Void> implicitCommit(ReactiveTransactionAttemptContext atr, b
186
160
// CoreTransactionAttemptContext.class.getDeclaredMethod("implicitCommit", Boolean.class);
187
161
Method [] methods = CoreTransactionAttemptContext .class .getDeclaredMethods ();
188
162
Method method = null ;
189
- for (Method m : methods ){
190
- if ( m .getName ().equals ("implicitCommit" )){
163
+ for (Method m : methods ) {
164
+ if ( m .getName ().equals ("implicitCommit" )) {
191
165
method = m ;
192
166
break ;
193
167
}
194
168
}
195
- if (method == null ){
169
+ if (method == null ) {
196
170
throw new RuntimeException ("did not find implicitCommit method" );
197
171
}
198
172
method .setAccessible (true );
199
- return (Mono <Void >)method .invoke (coreTransactionsReactive , b );
173
+ return (Mono <Void >) method .invoke (coreTransactionsReactive , b );
200
174
} catch (Throwable err ) {
201
175
throw new RuntimeException (err );
202
176
}
@@ -214,10 +188,30 @@ public static AttemptState getState(ReactiveTransactionAttemptContext atr) {
214
188
}
215
189
}
216
190
217
- public static ReactiveTransactionAttemptContext createReactiveTransactionAttemptContext (CoreTransactionAttemptContext core , JsonSerializer jsonSerializer ) {
191
+ public static ReactiveTransactionAttemptContext createReactiveTransactionAttemptContext (
192
+ CoreTransactionAttemptContext core , JsonSerializer jsonSerializer ) {
218
193
return new ReactiveTransactionAttemptContext (core , jsonSerializer );
219
194
}
220
195
196
+ public static CoreTransactionsReactive getCoreTransactionsReactive (ReactiveTransactions transactions ) {
197
+ try {
198
+ Field field = ReactiveTransactions .class .getDeclaredField ("internal" );
199
+ field .setAccessible (true );
200
+ return (CoreTransactionsReactive ) field .get (transactions );
201
+ } catch (Throwable err ) {
202
+ throw new RuntimeException (err );
203
+ }
204
+ }
205
+
206
+ public static TransactionAttemptContext newTransactionAttemptContext (CoreTransactionAttemptContext ctx ,
207
+ JsonSerializer jsonSerializer ) {
208
+ return new TransactionAttemptContext (ctx , jsonSerializer );
209
+ }
210
+
211
+ public static TransactionResult run (Transactions transactions , Consumer <TransactionAttemptContext > transactionLogic , CoreTransactionOptions coreTransactionOptions ) {
212
+ return reactive (transactions ).runBlocking (transactionLogic , coreTransactionOptions );
213
+ }
214
+
221
215
// todo gp if needed let's expose in the SDK
222
216
// static private String configDebug(TransactionConfig config, PerTransactionConfig perConfig) {
223
217
// StringBuilder sb = new StringBuilder();
0 commit comments