Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 33 additions & 21 deletions src/main/java/org/dataloader/DataLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
*
* @param <K> type parameter indicating the type of the data load keys
* @param <V> type parameter indicating the type of the data that is returned
*
* @author <a href="https://github.com/aschrijver/">Arnold Schrijver</a>
* @author <a href="https://github.com/bbakerman/">Brad Baker</a>
*/
Expand Down Expand Up @@ -133,6 +134,7 @@ public Object getBatchLoadFunction() {
* This allows you to change the current {@link DataLoader} and turn it into a new one
*
* @param builderConsumer the {@link DataLoaderFactory.Builder} consumer for changing the {@link DataLoader}
*
* @return a newly built {@link DataLoader} instance
*/
public DataLoader<K, V> transform(Consumer<DataLoaderFactory.Builder<K, V>> builderConsumer) {
Expand Down Expand Up @@ -167,6 +169,7 @@ public Duration getTimeSinceDispatch() {
* and returned from cache).
*
* @param key the key to load
*
* @return the future of the value
*/
public CompletableFuture<V> load(K key) {
Expand All @@ -184,6 +187,7 @@ public CompletableFuture<V> load(K key) {
* NOTE : This will NOT cause a data load to happen. You must call {@link #load(Object)} for that to happen.
*
* @param key the key to check
*
* @return an Optional to the future of the value
*/
public Optional<CompletableFuture<V>> getIfPresent(K key) {
Expand All @@ -202,6 +206,7 @@ public Optional<CompletableFuture<V>> getIfPresent(K key) {
* NOTE : This will NOT cause a data load to happen. You must call {@link #load(Object)} for that to happen.
*
* @param key the key to check
*
* @return an Optional to the future of the value
*/
public Optional<CompletableFuture<V>> getIfCompleted(K key) {
Expand All @@ -221,6 +226,7 @@ public Optional<CompletableFuture<V>> getIfCompleted(K key) {
*
* @param key the key to load
* @param keyContext a context object that is specific to this key
*
* @return the future of the value
*/
public CompletableFuture<V> load(@NonNull K key, @Nullable Object keyContext) {
Expand All @@ -236,6 +242,7 @@ public CompletableFuture<V> load(@NonNull K key, @Nullable Object keyContext) {
* and returned from cache).
*
* @param keys the list of keys to load
*
* @return the composite future of the list of values
*/
public CompletableFuture<List<V>> loadMany(List<K> keys) {
Expand All @@ -255,24 +262,23 @@ public CompletableFuture<List<V>> loadMany(List<K> keys) {
*
* @param keys the list of keys to load
* @param keyContexts the list of key calling context objects
*
* @return the composite future of the list of values
*/
public CompletableFuture<List<V>> loadMany(List<K> keys, List<Object> keyContexts) {
nonNull(keys);
nonNull(keyContexts);

synchronized (this) {
List<CompletableFuture<V>> collect = new ArrayList<>(keys.size());
for (int i = 0; i < keys.size(); i++) {
K key = keys.get(i);
Object keyContext = null;
if (i < keyContexts.size()) {
keyContext = keyContexts.get(i);
}
collect.add(load(key, keyContext));
List<CompletableFuture<V>> collect = new ArrayList<>(keys.size());
for (int i = 0; i < keys.size(); i++) {
K key = keys.get(i);
Object keyContext = null;
if (i < keyContexts.size()) {
keyContext = keyContexts.get(i);
}
return CompletableFutureKit.allOf(collect);
collect.add(load(key, keyContext));
}
return CompletableFutureKit.allOf(collect);
}

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

synchronized (this) {
Map<K, CompletableFuture<V>> collect = new HashMap<>(keysAndContexts.size());
for (Map.Entry<K, ?> entry : keysAndContexts.entrySet()) {
K key = entry.getKey();
Object keyContext = entry.getValue();
collect.put(key, load(key, keyContext));
}
return CompletableFutureKit.allOf(collect);
Map<K, CompletableFuture<V>> collect = new HashMap<>(keysAndContexts.size());
for (Map.Entry<K, ?> entry : keysAndContexts.entrySet()) {
K key = entry.getKey();
Object keyContext = entry.getValue();
collect.put(key, load(key, keyContext));
}
return CompletableFutureKit.allOf(collect);
}

/**
Expand Down Expand Up @@ -359,6 +364,7 @@ public int dispatchDepth() {
* on the next load request.
*
* @param key the key to remove
*
* @return the data loader for fluent coding
*/
public DataLoader<K, V> clear(K key) {
Expand All @@ -372,6 +378,7 @@ public DataLoader<K, V> clear(K key) {
*
* @param key the key to remove
* @param handler a handler that will be called after the async remote clear completes
*
* @return the data loader for fluent coding
*/
public DataLoader<K, V> clear(K key, BiConsumer<Void, Throwable> handler) {
Expand All @@ -397,6 +404,7 @@ public DataLoader<K, V> clearAll() {
* Clears the entire cache map of the loader, and of the cached value store.
*
* @param handler a handler that will be called after the async remote clear all completes
*
* @return the data loader for fluent coding
*/
public DataLoader<K, V> clearAll(BiConsumer<Void, Throwable> handler) {
Expand All @@ -414,6 +422,7 @@ public DataLoader<K, V> clearAll(BiConsumer<Void, Throwable> handler) {
*
* @param key the key
* @param value the value
*
* @return the data loader for fluent coding
*/
public DataLoader<K, V> prime(K key, V value) {
Expand All @@ -425,6 +434,7 @@ public DataLoader<K, V> prime(K key, V value) {
*
* @param key the key
* @param error the exception to prime instead of a value
*
* @return the data loader for fluent coding
*/
public DataLoader<K, V> prime(K key, Exception error) {
Expand All @@ -438,6 +448,7 @@ public DataLoader<K, V> prime(K key, Exception error) {
*
* @param key the key
* @param value the value
*
* @return the data loader for fluent coding
*/
public DataLoader<K, V> prime(K key, CompletableFuture<V> value) {
Expand All @@ -457,6 +468,7 @@ public DataLoader<K, V> prime(K key, CompletableFuture<V> value) {
* If no cache key function is present in {@link DataLoaderOptions}, then the returned value equals the input key.
*
* @param key the input key
*
* @return the cache key after the input is transformed with the cache key function
*/
public Object getCacheKey(K key) {
Expand Down Expand Up @@ -495,8 +507,8 @@ public ValueCache<K, V> getValueCache() {
@Override
public String toString() {
return "DataLoader{" +
"name='" + name + '\'' +
", stats=" + stats +
'}';
"name='" + name + '\'' +
", stats=" + stats +
'}';
}
}