11package org .dataloader ;
22
33import org .dataloader .annotations .PublicApi ;
4- import org .dataloader .registries .DispatchPredicate ;
54import org .dataloader .stats .Statistics ;
65
76import java .util .ArrayList ;
2221@ PublicApi
2322public class DataLoaderRegistry {
2423 protected final Map <String , DataLoader <?, ?>> dataLoaders = new ConcurrentHashMap <>();
25- protected final Map <DataLoader <?, ?>, DispatchPredicate > dataLoaderPredicates = new ConcurrentHashMap <>();
26- protected final DispatchPredicate dispatchPredicate ;
27-
2824
2925 public DataLoaderRegistry () {
30- this .dispatchPredicate = DispatchPredicate .DISPATCH_ALWAYS ;
3126 }
3227
3328 protected DataLoaderRegistry (Builder <?> builder ) {
3429 this .dataLoaders .putAll (builder .dataLoaders );
35- this .dataLoaderPredicates .putAll (builder .dataLoaderPredicates );
36- this .dispatchPredicate = builder .dispatchPredicate ;
3730 }
3831
3932
@@ -50,21 +43,6 @@ public DataLoaderRegistry register(String key, DataLoader<?, ?> dataLoader) {
5043 return this ;
5144 }
5245
53- /**
54- * This will register a new dataloader and dispatch predicate associated with that data loader
55- *
56- * @param key the key to put the data loader under
57- * @param dataLoader the data loader to register
58- * @param dispatchPredicate the dispatch predicate to associate with this data loader
59- *
60- * @return this registry
61- */
62- public DataLoaderRegistry register (String key , DataLoader <?, ?> dataLoader , DispatchPredicate dispatchPredicate ) {
63- dataLoaders .put (key , dataLoader );
64- dataLoaderPredicates .put (dataLoader , dispatchPredicate );
65- return this ;
66- }
67-
6846 /**
6947 * Computes a data loader if absent or return it if it was
7048 * already registered at that key.
@@ -98,8 +76,6 @@ public DataLoaderRegistry combine(DataLoaderRegistry registry) {
9876
9977 this .dataLoaders .forEach (combined ::register );
10078 registry .dataLoaders .forEach (combined ::register );
101- combined .dataLoaderPredicates .putAll (this .dataLoaderPredicates );
102- combined .dataLoaderPredicates .putAll (registry .dataLoaderPredicates );
10379 return combined ;
10480 }
10581
@@ -117,20 +93,6 @@ public DataLoaderRegistry combine(DataLoaderRegistry registry) {
11793 return new LinkedHashMap <>(dataLoaders );
11894 }
11995
120- /**
121- * @return the current dispatch predicate
122- */
123- public DispatchPredicate getDispatchPredicate () {
124- return dispatchPredicate ;
125- }
126-
127- /**
128- * @return a map of data loaders to specific dispatch predicates
129- */
130- public Map <DataLoader <?, ?>, DispatchPredicate > getDataLoaderPredicates () {
131- return new LinkedHashMap <>(dataLoaderPredicates );
132- }
133-
13496 /**
13597 * This will unregister a new dataloader
13698 *
@@ -139,10 +101,7 @@ public DispatchPredicate getDispatchPredicate() {
139101 * @return this registry
140102 */
141103 public DataLoaderRegistry unregister (String key ) {
142- DataLoader <?, ?> dataLoader = dataLoaders .remove (key );
143- if (dataLoader != null ) {
144- dataLoaderPredicates .remove (dataLoader );
145- }
104+ dataLoaders .remove (key );
146105 return this ;
147106 }
148107
@@ -168,11 +127,11 @@ public Set<String> getKeys() {
168127 }
169128
170129 /**
171- * This will be called {@link org.dataloader.DataLoader#dispatch()} on each of the registered
130+ * This will called {@link org.dataloader.DataLoader#dispatch()} on each of the registered
172131 * {@link org.dataloader.DataLoader}s
173132 */
174133 public void dispatchAll () {
175- dispatchAllWithCount ( );
134+ getDataLoaders (). forEach ( DataLoader :: dispatch );
176135 }
177136
178137 /**
@@ -183,12 +142,8 @@ public void dispatchAll() {
183142 */
184143 public int dispatchAllWithCount () {
185144 int sum = 0 ;
186- for (Map .Entry <String , DataLoader <?, ?>> entry : dataLoaders .entrySet ()) {
187- DataLoader <?, ?> dataLoader = entry .getValue ();
188- String key = entry .getKey ();
189- if (shouldDispatch (key , dataLoader )) {
190- sum += dataLoader .dispatchWithCounts ().getKeysCount ();
191- }
145+ for (DataLoader <?, ?> dataLoader : getDataLoaders ()) {
146+ sum += dataLoader .dispatchWithCounts ().getKeysCount ();
192147 }
193148 return sum ;
194149 }
@@ -198,47 +153,11 @@ public int dispatchAllWithCount() {
198153 * {@link org.dataloader.DataLoader}s
199154 */
200155 public int dispatchDepth () {
201- return dataLoaders .values ().stream ().mapToInt (DataLoader ::dispatchDepth ).sum ();
202- }
203-
204- /**
205- * This will immediately dispatch the {@link DataLoader}s in the registry
206- * without testing the predicates
207- */
208- public void dispatchAllImmediately () {
209- dispatchAllWithCountImmediately ();
210- }
211-
212- /**
213- * This will immediately dispatch the {@link DataLoader}s in the registry
214- * without testing the predicates
215- *
216- * @return total number of entries that were dispatched from registered {@link org.dataloader.DataLoader}s.
217- */
218- public int dispatchAllWithCountImmediately () {
219- return dataLoaders .values ().stream ()
220- .mapToInt (dataLoader -> dataLoader .dispatchWithCounts ().getKeysCount ())
221- .sum ();
222- }
223-
224-
225- /**
226- * Returns true if the dataloader has a predicate which returned true, OR the overall
227- * registry predicate returned true.
228- *
229- * @param dataLoaderKey the key in the dataloader map
230- * @param dataLoader the dataloader
231- *
232- * @return true if it should dispatch
233- */
234- protected boolean shouldDispatch (String dataLoaderKey , DataLoader <?, ?> dataLoader ) {
235- DispatchPredicate dispatchPredicate = dataLoaderPredicates .get (dataLoader );
236- if (dispatchPredicate != null ) {
237- if (dispatchPredicate .test (dataLoaderKey , dataLoader )) {
238- return true ;
239- }
156+ int totalDispatchDepth = 0 ;
157+ for (DataLoader <?, ?> dataLoader : getDataLoaders ()) {
158+ totalDispatchDepth += dataLoader .dispatchDepth ();
240159 }
241- return this . dispatchPredicate . test ( dataLoaderKey , dataLoader ) ;
160+ return totalDispatchDepth ;
242161 }
243162
244163 /**
@@ -256,19 +175,15 @@ public Statistics getStatistics() {
256175 /**
257176 * @return A builder of {@link DataLoaderRegistry}s
258177 */
259- public static Builder <?> newRegistry () {
260- //noinspection rawtypes
178+ public static Builder newRegistry () {
261179 return new Builder ();
262180 }
263181
264182 public static class Builder <B extends Builder <B >> {
265183
266184 private final Map <String , DataLoader <?, ?>> dataLoaders = new HashMap <>();
267- private final Map <DataLoader <?, ?>, DispatchPredicate > dataLoaderPredicates = new ConcurrentHashMap <>();
268-
269- private DispatchPredicate dispatchPredicate = DispatchPredicate .DISPATCH_ALWAYS ;
270185
271- private B self () {
186+ protected B self () {
272187 //noinspection unchecked
273188 return (B ) this ;
274189 }
@@ -287,22 +202,7 @@ public B register(String key, DataLoader<?, ?> dataLoader) {
287202 }
288203
289204 /**
290- * This will register a new dataloader with a specific {@link DispatchPredicate}
291- *
292- * @param key the key to put the data loader under
293- * @param dataLoader the data loader to register
294- * @param dispatchPredicate the dispatch predicate
295- *
296- * @return this builder for a fluent pattern
297- */
298- public B register (String key , DataLoader <?, ?> dataLoader , DispatchPredicate dispatchPredicate ) {
299- register (key , dataLoader );
300- dataLoaderPredicates .put (dataLoader , dispatchPredicate );
301- return self ();
302- }
303-
304- /**
305- * This will combine the data loaders in this builder with the ones
205+ * This will combine together the data loaders in this builder with the ones
306206 * from a previous {@link DataLoaderRegistry}
307207 *
308208 * @param otherRegistry the previous {@link DataLoaderRegistry}
@@ -311,20 +211,6 @@ public B register(String key, DataLoader<?, ?> dataLoader, DispatchPredicate dis
311211 */
312212 public B registerAll (DataLoaderRegistry otherRegistry ) {
313213 dataLoaders .putAll (otherRegistry .dataLoaders );
314- dataLoaderPredicates .putAll (otherRegistry .dataLoaderPredicates );
315- return self ();
316- }
317-
318- /**
319- * This sets a predicate on the {@link DataLoaderRegistry} that will control
320- * whether all {@link DataLoader}s in the {@link DataLoaderRegistry }should be dispatched.
321- *
322- * @param dispatchPredicate the predicate
323- *
324- * @return this builder for a fluent pattern
325- */
326- public B dispatchPredicate (DispatchPredicate dispatchPredicate ) {
327- this .dispatchPredicate = dispatchPredicate ;
328214 return self ();
329215 }
330216
0 commit comments