-
Notifications
You must be signed in to change notification settings - Fork 95
Added the ability to have call context on batch loader calls #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.dataloader; | ||
|
||
/** | ||
* A BatchContextProvider is used by the {@link org.dataloader.DataLoader} code to | ||
* provide context to the {@link org.dataloader.BatchLoader} call. A common use | ||
* case is for propagating user security credentials or database connection parameters. | ||
*/ | ||
public interface BatchContextProvider { | ||
/** | ||
* @return a context object that may be needed in batch calls | ||
*/ | ||
Object get(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,11 +74,33 @@ | |
public interface BatchLoader<K, V> { | ||
|
||
/** | ||
* Called to batch load the provided keys and return a promise to a list of values | ||
* Called to batch load the provided keys and return a promise to a list of values. | ||
* | ||
* If you need calling context then implement the {@link #load(java.util.List, Object)} method | ||
* instead. | ||
* | ||
* @param keys the collection of keys to load | ||
* | ||
* @return a promise of the values for those keys | ||
*/ | ||
CompletionStage<List<V>> load(List<K> keys); | ||
|
||
/** | ||
* Called to batch load the provided keys and return a promise to a list of values. This default | ||
* version can be given a context object to that maybe be useful during the call. A typical use case | ||
* is passing in security credentials or database connection details say. | ||
* | ||
* This method is implemented as a default method in order to preserve the API for previous | ||
* callers. It is always called first by the {@link org.dataloader.DataLoader} code and simply | ||
* delegates to the {@link #load(java.util.List)} method. | ||
* | ||
* @param keys the collection of keys to load | ||
* @param context a context object that can help with the call | ||
* | ||
* @return a promise of the values for those keys | ||
*/ | ||
@SuppressWarnings("unused") | ||
default CompletionStage<List<V>> load(List<K> keys, Object context) { | ||
return load(keys); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Left in for API compat reasons. Ideally I would have just 1 method but it doesnt seem enough value to break people |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.dataloader; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.util.Arrays.asList; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.Assert.assertThat; | ||
|
||
/** | ||
* Tests related to context. DataLoaderTest is getting to big and needs refactoring | ||
*/ | ||
public class DataLoaderContextTest { | ||
|
||
@Test | ||
public void context_is_passed_to_batch_loader_function() throws Exception { | ||
BatchLoader<String, String> batchLoader = new BatchLoader<String, String>() { | ||
@Override | ||
public CompletionStage<List<String>> load(List<String> keys) { | ||
throw new UnsupportedOperationException("this wont be called"); | ||
} | ||
|
||
@Override | ||
public CompletionStage<List<String>> load(List<String> keys, Object context) { | ||
List<String> list = keys.stream().map(k -> k + "-" + context).collect(Collectors.toList()); | ||
return CompletableFuture.completedFuture(list); | ||
} | ||
}; | ||
DataLoaderOptions options = DataLoaderOptions.newOptions() | ||
.setBatchContextProvider(() -> "ctx"); | ||
DataLoader<String, String> loader = new DataLoader<>(batchLoader, options); | ||
|
||
loader.load("A"); | ||
loader.load("B"); | ||
loader.loadMany(asList("C", "D")); | ||
|
||
List<String> results = loader.dispatchAndJoin(); | ||
|
||
assertThat(results, equalTo(asList("A-ctx", "B-ctx", "C-ctx", "D-ctx"))); | ||
} | ||
|
||
@Test | ||
public void null_is_passed_as_context_if_you_do_nothing() throws Exception { | ||
BatchLoader<String, String> batchLoader = new BatchLoader<String, String>() { | ||
@Override | ||
public CompletionStage<List<String>> load(List<String> keys) { | ||
throw new UnsupportedOperationException("this wont be called"); | ||
} | ||
|
||
@Override | ||
public CompletionStage<List<String>> load(List<String> keys, Object context) { | ||
List<String> list = keys.stream().map(k -> k + "-" + context).collect(Collectors.toList()); | ||
return CompletableFuture.completedFuture(list); | ||
} | ||
}; | ||
DataLoader<String, String> loader = new DataLoader<>(batchLoader); | ||
|
||
loader.load("A"); | ||
loader.load("B"); | ||
loader.loadMany(asList("C", "D")); | ||
|
||
List<String> results = loader.dispatchAndJoin(); | ||
|
||
assertThat(results, equalTo(asList("A-null", "B-null", "C-null", "D-null"))); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thought I had was that we deprecated the old method and remove it later in a 3.0 but it seemed like too big leap (deprecate+remove later versus just have them implement 2 methods if they want context) for low value.
What are other peoples thoughts on this? @andimarek ? @kaqqao ?