Search in sources :

Example 51 with CacheFactory

use of org.apache.geode.cache.CacheFactory in project geode by apache.

the class BackupJUnitTest method createCache.

private void createCache() throws IOException {
    cache = (GemFireCacheImpl) new CacheFactory(props).create();
    ds = cache.getDistributedSystem();
}
Also used : CacheFactory(org.apache.geode.cache.CacheFactory)

Example 52 with CacheFactory

use of org.apache.geode.cache.CacheFactory in project geode by apache.

the class PdxDeleteFieldJUnitTest method testPdxDeleteField.

@Test
public void testPdxDeleteField() throws Exception {
    String DS_NAME = "PdxDeleteFieldJUnitTestDiskStore";
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "");
    File f = new File(DS_NAME);
    f.mkdir();
    try {
        Cache cache = (new CacheFactory(props)).setPdxPersistent(true).setPdxDiskStore(DS_NAME).create();
        try {
            {
                DiskStoreFactory dsf = cache.createDiskStoreFactory();
                dsf.setDiskDirs(new File[] { f });
                dsf.create(DS_NAME);
            }
            RegionFactory<String, PdxValue> rf1 = cache.createRegionFactory(RegionShortcut.LOCAL_PERSISTENT);
            rf1.setDiskStoreName(DS_NAME);
            Region<String, PdxValue> region1 = rf1.create("region1");
            PdxValue pdxValue = new PdxValue(1, 2L);
            region1.put("key1", pdxValue);
            byte[] pdxValueBytes = BlobHelper.serializeToBlob(pdxValue);
            {
                PdxValue deserializedPdxValue = (PdxValue) BlobHelper.deserializeBlob(pdxValueBytes);
                assertEquals(1, deserializedPdxValue.value);
                assertEquals(2L, deserializedPdxValue.fieldToDelete);
            }
            cache.close();
            Collection<PdxType> types = DiskStoreImpl.pdxDeleteField(DS_NAME, new File[] { f }, PdxValue.class.getName(), "fieldToDelete");
            assertEquals(1, types.size());
            PdxType pt = types.iterator().next();
            assertEquals(PdxValue.class.getName(), pt.getClassName());
            assertEquals(null, pt.getPdxField("fieldToDelete"));
            types = DiskStoreImpl.getPdxTypes(DS_NAME, new File[] { f });
            assertEquals(1, types.size());
            pt = types.iterator().next();
            assertEquals(PdxValue.class.getName(), pt.getClassName());
            assertEquals(true, pt.getHasDeletedField());
            assertEquals(null, pt.getPdxField("fieldToDelete"));
            cache = (new CacheFactory(props)).setPdxPersistent(true).setPdxDiskStore(DS_NAME).create();
            {
                DiskStoreFactory dsf = cache.createDiskStoreFactory();
                dsf.setDiskDirs(new File[] { f });
                dsf.create(DS_NAME);
                PdxValue deserializedPdxValue = (PdxValue) BlobHelper.deserializeBlob(pdxValueBytes);
                assertEquals(1, deserializedPdxValue.value);
                assertEquals(0L, deserializedPdxValue.fieldToDelete);
            }
        } finally {
            if (!cache.isClosed()) {
                cache.close();
            }
        }
    } finally {
        FileUtils.deleteDirectory(f);
    }
}
Also used : PdxType(org.apache.geode.pdx.internal.PdxType) Properties(java.util.Properties) CacheFactory(org.apache.geode.cache.CacheFactory) File(java.io.File) DiskStoreFactory(org.apache.geode.cache.DiskStoreFactory) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 53 with CacheFactory

use of org.apache.geode.cache.CacheFactory 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 54 with CacheFactory

use of org.apache.geode.cache.CacheFactory in project geode by apache.

the class PdxRenameDUnitTest method testPdxRenameVersioning.

