Java Code Examples for com.gemstone.gemfire.cache.Region#getSubregion()

The following examples show how to use com.gemstone.gemfire.cache.Region#getSubregion() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RegionUtil.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the last region
 * @param regionPath
 * @return the last region
 */
public static Region getLastRegionInPath(String regionPath)
{
	if (regionPath == null) {
		return null;
	}
	
	Cache cache = CacheFactory.getAnyInstance();
	String split[] = regionPath.split("/");
	if (split.length == 0) {
		return null;
	}
	
	Region region = cache.getRegion(split[0]);
	if (region != null) {
 	for (int i = 1; i < split.length; i++) {
 		Region subregion = region.getSubregion(split[i]);
 		if (subregion == null) {
 			break;
 		}
 		region = subregion;
 	}
	}
	return region;
}
 
Example 2
Source File: ConsoleDistributionManagerTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Puts (or creates) a value in a region named <code>regionName</code>
 * named <code>entryName</code>.
 */
protected void remoteCreateEntry(String regionName,
                                        String entryName,
                                        Object value)
  throws CacheException {
  
  Region root = getRootRegion();
  Region region = root.getSubregion(regionName);
  region.create(entryName, value);
  
  
  getLogWriter().info("Put value " + value + " in entry " +
                      entryName + " in region '" +
                      region.getFullPath() +"'");
  
}
 
Example 3
Source File: RegionUtil.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the last region
 * @param regionPath
 * @return the last region
 */
public static Region getLastRegionInPath(String regionPath)
{
	if (regionPath == null) {
		return null;
	}
	
	Cache cache = CacheFactory.getAnyInstance();
	String split[] = regionPath.split("/");
	if (split.length == 0) {
		return null;
	}
	
	Region region = cache.getRegion(split[0]);
	if (region != null) {
 	for (int i = 1; i < split.length; i++) {
 		Region subregion = region.getSubregion(split[i]);
 		if (subregion == null) {
 			break;
 		}
 		region = subregion;
 	}
	}
	return region;
}
 
Example 4
Source File: ConsoleDistributionManagerTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Puts (or creates) a value in a region named <code>regionName</code>
 * named <code>entryName</code>.
 */
protected void remoteCreateEntry(String regionName,
                                        String entryName,
                                        Object value)
  throws CacheException {
  
  Region root = getRootRegion();
  Region region = root.getSubregion(regionName);
  region.create(entryName, value);
  
  
  getLogWriter().info("Put value " + value + " in entry " +
                      entryName + " in region '" +
                      region.getFullPath() +"'");
  
}
 
Example 5
Source File: RegionUtil.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the parent region of the specified path.
 * @param regionPath The region path.
 * @return the parent region of the specified path.
 */
public static Region getParentRegion(String regionPath)
{
	if (regionPath == null) {
		return null;
	}
	
	Cache cache = CacheFactory.getAnyInstance();
	String split[];
	if (regionPath.startsWith("/")) {
		split = regionPath.substring(1).split("/");
	} else {
		split = regionPath.split("/");
	}
	if (split.length == 0) {
		return null;
	}
	Region region = cache.getRegion(split[0]);
	if (region != null) {
		int i;
 	for (i = 1; i < split.length; i++) {
 		Region subregion = region.getSubregion(split[i]);
 		if (subregion == null) {
 			break;
 		}
 		region = subregion;
 	}
 	if (i != split.length - 1) {
 		region = null;
 	}
	}
	return region;
}
 
Example 6
Source File: RegionUtil.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the parent region of the specified path.
 * @param regionPath The region path.
 * @return the parent region of the specified path.
 */
public static Region getParentRegion(String regionPath)
{
	if (regionPath == null) {
		return null;
	}
	
	Cache cache = CacheFactory.getAnyInstance();
	String split[];
	if (regionPath.startsWith("/")) {
		split = regionPath.substring(1).split("/");
	} else {
		split = regionPath.split("/");
	}
	if (split.length == 0) {
		return null;
	}
	Region region = cache.getRegion(split[0]);
	if (region != null) {
		int i;
 	for (i = 1; i < split.length; i++) {
 		Region subregion = region.getSubregion(split[i]);
 		if (subregion == null) {
 			break;
 		}
 		region = subregion;
 	}
 	if (i != split.length - 1) {
 		region = null;
 	}
	}
	return region;
}
 
