Skip to content

Commit 5a86e94

Browse files
authored
update!: moved txn and run methods to DataHub (#52)
1 parent 670e5d8 commit 5a86e94

3 files changed

Lines changed: 81 additions & 85 deletions

File tree

src/main/java/com/github/sttk/sabi/DataHub.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ public record FailToCastDataConn(String name, String castToType) {}
9494
*/
9595
public record RuntimeExceptionOccurred() {}
9696

97+
/**
98+
* Represents an error reason that occurred when failing to cast the {@code DataHub} instance
99+
* itself to the expected data access interface type for a {@link Logic}.
100+
*
101+
* @param castFromType The actual type of the {@code DataHub} instance that failed to cast.
102+
*/
103+
public record FailToCastDataHub(String castFromType) {}
104+
97105
private final DataHubInner inner = new DataHubInner();
98106

99107
/** Constructs a new {@code DataHub} instance. */
@@ -161,4 +169,68 @@ void rollback() {
161169
void end() {
162170
inner.end();
163171
}
172+
173+
/**
174+
* Executes the provided application {@link Logic} without transactional boundaries. The {@code
175+
* DataHub} instance is treated as the data access object {@code D} to the {@link Logic}'s {@code
176+
* run} method.
177+
*
178+
* @param <D> The type of the data access object, which typically is {@code DataHub} or an
179+
* interface implemented by {@code DataHub} that {@link Logic} expects.
180+
* @param logic The application logic to execute.
181+
* @throws Err if an {@link Err} or {@link RuntimeException} occurs during logic execution or if
182+
* the {@code DataHub} cannot be cast to the expected data access type.
183+
*/
184+
public <D> void run(Logic<D> logic) throws Err {
185+
D data;
186+
try {
187+
@SuppressWarnings("unchecked")
188+
D d = (D) this;
189+
data = d;
190+
} catch (Exception e) {
191+
throw new Err(new FailToCastDataHub(this.getClass().getName()));
192+
}
193+
try {
194+
this.begin();
195+
logic.run(data);
196+
} catch (Err | RuntimeException e) {
197+
throw e;
198+
} finally {
199+
this.end();
200+
}
201+
}
202+
203+
/**
204+
* Executes the provided application {@link Logic} within a transactional context. The {@code
205+
* DataHub} instance is treated as the data access object {@code D} to the {@link Logic}'s {@code
206+
* run} method. If the logic completes successfully, a commit operation is attempted. If any
207+
* {@link Err}, {@link RuntimeException}, or {@link Error} occurs, a rollback operation is
208+
* performed.
209+
*
210+
* @param <D> The type of the data access object, which typically is {@code DataHub} or an
211+
* interface implemented by {@code DataHub} that {@link Logic} expects.
212+
* @param logic The application logic to execute transactionally.
213+
* @throws Err if an {@link Err}, {@link RuntimeException}, or {@link Error} occurs during logic
214+
* execution, pre-commit, or commit. The original exception is re-thrown after rollback.
215+
*/
216+
public <D> void txn(Logic<D> logic) throws Err {
217+
D data;
218+
try {
219+
@SuppressWarnings("unchecked")
220+
D d = (D) this;
221+
data = d;
222+
} catch (Exception e) {
223+
throw new Err(new FailToCastDataHub(this.getClass().getName()));
224+
}
225+
try {
226+
this.begin();
227+
logic.run(data);
228+
this.commit();
229+
} catch (Err | RuntimeException | Error e) {
230+
this.rollback();
231+
throw e;
232+
} finally {
233+
this.end();
234+
}
235+
}
164236
}

src/main/java/com/github/sttk/sabi/Sabi.java

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@
3434
* }</code></pre>
3535
*/
3636
public final class Sabi {
37-
/**
38-
* Represents an error reason that occurred when failing to cast the {@code DataHub} instance
39-
* itself to the expected data access interface type for a {@link Logic}.
40-
*
41-
* @param castFromType The actual type of the {@code DataHub} instance that failed to cast.
42-
*/
43-
public record FailToCastDataHub(String castFromType) {}
44-
4537
private Sabi() {}
4638

4739
/**
@@ -71,72 +63,4 @@ public static void uses(String name, DataSrc ds) {
7163
public static AutoCloseable setup() throws Err {
7264
return DataHubInner.setupGlobals();
7365
}
74-
75-
/**
76-
* Executes the provided application {@link Logic} without transactional boundaries. The {@code
77-
* DataHub} instance in the parameters is passed as the data access object {@code D} to the {@link
78-
* Logic}'s {@code run} method.
79-
*
80-
* @param <D> The type of the data access object, which typically is {@code DataHub} or an
81-
* interface implemented by {@code DataHub} that {@link Logic} expects.
82-
* @param logic The application logic to execute.
83-
* @param hub An instance of a DataHub subclass that inherits the data interface for logic
84-
* arguments.
85-
* @throws Err if an {@link Err} or {@link RuntimeException} occurs during logic execution or if
86-
* the {@code DataHub} cannot be cast to the expected data access type.
87-
*/
88-
public static <D> void run(Logic<D> logic, DataHub hub) throws Err {
89-
D data;
90-
try {
91-
@SuppressWarnings("unchecked")
92-
D d = (D) hub;
93-
data = d;
94-
} catch (Exception e) {
95-
throw new Err(new FailToCastDataHub(hub.getClass().getName()));
96-
}
97-
try {
98-
hub.begin();
99-
logic.run(data);
100-
} catch (Err | RuntimeException e) {
101-
throw e;
102-
} finally {
103-
hub.end();
104-
}
105-
}
106-
107-
/**
108-
* Executes the provided application {@link Logic} within a transactional context. The {@code
109-
* DataHub} instance in the parameter is passed as the data access object {@code D} to the {@link
110-
* Logic}'s {@code run} method. If the logic completes successfully, a commit operation is
111-
* attempted. If any {@link Err}, {@link RuntimeException}, or {@link Error} occurs, a rollback
112-
* operation is performed.
113-
*
114-
* @param <D> The type of the data access object, which typically is {@code DataHub} or an
115-
* interface implemented by {@code DataHub} that {@link Logic} expects.
116-
* @param logic The application logic to execute transactionally.
117-
* @param hub An instance of a DataHub subclass that inherits the data interface for logic
118-
* arguments.
119-
* @throws Err if an {@link Err}, {@link RuntimeException}, or {@link Error} occurs during logic
120-
* execution, pre-commit, or commit. The original exception is re-thrown after rollback.
121-
*/
122-
public static <D> void txn(Logic<D> logic, DataHub hub) throws Err {
123-
D data;
124-
try {
125-
@SuppressWarnings("unchecked")
126-
D d = (D) hub;
127-
data = d;
128-
} catch (Exception e) {
129-
throw new Err(new FailToCastDataHub(hub.getClass().getName()));
130-
}
131-
try {
132-
hub.begin();
133-
logic.run(data);
134-
hub.commit();
135-
} catch (Err | RuntimeException | Error e) {
136-
hub.rollback();
137-
throw e;
138-
} finally {
139-
hub.end();
140-
}
141-
}
14266
}