@Test
public void testPdxRenameVersioning() throws Exception {
    final String DS_NAME = "PdxRenameDUnitTestDiskStore";
    final String DS_NAME2 = "PdxRenameDUnitTestDiskStore2";
    final int[] locatorPorts = AvailablePortHelper.getRandomAvailableTCPPorts(2);
    final File f = new File(DS_NAME);
    f.mkdir();
    final File f2 = new File(DS_NAME2);
    f2.mkdir();
    this.filesToBeDeleted.add(DS_NAME);
    this.filesToBeDeleted.add(DS_NAME2);
    final Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "localhost[" + locatorPorts[0] + "],localhost[" + locatorPorts[1] + "]");
    props.setProperty(ENABLE_CLUSTER_CONFIGURATION, "false");
    Host host = Host.getHost(0);
    VM vm1 = host.getVM(0);
    VM vm2 = host.getVM(1);
    vm1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            disconnectFromDS();
            props.setProperty(START_LOCATOR, "localhost[" + locatorPorts[0] + "]");
            final Cache cache = (new CacheFactory(props)).setPdxPersistent(true).setPdxDiskStore(DS_NAME).create();
            DiskStoreFactory dsf = cache.createDiskStoreFactory();
            dsf.setDiskDirs(new File[] { f });
            dsf.create(DS_NAME);
            RegionFactory<String, PdxValue> rf1 = cache.createRegionFactory(RegionShortcut.REPLICATE_PERSISTENT);
            rf1.setDiskStoreName(DS_NAME);
            Region<String, PdxValue> region1 = rf1.create("region1");
            region1.put("key1", new PdxValue(1));
            return null;
        }
    });
    vm2.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            disconnectFromDS();
            props.setProperty(START_LOCATOR, "localhost[" + locatorPorts[1] + "]");
            final Cache cache = (new CacheFactory(props)).setPdxReadSerialized(true).setPdxPersistent(true).setPdxDiskStore(DS_NAME2).create();
            DiskStoreFactory dsf = cache.createDiskStoreFactory();
            dsf.setDiskDirs(new File[] { f2 });
            dsf.create(DS_NAME2);
            RegionFactory rf1 = cache.createRegionFactory(RegionShortcut.REPLICATE_PERSISTENT);
            rf1.setDiskStoreName(DS_NAME2);
            Region region1 = rf1.create("region1");
            Object v = region1.get("key1");
            assertNotNull(v);
            cache.close();
            return null;
        }
    });
    vm1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Cache cache = CacheFactory.getAnyInstance();
            if (cache != null && !cache.isClosed()) {
                cache.close();
            }
            return null;
        }
    });
    vm1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Collection<Object> renameResults = DiskStoreImpl.pdxRename(DS_NAME, new File[] { f }, "apache", "pivotal");
            assertEquals(2, renameResults.size());
            for (Object o : renameResults) {
                if (o instanceof PdxType) {
                    PdxType t = (PdxType) o;
                    assertEquals("org.pivotal.geode.internal.PdxRenameDUnitTest$PdxValue", t.getClassName());
                } else {
                    EnumInfo ei = (EnumInfo) o;
                    assertEquals("org.pivotal.geode.internal.PdxRenameDUnitTest$Day", ei.getClassName());
                }
            }
            return null;
        }
    });
    vm1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            props.setProperty(START_LOCATOR, "localhost[" + locatorPorts[0] + "]");
            final Cache cache = (new CacheFactory(props)).setPdxPersistent(true).setPdxDiskStore(DS_NAME).create();
            DiskStoreFactory dsf = cache.createDiskStoreFactory();
            dsf.setDiskDirs(new File[] { f });
            dsf.create(DS_NAME);
            RegionFactory<String, PdxValue> rf1 = cache.createRegionFactory(RegionShortcut.REPLICATE_PERSISTENT);
            rf1.setDiskStoreName(DS_NAME);
            Region<String, PdxValue> region1 = rf1.create("region1");
            return null;
        }
    });
    vm2.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            disconnectFromDS();
            props.setProperty(START_LOCATOR, "localhost[" + locatorPorts[1] + "]");
            final Cache cache = (new CacheFactory(props)).setPdxReadSerialized(true).setPdxPersistent(true).setPdxDiskStore(DS_NAME2).create();
            DiskStoreFactory dsf = cache.createDiskStoreFactory();
            dsf.setDiskDirs(new File[] { f2 });
            dsf.create(DS_NAME2);
            RegionFactory rf1 = cache.createRegionFactory(RegionShortcut.REPLICATE_PERSISTENT);
            rf1.setDiskStoreName(DS_NAME2);
            Region region1 = rf1.create("region1");
            PdxInstance v = (PdxInstance) region1.get("key1");
            assertNotNull(v);
            assertEquals("org.pivotal.geode.internal.PdxRenameDUnitTest$PdxValue", ((PdxInstanceImpl) v).getClassName());
            cache.close();
            return null;
        }
    });
    vm1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Cache cache = CacheFactory.getAnyInstance();
            if (cache != null && !cache.isClosed()) {
                cache.close();
            }
            return null;
        }
    });
}
Also used : PdxType(org.apache.geode.pdx.internal.PdxType) EnumInfo(org.apache.geode.pdx.internal.EnumInfo) Host(org.apache.geode.test.dunit.Host) Properties(java.util.Properties) IOException(java.io.IOException) DiskStoreFactory(org.apache.geode.cache.DiskStoreFactory) RegionFactory(org.apache.geode.cache.RegionFactory) PdxInstance(org.apache.geode.pdx.PdxInstance) PdxInstanceImpl(org.apache.geode.pdx.internal.PdxInstanceImpl) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) Collection(java.util.Collection) CacheFactory(org.apache.geode.cache.CacheFactory) File(java.io.File) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 55 with CacheFactory