Example 7
Source File: rmdir.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
private void remove_local(String regionPath)
{
	if (regionPath == null) {
		return;
	}
	
	String currentPath = gfsh.getCurrentPath();
	String fullPath = gfsh.getFullPath(regionPath, currentPath);
	if (fullPath == null) {
		gfsh.println("Error: invalid region path");
	} else if (fullPath.equals("/")) {
		gfsh.println("Error: cannot remove top level");
	} else {
		Region region = gfsh.getCache().getRegion(fullPath);
		if (region == null) {
			gfsh.println("Error: undefined region path " + fullPath);
			return;
		} 
		region.close();
		
		// correct the current path if the removed region path
		// lies in the current path
		String currentSplit[] = currentPath.split("/");
		Cache cache = gfsh.getCache();
		Region currentRegion = null;
		if (currentSplit.length > 1) {
			currentRegion = region = cache.getRegion(currentSplit[1]);
			if (region != null) {
				for (int i = 2; i < currentSplit.length; i++) {
					region = region.getSubregion(currentSplit[i]);
					if (region == null) {
						break;
					}
					currentRegion = region;
				}
			}
		}
		if (currentRegion == null) {
			gfsh.setCurrentPath("/");
		} else {
			gfsh.setCurrentPath(currentRegion.getFullPath());
		}
		gfsh.setCurrentRegion(currentRegion);
		
		gfsh.println("Region removed from local VM: " + regionPath);
	}
}
 
Example 8
Source File: RegionUtil.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public static Region getRegion(String regionPath, Scope scope, DataPolicy dataPolicy, Pool pool,
		boolean enableBridgeConflation) throws CacheException
{
	Cache cache = null;
	Region region = null;

	try {
		cache = CacheFactory.getAnyInstance();
		region = cache.getRegion(regionPath);

		if (region != null) {
			return region;
		}
	} catch (CacheClosedException ex) {
		Properties props = new Properties();
		DistributedSystem system = DistributedSystem.connect(props);
		cache = CacheFactory.create(system);
	}

	// Parse region path
	StringTokenizer st = new StringTokenizer(regionPath, "/");
	String regionName;
	int index = 0;
	int count = st.countTokens();
	AttributesFactory factory = new AttributesFactory();
	factory.setDataPolicy(DataPolicy.NORMAL);

	while (st.hasMoreTokens()) {
		regionName = st.nextToken();
		index++;

		if (index == count) {
			factory.setDataPolicy(dataPolicy);
			factory.setPoolName(pool.getName());
		}

		if (index == 1) {
			region = cache.getRegion(regionName);

			if (region == null) {
				factory.setScope(scope);
				region = cache.createRegion(regionName, factory.create());
			}
		} else {
			Region subregion = region.getSubregion(regionName);
			if (subregion == null) {
				factory.setScope(scope);
				subregion = region.createSubregion(regionName, factory.create());
			}

			region = subregion;
		}
	}

	return region;
}
 
Example 9
Source File: CacheXml61Test.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that a region created with a named attributes set programmatically
 * for delta propogation has the correct attributes.
 * 
 */
