Skip to content

Commit dd96e13

Browse files
authored
Merge pull request #105 from dugenkui03/add_GuardedBy
Add @GuardedBy and remove redundant synchronized
2 parents 7c16e61 + c631323 commit dd96e13

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/main/java/org/dataloader/DataLoaderHelper.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.dataloader;
22

3+
import org.dataloader.annotations.GuardedBy;
34
import org.dataloader.annotations.Internal;
45
import org.dataloader.impl.CompletableFutureKit;
56
import org.dataloader.stats.StatisticsCollector;
@@ -287,6 +288,7 @@ private void possiblyClearCacheEntriesOnExceptions(List<K> keys) {
287288
}
288289
}
289290

291+
@GuardedBy("dataLoader")
290292
private CompletableFuture<V> loadFromCache(K key, Object loadContext, boolean batchingEnabled) {
291293
final Object cacheKey = loadContext == null ? getCacheKey(key) : getCacheKeyWithContext(key, loadContext);
292294

@@ -296,15 +298,12 @@ private CompletableFuture<V> loadFromCache(K key, Object loadContext, boolean ba
296298
return futureCache.get(cacheKey);
297299
}
298300

299-
CompletableFuture<V> loadCallFuture;
300-
synchronized (dataLoader) {
301-
loadCallFuture = queueOrInvokeLoader(key, loadContext, batchingEnabled, true);
302-
}
303-
301+
CompletableFuture<V> loadCallFuture = queueOrInvokeLoader(key, loadContext, batchingEnabled, true);
304302
futureCache.set(cacheKey, loadCallFuture);
305303
return loadCallFuture;
306304
}
307305

306+
@GuardedBy("dataLoader")
308307
private CompletableFuture<V> queueOrInvokeLoader(K key, Object loadContext, boolean batchingEnabled, boolean cachingEnabled) {
309308
if (batchingEnabled) {
310309
CompletableFuture<V> loadCallFuture = new CompletableFuture<>();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.dataloader.annotations;
2+
3+
import static java.lang.annotation.ElementType.FIELD;
4+
import static java.lang.annotation.ElementType.METHOD;
5+
import static java.lang.annotation.RetentionPolicy.CLASS;
6+
7+
import java.lang.annotation.Retention;
8+
import java.lang.annotation.Target;
9+
10+
/**
11+
* Indicates that the annotated element should be used only while holding the specified lock.
12+
*/
13+
@Target({FIELD, METHOD})
14+
@Retention(CLASS)
15+
public @interface GuardedBy {
16+
17+
/**
18+
* The lock that should be held.
19+
*/
20+
String value();
21+
}

0 commit comments

Comments
 (0)