use of org.apache.geode.cache.CacheLoader in project geode by apache.
the class RegionCreateFunction method createRegion.
public static <K, V> Region<?, ?> createRegion(Cache cache, RegionFunctionArgs regionCreateArgs) {
Region<K, V> createdRegion = null;
final String regionPath = regionCreateArgs.getRegionPath();
final RegionShortcut regionShortcut = regionCreateArgs.getRegionShortcut();
final String useAttributesFrom = regionCreateArgs.getUseAttributesFrom();
// If a region path indicates a sub-region, check whether the parent region exists
RegionPath regionPathData = new RegionPath(regionPath);
String parentRegionPath = regionPathData.getParent();
Region<?, ?> parentRegion = null;
if (parentRegionPath != null && !Region.SEPARATOR.equals(parentRegionPath)) {
parentRegion = cache.getRegion(parentRegionPath);
if (parentRegion == null) {
throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__PARENT_REGION_FOR_0_DOESNOT_EXIST, new Object[] { regionPath }));
}
if (parentRegion.getAttributes().getPartitionAttributes() != null) {
// For a PR, sub-regions are not supported.
throw new CreateSubregionException(CliStrings.format(CliStrings.CREATE_REGION__MSG__0_IS_A_PR_CANNOT_HAVE_SUBREGIONS, parentRegion.getFullPath()));
}
}
// One of Region Shortcut OR Use Attributes From has to be given
if (regionShortcut == null && useAttributesFrom == null) {
throw new IllegalArgumentException(CliStrings.CREATE_REGION__MSG__ONE_OF_REGIONSHORTCUT_AND_USEATTRIBUESFROM_IS_REQUIRED);
}
boolean isPartitioned = false;
RegionFactory<K, V> factory = null;
RegionAttributes<K, V> regionAttributes = null;
if (regionShortcut != null) {
regionAttributes = cache.getRegionAttributes(regionShortcut.toString());
if (logger.isDebugEnabled()) {
logger.debug("Using shortcut {} for {} region attributes : {}", regionShortcut, regionPath, regionAttributes);
}
if (regionAttributes == null) {
if (logger.isDebugEnabled()) {
logger.debug("Shortcut {} doesn't have attributes in {}", regionShortcut, cache.listRegionAttributes());
}
throw new IllegalStateException(CliStrings.format(CliStrings.CREATE_REGION__MSG__COULDNOT_LOAD_REGION_ATTRIBUTES_FOR_SHORTCUT_0, regionShortcut));
}
} else {
if (logger.isDebugEnabled()) {
logger.debug("Using Manager's region attributes for {}", regionPath);
}
regionAttributes = regionCreateArgs.getRegionAttributes();
if (logger.isDebugEnabled()) {
logger.debug("Using Attributes : {}", regionAttributes);
}
}
isPartitioned = regionAttributes.getPartitionAttributes() != null;
factory = cache.createRegionFactory(regionAttributes);
if (!isPartitioned && regionCreateArgs.hasPartitionAttributes()) {
throw new IllegalArgumentException(CliStrings.format(CliStrings.CREATE_REGION__MSG__OPTION_0_CAN_BE_USED_ONLY_FOR_PARTITIONEDREGION, regionCreateArgs.getPartitionArgs().getUserSpecifiedPartitionAttributes()));
}
if (isPartitioned) {
PartitionAttributes<K, V> partitionAttributes = extractPartitionAttributes(cache, regionAttributes, regionCreateArgs);
DataPolicy originalDataPolicy = regionAttributes.getDataPolicy();
factory.setPartitionAttributes(partitionAttributes);
// We have to do this because AttributesFactory.setPartitionAttributes()
// checks RegionAttributes.hasDataPolicy() which is set only when the data
// policy is set explicitly
factory.setDataPolicy(originalDataPolicy);
}
// Set Constraints
final String keyConstraint = regionCreateArgs.getKeyConstraint();
final String valueConstraint = regionCreateArgs.getValueConstraint();
if (keyConstraint != null && !keyConstraint.isEmpty()) {
Class<K> keyConstraintClass = CliUtil.forName(keyConstraint, CliStrings.CREATE_REGION__KEYCONSTRAINT);
factory.setKeyConstraint(keyConstraintClass);
}
if (valueConstraint != null && !valueConstraint.isEmpty()) {
Class<V> valueConstraintClass = CliUtil.forName(valueConstraint, CliStrings.CREATE_REGION__VALUECONSTRAINT);
factory.setValueConstraint(valueConstraintClass);
}
// Expiration attributes
final RegionFunctionArgs.ExpirationAttrs entryExpirationIdleTime = regionCreateArgs.getEntryExpirationIdleTime();
if (entryExpirationIdleTime != null) {
factory.setEntryIdleTimeout(entryExpirationIdleTime.convertToExpirationAttributes());
}
final RegionFunctionArgs.ExpirationAttrs entryExpirationTTL = regionCreateArgs.getEntryExpirationTTL();
if (entryExpirationTTL != null) {
factory.setEntryTimeToLive(entryExpirationTTL.convertToExpirationAttributes());
}
final RegionFunctionArgs.ExpirationAttrs regionExpirationIdleTime = regionCreateArgs.getRegionExpirationIdleTime();
if (regionExpirationIdleTime != null) {
factory.setEntryIdleTimeout(regionExpirationIdleTime.convertToExpirationAttributes());
}
final RegionFunctionArgs.ExpirationAttrs regionExpirationTTL = regionCreateArgs.getRegionExpirationTTL();
if (regionExpirationTTL != null) {
factory.setEntryTimeToLive(regionExpirationTTL.convertToExpirationAttributes());
}
// Associate a Disk Store
final String diskStore = regionCreateArgs.getDiskStore();
if (diskStore != null && !diskStore.isEmpty()) {
factory.setDiskStoreName(diskStore);
}
if (regionCreateArgs.isSetDiskSynchronous()) {
factory.setDiskSynchronous(regionCreateArgs.isDiskSynchronous());
}
if (regionCreateArgs.isSetOffHeap()) {
factory.setOffHeap(regionCreateArgs.isOffHeap());
}
// Set stats enabled
if (regionCreateArgs.isSetStatisticsEnabled()) {
factory.setStatisticsEnabled(regionCreateArgs.isStatisticsEnabled());
}
// Set conflation
if (regionCreateArgs.isSetEnableAsyncConflation()) {
factory.setEnableAsyncConflation(regionCreateArgs.isEnableAsyncConflation());
}
if (regionCreateArgs.isSetEnableSubscriptionConflation()) {
factory.setEnableSubscriptionConflation(regionCreateArgs.isEnableSubscriptionConflation());
}
// Gateway Sender Ids
final Set<String> gatewaySenderIds = regionCreateArgs.getGatewaySenderIds();
if (gatewaySenderIds != null && !gatewaySenderIds.isEmpty()) {
for (String gatewaySenderId : gatewaySenderIds) {
factory.addGatewaySenderId(gatewaySenderId);
}
}
// Async Queue Ids
final Set<String> asyncEventQueueIds = regionCreateArgs.getAsyncEventQueueIds();
if (asyncEventQueueIds != null && !asyncEventQueueIds.isEmpty()) {
for (String asyncEventQueueId : asyncEventQueueIds) {
factory.addAsyncEventQueueId(asyncEventQueueId);
}
}
// concurrency check enabled & concurrency level
if (regionCreateArgs.isSetConcurrencyChecksEnabled()) {
factory.setConcurrencyChecksEnabled(regionCreateArgs.isConcurrencyChecksEnabled());
}
if (regionCreateArgs.isSetConcurrencyLevel()) {
factory.setConcurrencyLevel(regionCreateArgs.getConcurrencyLevel());
}
// cloning enabled for delta
if (regionCreateArgs.isSetCloningEnabled()) {
factory.setCloningEnabled(regionCreateArgs.isCloningEnabled());
}
// multicast enabled for replication
if (regionCreateArgs.isSetMcastEnabled()) {
factory.setMulticastEnabled(regionCreateArgs.isMcastEnabled());
}
// Set plugins
final Set<String> cacheListeners = regionCreateArgs.getCacheListeners();
if (cacheListeners != null && !cacheListeners.isEmpty()) {
for (String cacheListener : cacheListeners) {
Class<CacheListener<K, V>> cacheListenerKlass = CliUtil.forName(cacheListener, CliStrings.CREATE_REGION__CACHELISTENER);
factory.addCacheListener(CliUtil.newInstance(cacheListenerKlass, CliStrings.CREATE_REGION__CACHELISTENER));
}
}
// Compression provider
if (regionCreateArgs.isSetCompressor()) {
Class<Compressor> compressorKlass = CliUtil.forName(regionCreateArgs.getCompressor(), CliStrings.CREATE_REGION__COMPRESSOR);
factory.setCompressor(CliUtil.newInstance(compressorKlass, CliStrings.CREATE_REGION__COMPRESSOR));
}
final String cacheLoader = regionCreateArgs.getCacheLoader();
if (cacheLoader != null) {
Class<CacheLoader<K, V>> cacheLoaderKlass = CliUtil.forName(cacheLoader, CliStrings.CREATE_REGION__CACHELOADER);
factory.setCacheLoader(CliUtil.newInstance(cacheLoaderKlass, CliStrings.CREATE_REGION__CACHELOADER));
}
final String cacheWriter = regionCreateArgs.getCacheWriter();
if (cacheWriter != null) {
Class<CacheWriter<K, V>> cacheWriterKlass = CliUtil.forName(cacheWriter, CliStrings.CREATE_REGION__CACHEWRITER);
factory.setCacheWriter(CliUtil.newInstance(cacheWriterKlass, CliStrings.CREATE_REGION__CACHEWRITER));
}
String regionName = regionPathData.getName();
if (parentRegion != null) {
createdRegion = factory.createSubregion(parentRegion, regionName);
} else {
createdRegion = factory.create(regionName);
}
return createdRegion;
}
use of org.apache.geode.cache.CacheLoader in project geode by apache.
the class DistributedCacheTestCase method remoteDefineEntry.
/**
* Defines an entry in the Region with the given name and scope. In 3.0 this method create a
* subregion named <code>entryName</code> (with the appropriate attributes) that contains an entry
* named <code>entryName</code>.
*
* @param regionName The name of a region that is a sub-region of the root region, or a global
* name
* @param entryName Must be {@link java.io.Serializable}
* @param doNetSearch Will the distributed region perform a netSearch when looking for objects? If
* <code>false</code> a {@link CacheException} will be thrown if an entry in the region is
* asked for and it not there.
*/
protected static void remoteDefineEntry(String regionName, String entryName, Scope scope, boolean doNetSearch) throws CacheException {
Region root = getRootRegion();
Region region = root.getSubregion(regionName);
AttributesFactory factory = new AttributesFactory();
factory.setScope(scope);
if (!doNetSearch) {
factory.setCacheLoader(new CacheLoader() {
public Object load(LoaderHelper helper) throws CacheLoaderException {
String s = "Should not be loading \"" + helper.getKey() + "\" in \"" + helper.getRegion().getFullPath() + "\"";
throw new CacheLoaderException(s);
}
public void close() {
}
});
}
Region sub = region.createSubregion(entryName, factory.create());
sub.create(entryName, null);
LogWriterUtils.getLogWriter().info("Defined Entry named '" + entryName + "' in region '" + sub.getFullPath() + "'");
}
use of org.apache.geode.cache.CacheLoader in project geode by apache.
the class RemoteTransactionDUnitTest method testPRTXGetOnLocalWithLoader.
@Test
public void testPRTXGetOnLocalWithLoader() {
Host host = Host.getHost(0);
VM accessor = host.getVM(0);
VM datastore = host.getVM(1);
initAccessorAndDataStore(accessor, datastore, 0);
datastore.invoke(new SerializableCallable() {
public Object call() throws Exception {
AttributesMutator am = getCache().getRegion(CUSTOMER).getAttributesMutator();
am.setCacheLoader(new CacheLoader() {
public Object load(LoaderHelper helper) throws CacheLoaderException {
return new Customer("sup dawg", "addr");
}
public void close() {
}
});
CacheTransactionManager mgr = getGemfireCache().getTxManager();
mgr.begin();
Region cust = getCache().getRegion(CUSTOMER);
CustId custId = new CustId(6);
Customer s = (Customer) cust.get(custId);
mgr.commit();
Customer s2 = (Customer) cust.get(custId);
Customer expectedCust = new Customer("sup dawg", "addr");
assertEquals(s, expectedCust);
assertEquals(s2, expectedCust);
return null;
}
});
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
return null;
}
});
}
use of org.apache.geode.cache.CacheLoader in project geode by apache.
the class RemoteTransactionDUnitTest method testPRTXGetOnRemoteWithLoader.
@Test
public void testPRTXGetOnRemoteWithLoader() {
Host host = Host.getHost(0);
VM accessor = host.getVM(0);
VM datastore = host.getVM(1);
initAccessorAndDataStore(accessor, datastore, 0);
datastore.invoke(new SerializableCallable() {
public Object call() throws Exception {
AttributesMutator am = getCache().getRegion(CUSTOMER).getAttributesMutator();
am.setCacheLoader(new CacheLoader() {
public Object load(LoaderHelper helper) throws CacheLoaderException {
return new Customer("sup dawg", "add");
}
public void close() {
}
});
return null;
}
});
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
CacheTransactionManager mgr = getGemfireCache().getTxManager();
mgr.begin();
Region cust = getCache().getRegion(CUSTOMER);
Customer s = (Customer) cust.get(new CustId(8));
assertEquals(new Customer("sup dawg", "add"), s);
assertTrue(cust.containsKey(new CustId(8)));
TXStateProxy tx = ((TXManagerImpl) mgr).internalSuspend();
assertFalse(cust.containsKey(new CustId(8)));
((TXManagerImpl) mgr).internalResume(tx);
mgr.commit();
Customer s2 = (Customer) cust.get(new CustId(8));
Customer ex = new Customer("sup dawg", "add");
assertEquals(ex, s);
assertEquals(ex, s2);
return null;
}
});
}
use of org.apache.geode.cache.CacheLoader in project geode by apache.
the class CacheAdvisorDUnitTest method testNetLoadAdvice.
@Test
public void testNetLoadAdvice() throws Exception {
final String rgnName = getUniqueName();
Set expected = new HashSet();
for (int i = 0; i < vms.length; i++) {
VM vm = vms[i];
InternalDistributedMember id = ids[i];
if (i % 2 == 1) {
expected.add(id);
}
final int index = i;
vm.invoke(new CacheSerializableRunnable("CacheAdvisorDUnitTest.testNetLoadAdvice") {
public void run2() throws CacheException {
AttributesFactory fac = new AttributesFactory();
if (index % 2 == 1) {
fac.setCacheLoader(new CacheLoader() {
public Object load(LoaderHelper helper) throws CacheLoaderException {
return null;
}
public void close() {
}
});
}
createRegion(rgnName, fac.create());
}
});
}
RegionAttributes attrs = new AttributesFactory().create();
DistributedRegion rgn = (DistributedRegion) createRegion(rgnName, attrs);
assertEquals(expected, rgn.getCacheDistributionAdvisor().adviseNetLoad());
}
Aggregations