Search in sources :

Example 11 with PdxInstanceFactory

use of org.apache.geode.pdx.PdxInstanceFactory in project geode by apache.

the class PdxQueryDUnitTest method testLocalPRQuery.

/**
   * Tests query on with PR.
   */
@Test
public void testLocalPRQuery() 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 = 100;
    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            configAndStartBridgeServer(true, true);
        }
    });
    vm1.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            configAndStartBridgeServer(true, false);
        }
    });
    vm2.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            configAndStartBridgeServer(true, false);
        }
    });
    vm3.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            configAndStartBridgeServer(true, false);
        }
    });
    // Load region using class loader and execute query on the same thread.
    vm0.invoke(new CacheSerializableRunnable("Create Bridge Server") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(regionName);
            try {
                // Load TestObject
                for (int i = 0; i < numberOfEntries; i++) {
                    PdxInstanceFactory pdxInstanceFactory = PdxInstanceFactoryImpl.newCreator("PortfolioPdxVersion", false);
                    PortfolioPdxVersion portfolioPdxVersion = new PortfolioPdxVersion(new Integer(i), new Integer(i));
                    PdxInstance pdxInstance = portfolioPdxVersion.createPdxInstance(pdxInstanceFactory);
                    region.put("key-" + i, pdxInstance);
                }
            } catch (Exception ex) {
                Assert.fail("Failed to load the class.", ex);
            }
            QueryService localQueryService = null;
            try {
                localQueryService = region.getCache().getQueryService();
            } catch (Exception e) {
                Assert.fail("Failed to get QueryService.", e);
            }
            for (int i = 1; i < 3; i++) {
                try {
                    logger.info("### Executing Query on server:" + queryString[i]);
                    Query query = localQueryService.newQuery(queryString[i]);
                    SelectResults rs = (SelectResults) query.execute();
                    assertEquals(numberOfEntries, rs.size());
                } catch (Exception e) {
                    Assert.fail("Failed executing " + queryString[i], e);
                }
            }
        }
    });
    this.closeClient(vm2);
    this.closeClient(vm3);
    this.closeClient(vm1);
    this.closeClient(vm0);
}
Also used : PdxInstanceFactory(org.apache.geode.pdx.PdxInstanceFactory) Query(org.apache.geode.cache.query.Query) CacheException(org.apache.geode.cache.CacheException) Host(org.apache.geode.test.dunit.Host) CacheException(org.apache.geode.cache.CacheException) SelectResults(org.apache.geode.cache.query.SelectResults) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) PdxInstance(org.apache.geode.pdx.PdxInstance) QueryService(org.apache.geode.cache.query.QueryService) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 12 with PdxInstanceFactory

use of org.apache.geode.pdx.PdxInstanceFactory in project geode by apache.

the class PdxDeleteFieldJUnitTest method testPdxFieldDelete.

