use of org.apache.geode.cache.AttributesFactory in project geode by apache.
the class CacheUtils method createRegion.
public static Region createRegion(String regionName, Class valueConstraint, boolean indexMaintenanceSynchronous) {
try {
AttributesFactory attributesFactory = new AttributesFactory();
attributesFactory.setValueConstraint(valueConstraint);
attributesFactory.setIndexMaintenanceSynchronous(indexMaintenanceSynchronous);
RegionAttributes regionAttributes = attributesFactory.create();
return cache.createRegion(regionName, regionAttributes);
} catch (Exception e) {
throw new AssertionError(e);
}
}
use of org.apache.geode.cache.AttributesFactory in project geode by apache.
the class CacheUtils method createRegion.
public static Region createRegion(Region parentRegion, String regionName, Class valueConstraint) {
try {
AttributesFactory attributesFactory = new AttributesFactory();
if (valueConstraint != null)
attributesFactory.setValueConstraint(valueConstraint);
RegionAttributes regionAttributes = attributesFactory.create();
return parentRegion.createSubregion(regionName, regionAttributes);
} catch (Exception e) {
throw new AssertionError(e);
}
}
use of org.apache.geode.cache.AttributesFactory in project geode by apache.
the class QueryJUnitTest method test007UndefinedResults.
@Test
public void test007UndefinedResults() {
CacheUtils.log("testQueryExceptionLogMessage");
Region region = CacheUtils.createRegion("Portfolios", Portfolio.class);
region.put("1", new Portfolio(1));
region.put("2", new Portfolio(0));
String queryStr = "SELECT DISTINCT * FROM /Portfolios.ketset";
Query q = CacheUtils.getQueryService().newQuery(queryStr);
Object results = null;
try {
results = q.execute();
} catch (Exception e) {
fail("Query execution failed " + e);
}
assertEquals(0, ((SelectResults) results).size());
PartitionAttributesFactory paf = new PartitionAttributesFactory();
AttributesFactory af = new AttributesFactory();
af.setPartitionAttributes(paf.create());
region = CacheUtils.createRegion("PortfoliosPR", af.create(), false);
region.put("1", new Portfolio(1));
region.put("2", new Portfolio(0));
queryStr = "SELECT DISTINCT * FROM /PortfoliosPR.ketset";
q = CacheUtils.getQueryService().newQuery(queryStr);
try {
results = q.execute();
} catch (Exception e) {
fail("Query execution failed " + e);
}
assertEquals(0, ((SelectResults) results).size());
}
use of org.apache.geode.cache.AttributesFactory in project geode by apache.
the class PdxQueryDUnitTest method testPutAllWithIndexes.
/**
* This test creates 3 cache servers with a PR and one client which puts some PDX values in PR and
* runs a query. This was failing randomly in a POC.
*/
@Test
public void testPutAllWithIndexes() {
final String name = "testRegion";
final Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
VM vm2 = host.getVM(2);
VM vm3 = host.getVM(3);
final Properties config = new Properties();
config.setProperty("locators", "localhost[" + DistributedTestUtils.getDUnitLocatorPort() + "]");
// Start server
vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
Cache cache = new CacheFactory(config).create();
AttributesFactory factory = new AttributesFactory();
PartitionAttributesFactory prfactory = new PartitionAttributesFactory();
prfactory.setRedundantCopies(0);
factory.setPartitionAttributes(prfactory.create());
cache.createRegionFactory(factory.create()).create(name);
try {
startCacheServer(0, false);
} catch (Exception ex) {
Assert.fail("While starting CacheServer", ex);
}
// Create Index on empty region
try {
cache.getQueryService().createIndex("myFuncIndex", "intId", "/" + name);
} catch (Exception e) {
Assert.fail("index creation failed", e);
}
}
});
// Start server
vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
Cache cache = new CacheFactory(config).create();
AttributesFactory factory = new AttributesFactory();
PartitionAttributesFactory prfactory = new PartitionAttributesFactory();
prfactory.setRedundantCopies(0);
factory.setPartitionAttributes(prfactory.create());
cache.createRegionFactory(factory.create()).create(name);
try {
startCacheServer(0, false);
} catch (Exception ex) {
Assert.fail("While starting CacheServer", ex);
}
}
});
// Start server
vm2.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
Cache cache = new CacheFactory(config).create();
AttributesFactory factory = new AttributesFactory();
PartitionAttributesFactory prfactory = new PartitionAttributesFactory();
prfactory.setRedundantCopies(0);
factory.setPartitionAttributes(prfactory.create());
cache.createRegionFactory(factory.create()).create(name);
try {
startCacheServer(0, false);
} catch (Exception ex) {
Assert.fail("While starting CacheServer", ex);
}
}
});
// Create client region
final int port = vm0.invoke(() -> PdxQueryDUnitTest.getCacheServerPort());
final String host0 = NetworkUtils.getServerHostName(vm2.getHost());
vm3.invoke(new CacheSerializableRunnable("Create region") {
public void run2() throws CacheException {
Properties config = new Properties();
config.setProperty("mcast-port", "0");
ClientCache cache = new ClientCacheFactory(config).addPoolServer(host0, port).setPoolPRSingleHopEnabled(true).setPoolSubscriptionEnabled(true).create();
AttributesFactory factory = new AttributesFactory();
cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(name);
}
});
vm3.invoke(new CacheSerializableRunnable("putAll() test") {
@Override
public void run2() throws CacheException {
try {
ClientCache cache = new ClientCacheFactory().create();
Region region = cache.getRegion(name);
QueryService queryService = cache.getQueryService();
String k;
for (int x = 0; x < 285; x++) {
k = Integer.valueOf(x).toString();
PortfolioPdx v = new PortfolioPdx(x, x);
region.put(k, v);
}
Query q = queryService.newQuery("SELECT DISTINCT * from /" + name + " WHERE ID = 2");
SelectResults qResult = (SelectResults) q.execute();
for (Object o : qResult.asList()) {
System.out.println("o = " + o);
}
} catch (Exception e) {
Assert.fail("Querying failed: ", e);
}
}
});
Invoke.invokeInEveryVM(DistributedTestCase.class, "disconnectFromDS");
// }
}
use of org.apache.geode.cache.AttributesFactory in project geode by apache.
the class PdxQueryDUnitTest method testClientServerCountQuery.
/**
* Tests client-server query on PdxInstance.
*/
@Test
public void testClientServerCountQuery() throws CacheException {
final Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
VM vm2 = host.getVM(2);
VM vm3 = host.getVM(3);
final int numberOfEntries = 10;
final String queryStr = "SELECT COUNT(*) FROM " + regName + " WHERE id >= 0";
// Start server1
vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
configAndStartBridgeServer();
Region region = getRootRegion().getSubregion(regionName);
for (int i = 0; i < numberOfEntries; i++) {
region.put("key-" + i, new TestObject(i, "vmware"));
}
}
});
// Start server2
vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
configAndStartBridgeServer();
Region region = getRootRegion().getSubregion(regionName);
}
});
// Client pool.
final int port0 = vm0.invoke(() -> PdxQueryDUnitTest.getCacheServerPort());
final int port1 = vm1.invoke(() -> PdxQueryDUnitTest.getCacheServerPort());
final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
// Create client pool.
final String poolName = "testClientServerQueryPool";
createPool(vm2, poolName, new String[] { host0 }, new int[] { port0 }, true);
createPool(vm3, poolName, new String[] { host0 }, new int[] { port1 }, true);
// Create client region
vm3.invoke(new CacheSerializableRunnable("Create region") {
public void run2() throws CacheException {
QueryService localQueryService = null;
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
ClientServerTestCase.configureConnectionPool(factory, host0, port1, -1, true, -1, -1, null);
Region region = createRegion(regionName, rootRegionName, factory.create());
for (int i = 0; i < numberOfEntries; i++) {
region.put("key-" + i, new TestObject(i, "vmware"));
}
// Execute query locally.
try {
localQueryService = getCache().getQueryService();
} catch (Exception e) {
Assert.fail("Failed to get QueryService.", e);
}
try {
Query query = localQueryService.newQuery(queryStr);
SelectResults results = (SelectResults) query.execute();
assertEquals(numberOfEntries, ((Integer) results.asList().get(0)).intValue());
} catch (Exception e) {
Assert.fail("Failed executing " + queryStr, e);
}
}
});
// Execute client queries
SerializableRunnable executeQueries = new CacheSerializableRunnable("Execute queries") {
public void run2() throws CacheException {
QueryService remoteQueryService = null;
QueryService localQueryService = null;
SelectResults[][] rs = new SelectResults[1][2];
try {
remoteQueryService = (PoolManager.find(poolName)).getQueryService();
localQueryService = getCache().getQueryService();
} catch (Exception e) {
Assert.fail("Failed to get QueryService.", e);
}
try {
logger.info("### Executing Query on server:" + queryStr);
Query query = remoteQueryService.newQuery(queryStr);
rs[0][0] = (SelectResults) query.execute();
assertEquals(numberOfEntries, ((Integer) rs[0][0].asList().get(0)).intValue());
logger.info("### Executing Query locally:" + queryStr);
query = localQueryService.newQuery(queryStr);
rs[0][1] = (SelectResults) query.execute();
assertEquals(numberOfEntries, ((Integer) rs[0][1].asList().get(0)).intValue());
// Compare local and remote query results.
if (!CacheUtils.compareResultsOfWithAndWithoutIndex(rs)) {
fail("Local and Remote Query Results are not matching for query :" + queryStr);
}
} catch (Exception e) {
Assert.fail("Failed executing " + queryStr, e);
}
assertEquals(numberOfEntries, TestObject.numInstance);
}
};
vm3.invoke(executeQueries);
// Check for TestObject instances on Server2.
// It should be 0
vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {
public void run2() throws CacheException {
assertEquals(0, TestObject.numInstance);
}
});
this.closeClient(vm2);
this.closeClient(vm3);
this.closeClient(vm1);
this.closeClient(vm0);
}
Aggregations