public void testRegionAttributesForRegionEntryCloning() throws CacheException
{
  final String rNameBase = getUniqueName();
  final String r1 = rNameBase + "1";

  // Setting multi-cast via nested region attributes
  CacheCreation creation = new CacheCreation();
  RegionAttributesCreation attrs = new RegionAttributesCreation(creation);
  attrs.setScope(Scope.LOCAL);
  attrs.setEarlyAck(false);
  attrs.setCloningEnable(false);
  attrs.setMulticastEnabled(true);
  creation.createRegion(r1, attrs);
  
  testXml(creation);

  Cache c = getCache();
  assertTrue(c instanceof GemFireCacheImpl);
  c.loadCacheXml(generate(creation));

  Region reg1 = c.getRegion(r1);
  assertNotNull(reg1);
  assertEquals(Scope.LOCAL, reg1.getAttributes().getScope());
  assertFalse(reg1.getAttributes().getEarlyAck());
  assertTrue(reg1.getAttributes().getMulticastEnabled());
  assertFalse(reg1.getAttributes().getCloningEnabled());
  
  //  changing Clonned setting
  reg1.getAttributesMutator().setCloningEnabled(true);
  assertTrue(reg1.getAttributes().getCloningEnabled());

  reg1.getAttributesMutator().setCloningEnabled(false);
  assertFalse(reg1.getAttributes().getCloningEnabled());
  
  // for sub region - a child attribute should be inherited
  String sub = "subRegion";
  RegionAttributesCreation attrsSub = new RegionAttributesCreation(creation);
  attrsSub.setScope(Scope.LOCAL);
  reg1.createSubregion(sub, attrsSub);
  Region subRegion = reg1.getSubregion(sub);
  assertFalse(subRegion.getAttributes().getCloningEnabled());
  subRegion.getAttributesMutator().setCloningEnabled(true);
  assertTrue(subRegion.getAttributes().getCloningEnabled());
}
 
Example 10
Source File: PartitionedRegionBucketCreationDistributionDUnitTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * This functions performs following validations on the partitions regiions
 * <br>
 * (1) Size of bucket2Node region should be > 0.</br><br>
 * (3) In case of the partition regions with redundancy > 0 scope of the
 * bucket regions should be scope of the partition regions.</br><br>
 * (4) In case of the partition regions with redundancy > 0 no two bucket
 * regions with same bucketId should be generated on the same node.</br>
 * 
 * @param startIndexForRegion
 * @param endIndexForRegion
 * @return
 */
private CacheSerializableRunnable validateBucketCreationAfterPut(
    final int startIndexForRegion, final int endIndexForRegion)
{
  CacheSerializableRunnable validateAfterPut = new CacheSerializableRunnable(
      "validateAfterPut") {
    int innerStartIndexForRegion = startIndexForRegion;

    int innerEndIndexForRegion = endIndexForRegion;

    String innerPrPrefix = prPrefix;

    int innerMidIndexForRegion = innerStartIndexForRegion
        + (endIndexForRegion - startIndexForRegion) / 2;

    int innerQuarterIndex = innerStartIndexForRegion
        + (endIndexForRegion - startIndexForRegion) / 4;

    public void run2()
    {
      Cache cache = getCache();
      Region root = cache
          .getRegion(PartitionedRegionHelper.PR_ROOT_REGION_NAME);
      assertNotNull("Root regions is null", root);
      for (int i = innerStartIndexForRegion; i < innerEndIndexForRegion; i++) {
        PartitionedRegion pr = (PartitionedRegion)cache
            .getRegion(Region.SEPARATOR + innerPrPrefix + i);

        assertTrue(pr.getRegionAdvisor().getBucketSet().size() > 0);
        assertTrue("Size of local region map should be > 0 for region: " +  pr.getFullPath(), pr
            .getDataStore().localBucket2RegionMap.size() > 0);
        // taking the buckets which are local to the node and not all the
        // available buckets.
        Set bucketIds = pr.getDataStore().localBucket2RegionMap.keySet();
        Iterator buckteIdItr = bucketIds.iterator();
        while (buckteIdItr.hasNext()) {
          Integer key = (Integer) buckteIdItr.next();
          BucketRegion val = (BucketRegion)pr.getDataStore().localBucket2RegionMap
              .get(key);

          Region bucketRegion = root.getSubregion(pr.getBucketName(key.intValue()));
          assertTrue(bucketRegion.getFullPath().equals(val.getFullPath()));
          // Bucket region should not be null
          assertNotNull("Bucket region cannot be null", bucketRegion);
          // Parent region of the bucket region should be root
          assertEquals("Parent region is not root", root, bucketRegion
              .getParentRegion());
        }
      }
    }
  };
  return validateAfterPut;
}
 