src/test/java/com/github/sttk/sabi/internal/DataAccTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ void test() {
317317
try (var ac = Sabi.setup()) {
318318
suppressWarnings_unused(ac);
319319
try (var data = new SampleDataHub()) {
320-
Sabi.run(new SampleLogic(), data);
320+
data.run(new SampleLogic());
321321
} catch (Exception e) {
322322
fail(e);
323323
}
@@ -362,7 +362,7 @@ void test() {
362362
data.uses("foo", new FooDataSrc(1, "hello", logger, false));
363363
data.uses("bar", new BarDataSrc(2, logger, false));
364364

365-
Sabi.run(new SampleLogic(), data);
365+
data.run(new SampleLogic());
366366
} catch (Exception e) {
367367
fail(e);
368368
}
@@ -394,7 +394,7 @@ void test_not_run_logic_if_fail_to_setup_local_data_src() {
394394
data.uses("foo", new FooDataSrc(1, "hello", logger, true));
395395
data.uses("bar", new BarDataSrc(2, logger, false));
396396

397-
Sabi.run(new SampleLogic(), data);
397+
data.run(new SampleLogic());
398398
} catch (Err e) {
399399
switch (e.getReason()) {
400400
case DataHub.FailToSetupLocalDataSrcs r -> {
@@ -437,7 +437,7 @@ void test() {
437437
try (var data = new SampleDataHub()) {
438438
data.uses("foo", new FooDataSrc(2, "Hello", logger, false));
439439

440-
Sabi.run(new SampleLogic(), data);
440+
data.run(new SampleLogic());
441441
} catch (Exception e) {
442442
fail(e);
443443
}
@@ -482,7 +482,7 @@ void test() {
482482
try (var ac = Sabi.setup()) {
483483
suppressWarnings_unused(ac);
484484
try (var data = new SampleDataHub()) {
485-
Sabi.txn(new SampleLogic(), data);
485+
data.txn(new SampleLogic());
486486
} catch (Exception e) {
487487
fail(e);
488488
}
@@ -533,7 +533,7 @@ void test() {
533533
data.uses("foo", new FooDataSrc(1, "Hello", logger, false));
534534
data.uses("bar", new BarDataSrc(2, logger, false));
535535

536-
Sabi.txn(new SampleLogic(), data);
536+
data.txn(new SampleLogic());
537537
} catch (Exception e) {
538538
fail(e);
539539
}
@@ -571,7 +571,7 @@ void test_not_run_logic_if_fail_to_setup_local_data_src() {
571571
data.uses("foo", new FooDataSrc(1, "Hello", logger, true));
572572
data.uses("bar", new BarDataSrc(2, logger, false));
573573

574-
Sabi.txn(new SampleLogic(), data);
574+
data.txn(new SampleLogic());
575575
} catch (Err e) {
576576
switch (e.getReason()) {
577577
case DataHub.FailToSetupLocalDataSrcs r -> {
@@ -600,7 +600,7 @@ void test_not_run_logic_in_txn_and_rollback() {
600600
data.uses("foo", new FooDataSrc(1, "Hello", logger, false));
601601
data.uses("bar", new BarDataSrc(2, logger, false));
602602

603-
Sabi.txn(new FailingLogic(), data);
603+
data.txn(new FailingLogic());
604604
} catch (Err e) {
605605
assertThat(e.getReason()).isEqualTo("ZZZ");
606606
} catch (Exception e) {
@@ -643,7 +643,7 @@ void test() {
643643
try (var data = new SampleDataHub()) {
644644
data.uses("foo", new FooDataSrc(2, "Hello", logger, false));
645645

646-
Sabi.txn(new SampleLogic(), data);
646+
data.txn(new SampleLogic());
647647
} catch (Exception e) {
648648
fail(e);
649649
}

0 commit comments

Comments
 (0)