mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-12-03 11:41:37 +00:00
Service lookups answer from an index the catalog builds once per type instead of filtering every registration per call; a type's @Service annotation is read once
This commit is contained in:
1 parent
d7de5939d7
commit
b39c750e26
3 files changed
+56
-20
No files matched your search
@@ -5,11 +5,13 @@ import com.volmit.service.Providers;
|
|||||||
import com.volmit.service.Service;
|
import com.volmit.service.Service;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/** Frozen registrations. Lists use lexical feature/layer/kind ID order, and validators, lifecycle hooks, service
|
/** Frozen registrations. Lists use lexical feature/layer/kind ID order, and validators, lifecycle hooks, service
|
||||||
* providers are in module id order, each module's in the order it registered them, so
|
* providers are in module id order, each module's in the order it registered them, so
|
||||||
@@ -17,6 +19,19 @@ import java.util.function.Function;
|
|||||||
* concurrency contracts.
|
* concurrency contracts.
|
||||||
*/
|
*/
|
||||||
public final class FeatureCatalog {
|
public final class FeatureCatalog {
|
||||||
|
/** Services are asked for on the generation hot path, so each type's annotation is read once. */
|
||||||
|
private static final ClassValue<Providers> SERVICE_PROVIDERS = new ClassValue<>() {
|
||||||
|
@Override
|
||||||
|
protected Providers computeValue(Class<?> type) {
|
||||||
|
var service = type.isInterface() ? type.getAnnotation(Service.class) : null;
|
||||||
|
if (service == null) {
|
||||||
|
throw new IllegalArgumentException(type.getName() + " is not a service: services are interfaces marked @"
|
||||||
|
+ Service.class.getName());
|
||||||
|
}
|
||||||
|
return service.value();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
private final Map<Class<?>, FeatureDescriptor<?>> settings;
|
private final Map<Class<?>, FeatureDescriptor<?>> settings;
|
||||||
private final Map<Class<?>, ObjectRegistration<?>> objects;
|
private final Map<Class<?>, ObjectRegistration<?>> objects;
|
||||||
private final Map<String, MantleRegistration<?>> mantle;
|
private final Map<String, MantleRegistration<?>> mantle;
|
||||||
@@ -24,7 +39,8 @@ public final class FeatureCatalog {
|
|||||||
private final List<HeldTypeRegistration> heldTypes;
|
private final List<HeldTypeRegistration> heldTypes;
|
||||||
private final List<ValidatorRegistration> validators;
|
private final List<ValidatorRegistration> validators;
|
||||||
private final List<LifecycleRegistration> lifecycles;
|
private final List<LifecycleRegistration> lifecycles;
|
||||||
private final List<ServiceRegistration> services;
|
private final Map<Class<?>, List<ServiceRegistration>> registrationsByService;
|
||||||
|
private final Map<Class<?>, List<?>> providersByService;
|
||||||
|
|
||||||
FeatureCatalog(
|
FeatureCatalog(
|
||||||
Map<Class<?>, FeatureDescriptor<?>> settings,
|
Map<Class<?>, FeatureDescriptor<?>> settings,
|
||||||
@@ -42,7 +58,8 @@ public final class FeatureCatalog {
|
|||||||
this.heldTypes = sorted(heldTypes, registration -> registration.type().getName());
|
this.heldTypes = sorted(heldTypes, registration -> registration.type().getName());
|
||||||
this.validators = inModuleOrder(validators, ValidatorRegistration::module);
|
this.validators = inModuleOrder(validators, ValidatorRegistration::module);
|
||||||
this.lifecycles = inModuleOrder(lifecycles, LifecycleRegistration::module);
|
this.lifecycles = inModuleOrder(lifecycles, LifecycleRegistration::module);
|
||||||
this.services = inModuleOrder(services, ServiceRegistration::module);
|
this.registrationsByService = byService(inModuleOrder(services, ServiceRegistration::module));
|
||||||
|
this.providersByService = providersByService(registrationsByService);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Settings types that activate stages; the host decodes them from pack JSON. */
|
/** Settings types that activate stages; the host decodes them from pack JSON. */
|
||||||
@@ -81,39 +98,53 @@ public final class FeatureCatalog {
|
|||||||
/** Every provider of {@code service}, an interface marked {@link Service}, in module id order and each module's in the
|
/** Every provider of {@code service}, an interface marked {@link Service}, in module id order and each module's in the
|
||||||
* order it registered them; empty when no module provides it.
|
* order it registered them; empty when no module provides it.
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("unchecked") // ModuleRegistry.registerService checked that every provider implements its service.
|
||||||
public <S> List<S> services(Class<S> service) {
|
public <S> List<S> services(Class<S> service) {
|
||||||
requireService(service);
|
var providers = providersByService.get(service);
|
||||||
return services.stream()
|
if (providers == null) {
|
||||||
.filter(registration -> registration.service() == service)
|
providersOf(service);
|
||||||
.map(registration -> service.cast(registration.provider()))
|
return List.of();
|
||||||
.toList();
|
}
|
||||||
|
return (List<S>) providers;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Every provider of {@code service} with the module that registered it, in the order of {@link #services}, so the host
|
/** Every provider of {@code service} with the module that registered it, in the order of {@link #services}, so the host
|
||||||
* can name the module whose provider failed.
|
* can name the module whose provider failed.
|
||||||
*/
|
*/
|
||||||
public List<ServiceRegistration> registrations(Class<?> service) {
|
public List<ServiceRegistration> registrations(Class<?> service) {
|
||||||
requireService(service);
|
var registrations = registrationsByService.get(service);
|
||||||
return services.stream().filter(registration -> registration.service() == service).toList();
|
if (registrations == null) {
|
||||||
|
providersOf(service);
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
return registrations;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The one provider of {@code service}, a service whose providers are {@link Providers#ONE}, if a module provides it. */
|
/** The one provider of {@code service}, a service whose providers are {@link Providers#ONE}, if a module provides it. */
|
||||||
public <S> Optional<S> service(Class<S> service) {
|
public <S> Optional<S> service(Class<S> service) {
|
||||||
if (requireService(service).value() != Providers.ONE) {
|
if (providersOf(service) != Providers.ONE) {
|
||||||
throw new IllegalArgumentException("Service " + service.getName() + " may have several providers; ask for all of"
|
throw new IllegalArgumentException("Service " + service.getName() + " may have several providers; ask for all of"
|
||||||
+ " them with services(...)");
|
+ " them with services(...)");
|
||||||
}
|
}
|
||||||
return services(service).stream().findFirst();
|
var providers = services(service);
|
||||||
|
return providers.isEmpty() ? Optional.empty() : Optional.of(providers.getFirst());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The {@link Service} annotation of {@code type}, which must be an interface carrying one. */
|
/** How many modules may provide {@code service}, which must be an interface marked {@link Service}. */
|
||||||
static Service requireService(Class<?> type) {
|
static Providers providersOf(Class<?> service) {
|
||||||
var service = type.isInterface() ? type.getAnnotation(Service.class) : null;
|
return SERVICE_PROVIDERS.get(service);
|
||||||
if (service == null) {
|
}
|
||||||
throw new IllegalArgumentException(type.getName() + " is not a service: services are interfaces marked @"
|
|
||||||
+ Service.class.getName());
|
private static Map<Class<?>, List<ServiceRegistration>> byService(List<ServiceRegistration> inModuleOrder) {
|
||||||
}
|
return Map.copyOf(inModuleOrder.stream()
|
||||||
return service;
|
.collect(Collectors.groupingBy(ServiceRegistration::service, Collectors.toUnmodifiableList())));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<Class<?>, List<?>> providersByService(Map<Class<?>, List<ServiceRegistration>> registrationsByService) {
|
||||||
|
var providers = new HashMap<Class<?>, List<?>>();
|
||||||
|
registrationsByService.forEach((service, registrations) ->
|
||||||
|
providers.put(service, registrations.stream().map(ServiceRegistration::provider).toList()));
|
||||||
|
return Map.copyOf(providers);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static <T> List<T> inModuleOrder(List<T> registrations, Function<? super T, DiscoveredModule> module) {
|
private static <T> List<T> inModuleOrder(List<T> registrations, Function<? super T, DiscoveredModule> module) {
|
||||||
|
|||||||
@@ -161,7 +161,7 @@ public final class ModuleRegistry implements FeatureRegistry {
|
|||||||
Objects.requireNonNull(service, "service");
|
Objects.requireNonNull(service, "service");
|
||||||
Objects.requireNonNull(provider, "provider");
|
Objects.requireNonNull(provider, "provider");
|
||||||
requireRegistering("Provider of " + service.getName());
|
requireRegistering("Provider of " + service.getName());
|
||||||
var providers = FeatureCatalog.requireService(service).value();
|
var providers = FeatureCatalog.providersOf(service);
|
||||||
if (!service.isInstance(provider)) {
|
if (!service.isInstance(provider)) {
|
||||||
throw new IllegalArgumentException("Provider " + provider + " of module " + registering + " does not implement "
|
throw new IllegalArgumentException("Provider " + provider + " of module " + registering + " does not implement "
|
||||||
+ service.getName());
|
+ service.getName());
|
||||||
|
|||||||
@@ -395,6 +395,11 @@ class ModuleRegistryTest {
|
|||||||
var several = assertThrows(IllegalArgumentException.class, () -> catalog.service(Greeting.class));
|
var several = assertThrows(IllegalArgumentException.class, () -> catalog.service(Greeting.class));
|
||||||
assertTrue(several.getMessage().contains("services(...)"), several.getMessage());
|
assertTrue(several.getMessage().contains("services(...)"), several.getMessage());
|
||||||
assertThrows(IllegalArgumentException.class, () -> catalog.services(NotAService.class));
|
assertThrows(IllegalArgumentException.class, () -> catalog.services(NotAService.class));
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> catalog.registrations(NotAService.class), "asking again still refuses it");
|
||||||
|
assertEquals(List.of(alphaFirst, alphaSecond, zeta),
|
||||||
|
catalog.registrations(Greeting.class).stream().map(ServiceRegistration::provider).toList());
|
||||||
|
assertEquals(List.of(), catalog.registrations(Clock.class));
|
||||||
|
assertThrows(UnsupportedOperationException.class, () -> catalog.services(Greeting.class).clear(), "the catalog is frozen");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in new issue
Block a user