Example 11
Source File: PartitionedRegionBucketCreationDistributionDUnitTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
private CacheSerializableRunnable validateBucketScopeAfterPut(
    final int startIndexForRegion, final int endIndexForRegion)
{
  CacheSerializableRunnable validateAfterPut = new CacheSerializableRunnable(
      "validateBucketScopeAfterPut") {
    int innerStartIndexForRegion = startIndexForRegion;

    int innerEndIndexForRegion = endIndexForRegion;

    String innerPrPrefix = prPrefix;

    int innerMidIndexForRegion = innerStartIndexForRegion
        + (endIndexForRegion - startIndexForRegion) / 2;

    int innerQuarterIndex = innerStartIndexForRegion
        + (endIndexForRegion - startIndexForRegion) / 4;

    public void run2()
    {
      Cache cache = getCache();
      Region root = cache
          .getRegion(PartitionedRegionHelper.PR_ROOT_REGION_NAME);
      assertNotNull("Root regions is null", root);
      for (int i = innerStartIndexForRegion; i < innerEndIndexForRegion; i++) {
        PartitionedRegion pr = (PartitionedRegion)cache
            .getRegion(Region.SEPARATOR + innerPrPrefix + i);

        assertTrue(pr.getRegionAdvisor().getBucketSet().size() > 0);
        // taking the buckets which are local to the node and not all the
        // available buckets.
        Set bucketIds = pr.getDataStore().localBucket2RegionMap.keySet();
        Iterator buckteIdItr = bucketIds.iterator();
        while (buckteIdItr.hasNext()) {
          Integer key = (Integer) buckteIdItr.next();
          Region bucketRegion = root.getSubregion(pr.getBucketName(key.intValue()));
          assertNotNull("Bucket region cannot be null", bucketRegion);
          assertEquals(Scope.DISTRIBUTED_ACK, bucketRegion.getAttributes().getScope());
        }  // while 
      }
    }
  };
  return validateAfterPut;
}
 
Example 12
Source File: rmdir.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
private void remove_local(String regionPath)
{
	if (regionPath == null) {
		return;
	}
	
	String currentPath = gfsh.getCurrentPath();
	String fullPath = gfsh.getFullPath(regionPath, currentPath);
	if (fullPath == null) {
		gfsh.println("Error: invalid region path");
	} else if (fullPath.equals("/")) {
		gfsh.println("Error: cannot remove top level");
	} else {
		Region region = gfsh.getCache().getRegion(fullPath);
		if (region == null) {
			gfsh.println("Error: undefined region path " + fullPath);
			return;
		} 
		region.close();
		
		// correct the current path if the removed region path
		// lies in the current path
		String currentSplit[] = currentPath.split("/");
		Cache cache = gfsh.getCache();
		Region currentRegion = null;
		if (currentSplit.length > 1) {
			currentRegion = region = cache.getRegion(currentSplit[1]);
			if (region != null) {
				for (int i = 2; i < currentSplit.length; i++) {
					region = region.getSubregion(currentSplit[i]);
					if (region == null) {
						break;
					}
					currentRegion = region;
				}
			}
		}
		if (currentRegion == null) {
			gfsh.setCurrentPath("/");
		} else {
			gfsh.setCurrentPath(currentRegion.getFullPath());
		}
		gfsh.setCurrentRegion(currentRegion);
		
		gfsh.println("Region removed from local VM: " + regionPath);
	}
}
 
