forked from graphql-java/java-dataloader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadmeExamples.java
More file actions
244 lines (192 loc) · 7.86 KB
/
ReadmeExamples.java
File metadata and controls
244 lines (192 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import org.dataloader.BatchLoader;
import org.dataloader.BatchLoaderEnvironment;
import org.dataloader.BatchLoaderWithContext;
import org.dataloader.CacheMap;
import org.dataloader.DataLoader;
import org.dataloader.DataLoaderOptions;
import org.dataloader.MappedBatchLoaderWithContext;
import org.dataloader.Try;
import org.dataloader.fixtures.SecurityCtx;
import org.dataloader.fixtures.User;
import org.dataloader.fixtures.UserManager;
import org.dataloader.stats.Statistics;
import org.dataloader.stats.ThreadLocalStatisticsCollector;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.stream.Collectors;
import static java.lang.String.format;
@SuppressWarnings("ALL")
public class ReadmeExamples {
UserManager userManager = new UserManager();
public static void main(String[] args) {
ReadmeExamples examples = new ReadmeExamples();
examples.basicExample();
}
@SuppressWarnings({"Convert2Lambda", "Convert2MethodRef", "CodeBlock2Expr"})
void basicExample() {
BatchLoader<Long, User> lessEfficientUserBatchLoader = new BatchLoader<Long, User>() {
@Override
public CompletionStage<List<User>> load(List<Long> userIds) {
return CompletableFuture.supplyAsync(() -> {
//
// notice how it makes N calls to load by single user id out of the batch of N keys
//
return userIds.stream()
.map(id -> userManager.loadUserById(id))
.collect(Collectors.toList());
});
}
};
BatchLoader<Long, User> userBatchLoader = new BatchLoader<Long, User>() {
@Override
public CompletionStage<List<User>> load(List<Long> userIds) {
return CompletableFuture.supplyAsync(() -> {
return userManager.loadUs
7440
ersById(userIds);
});
}
};
DataLoader<Long, User> userLoader = DataLoader.newDataLoader(userBatchLoader);
CompletionStage<User> load1 = userLoader.load(1L);
userLoader.load(1L)
.thenAccept(user -> {
System.out.println("user = " + user);
userLoader.load(user.getInvitedByID())
.thenAccept(invitedBy -> {
System.out.println("invitedBy = " + invitedBy);
});
});
userLoader.load(2L)
.thenAccept(user -> {
System.out.println("user = " + user);
userLoader.load(user.getInvitedByID())
.thenAccept(invitedBy -> {
System.out.println("invitedBy = " + invitedBy);
});
});
userLoader.dispatchAndJoin();
}
private void callContextExample() {
DataLoaderOptions options = DataLoaderOptions.newOptions()
.setBatchLoaderEnvironmentProvider(() -> SecurityCtx.getCallingUserCtx());
BatchLoaderWithContext<String, String> batchLoader = new BatchLoaderWithContext<String, String>() {
@Override
public CompletionStage<List<String>> load(List<String> keys, BatchLoaderEnvironment environment) {
SecurityCtx callCtx = environment.getContext();
return callDatabaseForResults(callCtx, keys);
}
};
DataLoader<String, String> loader = DataLoader.newDataLoader(batchLoader, options);
}
private CompletionStage<List<String>> callDatabaseForResults(SecurityCtx callCtx, List<String> keys) {
return null;
}
private void mapBatchLoader() {
MappedBatchLoaderWithContext<Long, User> mapBatchLoader = new MappedBatchLoaderWithContext<Long, User>() {
@Override
public CompletionStage<Map<Long, User>> load(List<Long> userIds, BatchLoaderEnvironment environment) {
SecurityCtx callCtx = environment.getContext();
return CompletableFuture.supplyAsync(() -> userManager.loadMapOfUsersById(callCtx, userIds));
}
};
DataLoader<Long, User> userLoader = DataLoader.newMappedDataLoader(mapBatchLoader);
// ...
}
private void tryExample() {
Try<String> tryS = Try.tryCall(() -> {
if (rollDice()) {
return "OK";
} else {
throw new RuntimeException("Bang");
}
});
if (tryS.isSuccess()) {
System.out.println("It work " + tryS.get());
} else {
System.out.println("It failed with exception : " + tryS.getThrowable());
}
}
private void tryBatcLoader() {
DataLoader<String, User> dataLoader = DataLoader.newDataLoaderWithTry(new BatchLoader<String, Try<User>>() {
@Override
public CompletionStage<List<Try<User>>> load(List<String> keys) {
return CompletableFuture.supplyAsync(() -> {
List<Try<User>> users = new ArrayList<>();
for (String key : keys) {
Try<User> userTry = loadUser(key);
users.add(userTry);
}
return users;
});
}
});
}
DataLoader<String, User> userDataLoader;
private void clearCacheOnError() {
userDataLoader.load("r2d2").whenComplete((user, throwable) -> {
if (throwable != null) {
userDataLoader.clear("r2dr");
throwable.printStackTrace();
} else {
processUser(user);
}
});
}
BatchLoader<String, User> userBatchLoader;
private void disableCache() {
DataLoader.newDataLoader(userBatchLoader, DataLoaderOptions.newOptions().setCachingEnabled(false));
userDataLoader.load("A");
userDataLoader.load("B");
userDataLoader.load("A");
userDataLoader.dispatch();
// will result in keys to the batch loader with [ "A", "B", "A" ]
}
class MyCustomCache implements CacheMap {
@Override
public boolean containsKey(Object key) {
return false;
}
@Override
public Object get(Object key) {
return null;
}
@Override
public CacheMap set(Object key, Object value) {
return null;
}
@Override
public CacheMap delete(Object key) {
return null;
}
@Override
public CacheMap clear() {
return null;
}
}
private void customCache() {
MyCustomCache customCache = new MyCustomCache();
DataLoaderOptions options = DataLoaderOptions.newOptions().setCacheMap(customCache);
DataLoader.newDataLoader(userBatchLoader, options);
}
private void processUser(User user) {
}
private Try<User> loadUser(String key) {
return null;
}
private boolean rollDice() {
return false;
}
private void statsExample() {
Statistics statistics = userDataLoader.getStatistics();
System.out.println(format("load : %d", statistics.getLoadCount()));
System.out.println(format("batch load: %d", statistics.getBatchLoadCount()));
System.out.println(format("cache hit: %d", statistics.getCacheHitCount()));
System.out.println(format("cache hit ratio: %d", statistics.getCacheHitRatio()));
}
private void statsConfigExample() {
DataLoaderOptions options = DataLoaderOptions.newOptions().setStatisticsCollector(() -> new ThreadLocalStatisticsCollector());
DataLoader<String, User> userDataLoader = DataLoader.newDataLoader(userBatchLoader, options);
}
}