88import com .github .sttk .sabi .internal .DataHubInner ;
99import java .util .Map ;
1010
11+ /**
12+ * {@code DataHub} is a central component in the Sabi framework that manages {@link DataSrc} and
13+ * {@link DataConn} instances, facilitating data access and transaction management. It implements
14+ * both {@link DataAcc} for data access operations and {@link AutoCloseable} for resource
15+ * management.
16+ *
17+ * <p>This class allows for the registration and unregistration of local {@link DataSrc} objects,
18+ * and provides methods to execute application logic with or without transactional boundaries.
19+ */
1120public class DataHub implements DataAcc , AutoCloseable {
21+ /**
22+ * Represents an error reason that occurred when failing to set up global {@link DataSrc}
23+ * instances.
24+ *
25+ * @param errors A map containing the names of the data sources and the corresponding exceptions
26+ * that occurred during their setup.
27+ */
1228 public record FailToSetupGlobalDataSrcs (Map <String , Exc > errors ) {}
1329
30+ /**
31+ * Represents an error reason that occurred when failing to set up local {@link DataSrc}
32+ * instances.
33+ *
34+ * @param errors A map containing the names of the data sources and the corresponding exceptions
35+ * that occurred during their setup.
36+ */
1437 public record FailToSetupLocalDataSrcs (Map <String , Exc > errors ) {}
1538
39+ /**
40+ * Represents an error reason that occurred when failing to commit one or more {@link DataConn}
41+ * instances.
42+ *
43+ * @param errors A map containing the names of the data connections and the corresponding
44+ * exceptions that occurred during their commit.
45+ */
1646 public record FailToCommitDataConn (Map <String , Exc > errors ) {}
1747
48+ /**
49+ * Represents an error reason that occurred when failing to pre-commit one or more {@link
50+ * DataConn} instances.
51+ *
52+ * @param errors A map containing the names of the data connections and the corresponding
53+ * exceptions that occurred during their pre-commit.
54+ */
1855 public record FailToPreCommitDataConn (Map <String , Exc > errors ) {}
1956
57+ /**
58+ * Represents an error reason where no {@link DataSrc} was found to create a {@link DataConn} with
59+ * the specified name and type.
60+ *
61+ * @param name The name of the data source requested.
62+ * @param dataConnType The type of the data connection requested.
63+ */
2064 public record NoDataSrcToCreateDataConn (String name , String dataConnType ) {}
2165
66+ /**
67+ * Represents an error reason that occurred when failing to create a {@link DataConn} instance.
68+ *
69+ * @param name The name of the data source from which the connection was attempted.
70+ * @param dataConnType The type of the data connection that failed to be created.
71+ */
2272 public record FailToCreateDataConn (String name , String dataConnType ) {}
2373
74+ /**
75+ * Represents an error reason where the created {@link DataConn} instance was null.
76+ *
77+ * @param name The name of the data source.
78+ * @param dataConnType The type of the data connection expected.
79+ */
2480 public record CreatedDataConnIsNull (String name , String dataConnType ) {}
2581
82+ /**
83+ * Represents an error reason that occurred when failing to cast a {@link DataConn} to the
84+ * requested type.
85+ *
86+ * @param name The name of the data connection.
87+ * @param castToType The type to which the data connection was attempted to be cast.
88+ */
2689 public record FailToCastDataConn (String name , String castToType ) {}
2790
91+ /**
92+ * Represents an error reason that occurred when failing to cast the {@code DataHub} instance
93+ * itself to the expected data access interface type for a {@link Logic}.
94+ *
95+ * @param castFromType The actual type of the {@code DataHub} instance that failed to cast.
96+ */
2897 public record FailToCastDataHub (String castFromType ) {}
2998
30- public record RuntimeExceptionOccured () {}
99+ /**
100+ * Represents an unexpected {@link RuntimeException} that occurred during pre-commit or commit
101+ * operations.
102+ */
103+ public record RuntimeExceptionOccurred () {}
31104
32105 private final DataHubInner inner = new DataHubInner ();
33106
107+ /** Constructs a new {@code DataHub} instance. */
34108 public DataHub () {}
35109
110+ /**
111+ * Registers a local {@link DataSrc} with the specified name for use within this {@code DataHub}
112+ * instance. This allows specific data sources to be managed independently from globally
113+ * registered ones.
114+ *
115+ * @param name The unique name for the {@link DataSrc}.
116+ * @param ds The {@link DataSrc} instance to register.
117+ */
36118 public void uses (String name , DataSrc ds ) {
37119 inner .uses (name , ds );
38120 }
39121
122+ /**
123+ * Unregisters a local {@link DataSrc} with the given name from this {@code DataHub} instance.
124+ *
125+ * @param name The name of the {@link DataSrc} to unregister.
126+ */
40127 public void disuses (String name ) {
41128 inner .disuses (name );
42129 }
43130
131+ /**
132+ * Executes the provided application {@link Logic} without transactional boundaries. The {@code
133+ * DataHub} instance itself is passed as the data access object {@code D} to the {@link Logic}'s
134+ * {@code run} method.
135+ *
136+ * @param <D> The type of the data access object, which typically is {@code DataHub} or an
137+ * interface implemented by {@code DataHub} that {@link Logic} expects.
138+ * @param logic The application logic to execute.
139+ * @throws Exc if an {@link Exc} or {@link RuntimeException} occurs during logic execution or if
140+ * the {@code DataHub} cannot be cast to the expected data access type.
141+ */
44142 public <D > void run (Logic <D > logic ) throws Exc {
45143 D data ;
46144 try {
@@ -60,6 +158,19 @@ public <D> void run(Logic<D> logic) throws Exc {
60158 }
61159 }
62160
161+ /**
162+ * Executes the provided application {@link Logic} within a transactional context. The {@code
163+ * DataHub} instance is passed as the data access object {@code D} to the {@link Logic}'s {@code
164+ * run} method. If the logic completes successfully, a commit operation is attempted. If any
165+ * {@link Exc}, {@link RuntimeException}, or {@link Error} occurs, a rollback operation is
166+ * performed.
167+ *
168+ * @param <D> The type of the data access object, which typically is {@code DataHub} or an
169+ * interface implemented by {@code DataHub} that {@link Logic} expects.
170+ * @param logic The application logic to execute transactionally.
171+ * @throws Exc if an {@link Exc}, {@link RuntimeException}, or {@link Error} occurs during logic
172+ * execution, pre-commit, or commit. The original exception is re-thrown after rollback.
173+ */
63174 public <D > void txn (Logic <D > logic ) throws Exc {
64175 D data ;
65176 try {
@@ -82,11 +193,27 @@ public <D> void txn(Logic<D> logic) throws Exc {
82193 }
83194 }
84195
196+ /**
197+ * Retrieves a {@link DataConn} instance from the managed data sources. This method is part of the
198+ * {@link DataAcc} interface implementation.
199+ *
200+ * @param <C> The type of the {@link DataConn} to retrieve, which must extend {@link DataConn}.
201+ * @param name The name of the data source from which to get the connection.
202+ * @param cls The {@link Class} object representing the desired type of the data connection.
203+ * @return A {@link DataConn} instance of the specified type.
204+ * @throws Exc if no data source is found, if the connection cannot be created, if the created
205+ * connection is null, or if the connection cannot be cast to the specified class.
206+ */
85207 @ Override
86208 public <C extends DataConn > C getDataConn (String name , Class <C > cls ) throws Exc {
87209 return inner .getDataConn (name , cls );
88210 }
89211
212+ /**
213+ * Closes all {@link DataConn} instances managed by this {@code DataHub}, releasing their
214+ * resources. This method is part of the {@link AutoCloseable} interface and should be called to
215+ * ensure proper resource cleanup, ideally in a try-with-resources statement.
216+ */
90217 @ Override
91218 public void close () {
92219 inner .close ();
0 commit comments