Example 13
Source File: RegionUtil.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public static Region getRegion(String regionPath, Scope scope, DataPolicy dataPolicy, Pool pool,
		boolean enableBridgeConflation) throws CacheException
{
	Cache cache = null;
	Region region = null;

	try {
		cache = CacheFactory.getAnyInstance();
		region = cache.getRegion(regionPath);

		if (region != null) {
			return region;
		}
	} catch (CacheClosedException ex) {
		Properties props = new Properties();
		DistributedSystem system = DistributedSystem.connect(props);
		cache = CacheFactory.create(system);
	}

	// Parse region path
	StringTokenizer st = new StringTokenizer(regionPath, "/");
	String regionName;
	int index = 0;
	int count = st.countTokens();
	AttributesFactory factory = new AttributesFactory();
	factory.setDataPolicy(DataPolicy.NORMAL);

	while (st.hasMoreTokens()) {
		regionName = st.nextToken();
		index++;

		if (index == count) {
			factory.setDataPolicy(dataPolicy);
			factory.setPoolName(pool.getName());
		}

		if (index == 1) {
			region = cache.getRegion(regionName);

			if (region == null) {
				factory.setScope(scope);
				region = cache.createRegion(regionName, factory.create());
			}
		} else {
			Region subregion = region.getSubregion(regionName);
			if (subregion == null) {
				factory.setScope(scope);
				subregion = region.createSubregion(regionName, factory.create());
			}

			region = subregion;
		}
	}

	return region;
}
 
Example 14
Source File: CacheXml61Test.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that a region created with a named attributes set programmatically
 * for delta propogation has the correct attributes.
 * 
 */
public void testRegionAttributesForRegionEntryCloning() throws CacheException
{
  final String rNameBase = getUniqueName();
  final String r1 = rNameBase + "1";

  // Setting multi-cast via nested region attributes
  CacheCreation creation = new CacheCreation();
  RegionAttributesCreation attrs = new RegionAttributesCreation(creation);
  attrs.setScope(Scope.LOCAL);
  attrs.setEarlyAck(false);
  attrs.setCloningEnable(false);
  attrs.setMulticastEnabled(true);
  creation.createRegion(r1, attrs);
  
  testXml(creation);

  Cache c = getCache();
  assertTrue(c instanceof GemFireCacheImpl);
  c.loadCacheXml(generate(creation));

  Region reg1 = c.getRegion(r1);
  assertNotNull(reg1);
  assertEquals(Scope.LOCAL, reg1.getAttributes().getScope());
  assertFalse(reg1.getAttributes().getEarlyAck());
  assertTrue(reg1.getAttributes().getMulticastEnabled());
  assertFalse(reg1.getAttributes().getCloningEnabled());
  
  //  changing Clonned setting
  reg1.getAttributesMutator().setCloningEnabled(true);
  assertTrue(reg1.getAttributes().getCloningEnabled());

  reg1.getAttributesMutator().setCloningEnabled(false);
  assertFalse(reg1.getAttributes().getCloningEnabled());
  
  // for sub region - a child attribute should be inherited
  String sub = "subRegion";
  RegionAttributesCreation attrsSub = new RegionAttributesCreation(creation);
  attrsSub.setScope(Scope.LOCAL);
  reg1.createSubregion(sub, attrsSub);
  Region subRegion = reg1.getSubregion(sub);
  assertFalse(subRegion.getAttributes().getCloningEnabled());
  subRegion.getAttributesMutator().setCloningEnabled(true);
  assertTrue(subRegion.getAttributes().getCloningEnabled());
}
 
Example 15
Source File: PartitionedRegionBucketCreationDistributionDUnitTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * This functions performs following validations on the partitions regiions
 * <br>
 * (1) Size of bucket2Node region should be > 0.</br><br>
 * (3) In case of the partition regions with redundancy > 0 scope of the
 * bucket regions should be scope of the partition regions.</br><br>
 * (4) In case of the partition regions with redundancy > 0 no two bucket
 * regions with same bucketId should be generated on the same node.</br>
 * 
 * @param startIndexForRegion
 * @param endIndexForRegion
 * @return
 */
