/** * Adds or updates the {@link DefaultSizeOfEngineConfiguration} 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 CacheConfigurationBuilder<K, V> withSizeOfMaxObjectGraph(long size) { return mapServiceConfiguration(DefaultSizeOfEngineConfiguration.class, existing -> ofNullable(existing) .map(e -> new DefaultSizeOfEngineConfiguration(e.getMaxObjectSize(), e.getUnit(), size)) .orElse(new DefaultSizeOfEngineConfiguration(DEFAULT_MAX_OBJECT_SIZE, DEFAULT_UNIT, size))); }
@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 or updates the {@link DefaultSizeOfEngineConfiguration} 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 CacheConfigurationBuilder<K, V> withSizeOfMaxObjectSize(long size, MemoryUnit unit) { return mapServiceConfiguration(DefaultSizeOfEngineConfiguration.class, existing -> ofNullable(existing) .map(e -> new DefaultSizeOfEngineConfiguration(size, unit, e.getMaxObjectGraphSize())) .orElse(new DefaultSizeOfEngineConfiguration(size, unit, DEFAULT_OBJECT_GRAPH_SIZE))); }
@Test public void testValidArguments() { DefaultSizeOfEngineConfiguration configuration = new DefaultSizeOfEngineConfiguration(10l, MemoryUnit.B, 10l); assertThat(configuration.getMaxObjectGraphSize(), equalTo(10l)); assertThat(configuration.getMaxObjectSize(), equalTo(10l)); assertThat(configuration.getUnit(), equalTo(MemoryUnit.B)); }
@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)); } }
@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)); }
@Override public SizeOfEngine createSizeOfEngine(ResourceUnit resourceUnit, ServiceConfiguration<?>... serviceConfigs) { boolean isByteSized = resourceUnit instanceof MemoryUnit; if(!isByteSized) { return new NoopSizeOfEngine(); // Noop Size of Engine } DefaultSizeOfEngineConfiguration config = ServiceUtils.findSingletonAmongst(DefaultSizeOfEngineConfiguration.class, (Object[]) serviceConfigs); if(config != null) { long maxSize = config.getUnit().toBytes(config.getMaxObjectSize()); return new DefaultSizeOfEngine(config.getMaxObjectGraphSize(), maxSize); } return new DefaultSizeOfEngine(maxObjectGraphSize, maxObjectSize); } }
/** * 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))); } }
@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)); }
/** * 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 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 testIllegalMaxObjectSizeArgument() { try { new DefaultSizeOfEngineConfiguration(0, MemoryUnit.B, 1l); fail(); } catch (Exception illegalArgument) { assertThat(illegalArgument, instanceOf(IllegalArgumentException.class)); assertThat(illegalArgument.getMessage(), equalTo("ObjectGraphSize/ObjectSize can only accept positive values.")); } }
public DefaultSizeOfEngineConfigurationParser() { super(DefaultSizeOfEngineConfiguration.class, CacheTemplate::heapStoreSettings, config -> new DefaultSizeOfEngineConfiguration(config.getMaxObjectSize(), config.getUnit(), config.getMaxObjectGraphSize()), CacheType::getHeapStoreSettings, CacheType::setHeapStoreSettings, 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())) )); } }
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 testSizeOf() { CacheConfigurationBuilder<String, String> builder = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class, heap(10)); builder = builder.withSizeOfMaxObjectSize(10, MemoryUnit.B).withSizeOfMaxObjectGraph(100); CacheConfiguration<String, String> configuration = builder.build(); DefaultSizeOfEngineConfiguration sizeOfEngineConfiguration = ServiceUtils.findSingletonAmongst(DefaultSizeOfEngineConfiguration.class, configuration.getServiceConfigurations()); assertThat(sizeOfEngineConfiguration, notNullValue()); assertEquals(sizeOfEngineConfiguration.getMaxObjectSize(), 10); assertEquals(sizeOfEngineConfiguration.getUnit(), MemoryUnit.B); assertEquals(sizeOfEngineConfiguration.getMaxObjectGraphSize(), 100); builder = builder.withSizeOfMaxObjectGraph(1000); configuration = builder.build(); sizeOfEngineConfiguration = ServiceUtils.findSingletonAmongst(DefaultSizeOfEngineConfiguration.class, configuration.getServiceConfigurations()); assertEquals(sizeOfEngineConfiguration.getMaxObjectGraphSize(), 1000); }
@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)); }
/** * 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; }
@Test public void testIllegalMaxObjectGraphSizeArgument() { try { new DefaultSizeOfEngineConfiguration(1l, MemoryUnit.B, 0); fail(); } catch (Exception illegalArgument) { assertThat(illegalArgument, instanceOf(IllegalArgumentException.class)); assertThat(illegalArgument.getMessage(), equalTo("ObjectGraphSize/ObjectSize can only accept positive values.")); } }
@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.")); } }
@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.")); } }