@Test public void testValidArguments() { DefaultSizeOfEngineProviderConfiguration configuration = new DefaultSizeOfEngineProviderConfiguration(10l, MemoryUnit.B, 10l); assertThat(configuration.getMaxObjectGraphSize(), equalTo(10l)); assertThat(configuration.getMaxObjectSize(), equalTo(10l)); assertThat(configuration.getUnit(), equalTo(MemoryUnit.B)); } }
@Override public SizeOfEngineProvider create(ServiceCreationConfiguration<SizeOfEngineProvider> configuration) { long maxTraversals = DefaultSizeOfEngineConfiguration.DEFAULT_OBJECT_GRAPH_SIZE; long maxSize = DefaultSizeOfEngineConfiguration.DEFAULT_MAX_OBJECT_SIZE; if(configuration != null) { DefaultSizeOfEngineProviderConfiguration sizeOfEngineConfiguration = (DefaultSizeOfEngineProviderConfiguration)configuration; maxTraversals = sizeOfEngineConfiguration.getMaxObjectGraphSize(); maxSize = sizeOfEngineConfiguration.getUnit().toBytes(sizeOfEngineConfiguration.getMaxObjectSize()); } return new DefaultSizeOfEngineProvider(maxTraversals, maxSize); }
/** * Adds a default {@link SizeOfEngine} configuration, that limits the max object graph to * size, to the returned builder. * * @param size the max object graph size * @return a new builder with the added configuration */ public CacheManagerBuilder<T> withDefaultSizeOfMaxObjectGraph(long size) { DefaultSizeOfEngineProviderConfiguration configuration = configBuilder.findServiceByClass(DefaultSizeOfEngineProviderConfiguration.class); if (configuration == null) { return new CacheManagerBuilder<>(this, configBuilder.addService(new DefaultSizeOfEngineProviderConfiguration(DEFAULT_MAX_OBJECT_SIZE, DEFAULT_UNIT, size))); } else { ConfigurationBuilder builder = configBuilder.removeService(configuration); return new CacheManagerBuilder<>(this, builder.addService(new DefaultSizeOfEngineProviderConfiguration(configuration .getMaxObjectSize(), configuration.getUnit(), size))); } }
/** * Adds a default {@link SizeOfEngine} configuration, that limits the max object size, to * the returned builder. * * @param size the max object size * @param unit the max object size unit * @return a new builder with the added configuration */ public CacheManagerBuilder<T> withDefaultSizeOfMaxObjectSize(long size, MemoryUnit unit) { DefaultSizeOfEngineProviderConfiguration configuration = configBuilder.findServiceByClass(DefaultSizeOfEngineProviderConfiguration.class); if (configuration == null) { return new CacheManagerBuilder<>(this, configBuilder.addService(new DefaultSizeOfEngineProviderConfiguration(size, unit, DEFAULT_OBJECT_GRAPH_SIZE))); } else { ConfigurationBuilder builder = configBuilder.removeService(configuration); return new CacheManagerBuilder<>(this, builder.addService(new DefaultSizeOfEngineProviderConfiguration(size, unit, configuration .getMaxObjectGraphSize()))); } }
@Test public void parseServiceCreationConfiguration() throws SAXException, JAXBException, ParserConfigurationException, IOException, ClassNotFoundException { Configuration xmlConfig = new XmlConfiguration(getClass().getResource("/configs/sizeof-engine.xml")); assertThat(xmlConfig.getServiceCreationConfigurations()).hasSize(1); ServiceCreationConfiguration<?> configuration = xmlConfig.getServiceCreationConfigurations().iterator().next(); assertThat(configuration).isExactlyInstanceOf(DefaultSizeOfEngineProviderConfiguration.class); DefaultSizeOfEngineProviderConfiguration sizeOfEngineProviderConfig = (DefaultSizeOfEngineProviderConfiguration) configuration; assertThat(sizeOfEngineProviderConfig.getMaxObjectGraphSize()).isEqualTo(200); assertThat(sizeOfEngineProviderConfig.getMaxObjectSize()).isEqualTo(100000); }
/** * Adds or updates the {@link DefaultSizeOfEngineProviderConfiguration} with the specified object graph maximum size to the configured * builder. * <p> * {@link SizeOfEngine} is what enables the heap tier to be sized in {@link MemoryUnit}. * * @param size the maximum graph size * @return a new builder with the added / updated configuration */ public UserManagedCacheBuilder<K, V, T> withSizeOfMaxObjectGraph(long size) { UserManagedCacheBuilder<K, V, T> otherBuilder = new UserManagedCacheBuilder<>(this); removeAnySizeOfEngine(otherBuilder); otherBuilder.objectGraphSize = size; otherBuilder.serviceCreationConfigurations.add(new DefaultSizeOfEngineProviderConfiguration(otherBuilder.maxObjectSize, otherBuilder.sizeOfUnit, otherBuilder.objectGraphSize)); return otherBuilder; }
@Test public void testCacheManagerDefaultObjectSize() throws Exception { final URL resource = XmlConfigurationTest.class.getResource("/configs/sizeof-engine-cm-defaults-two.xml"); XmlConfiguration xmlConfig = new XmlConfiguration(new XmlConfiguration(resource)); DefaultSizeOfEngineProviderConfiguration sizeOfEngineProviderConfig = findSingletonAmongst(DefaultSizeOfEngineProviderConfiguration.class, xmlConfig.getServiceCreationConfigurations()); assertThat(sizeOfEngineProviderConfig, notNullValue()); assertThat(sizeOfEngineProviderConfig.getMaxObjectGraphSize(), is(200L)); assertThat(sizeOfEngineProviderConfig.getMaxObjectSize(), is(Long.MAX_VALUE)); }
@Override public SizeOfEngineProvider create(ServiceCreationConfiguration<SizeOfEngineProvider> configuration) { long maxTraversals = DefaultSizeOfEngineConfiguration.DEFAULT_OBJECT_GRAPH_SIZE; long maxSize = DefaultSizeOfEngineConfiguration.DEFAULT_MAX_OBJECT_SIZE; if(configuration != null) { DefaultSizeOfEngineProviderConfiguration sizeOfEngineConfiguration = (DefaultSizeOfEngineProviderConfiguration)configuration; maxTraversals = sizeOfEngineConfiguration.getMaxObjectGraphSize(); maxSize = sizeOfEngineConfiguration.getUnit().toBytes(sizeOfEngineConfiguration.getMaxObjectSize()); } return new DefaultSizeOfEngineProvider(maxTraversals, maxSize); }
/** * Adds a default {@link SizeOfEngine} configuration, that limits the max object graph to * size, to the returned builder. * * @param size the max object graph size * @return a new builder with the added configuration */ public CacheManagerBuilder<T> withDefaultSizeOfMaxObjectGraph(long size) { DefaultSizeOfEngineProviderConfiguration configuration = configBuilder.findServiceByClass(DefaultSizeOfEngineProviderConfiguration.class); if (configuration == null) { return new CacheManagerBuilder<>(this, configBuilder.addService(new DefaultSizeOfEngineProviderConfiguration(DEFAULT_MAX_OBJECT_SIZE, DEFAULT_UNIT, size))); } else { ConfigurationBuilder builder = configBuilder.removeService(configuration); return new CacheManagerBuilder<>(this, builder.addService(new DefaultSizeOfEngineProviderConfiguration(configuration .getMaxObjectSize(), configuration.getUnit(), size))); } }
/** * Adds a default {@link SizeOfEngine} configuration, that limits the max object size, to * the returned builder. * * @param size the max object size * @param unit the max object size unit * @return a new builder with the added configuration */ public CacheManagerBuilder<T> withDefaultSizeOfMaxObjectSize(long size, MemoryUnit unit) { DefaultSizeOfEngineProviderConfiguration configuration = configBuilder.findServiceByClass(DefaultSizeOfEngineProviderConfiguration.class); if (configuration == null) { return new CacheManagerBuilder<>(this, configBuilder.addService(new DefaultSizeOfEngineProviderConfiguration(size, unit, DEFAULT_OBJECT_GRAPH_SIZE))); } else { ConfigurationBuilder builder = configBuilder.removeService(configuration); return new CacheManagerBuilder<>(this, builder.addService(new DefaultSizeOfEngineProviderConfiguration(size, unit, configuration .getMaxObjectGraphSize()))); } }
/** * Adds or updates the {@link DefaultSizeOfEngineProviderConfiguration} with the specified maximum mapping size to the configured * builder. * <p> * {@link SizeOfEngine} is what enables the heap tier to be sized in {@link MemoryUnit}. * * @param size the maximum mapping size * @param unit the memory unit * @return a new builder with the added / updated configuration */ public UserManagedCacheBuilder<K, V, T> withSizeOfMaxObjectSize(long size, MemoryUnit unit) { UserManagedCacheBuilder<K, V, T> otherBuilder = new UserManagedCacheBuilder<>(this); removeAnySizeOfEngine(otherBuilder); otherBuilder.maxObjectSize = size; otherBuilder.sizeOfUnit = unit; otherBuilder.serviceCreationConfigurations.add(new DefaultSizeOfEngineProviderConfiguration(otherBuilder.maxObjectSize, otherBuilder.sizeOfUnit, otherBuilder.objectGraphSize)); return otherBuilder; }
public DefaultSizeOfEngineProviderConfigurationParser() { super(DefaultSizeOfEngineProviderConfiguration.class, ConfigType::getHeapStore, ConfigType::setHeapStore, config -> { SizeOfEngineLimits sizeOfEngineLimits = new SizeOfEngineLimits(config); return new DefaultSizeOfEngineProviderConfiguration(sizeOfEngineLimits.getMaxObjectSize(), sizeOfEngineLimits.getUnit(), sizeOfEngineLimits.getMaxObjectGraphSize()); }, config -> new SizeofType() .withMaxObjectGraphSize(new SizeofType.MaxObjectGraphSize().withValue(BigInteger.valueOf(config.getMaxObjectGraphSize()))) .withMaxObjectSize(new MemoryType() .withValue(BigInteger.valueOf(config.getMaxObjectSize())) .withUnit(MemoryUnit.fromValue(config.getUnit().toString())) ) ); } }
@Test public void testCacheManagerDefaultObjectGraphSize() throws Exception { final URL resource = XmlConfigurationTest.class.getResource("/configs/sizeof-engine-cm-defaults-one.xml"); XmlConfiguration xmlConfig = new XmlConfiguration(new XmlConfiguration(resource)); DefaultSizeOfEngineProviderConfiguration sizeOfEngineProviderConfig = findSingletonAmongst(DefaultSizeOfEngineProviderConfiguration.class, xmlConfig.getServiceCreationConfigurations()); assertThat(sizeOfEngineProviderConfig, notNullValue()); assertThat(sizeOfEngineProviderConfig.getMaxObjectGraphSize(), is(1000L)); assertThat(sizeOfEngineProviderConfig.getMaxObjectSize(), is(100000L)); }
@Test public void testIllegalMaxObjectGraphSizeArgument() { try { new DefaultSizeOfEngineProviderConfiguration(1l, MemoryUnit.B, 0); fail(); } catch (Exception illegalArgument) { assertThat(illegalArgument, instanceOf(IllegalArgumentException.class)); assertThat(illegalArgument.getMessage(), equalTo("SizeOfEngine cannot take non-positive arguments.")); } }
public DefaultSizeOfEngineProviderConfigurationParser() { super(DefaultSizeOfEngineProviderConfiguration.class, ConfigType::getHeapStore, ConfigType::setHeapStore, config -> { SizeOfEngineLimits sizeOfEngineLimits = new SizeOfEngineLimits(config); return new DefaultSizeOfEngineProviderConfiguration(sizeOfEngineLimits.getMaxObjectSize(), sizeOfEngineLimits.getUnit(), sizeOfEngineLimits.getMaxObjectGraphSize()); }, config -> new SizeofType() .withMaxObjectGraphSize(new SizeofType.MaxObjectGraphSize().withValue(BigInteger.valueOf(config.getMaxObjectGraphSize()))) .withMaxObjectSize(new MemoryType() .withValue(BigInteger.valueOf(config.getMaxObjectSize())) .withUnit(MemoryUnit.fromValue(config.getUnit().toString())) ) ); } }
@Test public void testSizeOfEngineLimits() throws Exception { final URL resource = XmlConfigurationTest.class.getResource("/configs/sizeof-engine.xml"); XmlConfiguration xmlConfig = new XmlConfiguration(new XmlConfiguration(resource)); DefaultSizeOfEngineProviderConfiguration sizeOfEngineProviderConfig = findSingletonAmongst(DefaultSizeOfEngineProviderConfiguration.class, xmlConfig.getServiceCreationConfigurations()); assertThat(sizeOfEngineProviderConfig, notNullValue()); assertEquals(sizeOfEngineProviderConfig.getMaxObjectGraphSize(), 200); assertEquals(sizeOfEngineProviderConfig.getMaxObjectSize(), 100000); CacheConfiguration<?, ?> cacheConfig = xmlConfig.getCacheConfigurations().get("usesDefaultSizeOfEngine"); DefaultSizeOfEngineConfiguration sizeOfEngineConfig = findSingletonAmongst(DefaultSizeOfEngineConfiguration.class, cacheConfig.getServiceConfigurations()); assertThat(sizeOfEngineConfig, nullValue()); CacheConfiguration<?, ?> cacheConfig1 = xmlConfig.getCacheConfigurations().get("usesConfiguredInCache"); DefaultSizeOfEngineConfiguration sizeOfEngineConfig1 = findSingletonAmongst(DefaultSizeOfEngineConfiguration.class, cacheConfig1.getServiceConfigurations()); assertThat(sizeOfEngineConfig1, notNullValue()); assertEquals(sizeOfEngineConfig1.getMaxObjectGraphSize(), 500); assertEquals(sizeOfEngineConfig1.getMaxObjectSize(), 200000); CacheConfiguration<?, ?> cacheConfig2 = xmlConfig.getCacheConfigurations().get("usesPartialOneConfiguredInCache"); DefaultSizeOfEngineConfiguration sizeOfEngineConfig2 = findSingletonAmongst(DefaultSizeOfEngineConfiguration.class, cacheConfig2.getServiceConfigurations()); assertThat(sizeOfEngineConfig2, notNullValue()); assertThat(sizeOfEngineConfig2.getMaxObjectGraphSize(), is(500L)); assertThat(sizeOfEngineConfig2.getMaxObjectSize(), is(Long.MAX_VALUE)); CacheConfiguration<?, ?> cacheConfig3 = xmlConfig.getCacheConfigurations().get("usesPartialTwoConfiguredInCache"); DefaultSizeOfEngineConfiguration sizeOfEngineConfig3 = findSingletonAmongst(DefaultSizeOfEngineConfiguration.class, cacheConfig3.getServiceConfigurations()); assertThat(sizeOfEngineConfig3, notNullValue()); assertThat(sizeOfEngineConfig3.getMaxObjectGraphSize(), is(1000L)); assertThat(sizeOfEngineConfig3.getMaxObjectSize(), is(200000L)); }
@Test public void testIllegalMaxObjectSizeArgument() { try { new DefaultSizeOfEngineProviderConfiguration(0, MemoryUnit.B, 1l); fail(); } catch (Exception illegalArgument) { assertThat(illegalArgument, instanceOf(IllegalArgumentException.class)); assertThat(illegalArgument.getMessage(), equalTo("SizeOfEngine cannot take non-positive arguments.")); } }
@Test public void unparseServiceCreationConfiguration() { ConfigType configType = new ConfigType(); Configuration config = ConfigurationBuilder.newConfigurationBuilder() .addService(new DefaultSizeOfEngineProviderConfiguration(123, MemoryUnit.MB, 987)).build(); configType = new DefaultSizeOfEngineProviderConfigurationParser().unparseServiceCreationConfiguration(config, configType); SizeofType heapStore = configType.getHeapStore(); assertThat(heapStore.getMaxObjectGraphSize().getValue()).isEqualTo(987); assertThat(heapStore.getMaxObjectSize().getValue()).isEqualTo(123); assertThat(heapStore.getMaxObjectSize().getUnit().value()).isEqualTo("MB"); } }
/** * Adds or updates the {@link DefaultSizeOfEngineProviderConfiguration} with the specified object graph maximum size to the configured * builder. * <p> * {@link SizeOfEngine} is what enables the heap tier to be sized in {@link MemoryUnit}. * * @param size the maximum graph size * @return a new builder with the added / updated configuration */ public UserManagedCacheBuilder<K, V, T> withSizeOfMaxObjectGraph(long size) { UserManagedCacheBuilder<K, V, T> otherBuilder = new UserManagedCacheBuilder<>(this); removeAnySizeOfEngine(otherBuilder); otherBuilder.objectGraphSize = size; otherBuilder.serviceCreationConfigurations.add(new DefaultSizeOfEngineProviderConfiguration(otherBuilder.maxObjectSize, otherBuilder.sizeOfUnit, otherBuilder.objectGraphSize)); return otherBuilder; }
/** * Adds or updates the {@link DefaultSizeOfEngineProviderConfiguration} with the specified maximum mapping size to the configured * builder. * <p> * {@link SizeOfEngine} is what enables the heap tier to be sized in {@link MemoryUnit}. * * @param size the maximum mapping size * @param unit the memory unit * @return a new builder with the added / updated configuration */ public UserManagedCacheBuilder<K, V, T> withSizeOfMaxObjectSize(long size, MemoryUnit unit) { UserManagedCacheBuilder<K, V, T> otherBuilder = new UserManagedCacheBuilder<>(this); removeAnySizeOfEngine(otherBuilder); otherBuilder.maxObjectSize = size; otherBuilder.sizeOfUnit = unit; otherBuilder.serviceCreationConfigurations.add(new DefaultSizeOfEngineProviderConfiguration(otherBuilder.maxObjectSize, otherBuilder.sizeOfUnit, otherBuilder.objectGraphSize)); return otherBuilder; }