use of org.apache.geode.cache.CacheFactory in project geode by apache.

the class PdxRenameJUnitTest method testGetPdxTypes.

@Test
public void testGetPdxTypes() throws Exception {
    String DS_NAME = "PdxRenameJUnitTestDiskStore";
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "");
    File f = new File(DS_NAME);
    f.mkdir();
    try {
        final Cache cache = (new CacheFactory(props)).setPdxPersistent(true).setPdxDiskStore(DS_NAME).create();
        try {
            DiskStoreFactory dsf = cache.createDiskStoreFactory();
            dsf.setDiskDirs(new File[] { f });
            dsf.create(DS_NAME);
            RegionFactory<String, PdxValue> rf1 = cache.createRegionFactory(RegionShortcut.LOCAL_PERSISTENT);
            rf1.setDiskStoreName(DS_NAME);
            Region<String, PdxValue> region1 = rf1.create("region1");
            region1.put("key1", new PdxValue(1));
            cache.close();
            Collection<PdxType> types = DiskStoreImpl.getPdxTypes(DS_NAME, new File[] { f });
            assertEquals(1, types.size());
            assertEquals(PdxValue.class.getName(), types.iterator().next().getClassName());
        } finally {
            if (!cache.isClosed()) {
                cache.close();
            }
        }
    } finally {
        FileUtils.deleteDirectory(f);
    }
}
Also used : PdxType(org.apache.geode.pdx.internal.PdxType) Properties(java.util.Properties) CacheFactory(org.apache.geode.cache.CacheFactory) File(java.io.File) DiskStoreFactory(org.apache.geode.cache.DiskStoreFactory) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

CacheFactory (org.apache.geode.cache.CacheFactory)125 Properties (java.util.Properties)51 Test (org.junit.Test)51 Cache (org.apache.geode.cache.Cache)44 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)38 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)28 Region (org.apache.geode.cache.Region)21 ClientCacheFactory (org.apache.geode.cache.client.ClientCacheFactory)18 CacheServer (org.apache.geode.cache.server.CacheServer)17 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)17 File (java.io.File)15 Before (org.junit.Before)14 SerializationTest (org.apache.geode.test.junit.categories.SerializationTest)13 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)12 ClientCache (org.apache.geode.cache.client.ClientCache)11 Host (org.apache.geode.test.dunit.Host)11 IOException (java.io.IOException)10 AttributesFactory (org.apache.geode.cache.AttributesFactory)10 PdxType (org.apache.geode.pdx.internal.PdxType)9 MyPdxSerializer (com.examples.snapshot.MyPdxSerializer)8