@Test
public void testPdxFieldDelete() throws Exception {
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "");
    try {
        Cache cache = (new CacheFactory(props)).create();
        try {
            PdxValue pdxValue = new PdxValue(1, 2L);
            byte[] pdxValueBytes = BlobHelper.serializeToBlob(pdxValue);
            {
                PdxValue deserializedPdxValue = (PdxValue) BlobHelper.deserializeBlob(pdxValueBytes);
                assertEquals(1, deserializedPdxValue.value);
                assertEquals(2L, deserializedPdxValue.fieldToDelete);
            }
            PdxType pt;
            // force PdxInstance on deserialization
            DefaultQuery.setPdxReadSerialized(true);
            try {
                PdxInstanceImpl pi = (PdxInstanceImpl) BlobHelper.deserializeBlob(pdxValueBytes);
                pt = pi.getPdxType();
                assertEquals(1, pi.getField("value"));
                assertEquals(2L, pi.getField("fieldToDelete"));
            } finally {
                DefaultQuery.setPdxReadSerialized(false);
            }
            assertEquals(PdxValue.class.getName(), pt.getClassName());
            PdxField field = pt.getPdxField("fieldToDelete");
            pt.setHasDeletedField(true);
            field.setDeleted(true);
            assertEquals(null, pt.getPdxField("fieldToDelete"));
            assertEquals(2, pt.getFieldCount());
            {
                PdxValue deserializedPdxValue = (PdxValue) BlobHelper.deserializeBlob(pdxValueBytes);
                assertEquals(1, deserializedPdxValue.value);
                // fieldToDelete should now be 0 (the default) instead of 2.
                assertEquals(0L, deserializedPdxValue.fieldToDelete);
            }
            // force PdxInstance on deserialization
            DefaultQuery.setPdxReadSerialized(true);
            try {
                PdxInstance pi = (PdxInstance) BlobHelper.deserializeBlob(pdxValueBytes);
                assertEquals(1, pi.getField("value"));
                assertEquals(false, pi.hasField("fieldToDelete"));
                assertEquals(null, pi.getField("fieldToDelete"));
                assertSame(pt, ((PdxInstanceImpl) pi).getPdxType());
                PdxValue deserializedPdxValue = (PdxValue) pi.getObject();
                assertEquals(1, deserializedPdxValue.value);
                assertEquals(0L, deserializedPdxValue.fieldToDelete);
            } finally {
                DefaultQuery.setPdxReadSerialized(false);
            }
            TypeRegistry tr = ((GemFireCacheImpl) cache).getPdxRegistry();
            // Clear the local registry so we will regenerate a type for the same class
            tr.testClearLocalTypeRegistry();
            {
                PdxInstanceFactory piFactory = cache.createPdxInstanceFactory(PdxValue.class.getName());
                piFactory.writeInt("value", 1);
                PdxInstance pi = piFactory.create();
                assertEquals(1, pi.getField("value"));
                assertEquals(null, pi.getField("fieldToDelete"));
                PdxType pt2 = ((PdxInstanceImpl) pi).getPdxType();
                assertEquals(null, pt2.getPdxField("fieldToDelete"));
                assertEquals(1, pt2.getFieldCount());
            }
        } finally {
            if (!cache.isClosed()) {
                cache.close();
            }
        }
    } finally {
    }
}
Also used : PdxInstanceFactory(org.apache.geode.pdx.PdxInstanceFactory) PdxType(org.apache.geode.pdx.internal.PdxType) PdxInstanceImpl(org.apache.geode.pdx.internal.PdxInstanceImpl) PdxInstance(org.apache.geode.pdx.PdxInstance) PdxField(org.apache.geode.pdx.internal.PdxField) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) Properties(java.util.Properties) CacheFactory(org.apache.geode.cache.CacheFactory) TypeRegistry(org.apache.geode.pdx.internal.TypeRegistry) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 13 with PdxInstanceFactory

use of org.apache.geode.pdx.PdxInstanceFactory in project geode by apache.

the class TypedJsonJUnitTest method testPDXObject.

@Test
public void testPDXObject() {
    final Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    DistributedSystem.connect(props);
    Cache cache = new CacheFactory().create();
    PdxInstanceFactory pf = PdxInstanceFactoryImpl.newCreator("Portfolio", false);
    Portfolio p = new Portfolio(2);
    pf.writeInt("ID", 111);
    pf.writeString("status", "active");
    pf.writeString("secId", "IBM");
    pf.writeObject("portfolio", p);
    PdxInstance pi = pf.create();
    TypedJson tJsonObj = new TypedJson(RESULT, pi);
    System.out.println(tJsonObj);
    cache.close();
}
Also used : PdxInstanceFactory(org.apache.geode.pdx.PdxInstanceFactory) PdxInstance(org.apache.geode.pdx.PdxInstance) Portfolio(org.apache.geode.cache.query.data.Portfolio) TypedJson(org.apache.geode.management.internal.cli.json.TypedJson) CacheFactory(org.apache.geode.cache.CacheFactory) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 14 with PdxInstanceFactory

use of org.apache.geode.pdx.PdxInstanceFactory in project geode by apache.

the class TypedJsonJUnitTest method testNestedPDXObject.

