Skip to content

Commit b2497e6

Browse files
authored
update!: overhauled to align with the sabi-rust (#56)
1 parent 7e6159f commit b2497e6

35 files changed

Lines changed: 5192 additions & 4086 deletions

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

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,43 @@
44
*/
55
package com.github.sttk.sabi;
66

7+
import com.github.sttk.sabi.internal.AsyncGroupImpl;
8+
79
/**
8-
* An interface for asynchronously executing multiple {@link Runner} instances and waiting for their
9-
* completion.
10+
* Manages asynchronous background tasks executed during data source and data connection lifecycle
11+
* events.
12+
*
13+
* <p>An instance of {@code AsyncGroup} is passed to methods of {@link DataSrc} (such as {@link
14+
* DataSrc#setup(AsyncGroup)}) and {@link DataConn} (such as {@link DataConn#commit(AsyncGroup)} and
15+
* {@link DataConn#rollback(AsyncGroup)}). Implementations of data sources and data connections can
16+
* register background asynchronous operations (such as parallel cleanup or pre-commit validations)
17+
* using the {@link #add(Runner)} method.
1018
*
11-
* <p>Implementations of this interface allow adding multiple {@link Runner} objects, which are then
12-
* executed concurrently. The group waits until all added runners have finished their execution. Any
13-
* errors occurring during the execution of a {@link Runner} are stored and can be retrieved by
14-
* their names in a map.
19+
* <p>All registered {@link Runner} tasks are managed by this group and executed asynchronously. If
20+
* any registered task fails, is interrupted, or throws an unhandled runtime exception,
21+
* corresponding error records defined in this interface are produced to report the failure.
1522
*/
16-
public sealed interface AsyncGroup permits com.github.sttk.sabi.internal.AsyncGroupImpl {
23+
public sealed interface AsyncGroup permits AsyncGroupImpl {
1724

18-
/**
19-
* Represents the reason for a new {@link com.github.sttk.errs.Err} exception object when an
20-
* exception occurred during the execution of a {@link Runner} and the exception class was not the
21-
* {@link com.github.sttk.errs.Err}.
22-
*/
25+
/** Indicates that a registered {@link Runner} task failed during its execution. */
2326
record RunnerFailed() {}
2427

25-
/**
26-
* Represents the reason for an {@link com.github.sttk.errs.Err} exception object when the
27-
* creation of a thread for asynchronous execution of a {@link Runner} fails.
28-
*/
28+
/** Indicates that a registered {@link Runner} task execution was interrupted. */
2929
record RunnerInterrupted() {}
3030

31-
/** Represents an unexpected {@link RuntimeException} that occurred. */
32-
record RuntimeExceptionOccurred() {}
31+
/**
32+
* Indicates that an unhandled runtime exception was thrown during the execution of a registered
33+
* {@link Runner} task.
34+
*/
35+
record RuntimeExceptionOccured() {}
3336

3437
/**
35-
* Adds a {@link Runner} to this group for asynchronous execution. The added runner will be
36-
* executed in a separate thread.
38+
* Adds a background task to be executed asynchronously by this group.
39+
*
40+
* <p>The task is encapsulated in a {@link Runner} functional interface and scheduled for parallel
41+
* asynchronous execution.
3742
*
38-
* @param runner The {@link Runner} to be added and executed asynchronously.
43+
* @param runner the {@link Runner} task to be added and executed asynchronously
3944
*/
40-
void add(final Runner runner);
45+
void add(Runner runner);
4146
}

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,29 @@
77
import com.github.sttk.errs.Err;
88

99
/**
10-
* An interface designed for implementing data access operations through default methods in its
11-
* sub-interfaces.
10+
* Provides access to named {@link DataConn} data connection instances.
1211
*
13-
* <p>Sub-interfaces of {@code DataAcc} are expected to define and implement data access methods as
14-
* default methods. Within these default methods, the connection to the underlying data store should
15-
* be obtained using the {@link #getDataConn(String, Class)} method provided by this interface. This
16-
* design promotes a clear separation of concerns, allowing data access logic to be encapsulated
17-
* within the interface itself.
12+
* <p>This interface defines the contract for retrieving managed data connections registered under
13+
* specific names within a data access scope (such as a {@link DataHub}). Application business logic
14+
* uses this interface to obtain connection objects required to perform database or external service
15+
* operations.
1816
*/
1917
public interface DataAcc {
18+
2019
/**
21-
* Retrieves a connection to a data store. This method is intended to be used by default methods
22-
* in sub-interfaces to obtain the necessary connection for performing data access operations.
20+
* Retrieves a data connection associated with the specified data source name and casts it to the
21+
* requested connection class type.
22+
*
23+
* <p>If a connection for the given {@code name} has already been created within the current
24+
* transaction or execution scope, that connection instance is returned. Otherwise, a new
25+
* connection is created via the corresponding registered data source.
2326
*
24-
* @param <C> The type of the data connection, which must extend {@link DataConn}.
25-
* @param name The name identifying the specific data connection to retrieve.
26-
* @param cls The {@link Class} object representing the type of the desired data connection.
27-
* @return A data connection object of the specified type.
28-
* @throws Err if an error occurs while obtaining the data connection.
27+
* @param <C> the expected type of {@link DataConn}
28+
* @param name the registered logical name of the data source
29+
* @param cls the {@link Class} representing the target connection type {@code C}
30+
* @return the data connection instance associated with the specified name
31+
* @throws Err if no data source with the specified name is found, if creating the connection
32+
* fails or yields null, or if the connection cannot be cast to the target type
2933
*/
3034
<C extends DataConn> C getDataConn(String name, Class<C> cls) throws Err;
3135
}

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

Lines changed: 69 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,78 +5,105 @@
55
package com.github.sttk.sabi;
66

77
import com.github.sttk.errs.Err;
8+
import java.util.List;
89

910
/**
10-
* The interface that abstracts a connection per session to an external data service, such as a
11-
* database, file system, or messaging service.
11+
* Represents a data connection participating in transaction lifecycles managed by a {@link
12+
* DataHub}.
1213
*
13-
* <p>Its primary purpose is to enable cohesive transaction operations across multiple external data
14-
* services within a single transaction context. Implementations of this interface provide the
15-
* concrete input/output operations for their respective data services.
16-
*
17-
* <p>Methods declared within this interface are designed to handle transactional logic. The
18-
* AsyncGroup parameter in various methods allows for asynchronous processing when commit or
19-
* rollback operations are time-consuming.
14+
* <p>Implementations encapsulate connection state and operations for external resources such as
15+
* relational databases, NoSQL stores, or web services. During a transaction, connections progress
16+
* through pre-commit, commit, and post-commit phases, and support rollback and failure reporting
17+
* when errors occur.
2018
*/
2119
public interface DataConn {
20+
2221
/**
23-
* Commits the changes made within the current session to the external data service. This method
24-
* is responsible for finalizing all operations performed since the last commit or rollback.
22+
* Represents an error when one or more data connections fail during the pre-commit phase.
2523
*
26-
* @param ag An {@link AsyncGroup} that can be used to perform asynchronous operations if the
27-
* commit process is time-consuming.
28-
* @throws Err if an error occurs during the commit operation.
24+
* @param errors the list of error entries detailing connection names and failure causes
2925
*/
30-
void commit(AsyncGroup ag) throws Err;
26+
public record FailToPreCommitDataConn(List<ErrEntry> errors) {}
3127

3228
/**
33-
* Performs any necessary pre-commit operations. This method is called before the {@link
34-
* #commit(AsyncGroup)} method.
29+
* Represents an error when one or more data connections fail during the commit phase.
3530
*
36-
* @param ag An {@link AsyncGroup} that can be used for asynchronous pre-commit tasks.
37-
* @throws Err if an error occurs during the pre-commit operation.
31+
* @param errors the list of error entries detailing connection names and failure causes
3832
*/
39-
default void preCommit(AsyncGroup ag) throws Err {}
33+
public record FailToCommitDataConn(List<ErrEntry> errors) {}
34+
35+
/**
36+
* Represents an error when one or more data connections fail during the post-commit phase.
37+
*
38+
* @param errors the list of error entries detailing connection names and failure causes
39+
*/
40+
public record FailToPostCommitDataConn(List<ErrEntry> errors) {}
41+
42+
///
43+
44+
/**
45+
* Commits changes made through this connection.
46+
*
47+
* <p>Async background tasks may be registered to the provided {@link AsyncGroup} during commit
48+
* processing.
49+
*
50+
* @param ag the asynchronous group for registering background tasks
51+
* @throws Err if committing the transaction changes fails
52+
*/
53+
void commit(AsyncGroup ag) throws Err;
4054

4155
/**
42-
* Performs any necessary post-commit operations. This method is called after the {@link
43-
* #commit(AsyncGroup)} method has successfully completed.
56+
* Prepares this connection for commit prior to the main commit phase.
57+
*
58+
* <p>The default implementation does nothing. Subclasses can override this method to perform
59+
* pre-commit checks or flush pending writes.
4460
*
45-
* @param ag An {@link AsyncGroup} that can be used for asynchronous post-commit tasks.
61+
* @param ag the asynchronous group for registering background tasks
62+
* @throws Err if pre-commit preparation or validation fails
4663
*/
47-
default void postCommit(AsyncGroup ag) {}
64+
default void preCommit(AsyncGroup ag) throws Err {}
4865

4966
/**
50-
* Indicates whether a force-back operation should be performed. A force-back is a mechanism to
51-
* revert the committed changes when this connection had been already committed but the other
52-
* connection had failed.
67+
* Performs post-commit operations after all connections in a transaction have successfully
68+
* committed.
5369
*
54-
* @return {@code true} if a force-back is required, {@code false} otherwise.
70+
* <p>The default implementation does nothing. Subclasses can override this method to perform
71+
* cleanup or trigger post-commit notifications.
72+
*
73+
* @param ag the asynchronous group for registering background tasks
74+
* @throws Err if post-commit processing fails
5575
*/
56-
default boolean shouldForceBack() {
57-
return false;
58-
}
76+
default void postCommit(AsyncGroup ag) throws Err {}
5977

6078
/**
61-
* Rolls back the changes made within the current session, discarding all operations performed
62-
* since the last commit or rollback.
79+
* Checks whether this connection has been successfully committed.
6380
*
64-
* @param ag An {@link AsyncGroup} that can be used to perform asynchronous operations if the
65-
* rollback process is time-consuming.
81+
* @return {@code true} if this connection was committed; {@code false} otherwise
6682
*/
67-
void rollback(AsyncGroup ag);
83+
boolean isCommitted();
6884

6985
/**
70-
* Performs a force-back operation to revert the committed changes when this connection had been
71-
* already committed but the other connection had failed.
86+
* Rolls back changes made through this connection.
87+
*
88+
* <p>This method is invoked when a transaction fails and must restore the connection or
89+
* underlying storage to its pre-transaction state.
7290
*
73-
* @param ag An {@link AsyncGroup} that can be used for asynchronous force-back tasks.
91+
* @param ag the asynchronous group for registering background tasks
92+
* @throws Err if rolling back connection changes fails
7493
*/
75-
default void forceBack(AsyncGroup ag) {}
94+
void rollback(AsyncGroup ag) throws Err;
7695

7796
/**
78-
* Closes the connection to the external data service, releasing any associated resources. This
79-
* method should be called to ensure proper resource management.
97+
* Notifies this connection of a transaction failure with detailed failure reports.
98+
*
99+
* <p>The default implementation does nothing. Implementations can inspect failure reports to log
100+
* diagnostics or take recovery actions.
101+
*
102+
* @param ag the asynchronous group for registering background tasks
103+
* @param reports the list of transaction failure reports
80104
*/
105+
default void onTxnFailure(AsyncGroup ag, List<TxnFailureReport> reports) {}
106+
107+
/** Closes and disposes of this data connection, releasing any held resources. */
81108
void close();
82109
}

0 commit comments

Comments
 (0)