Search in sources :

Example 11 with PdxType

use of org.apache.geode.pdx.internal.PdxType in project geode by apache.

the class PDXCommands method pdxRename.

@CliCommand(value = CliStrings.PDX_RENAME, help = CliStrings.PDX_RENAME__HELP)
@CliMetaData(shellOnly = true, relatedTopic = { CliStrings.TOPIC_GEODE_DISKSTORE })
@ResourceOperation(resource = Resource.DATA, operation = Operation.MANAGE)
public Result pdxRename(@CliOption(key = CliStrings.PDX_RENAME_OLD, mandatory = true, help = CliStrings.PDX_RENAME_OLD__HELP) String oldClassName, @CliOption(key = CliStrings.PDX_RENAME_NEW, mandatory = true, help = CliStrings.PDX_RENAME_NEW__HELP) String newClassName, @CliOption(key = CliStrings.PDX_DISKSTORE, mandatory = true, help = CliStrings.PDX_DISKSTORE__HELP) String diskStore, @CliOption(key = CliStrings.PDX_DISKDIR, mandatory = true, help = CliStrings.PDX_DISKDIR__HELP) String[] diskDirs) {
    try {
        final File[] dirs = new File[diskDirs.length];
        for (int i = 0; i < diskDirs.length; i++) {
            dirs[i] = new File((diskDirs[i]));
        }
        Collection<Object> results = DiskStoreImpl.pdxRename(diskStore, dirs, oldClassName, newClassName);
        if (results.isEmpty()) {
            return ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.PDX_RENAME__EMPTY));
        }
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PrintStream printStream = new PrintStream(outputStream);
        for (Object p : results) {
            if (p instanceof PdxType) {
                ((PdxType) p).toStream(printStream, false);
            } else {
                ((EnumInfo) p).toStream(printStream);
            }
        }
        String resultString = CliStrings.format(CliStrings.PDX_RENAME__SUCCESS, outputStream.toString());
        return ResultBuilder.createInfoResult(resultString.toString());
    } catch (Exception e) {
        return ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.PDX_RENAME__ERROR, e.getMessage()));
    }
}
Also used : PrintStream(java.io.PrintStream) PdxType(org.apache.geode.pdx.internal.PdxType) EnumInfo(org.apache.geode.pdx.internal.EnumInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Example 12 with PdxType

use of org.apache.geode.pdx.internal.PdxType 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 13 with PdxType

use of org.apache.geode.pdx.internal.PdxType 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 14 with PdxType

use of org.apache.geode.pdx.internal.PdxType 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 15 with PdxType

use of org.apache.geode.pdx.internal.PdxType 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

PdxType (org.apache.geode.pdx.internal.PdxType)32 Test (org.junit.Test)18 EnumInfo (org.apache.geode.pdx.internal.EnumInfo)14 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)11 TypeRegistry (org.apache.geode.pdx.internal.TypeRegistry)10 IOException (java.io.IOException)9 Cache (org.apache.geode.cache.Cache)9 CacheFactory (org.apache.geode.cache.CacheFactory)9 Region (org.apache.geode.cache.Region)8 SerializationTest (org.apache.geode.test.junit.categories.SerializationTest)8 File (java.io.File)7 Properties (java.util.Properties)6 InternalCache (org.apache.geode.internal.cache.InternalCache)6 Map (java.util.Map)5 DiskStoreFactory (org.apache.geode.cache.DiskStoreFactory)5 GemFireCacheImpl (org.apache.geode.internal.cache.GemFireCacheImpl)5 PdxInstance (org.apache.geode.pdx.PdxInstance)5 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)5 ArrayList (java.util.ArrayList)4 PdxInstanceImpl (org.apache.geode.pdx.internal.PdxInstanceImpl)4