Skip to content

Commit 43cca0c

Browse files
committed
More refactoring of code. Re-naming to more meaningful names
1 parent 1467819 commit 43cca0c

File tree

11 files changed

+500
-476
lines changed

11 files changed

+500
-476
lines changed

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Java `DataLoader`
1+
# `java-dataloader`
22

33
[![Build Status](https://travis-ci.org/bbakerman/java-dataloader.svg?branch=master)](https://travis-ci.org/bbakerman/java-dataloader/)  
44
[![Apache licensed](https://img.shields.io/hexpm/l/plug.svg?maxAge=2592000)](https://github.com/bbakerman/java-dataloader/blob/master/LICENSE)  
@@ -10,8 +10,11 @@ This small and simple utility library is a Pure Java 8 port of [Facebook DataLoa
1010
It can serve as integral part of your application's data layer to provide a
1111
consistent API over various back-ends and reduce message communication overhead through batching and caching.
1212

13-
An important use case for `java-dataloader` is improving the efficiency of GraphQL query execution, but there are
14-
many other use cases where you can benefit from using this utility.
13+
An important use case for `java-dataloader` is improving the efficiency of GraphQL query execution. Graphql fields
14+
are resolved in a independent manner and with a true graph of objects, you may be fetching the same object many times.
15+
Also a naive implementation of graphql data fetchers can easily lead to the dreaded "n+1" fetch problem.
16+
17+
There are many other use cases where you can also benefit from using this utility.
1518

1619
Most of the code is ported directly from Facebook's reference implementation, with one IMPORTANT adaptation to make
1720
it work for Java 8. ([more on this below](manual-dispatching)).
@@ -67,16 +70,17 @@ The original data loader was written in Javascript for NodeJS. NodeJS is single-
6770
asynchronous logic by invoking functions on separate threads in an event loop, as explained
6871
[in this post](http://stackoverflow.com/a/19823583/3455094) on StackOverflow.
6972

70-
Now in NodeJS generates so-call 'ticks' in which queued functions are dispatched for execution, and Facebook `DataLoader` uses
71-
the `nextTick()` function in NodeJS to _automatically_ dequeue load requests and send them to the batch execution function for processing.
73+
NodeJS generates so-call 'ticks' in which queued functions are dispatched for execution, and Facebook `DataLoader` uses
74+
the `nextTick()` function in NodeJS to _automatically_ dequeue load requests and send them to the batch execution function
75+
for processing.
7276

73-
And here there is an **IMPORTANT DIFFERENCE** compared to how _this_ data loader operates!!
77+
And here there is an **IMPORTANT DIFFERENCE** compared to how `java-dataloader` operates!!
7478

7579
In NodeJS the batch preparation will not affect the asynchronous processing behaviour in any way. It will just prepare
7680
batches in 'spare time' as it were.
7781

78-
This is different in Java as you will actually _delay_ the execution of your load requests, until the moment where you make a call
79-
to `dataLoader.dispatch()` in comparison to when you would just handle futures directly.
82+
This is different in Java as you will actually _delay_ the execution of your load requests, until the moment where you make a
83+
call to `dataLoader.dispatch()`.
8084

8185
Does this make Java `DataLoader` any less useful than the reference implementation? We would argue this is not the case,
8286
and there are also gains to this different mode of operation:

build.gradle

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import java.text.SimpleDateFormat
2+
13
buildscript {
24
repositories {
35
jcenter()
@@ -12,7 +14,7 @@ apply plugin: 'maven-publish'
1214
publishing {
1315
repositories {
1416
maven {
15-
url 'https://dl.bintray.com/engagingspaces/maven'
17+
url 'https://dl.bintray.com/bbakerman/maven'
1618
}
1719
}
1820
}
@@ -33,8 +35,9 @@ repositories {
3335
}
3436
}
3537

38+
def releaseVersion = System.properties.RELEASE_VERSION
39+
version = releaseVersion ? releaseVersion : new SimpleDateFormat('yyyy-MM-dd\'T\'HH-mm-ss').format(new Date())
3640
group = 'org.dataloader'
37-
version = '1.0.0'
3841

3942
compileJava {
4043
sourceCompatibility = 1.8

gradle/publishing.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ bintray {
6767
publish = true
6868
pkg {
6969
repo = 'maven'
70-
name = "$project.name"
70+
name = "java-dataloader"
7171
desc = projectDescription
7272
userOrg = 'bbakerman'
7373
websiteUrl = 'https://github.com/bbakerman/java-dataloader'

src/main/java/org/dataloader/BatchLoader.java

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,56 @@
1616

1717
package org.dataloader;
1818

19-
import org.dataloader.impl.CombinedFuturesImpl;
20-
21-
import java.util.Collection;
19+
import java.util.List;
2220

2321
/**
24-
* Function that is invoked for batch loading the list of data values indicated by the provided list of keys. The
25-
* function returns a {@link CombinedFuturesImpl} to aggregate results of individual load requests.
22+
* A function that is invoked for batch loading a list of data values indicated by the provided list of keys. The
23+
* function returns a {@link PromisedValues} to aggregate results of individual load requests.
24+
*
25+
* There are a few constraints that must be upheld:
26+
* <ul>
27+
* <li>The list of values must be the same size as the list of keys.</li>
28+
* <li>Each index in the list of values must correspond to the same index in the list of keys.</li>
29+
* </ul>
30+
*
31+
* For example, if your batch function was provided the list of keys: [ 2, 9, 6, 1 ], and loading from a back-end service returned the values:
32+
*
33+
* <pre>
34+
* [
35+
* { id: 9, name: 'Chicago' },
36+
* { id: 1, name: 'New York' },
37+
* { id: 2, name: 'San Francisco' },
38+
* ]
39+
* </pre>
40+
*
41+
* The back-end service returned results in a different order than we requested, likely because it was more efficient for it to do so. Also, it omitted a result for key 6, which we can interpret as no value
42+
* existing for that key.
43+
*
44+
* To uphold the constraints of the batch function, it must return an List of values the same length as the List of keys, and re-order them to ensure each index aligns with the original keys [ 2, 9, 6, 1 ]:
45+
*
46+
* <pre>
47+
* [
48+
* { id: 2, name: 'San Francisco' },
49+
* { id: 9, name: 'Chicago' },
50+
* null,
51+
* { id: 1, name: 'New York' }
52+
* ]
53+
* </pre>
2654
*
2755
* @param <K> type parameter indicating the type of keys to use for data load requests.
56+
* @param <V> type parameter indicating the type of values returned
2857
*
2958
* @author <a href="https://github.com/aschrijver/">Arnold Schrijver</a>
3059
*/
3160
@FunctionalInterface
32-
public interface BatchLoader<K> {
61+
public interface BatchLoader<K, V> {
3362

3463
/**
35-
* Batch load the provided keys and return a composite future of the result.
64+
* Called to batch load the provided keys and return a {@link PromisedValues} which is a promise to a list of values
3665
*
37-
* @param keys the list of keys to load
66+
* @param keys the collection of keys to load
3867
*
39-
* @return the composite future
68+
* @return a promise of the values for those keys
4069
*/
41-
CombinedFutures load(Collection<K> keys);
70+
PromisedValues<V> load(List<K> keys);
4271
}

src/main/java/org/dataloader/CombinedFutures.java

Lines changed: 0 additions & 167 deletions
This file was deleted.

0 commit comments

Comments
 (0)