Class DataLoaderRegistry
- java.lang.Object
-
- org.dataloader.DataLoaderRegistry
-
- Direct Known Subclasses:
ScheduledDataLoaderRegistry
@PublicApi @NullMarked public class DataLoaderRegistry extends java.lang.Object
This allows data loaders to be registered together into a single place, so they can be dispatched as one. It also allows you to retrieve data loaders by name from a central place.Notes on
DataLoaderInstrumentation: ADataLoaderRegistrycan have an instrumentation associated with it. As eachDataLoaderis added to the registry, theDataLoaderInstrumentationof the registry is applied to thatDataLoader.The
DataLoaderis changed and hence the object in the registry is not the same one as was originally registered. So you MUST get access to theDataLoaderviagetDataLoader(String)methods and not use the originalDataLoaderobject.If the
DataLoaderhas noDataLoaderInstrumentationthen the registry one is added to it. If it does have one already then aChainedDataLoaderInstrumentationis created with the registryDataLoaderInstrumentationin it first and then any otherDataLoaderInstrumentations added after that. If the registryDataLoaderInstrumentationinstance andDataLoaderDataLoaderInstrumentationinstance are the same object, then nothing is changed, since the same instrumentation code is being run.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDataLoaderRegistry.Builder
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Map<java.lang.String,DataLoader<?,?>>dataLoadersprotected @Nullable DataLoaderInstrumentationinstrumentation
-
Constructor Summary
Constructors Modifier Constructor Description DataLoaderRegistry()protectedDataLoaderRegistry(java.util.Map<java.lang.String,DataLoader<?,?>> dataLoaders, @Nullable DataLoaderInstrumentation instrumentation)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description DataLoaderRegistrycombine(DataLoaderRegistry registry)This will combine all the current data loaders in this registry and all the data loaders from the specified registry and return a new combined registry<K,V>
DataLoader<K,V>computeIfAbsent(java.lang.String key, java.util.function.Function<java.lang.String,DataLoader<?,?>> mappingFunction)Computes a data loader if absent or return it if it was already registered at that key.voiddispatchAll()This will be calledDataLoader.dispatch()on each of the registeredDataLoadersintdispatchAllWithCount()Similar todispatchAll(), this callsDataLoader.dispatch()on each of the registeredDataLoaders, but returns the number of dispatches.intdispatchDepth()<K,V>
DataLoader<K,V>getDataLoader(java.lang.String key)Returns the dataloader that was registered under the specified keyjava.util.List<DataLoader<?,?>>getDataLoaders()java.util.Map<java.lang.String,DataLoader<?,?>>getDataLoadersMap()@Nullable DataLoaderInstrumentationgetInstrumentation()java.util.Set<java.lang.String>getKeys()StatisticsgetStatistics()static DataLoaderRegistry.BuildernewRegistry()DataLoaderRegistryregister(java.lang.String key, DataLoader<?,?> dataLoader)This will register a newDataLoaderDataLoaderRegistryregister(DataLoader<?,?> dataLoader)This will register a new named dataloader.<K,V>
DataLoader<K,V>registerAndGet(java.lang.String key, DataLoader<?,?> dataLoader)This will register a newDataLoaderand then return it.DataLoaderRegistryunregister(java.lang.String key)This will unregister a new dataloader
-
-
-
Field Detail
-
dataLoaders
protected final java.util.Map<java.lang.String,DataLoader<?,?>> dataLoaders
-
instrumentation
protected final @Nullable DataLoaderInstrumentation instrumentation
-
-
Constructor Detail
-
DataLoaderRegistry
public DataLoaderRegistry()
-
DataLoaderRegistry
protected DataLoaderRegistry(java.util.Map<java.lang.String,DataLoader<?,?>> dataLoaders, @Nullable DataLoaderInstrumentation instrumentation)
-
-
Method Detail
-
getInstrumentation
public @Nullable DataLoaderInstrumentation getInstrumentation()
- Returns:
- the
DataLoaderInstrumentationassociated with this registry which can be null
-
register
public DataLoaderRegistry register(DataLoader<?,?> dataLoader)
This will register a new named dataloader. TheDataLoadermust be named something and cannot have a null name.Note: Registration can change the data loader instance since it might get an
DataLoaderInstrumentationapplied to it. So theDataLoaderinstance your read viagetDataLoader(String)might not be the same object that was registered.- Parameters:
dataLoader- the named data loader to register- Returns:
- this registry
-
register
public DataLoaderRegistry register(java.lang.String key, DataLoader<?,?> dataLoader)
This will register a newDataLoaderNote: Registration can change the data loader instance since it might get an
DataLoaderInstrumentationapplied to it. So theDataLoaderinstance your read viagetDataLoader(String)might not be the same object that was registered.- Parameters:
key- the key to put the data loader underdataLoader- the data loader to register- Returns:
- this registry
-
registerAndGet
public <K,V> DataLoader<K,V> registerAndGet(java.lang.String key, DataLoader<?,?> dataLoader)
This will register a newDataLoaderand then return it.Note: Registration can change the data loader instance since it might get an
DataLoaderInstrumentationapplied to it. So theDataLoaderinstance your read viagetDataLoader(String)might not be the same object that was registered.- Parameters:
key- the key to put the data loader underdataLoader- the data loader to register- Returns:
- the data loader instance that was registered
-
computeIfAbsent
public <K,V> DataLoader<K,V> computeIfAbsent(java.lang.String key, java.util.function.Function<java.lang.String,DataLoader<?,?>> mappingFunction)
Computes a data loader if absent or return it if it was already registered at that key.Note: The entire method invocation is performed atomically, so the function is applied at most once per key.
Note: Registration can change the data loader instance since it might get an
DataLoaderInstrumentationapplied to it. So theDataLoaderinstance your read viagetDataLoader(String)might not be the same object that was registered.- Type Parameters:
K- the type of keysV- the type of values- Parameters:
key- the key of the data loadermappingFunction- the function to compute a data loader- Returns:
- a data loader
-
combine
public DataLoaderRegistry combine(DataLoaderRegistry registry)
This will combine all the current data loaders in this registry and all the data loaders from the specified registry and return a new combined registry- Parameters:
registry- the registry to combine into this registry- Returns:
- a new combined registry
-
getDataLoaders
public java.util.List<DataLoader<?,?>> getDataLoaders()
- Returns:
- the currently registered data loaders
-
getDataLoadersMap
public java.util.Map<java.lang.String,DataLoader<?,?>> getDataLoadersMap()
- Returns:
- the currently registered data loaders as a map
-
unregister
public DataLoaderRegistry unregister(java.lang.String key)
This will unregister a new dataloader- Parameters:
key- the key of the data loader to unregister- Returns:
- this registry
-
getDataLoader
public <K,V> DataLoader<K,V> getDataLoader(java.lang.String key)
Returns the dataloader that was registered under the specified key- Type Parameters:
K- the type of keysV- the type of values- Parameters:
key- the key of the data loader- Returns:
- a data loader or null if its not present
-
getKeys
public java.util.Set<java.lang.String> getKeys()
- Returns:
- the keys of the data loaders in this registry
-
dispatchAll
public void dispatchAll()
This will be calledDataLoader.dispatch()on each of the registeredDataLoaders
-
dispatchAllWithCount
public int dispatchAllWithCount()
Similar todispatchAll(), this callsDataLoader.dispatch()on each of the registeredDataLoaders, but returns the number of dispatches.- Returns:
- total number of entries that were dispatched from registered
DataLoaders.
-
dispatchDepth
public int dispatchDepth()
- Returns:
- The sum of all batched key loads that need to be dispatched from all registered
DataLoaders
-
getStatistics
public Statistics getStatistics()
- Returns:
- a combined set of statistics for all data loaders in this registry presented as the sum of all their statistics
-
newRegistry
public static DataLoaderRegistry.Builder newRegistry()
- Returns:
- A builder of
DataLoaderRegistrys
-
-