SGF-122 - Added namespace support for FixedPartitionAttributes
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.gemfire;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.gemstone.gemfire.cache.FixedPartitionAttributes;
|
||||
|
||||
/**
|
||||
* @author David Turanski
|
||||
*
|
||||
*/
|
||||
public class FixedPartitionAttributesFactoryBean implements FactoryBean<FixedPartitionAttributes>, InitializingBean {
|
||||
private Boolean primary;
|
||||
private String partitionName;
|
||||
private Integer numBuckets;
|
||||
private FixedPartitionAttributes fixedPartitionAttributes;
|
||||
/**
|
||||
* @param primary
|
||||
*/
|
||||
public void setPrimary(boolean primary) {
|
||||
this.primary = primary;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param partitionName the partitionName to set
|
||||
*/
|
||||
public void setPartitionName(String partitionName) {
|
||||
this.partitionName = partitionName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param numBuckets the numBuckets to set
|
||||
*/
|
||||
public void setNumBuckets(Integer numBuckets) {
|
||||
this.numBuckets = numBuckets;
|
||||
}
|
||||
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.beans.factory.FactoryBean#getObject()
|
||||
*/
|
||||
@Override
|
||||
public FixedPartitionAttributes getObject() throws Exception {
|
||||
return fixedPartitionAttributes;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
|
||||
*/
|
||||
@Override
|
||||
public Class<?> getObjectType() {
|
||||
return FixedPartitionAttributes.class;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.beans.factory.FactoryBean#isSingleton()
|
||||
*/
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
|
||||
*/
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.hasText(partitionName,"partitionName cannot be empty or null");
|
||||
|
||||
fixedPartitionAttributes = null;
|
||||
if (primary == null && numBuckets == null){
|
||||
fixedPartitionAttributes = FixedPartitionAttributes.createFixedPartition(partitionName);
|
||||
} else if (primary == null && numBuckets != null){
|
||||
fixedPartitionAttributes = FixedPartitionAttributes.createFixedPartition(partitionName, numBuckets);
|
||||
} else if (primary != null && numBuckets == null) {
|
||||
fixedPartitionAttributes = FixedPartitionAttributes.createFixedPartition(partitionName, primary);
|
||||
} else {
|
||||
fixedPartitionAttributes = FixedPartitionAttributes.createFixedPartition(partitionName,primary,numBuckets);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ import java.util.List;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
import com.gemstone.gemfire.cache.FixedPartitionAttributes;
|
||||
import com.gemstone.gemfire.cache.PartitionAttributes;
|
||||
import com.gemstone.gemfire.cache.PartitionResolver;
|
||||
import com.gemstone.gemfire.cache.partition.PartitionListener;
|
||||
@@ -58,6 +59,12 @@ public class PartitionAttributesFactoryBean implements FactoryBean<PartitionAttr
|
||||
public void setColocatedWith(String colocatedRegionFullPath) {
|
||||
paf.setColocatedWith(colocatedRegionFullPath);
|
||||
}
|
||||
|
||||
public void setFixedPartitionAttributes(List<FixedPartitionAttributes> fixedPartitionAttributes) {
|
||||
for (FixedPartitionAttributes fpa: fixedPartitionAttributes) {
|
||||
paf.addFixedPartitionAttributes(fpa);
|
||||
}
|
||||
}
|
||||
|
||||
public void setLocalMaxMemory(int mb) {
|
||||
paf.setLocalMaxMemory(mb);
|
||||
|
||||
@@ -16,12 +16,17 @@
|
||||
|
||||
package org.springframework.data.gemfire.config;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.ManagedList;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.data.gemfire.FixedPartitionAttributesFactoryBean;
|
||||
import org.springframework.data.gemfire.PartitionAttributesFactoryBean;
|
||||
import org.springframework.data.gemfire.PartitionedRegionFactoryBean;
|
||||
import org.springframework.data.gemfire.RegionAttributesFactoryBean;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.xml.DomUtils;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
@@ -40,6 +45,7 @@ class PartitionedRegionParser extends AbstractRegionParser {
|
||||
return PartitionedRegionFactoryBean.class;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
protected void doParseRegion(Element element, ParserContext parserContext, BeanDefinitionBuilder builder,
|
||||
boolean subRegion) {
|
||||
@@ -104,8 +110,22 @@ class PartitionedRegionParser extends AbstractRegionParser {
|
||||
parAttrBuilder.addPropertyValue("partitionListeners",
|
||||
parsePartitionListeners(parserContext, subElement, builder));
|
||||
}
|
||||
|
||||
List<Element> fixedPartitions = DomUtils.getChildElementsByTagName(element, "fixed-partition");
|
||||
if (! CollectionUtils.isEmpty(fixedPartitions)){
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
ManagedList fixedPartitionAttributes = new ManagedList();
|
||||
for (Element fp: fixedPartitions) {
|
||||
BeanDefinitionBuilder fpaBuilder = BeanDefinitionBuilder.genericBeanDefinition(FixedPartitionAttributesFactoryBean.class);
|
||||
ParsingUtils.setPropertyValue(fp, fpaBuilder, "partition-name");
|
||||
ParsingUtils.setPropertyValue(fp, fpaBuilder, "num-buckets");
|
||||
ParsingUtils.setPropertyValue(fp, fpaBuilder, "primary");
|
||||
fixedPartitionAttributes.add(fpaBuilder.getBeanDefinition());
|
||||
}
|
||||
parAttrBuilder.addPropertyValue("fixedPartitionAttributes", fixedPartitionAttributes);
|
||||
}
|
||||
|
||||
//
|
||||
// // add partition attributes attributes
|
||||
attrBuilder.addPropertyValue("partitionAttributes", parAttrBuilder.getBeanDefinition());
|
||||
// add partition/overflow settings as attributes
|
||||
|
||||
@@ -1072,6 +1072,36 @@ is created or any bucket in a partitioned region becomes primary
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
</xsd:element>
|
||||
<xsd:element name="fixed-partition" minOccurs="0" maxOccurs="unbounded">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation source="com.gemstone.gemfire.cache.partition.FixedPartitionAttributes"><![CDATA[
|
||||
Create a fixed partition with the given attributes. Required for a FixedPartitionResolver.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="partition-name" type="xsd:string" use="required">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation source="com.gemstone.gemfire.cache.partition.FixedPartitionAttributes"><![CDATA[
|
||||
Specifies the fixed partition name
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="primary" use="optional" default="true">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation source="com.gemstone.gemfire.cache.partition.FixedPartitionAttributes"><![CDATA[
|
||||
Specifies if this member is primary for this partition
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="num-buckets" use="optional">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation source="com.gemstone.gemfire.cache.partition.FixedPartitionAttributes"><![CDATA[
|
||||
Specifies the number of buckets to allocate to the fixed partition
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="eviction" minOccurs="0"
|
||||
maxOccurs="1">
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
import com.gemstone.gemfire.cache.CacheListener;
|
||||
import com.gemstone.gemfire.cache.FixedPartitionAttributes;
|
||||
import com.gemstone.gemfire.cache.PartitionAttributes;
|
||||
import com.gemstone.gemfire.cache.Region;
|
||||
import com.gemstone.gemfire.cache.RegionAttributes;
|
||||
@@ -55,6 +56,7 @@ public class PartitionedRegionNamespaceTest {
|
||||
testBasicPartition();
|
||||
testComplexPartition();
|
||||
testPartitionOptions();
|
||||
testFixedPartition();
|
||||
}
|
||||
|
||||
private void testBasicPartition() throws Exception {
|
||||
@@ -64,7 +66,8 @@ public class PartitionedRegionNamespaceTest {
|
||||
@SuppressWarnings("rawtypes")
|
||||
private void testPartitionOptions() throws Exception {
|
||||
assertTrue(context.containsBean("options"));
|
||||
RegionFactoryBean fb = context.getBean("&options", RegionFactoryBean.class);
|
||||
RegionFactoryBean fb = context.getBean("&options",
|
||||
RegionFactoryBean.class);
|
||||
assertTrue(fb instanceof PartitionedRegionFactoryBean);
|
||||
assertEquals(null, TestUtils.readField("scope", fb));
|
||||
assertEquals("redundant", TestUtils.readField("name", fb));
|
||||
@@ -76,20 +79,24 @@ public class PartitionedRegionNamespaceTest {
|
||||
|
||||
assertEquals(1, pAttr.getRedundantCopies());
|
||||
assertEquals(4, pAttr.getTotalNumBuckets());
|
||||
assertSame(SimplePartitionResolver.class, pAttr.getPartitionResolver().getClass());
|
||||
assertSame(SimplePartitionResolver.class, pAttr.getPartitionResolver()
|
||||
.getClass());
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private void testComplexPartition() throws Exception {
|
||||
assertTrue(context.containsBean("complex"));
|
||||
RegionFactoryBean fb = context.getBean("&complex", RegionFactoryBean.class);
|
||||
RegionFactoryBean fb = context.getBean("&complex",
|
||||
RegionFactoryBean.class);
|
||||
CacheListener[] listeners = TestUtils.readField("cacheListeners", fb);
|
||||
assertFalse(ObjectUtils.isEmpty(listeners));
|
||||
assertEquals(2, listeners.length);
|
||||
assertSame(listeners[0], context.getBean("c-listener"));
|
||||
|
||||
assertSame(context.getBean("c-loader"), TestUtils.readField("cacheLoader", fb));
|
||||
assertSame(context.getBean("c-writer"), TestUtils.readField("cacheWriter", fb));
|
||||
assertSame(context.getBean("c-loader"),
|
||||
TestUtils.readField("cacheLoader", fb));
|
||||
assertSame(context.getBean("c-writer"),
|
||||
TestUtils.readField("cacheWriter", fb));
|
||||
|
||||
RegionAttributes attrs = TestUtils.readField("attributes", fb);
|
||||
PartitionAttributes pAttr = attrs.getPartitionAttributes();
|
||||
@@ -100,6 +107,22 @@ public class PartitionedRegionNamespaceTest {
|
||||
assertTrue(pAttr.getPartitionListeners()[0] instanceof TestPartitionListener);
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void testFixedPartition() throws Exception {
|
||||
RegionFactoryBean fb = context.getBean("&fixed",
|
||||
RegionFactoryBean.class);
|
||||
RegionAttributes attrs = TestUtils.readField("attributes", fb);
|
||||
PartitionAttributes pAttr = attrs.getPartitionAttributes();
|
||||
assertNotNull(pAttr.getFixedPartitionAttributes());
|
||||
assertEquals(3, pAttr.getFixedPartitionAttributes().size());
|
||||
|
||||
FixedPartitionAttributes fpa = (FixedPartitionAttributes) pAttr
|
||||
.getFixedPartitionAttributes().get(0);
|
||||
assertEquals(3, fpa.getNumBuckets());
|
||||
assertTrue(fpa.isPrimary());
|
||||
|
||||
}
|
||||
|
||||
public static class TestPartitionListener implements PartitionListener {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -30,6 +30,12 @@
|
||||
</gfe:partition-listener>
|
||||
</gfe:partitioned-region>
|
||||
|
||||
<gfe:partitioned-region id="fixed">
|
||||
<gfe:fixed-partition partition-name="p1" primary="true" num-buckets="3"/>
|
||||
<gfe:fixed-partition partition-name="p2"/>
|
||||
<gfe:fixed-partition partition-name="p3" primary="true"/>
|
||||
</gfe:partitioned-region>
|
||||
|
||||
<bean id="c-listener" class="org.springframework.data.gemfire.SimpleCacheListener"/>
|
||||
<bean id="c-loader" class="org.springframework.data.gemfire.SimpleCacheLoader"/>
|
||||
<bean id="c-writer" class="org.springframework.data.gemfire.SimpleCacheWriter"/>
|
||||
|
||||
Reference in New Issue
Block a user