SGF-117 - updated IndexFactoryBean to support current API - This is a breaking change with 1.2
This commit is contained in:
@@ -16,12 +16,11 @@
|
|||||||
|
|
||||||
package org.springframework.data.gemfire;
|
package org.springframework.data.gemfire;
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanNameAware;
|
import org.springframework.beans.factory.BeanNameAware;
|
||||||
import org.springframework.beans.factory.FactoryBean;
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import com.gemstone.gemfire.cache.RegionService;
|
import com.gemstone.gemfire.cache.RegionService;
|
||||||
@@ -29,12 +28,7 @@ import com.gemstone.gemfire.cache.client.Pool;
|
|||||||
import com.gemstone.gemfire.cache.client.PoolManager;
|
import com.gemstone.gemfire.cache.client.PoolManager;
|
||||||
import com.gemstone.gemfire.cache.query.Index;
|
import com.gemstone.gemfire.cache.query.Index;
|
||||||
import com.gemstone.gemfire.cache.query.IndexExistsException;
|
import com.gemstone.gemfire.cache.query.IndexExistsException;
|
||||||
import com.gemstone.gemfire.cache.query.IndexInvalidException;
|
|
||||||
import com.gemstone.gemfire.cache.query.IndexNameConflictException;
|
|
||||||
import com.gemstone.gemfire.cache.query.IndexType;
|
|
||||||
import com.gemstone.gemfire.cache.query.QueryService;
|
import com.gemstone.gemfire.cache.query.QueryService;
|
||||||
import com.gemstone.gemfire.cache.query.RegionNotFoundException;
|
|
||||||
import com.springsource.vfabric.licensing.log.Logger;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Factory bean for easy declarative creation of GemFire Indexes.
|
* Factory bean for easy declarative creation of GemFire Indexes.
|
||||||
@@ -48,8 +42,11 @@ public class IndexFactoryBean implements InitializingBean, BeanNameAware, Factor
|
|||||||
private String poolName;
|
private String poolName;
|
||||||
private RegionService cache;
|
private RegionService cache;
|
||||||
private String beanName;
|
private String beanName;
|
||||||
private String name, expression, from, imports;
|
private String name;
|
||||||
private IndexType type = IndexType.FUNCTIONAL;
|
private String expression;
|
||||||
|
private String from;
|
||||||
|
private String imports;
|
||||||
|
private String type;
|
||||||
private boolean override = true;
|
private boolean override = true;
|
||||||
|
|
||||||
public void afterPropertiesSet() throws Exception {
|
public void afterPropertiesSet() throws Exception {
|
||||||
@@ -67,7 +64,13 @@ public class IndexFactoryBean implements InitializingBean, BeanNameAware, Factor
|
|||||||
|
|
||||||
Assert.notNull(queryService, "Query service required for index creation");
|
Assert.notNull(queryService, "Query service required for index creation");
|
||||||
Assert.hasText(expression, "Index expression is required");
|
Assert.hasText(expression, "Index expression is required");
|
||||||
Assert.hasText(from, "Index from clause is required");
|
Assert.hasText(from, "Index from clause (regionPath) is required");
|
||||||
|
|
||||||
|
if (StringUtils.hasText(type)) {
|
||||||
|
if (type.equalsIgnoreCase("KEY") || type.equalsIgnoreCase("PRIMARY_KEY")) {
|
||||||
|
Assert.isNull(imports, "The imports property is not supported for a key index");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
String indexName = StringUtils.hasText(name) ? name : beanName;
|
String indexName = StringUtils.hasText(name) ? name : beanName;
|
||||||
|
|
||||||
@@ -76,41 +79,59 @@ public class IndexFactoryBean implements InitializingBean, BeanNameAware, Factor
|
|||||||
index = createIndex(queryService, indexName);
|
index = createIndex(queryService, indexName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Index createIndex(QueryService queryService, String indexName) throws Exception {
|
private Index createIndex(QueryService queryService, String indexName) throws Exception {
|
||||||
Collection<Index> indexes = queryService.getIndexes();
|
|
||||||
|
|
||||||
Index old = null;
|
Index existingIndex = null;
|
||||||
|
|
||||||
for (Index index : indexes) {
|
for (Index idx : queryService.getIndexes()) {
|
||||||
if (indexName.equals(index.getName())) {
|
if (idx.getName().equals(indexName)) {
|
||||||
if (!override) {
|
existingIndex = idx;
|
||||||
return index;
|
break;
|
||||||
}
|
|
||||||
old = index;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (existingIndex != null) {
|
||||||
if (old != null) {
|
if (!override) {
|
||||||
// compare indices
|
return existingIndex;
|
||||||
if (from.equals(old.getFromClause()) && expression.equals(old.getIndexedExpression())
|
} else {
|
||||||
&& type.equals(old.getType())) {
|
queryService.removeIndex(existingIndex);
|
||||||
return index;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Index index = null;
|
Index index = null;
|
||||||
try {
|
try {
|
||||||
if (StringUtils.hasText(imports)) {
|
if ("KEY".equalsIgnoreCase(type) || "PRIMARY_KEY".equalsIgnoreCase(type)) {
|
||||||
index = queryService.createIndex(indexName, type, expression, from, imports);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
index = queryService.createIndex(indexName, type, expression, from);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (IndexExistsException e) {
|
index = queryService.createKeyIndex(indexName, expression, from);
|
||||||
// This is ok
|
|
||||||
}
|
} else if ("HASH".equalsIgnoreCase(type)) {
|
||||||
|
if (StringUtils.hasText(imports)) {
|
||||||
|
index = queryService.createHashIndex(indexName, expression, from, imports);
|
||||||
|
} else {
|
||||||
|
index = queryService.createHashIndex(indexName, expression, from);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (StringUtils.hasText(imports)) {
|
||||||
|
index = queryService.createIndex(indexName, expression, from, imports);
|
||||||
|
} else {
|
||||||
|
index = queryService.createIndex(indexName, expression, from);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return index;
|
||||||
|
|
||||||
|
} catch (IndexExistsException e) {
|
||||||
|
for (Index idx : queryService.getIndexes()) {
|
||||||
|
if (idx.getName().equals(indexName)) {
|
||||||
|
return idx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
if (existingIndex != null) {
|
||||||
|
if (CollectionUtils.isEmpty(queryService.getIndexes())
|
||||||
|
|| !queryService.getIndexes().contains(existingIndex)) {
|
||||||
|
queryService.getIndexes().add(existingIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
@@ -189,7 +210,7 @@ public class IndexFactoryBean implements InitializingBean, BeanNameAware, Factor
|
|||||||
/**
|
/**
|
||||||
* @param type the type to set
|
* @param type the type to set
|
||||||
*/
|
*/
|
||||||
public void setType(IndexType type) {
|
public void setType(String type) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -808,7 +808,7 @@ public class StubCache implements Cache {
|
|||||||
String indexName = (String)invocation.getArguments()[0];
|
String indexName = (String)invocation.getArguments()[0];
|
||||||
String indexedExpression = (String)invocation.getArguments()[1];
|
String indexedExpression = (String)invocation.getArguments()[1];
|
||||||
String fromClause = (String)invocation.getArguments()[2];
|
String fromClause = (String)invocation.getArguments()[2];
|
||||||
return mockIndex(indexName, null, indexedExpression, fromClause, null, null);
|
return mockIndex(indexName, IndexType.FUNCTIONAL, indexedExpression, fromClause, null);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
when(qs.createIndex(anyString(), anyString(),anyString(),anyString())).thenAnswer(new Answer<Index>(){
|
when(qs.createIndex(anyString(), anyString(),anyString(),anyString())).thenAnswer(new Answer<Index>(){
|
||||||
@@ -817,42 +817,49 @@ public class StubCache implements Cache {
|
|||||||
String indexName = (String)invocation.getArguments()[0];
|
String indexName = (String)invocation.getArguments()[0];
|
||||||
String indexedExpression = (String)invocation.getArguments()[1];
|
String indexedExpression = (String)invocation.getArguments()[1];
|
||||||
String fromClause = (String)invocation.getArguments()[2];
|
String fromClause = (String)invocation.getArguments()[2];
|
||||||
|
String imports = (String)invocation.getArguments()[3];
|
||||||
return mockIndex(indexName, null, indexedExpression, fromClause, null, null);
|
return mockIndex(indexName, IndexType.FUNCTIONAL, indexedExpression, fromClause, imports);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
when(qs.createIndex(anyString(), any(IndexType.class), anyString(), anyString())).thenAnswer(new Answer<Index>(){
|
when(qs.createKeyIndex(anyString(), anyString(),anyString())).thenAnswer(new Answer<Index>(){
|
||||||
@Override
|
@Override
|
||||||
public Index answer(InvocationOnMock invocation) throws Throwable {
|
public Index answer(InvocationOnMock invocation) throws Throwable {
|
||||||
String indexName = (String)invocation.getArguments()[0];
|
String indexName = (String)invocation.getArguments()[0];
|
||||||
IndexType type = (IndexType)invocation.getArguments()[1];
|
String indexedExpression = (String)invocation.getArguments()[1];
|
||||||
String indexedExpression = (String)invocation.getArguments()[2];
|
String fromClause = (String)invocation.getArguments()[2];
|
||||||
String fromClause = (String)invocation.getArguments()[3];
|
|
||||||
|
|
||||||
return mockIndex(indexName, type, indexedExpression, null, fromClause, null);
|
return mockIndex(indexName, IndexType.PRIMARY_KEY, indexedExpression, fromClause, null);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
when(qs.createHashIndex(anyString(), anyString(),anyString())).thenAnswer(new Answer<Index>(){
|
||||||
|
|
||||||
when(qs.createIndex(anyString(), any(IndexType.class),anyString(), anyString(), anyString())).thenAnswer(new Answer<Index>(){
|
|
||||||
@Override
|
@Override
|
||||||
public Index answer(InvocationOnMock invocation) throws Throwable {
|
public Index answer(InvocationOnMock invocation) throws Throwable {
|
||||||
String indexName = (String)invocation.getArguments()[0];
|
String indexName = (String)invocation.getArguments()[0];
|
||||||
IndexType type = (IndexType)invocation.getArguments()[1];
|
String indexedExpression = (String)invocation.getArguments()[1];
|
||||||
String indexedExpression = (String)invocation.getArguments()[2];
|
String fromClause = (String)invocation.getArguments()[2];
|
||||||
String fromClause = (String)invocation.getArguments()[3];
|
|
||||||
String imports = (String)invocation.getArguments()[4];
|
return mockIndex(indexName, IndexType.HASH, indexedExpression, fromClause, null);
|
||||||
return mockIndex(indexName, type, indexedExpression, null, fromClause, imports);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
when(qs.createHashIndex(anyString(), anyString(),anyString(),anyString())).thenAnswer(new Answer<Index>(){
|
||||||
|
@Override
|
||||||
|
public Index answer(InvocationOnMock invocation) throws Throwable {
|
||||||
|
String indexName = (String)invocation.getArguments()[0];
|
||||||
|
String indexedExpression = (String)invocation.getArguments()[1];
|
||||||
|
String fromClause = (String)invocation.getArguments()[2];
|
||||||
|
String imports = (String)invocation.getArguments()[3];
|
||||||
|
|
||||||
|
return mockIndex(indexName, IndexType.HASH, indexedExpression, fromClause, imports);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return qs;
|
return qs;
|
||||||
}
|
}
|
||||||
|
|
||||||
Index mockIndex(String indexName, IndexType indexType,String indexedExpression, String regionPath, String fromClause, String imports){
|
Index mockIndex(String indexName, IndexType indexType,String indexedExpression, String fromClause, String imports){
|
||||||
Index idx = mock(Index.class);
|
Index idx = mock(Index.class);
|
||||||
when(idx.getFromClause()).thenReturn(fromClause);
|
when(idx.getFromClause()).thenReturn(fromClause);
|
||||||
when(idx.getIndexedExpression()).thenReturn(indexedExpression);
|
when(idx.getIndexedExpression()).thenReturn(indexedExpression);
|
||||||
|
|||||||
@@ -2144,12 +2144,11 @@ The name of the index bean definition. If property 'name' is not set, it will be
|
|||||||
]]></xsd:documentation>
|
]]></xsd:documentation>
|
||||||
</xsd:annotation>
|
</xsd:annotation>
|
||||||
</xsd:attribute>
|
</xsd:attribute>
|
||||||
<xsd:attribute name="type" use="optional"
|
<xsd:attribute name="type" use="optional">
|
||||||
default="FUNCTIONAL">
|
|
||||||
<xsd:simpleType>
|
<xsd:simpleType>
|
||||||
<xsd:restriction base="xsd:string">
|
<xsd:restriction base="xsd:string">
|
||||||
<xsd:enumeration value="FUNCTIONAL" />
|
<xsd:enumeration value="HASH" />
|
||||||
<xsd:enumeration value="PRIMARY_KEY" />
|
<xsd:enumeration value="PRIMARY_KEY"/>
|
||||||
</xsd:restriction>
|
</xsd:restriction>
|
||||||
</xsd:simpleType>
|
</xsd:simpleType>
|
||||||
</xsd:attribute>
|
</xsd:attribute>
|
||||||
@@ -2164,7 +2163,13 @@ The name of the index.
|
|||||||
<xsd:attribute name="expression" type="xsd:string"
|
<xsd:attribute name="expression" type="xsd:string"
|
||||||
use="required" />
|
use="required" />
|
||||||
<xsd:attribute name="from" type="xsd:string"
|
<xsd:attribute name="from" type="xsd:string"
|
||||||
use="required" />
|
use="required">
|
||||||
|
<xsd:annotation>
|
||||||
|
<xsd:documentation><![CDATA[
|
||||||
|
Corresponds to the regionPath parameter in createIndex methods.
|
||||||
|
]]></xsd:documentation>
|
||||||
|
</xsd:annotation>
|
||||||
|
</xsd:attribute>
|
||||||
<xsd:attribute name="imports" type="xsd:string"
|
<xsd:attribute name="imports" type="xsd:string"
|
||||||
use="optional" />
|
use="optional" />
|
||||||
<xsd:attribute name="override" type="xsd:string"
|
<xsd:attribute name="override" type="xsd:string"
|
||||||
|
|||||||
@@ -25,8 +25,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.data.gemfire.test.GemfireTestRunner;
|
import org.springframework.data.gemfire.test.GemfireTestRunner;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|
||||||
|
|
||||||
import com.gemstone.gemfire.cache.Cache;
|
import com.gemstone.gemfire.cache.Cache;
|
||||||
import com.gemstone.gemfire.cache.query.Index;
|
import com.gemstone.gemfire.cache.query.Index;
|
||||||
import com.gemstone.gemfire.cache.query.IndexType;
|
import com.gemstone.gemfire.cache.query.IndexType;
|
||||||
@@ -71,6 +69,6 @@ public class IndexNamespaceTest {
|
|||||||
assertEquals("tsi.name", idx.getIndexedExpression());
|
assertEquals("tsi.name", idx.getIndexedExpression());
|
||||||
assertEquals("complex-index", idx.getName());
|
assertEquals("complex-index", idx.getName());
|
||||||
assertEquals(name, idx.getRegion().getName());
|
assertEquals(name, idx.getRegion().getName());
|
||||||
assertEquals(IndexType.PRIMARY_KEY, idx.getType());
|
assertEquals(IndexType.HASH, idx.getType());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
<gfe:index id="simple" expression="status" from="/test-index"/>
|
<gfe:index id="simple" expression="status" from="/test-index"/>
|
||||||
|
|
||||||
<gfe:index id="complex" name="complex-index" expression="tsi.name" from="/test-index tsi" type="PRIMARY_KEY" imports="import java.util"/>
|
<gfe:index id="complex" name="complex-index" expression="tsi.name" from="/test-index tsi" type="HASH" imports="import java.util"/>
|
||||||
|
|
||||||
<gfe:cache />
|
<gfe:cache />
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user