Skip to content

Commit 496a298

Browse files
committed
Breaking change - adds a name to a DataLoader and deprecates a bunch of factory methods - added support in DLR for namings DLs - added direct register
1 parent bf3edd2 commit 496a298

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

src/main/java/org/dataloader/DataLoaderRegistry.java

+32-3
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,29 @@ private static DataLoaderOptions setInInstrumentation(DataLoaderOptions options,
130130
}
131131

132132
/**
133-
* This will register a new dataloader
133+
* This will register a new named dataloader. The {@link DataLoader} must be named something and
134+
* cannot have a null name.
135+
* <p>
136+
* Note: Registration can change the data loader instance since it might get an {@link DataLoaderInstrumentation} applied to
137+
* it. So the {@link DataLoader} instance your read via {@link DataLoaderRegistry#getDataLoader(String)} might not be the same
138+
* object that was registered.
139+
*
140+
* @param dataLoader the named data loader to register
141+
* @return this registry
142+
*/
143+
public DataLoaderRegistry register(DataLoader<?, ?> dataLoader) {
144+
String name = dataLoader.getName();
145+
assertState(name != null, () -> "The DataLoader must have a non null name");
146+
dataLoaders.put(name, nameAndInstrumentDL(name, instrumentation, dataLoader));
147+
return this;
148+
}
149+
150+
/**
151+
* This will register a new {@link DataLoader}
152+
* <p>
153+
* Note: Registration can change the data loader instance since it might get an {@link DataLoaderInstrumentation} applied to
154+
* it. So the {@link DataLoader} instance your read via {@link DataLoaderRegistry#getDataLoader(String)} might not be the same
155+
* object that was registered.
134156
*
135157
* @param key the key to put the data loader under
136158
* @param dataLoader the data loader to register
@@ -142,8 +164,11 @@ public DataLoaderRegistry register(String key, DataLoader<?, ?> dataLoader) {
142164
}
143165

144166
/**
145-
* This will register a new dataloader and then return it. It might have been wrapped into a new instance
146-
* because of the registry instrumentation for example.
167+
* This will register a new {@link DataLoader} and then return it.
168+
* <p>
169+
* Note: Registration can change the data loader instance since it might get an {@link DataLoaderInstrumentation} applied to
170+
* it. So the {@link DataLoader} instance your read via {@link DataLoaderRegistry#getDataLoader(String)} might not be the same
171+
* object that was registered.
147172
*
148173
* @param key the key to put the data loader under
149174
* @param dataLoader the data loader to register
@@ -160,6 +185,10 @@ public <K, V> DataLoader<K, V> registerAndGet(String key, DataLoader<?, ?> dataL
160185
* <p>
161186
* Note: The entire method invocation is performed atomically,
162187
* so the function is applied at most once per key.
188+
* <p>
189+
* Note: Registration can change the data loader instance since it might get an {@link DataLoaderInstrumentation} applied to
190+
* it. So the {@link DataLoader} instance your read via {@link DataLoaderRegistry#getDataLoader(String)} might not be the same
191+
* object that was registered.
163192
*
164193
* @param key the key of the data loader
165194
* @param mappingFunction the function to compute a data loader

src/test/java/org/dataloader/DataLoaderRegistryTest.java

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package org.dataloader;
22

3+
import org.dataloader.impl.DataLoaderAssertionException;
34
import org.dataloader.stats.SimpleStatisticsCollector;
45
import org.dataloader.stats.Statistics;
6+
import org.junit.jupiter.api.Assertions;
57
import org.junit.jupiter.api.Test;
68

9+
import java.util.List;
710
import java.util.concurrent.CompletableFuture;
811

912
import static java.util.Arrays.asList;
@@ -21,6 +24,7 @@ public void registration_works() {
2124
DataLoader<Object, Object> dlA = newDataLoader("a", identityBatchLoader);
2225
DataLoader<Object, Object> dlB = newDataLoader("b", identityBatchLoader);
2326
DataLoader<Object, Object> dlC = newDataLoader("c", identityBatchLoader);
27+
DataLoader<Object, Object> dlUnnamed = newDataLoader(identityBatchLoader);
2428

2529
DataLoaderRegistry registry = new DataLoaderRegistry();
2630

@@ -40,7 +44,7 @@ public void registration_works() {
4044

4145
// and unregister (fluently)
4246
DataLoaderRegistry dlR = registry.unregister("c");
43-
assertThat(dlR,equalTo(registry));
47+
assertThat(dlR, equalTo(registry));
4448

4549
assertThat(registry.getDataLoaders(), equalTo(asList(dlA, dlB)));
4650

@@ -49,12 +53,24 @@ public void registration_works() {
4953
assertThat(readDL, sameInstance(dlA));
5054

5155
assertThat(registry.getKeys(), hasItems("a", "b"));
56+
57+
58+
// named registry
59+
registry = new DataLoaderRegistry();
60+
registry.register(dlA);
61+
assertThat(registry.getDataLoaders(), equalTo(List.of(dlA)));
62+
63+
try {
64+
registry.register(dlUnnamed);
65+
Assertions.fail("Should have thrown an exception");
66+
} catch (DataLoaderAssertionException ignored) {
67+
}
5268
}
5369

5470
@Test
5571
public void registries_can_be_combined() {
5672

57-
DataLoader<Object, Object> dlA = newDataLoader("a",identityBatchLoader);
73+
DataLoader<Object, Object> dlA = newDataLoader("a", identityBatchLoader);
5874
DataLoader<Object, Object> dlB = newDataLoader("b", identityBatchLoader);
5975
DataLoader<Object, Object> dlC = newDataLoader("c", identityBatchLoader);
6076
DataLoader<Object, Object> dlD = newDataLoader("d", identityBatchLoader);
@@ -120,7 +136,7 @@ public void computeIfAbsent_creates_a_data_loader_if_there_was_no_value_at_key()
120136

121137
DataLoaderRegistry registry = new DataLoaderRegistry();
122138

123-
DataLoader<Object, Object> dlA = newDataLoader("a",identityBatchLoader);
139+
DataLoader<Object, Object> dlA = newDataLoader("a", identityBatchLoader);
124140
DataLoader<Object, Object> registered = registry.computeIfAbsent("a", (key) -> dlA);
125141

126142
assertThat(registered, equalTo(dlA));
@@ -133,7 +149,7 @@ public void computeIfAbsent_returns_an_existing_data_loader_if_there_was_a_value
133149

134150
DataLoaderRegistry registry = new DataLoaderRegistry();
135151

136-
DataLoader<Object, Object> dlA = newDataLoader("a",identityBatchLoader);
152+
DataLoader<Object, Object> dlA = newDataLoader("a", identityBatchLoader);
137153
registry.computeIfAbsent("a", (key) -> dlA);
138154

139155
// register again at same key

0 commit comments

Comments
 (0)