@Test
public void testNestedPDXObject() {
    final Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    DistributedSystem.connect(props);
    Cache cache = new CacheFactory().create();
    PdxInstanceFactory pf = PdxInstanceFactoryImpl.newCreator("Portfolio", false);
    pf.writeInt("ID", 111);
    pf.writeString("status", "active");
    pf.writeString("secId", "IBM");
    PdxInstance pi = pf.create();
    PDXContainer cont = new PDXContainer(1);
    cont.setPi(pi);
    TypedJson tJsonObj = new TypedJson(RESULT, cont);
    System.out.println(tJsonObj);
    cache.close();
}
Also used : PdxInstanceFactory(org.apache.geode.pdx.PdxInstanceFactory) PdxInstance(org.apache.geode.pdx.PdxInstance) TypedJson(org.apache.geode.management.internal.cli.json.TypedJson) CacheFactory(org.apache.geode.cache.CacheFactory) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 15 with PdxInstanceFactory

use of org.apache.geode.pdx.PdxInstanceFactory in project geode by apache.

the class QueryDataDUnitTest method putPdxInstances.

private void putPdxInstances(final String regionName) throws CacheException {
    Region region = this.managementTestRule.getCache().getRegion(regionName);
    PdxInstanceFactory pdxInstanceFactory = PdxInstanceFactoryImpl.newCreator("Portfolio", false);
    pdxInstanceFactory.writeInt("ID", 111);
    pdxInstanceFactory.writeString("status", "active");
    pdxInstanceFactory.writeString("secId", "IBM");
    PdxInstance pdxInstance = pdxInstanceFactory.create();
    region.put("IBM", pdxInstance);
    pdxInstanceFactory = PdxInstanceFactoryImpl.newCreator("Portfolio", false);
    pdxInstanceFactory.writeInt("ID", 222);
    pdxInstanceFactory.writeString("status", "inactive");
    pdxInstanceFactory.writeString("secId", "YHOO");
    pdxInstance = pdxInstanceFactory.create();
    region.put("YHOO", pdxInstance);
    pdxInstanceFactory = PdxInstanceFactoryImpl.newCreator("Portfolio", false);
    pdxInstanceFactory.writeInt("ID", 333);
    pdxInstanceFactory.writeString("status", "active");
    pdxInstanceFactory.writeString("secId", "GOOGL");
    pdxInstance = pdxInstanceFactory.create();
    region.put("GOOGL", pdxInstance);
    pdxInstanceFactory = PdxInstanceFactoryImpl.newCreator("Portfolio", false);
    pdxInstanceFactory.writeInt("ID", 111);
    pdxInstanceFactory.writeString("status", "inactive");
    pdxInstanceFactory.writeString("secId", "VMW");
    pdxInstance = pdxInstanceFactory.create();
    region.put("VMW", pdxInstance);
}
Also used : PdxInstanceFactory(org.apache.geode.pdx.PdxInstanceFactory) PdxInstance(org.apache.geode.pdx.PdxInstance) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) BucketRegion(org.apache.geode.internal.cache.BucketRegion) Region(org.apache.geode.cache.Region)

Aggregations

PdxInstanceFactory (org.apache.geode.pdx.PdxInstanceFactory)23 PdxInstance (org.apache.geode.pdx.PdxInstance)22 Test (org.junit.Test)18 Region (org.apache.geode.cache.Region)13 Host (org.apache.geode.test.dunit.Host)12 VM (org.apache.geode.test.dunit.VM)12 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)12 CacheException (org.apache.geode.cache.CacheException)11 QueryService (org.apache.geode.cache.query.QueryService)11 SelectResults (org.apache.geode.cache.query.SelectResults)11 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)8 ClientCache (org.apache.geode.cache.client.ClientCache)7 ClientCacheFactory (org.apache.geode.cache.client.ClientCacheFactory)7 CacheServer (org.apache.geode.cache.server.CacheServer)7 GemFireCacheImpl (org.apache.geode.internal.cache.GemFireCacheImpl)6 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)6 Query (org.apache.geode.cache.query.Query)4 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)4 PdxString (org.apache.geode.pdx.internal.PdxString)4 Cache (org.apache.geode.cache.Cache)3