private CacheSerializableRunnable validateBucketCreationAfterPut(
    final int startIndexForRegion, final int endIndexForRegion)
{
  CacheSerializableRunnable validateAfterPut = new CacheSerializableRunnable(
      "validateAfterPut") {
    int innerStartIndexForRegion = startIndexForRegion;

    int innerEndIndexForRegion = endIndexForRegion;

    String innerPrPrefix = prPrefix;

    int innerMidIndexForRegion = innerStartIndexForRegion
        + (endIndexForRegion - startIndexForRegion) / 2;

    int innerQuarterIndex = innerStartIndexForRegion
        + (endIndexForRegion - startIndexForRegion) / 4;

    public void run2()
    {
      Cache cache = getCache();
      Region root = cache
          .getRegion(PartitionedRegionHelper.PR_ROOT_REGION_NAME);
      assertNotNull("Root regions is null", root);
      for (int i = innerStartIndexForRegion; i < innerEndIndexForRegion; i++) {
        PartitionedRegion pr = (PartitionedRegion)cache
            .getRegion(Region.SEPARATOR + innerPrPrefix + i);

        assertTrue(pr.getRegionAdvisor().getBucketSet().size() > 0);
        assertTrue("Size of local region map should be > 0 for region: " +  pr.getFullPath(), pr
            .getDataStore().localBucket2RegionMap.size() > 0);
        // taking the buckets which are local to the node and not all the
        // available buckets.
        Set bucketIds = pr.getDataStore().localBucket2RegionMap.keySet();
        Iterator buckteIdItr = bucketIds.iterator();
        while (buckteIdItr.hasNext()) {
          Integer key = (Integer) buckteIdItr.next();
          BucketRegion val = (BucketRegion)pr.getDataStore().localBucket2RegionMap
              .get(key);

          Region bucketRegion = root.getSubregion(pr.getBucketName(key.intValue()));
          assertTrue(bucketRegion.getFullPath().equals(val.getFullPath()));
          // Bucket region should not be null
          assertNotNull("Bucket region cannot be null", bucketRegion);
          // Parent region of the bucket region should be root
          assertEquals("Parent region is not root", root, bucketRegion
              .getParentRegion());
        }
      }
    }
  };
  return validateAfterPut;
}
 
Example 16
Source File: PartitionedRegionBucketCreationDistributionDUnitTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
private CacheSerializableRunnable validateBucketScopeAfterPut(
    final int startIndexForRegion, final int endIndexForRegion)
{
  CacheSerializableRunnable validateAfterPut = new CacheSerializableRunnable(
      "validateBucketScopeAfterPut") {
    int innerStartIndexForRegion = startIndexForRegion;

    int innerEndIndexForRegion = endIndexForRegion;

    String innerPrPrefix = prPrefix;

    int innerMidIndexForRegion = innerStartIndexForRegion
        + (endIndexForRegion - startIndexForRegion) / 2;

    int innerQuarterIndex = innerStartIndexForRegion
        + (endIndexForRegion - startIndexForRegion) / 4;

    public void run2()
    {
      Cache cache = getCache();
      Region root = cache
          .getRegion(PartitionedRegionHelper.PR_ROOT_REGION_NAME);
      assertNotNull("Root regions is null", root);
      for (int i = innerStartIndexForRegion; i < innerEndIndexForRegion; i++) {
        PartitionedRegion pr = (PartitionedRegion)cache
            .getRegion(Region.SEPARATOR + innerPrPrefix + i);

        assertTrue(pr.getRegionAdvisor().getBucketSet().size() > 0);
        // taking the buckets which are local to the node and not all the
        // available buckets.
        Set bucketIds = pr.getDataStore().localBucket2RegionMap.keySet();
        Iterator buckteIdItr = bucketIds.iterator();
        while (buckteIdItr.hasNext()) {
          Integer key = (Integer) buckteIdItr.next();
          Region bucketRegion = root.getSubregion(pr.getBucketName(key.intValue()));
          assertNotNull("Bucket region cannot be null", bucketRegion);
          assertEquals(Scope.DISTRIBUTED_ACK, bucketRegion.getAttributes().getScope());
        }  // while 
      }
    }
  };
  return validateAfterPut;
}