Skip to content

Commit a2c1e2b

Browse files
committed
remove unnecessary synchronized
1 parent 6c214be commit a2c1e2b

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

src/main/java/org/dataloader/DataLoader.java

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
*
6464
* @param <K> type parameter indicating the type of the data load keys
6565
* @param <V> type parameter indicating the type of the data that is returned
66+
*
6667
* @author <a href="https://github.com/aschrijver/">Arnold Schrijver</a>
6768
* @author <a href="https://github.com/bbakerman/">Brad Baker</a>
6869
*/
@@ -133,6 +134,7 @@ public Object getBatchLoadFunction() {
133134
* This allows you to change the current {@link DataLoader} and turn it into a new one
134135
*
135136
* @param builderConsumer the {@link DataLoaderFactory.Builder} consumer for changing the {@link DataLoader}
137+
*
136138
* @return a newly built {@link DataLoader} instance
137139
*/
138140
public DataLoader<K, V> transform(Consumer<DataLoaderFactory.Builder<K, V>> builderConsumer) {
@@ -167,6 +169,7 @@ public Duration getTimeSinceDispatch() {
167169
* and returned from cache).
168170
*
169171
* @param key the key to load
172+
*
170173
* @return the future of the value
171174
*/
172175
public CompletableFuture<V> load(K key) {
@@ -184,6 +187,7 @@ public CompletableFuture<V> load(K key) {
184187
* NOTE : This will NOT cause a data load to happen. You must call {@link #load(Object)} for that to happen.
185188
*
186189
* @param key the key to check
190+
*
187191
* @return an Optional to the future of the value
188192
*/
189193
public Optional<CompletableFuture<V>> getIfPresent(K key) {
@@ -202,6 +206,7 @@ public Optional<CompletableFuture<V>> getIfPresent(K key) {
202206
* NOTE : This will NOT cause a data load to happen. You must call {@link #load(Object)} for that to happen.
203207
*
204208
* @param key the key to check
209+
*
205210
* @return an Optional to the future of the value
206211
*/
207212
public Optional<CompletableFuture<V>> getIfCompleted(K key) {
@@ -221,6 +226,7 @@ public Optional<CompletableFuture<V>> getIfCompleted(K key) {
221226
*
222227
* @param key the key to load
223228
* @param keyContext a context object that is specific to this key
229+
*
224230
* @return the future of the value
225231
*/
226232
public CompletableFuture<V> load(@NonNull K key, @Nullable Object keyContext) {
@@ -236,6 +242,7 @@ public CompletableFuture<V> load(@NonNull K key, @Nullable Object keyContext) {
236242
* and returned from cache).
237243
*
238244
* @param keys the list of keys to load
245+
*
239246
* @return the composite future of the list of values
240247
*/
241248
public CompletableFuture<List<V>> loadMany(List<K> keys) {
@@ -255,24 +262,23 @@ public CompletableFuture<List<V>> loadMany(List<K> keys) {
255262
*
256263
* @param keys the list of keys to load
257264
* @param keyContexts the list of key calling context objects
265+
*
258266
* @return the composite future of the list of values
259267
*/
260268
public CompletableFuture<List<V>> loadMany(List<K> keys, List<Object> keyContexts) {
261269
nonNull(keys);
262270
nonNull(keyContexts);
263271

264-
synchronized (this) {
265-
List<CompletableFuture<V>> collect = new ArrayList<>(keys.size());
266-
for (int i = 0; i < keys.size(); i++) {
267-
K key = keys.get(i);
268-
Object keyContext = null;
269-
if (i < keyContexts.size()) {
270-
keyContext = keyContexts.get(i);
271-
}
272-
collect.add(load(key, keyContext));
272+
List<CompletableFuture<V>> collect = new ArrayList<>(keys.size());
273+
for (int i = 0; i < keys.size(); i++) {
274+
K key = keys.get(i);
275+
Object keyContext = null;
276+
if (i < keyContexts.size()) {
277+
keyContext = keyContexts.get(i);
273278
}
274-
return CompletableFutureKit.allOf(collect);
279+
collect.add(load(key, keyContext));
275280
}
281+
return CompletableFutureKit.allOf(collect);
276282
}
277283

278284
/**
@@ -287,20 +293,19 @@ public CompletableFuture<List<V>> loadMany(List<K> keys, List<Object> keyContext
287293
* {@link org.dataloader.MappedBatchLoaderWithContext} to help retrieve data.
288294
*
289295
* @param keysAndContexts the map of keys to their respective contexts
296+
*
290297
* @return the composite future of the map of keys and values
291298
*/
292299
public CompletableFuture<Map<K, V>> loadMany(Map<K, ?> keysAndContexts) {
293300
nonNull(keysAndContexts);
294301

295-
synchronized (this) {
296-
Map<K, CompletableFuture<V>> collect = new HashMap<>(keysAndContexts.size());
297-
for (Map.Entry<K, ?> entry : keysAndContexts.entrySet()) {
298-
K key = entry.getKey();
299-
Object keyContext = entry.getValue();
300-
collect.put(key, load(key, keyContext));
301-
}
302-
return CompletableFutureKit.allOf(collect);
302+
Map<K, CompletableFuture<V>> collect = new HashMap<>(keysAndContexts.size());
303+
for (Map.Entry<K, ?> entry : keysAndContexts.entrySet()) {
304+
K key = entry.getKey();
305+
Object keyContext = entry.getValue();
306+
collect.put(key, load(key, keyContext));
303307
}
308+
return CompletableFutureKit.allOf(collect);
304309
}
305310

306311
/**
@@ -359,6 +364,7 @@ public int dispatchDepth() {
359364
* on the next load request.
360365
*
361366
* @param key the key to remove
367+
*
362368
* @return the data loader for fluent coding
363369
*/
364370
public DataLoader<K, V> clear(K key) {
@@ -372,6 +378,7 @@ public DataLoader<K, V> clear(K key) {
372378
*
373379
* @param key the key to remove
374380
* @param handler a handler that will be called after the async remote clear completes
381+
*
375382
* @return the data loader for fluent coding
376383
*/
377384
public DataLoader<K, V> clear(K key, BiConsumer<Void, Throwable> handler) {
@@ -397,6 +404,7 @@ public DataLoader<K, V> clearAll() {
397404
* Clears the entire cache map of the loader, and of the cached value store.
398405
*
399406
* @param handler a handler that will be called after the async remote clear all completes
407+
*
400408
* @return the data loader for fluent coding
401409
*/
402410
public DataLoader<K, V> clearAll(BiConsumer<Void, Throwable> handler) {
@@ -414,6 +422,7 @@ public DataLoader<K, V> clearAll(BiConsumer<Void, Throwable> handler) {
414422
*
415423
* @param key the key
416424
* @param value the value
425+
*
417426
* @return the data loader for fluent coding
418427
*/
419428
public DataLoader<K, V> prime(K key, V value) {
@@ -425,6 +434,7 @@ public DataLoader<K, V> prime(K key, V value) {
425434
*
426435
* @param key the key
427436
* @param error the exception to prime instead of a value
437+
*
428438
* @return the data loader for fluent coding
429439
*/
430440
public DataLoader<K, V> prime(K key, Exception error) {
@@ -438,6 +448,7 @@ public DataLoader<K, V> prime(K key, Exception error) {
438448
*
439449
* @param key the key
440450
* @param value the value
451+
*
441452
* @return the data loader for fluent coding
442453
*/
443454
public DataLoader<K, V> prime(K key, CompletableFuture<V> value) {
@@ -457,6 +468,7 @@ public DataLoader<K, V> prime(K key, CompletableFuture<V> value) {
457468
* If no cache key function is present in {@link DataLoaderOptions}, then the returned value equals the input key.
458469
*
459470
* @param key the input key
471+
*
460472
* @return the cache key after the input is transformed with the cache key function
461473
*/
462474
public Object getCacheKey(K key) {
@@ -495,8 +507,8 @@ public ValueCache<K, V> getValueCache() {
495507
@Override
496508
public String toString() {
497509
return "DataLoader{" +
498-
"name='" + name + '\'' +
499-
", stats=" + stats +
500-
'}';
510+
"name='" + name + '\'' +
511+
", stats=" + stats +
512+
'}';
501513
}
502514
}

0 commit comments

Comments
 (0)