Search in sources :

Example 1 with PdxField

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

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

the class DiskStoreImpl method pdxDeleteField.

private Collection<PdxType> pdxDeleteField(String className, String fieldName) throws IOException {
    // Since we are recovering a disk store, the cast from DiskRegionView -->
    // PlaceHolderDiskRegion
    // and from RegionEntry --> DiskEntry should be ok.
    // In offline mode, we need to schedule the regions to be recovered
    // explicitly.
    DiskRegionView foundPdx = null;
    for (DiskRegionView drv : getKnown()) {
        if (drv.getName().equals(PeerTypeRegistration.REGION_FULL_PATH)) {
            foundPdx = drv;
            scheduleForRecovery((PlaceHolderDiskRegion) drv);
        }
    }
    if (foundPdx == null) {
        throw new IllegalStateException("The disk store does not contain any PDX types.");
    }
    recoverRegionsThatAreReady();
    PersistentOplogSet oplogSet = (PersistentOplogSet) getOplogSet(foundPdx);
    ArrayList<PdxType> result = new ArrayList<PdxType>();
    for (RegionEntry re : foundPdx.getRecoveredEntryMap().regionEntries()) {
        Object value = re._getValueRetain(foundPdx, true);
        if (Token.isRemoved(value)) {
            continue;
        }
        if (value instanceof CachedDeserializable) {
            value = ((CachedDeserializable) value).getDeserializedForReading();
        }
        if (value instanceof EnumInfo) {
            // nothing to delete in an enum
            continue;
        }
        PdxType type = (PdxType) value;
        if (type.getClassName().equals(className)) {
            PdxField field = type.getPdxField(fieldName);
            if (field != null) {
                field.setDeleted(true);
                type.setHasDeletedField(true);
                result.add(type);
                oplogSet.offlineModify(foundPdx, (DiskEntry) re, BlobHelper.serializeToBlob(type), true);
            }
        }
    }
    return result;
}
Also used : PdxType(org.apache.geode.pdx.internal.PdxType) EnumInfo(org.apache.geode.pdx.internal.EnumInfo) ArrayList(java.util.ArrayList) PdxField(org.apache.geode.pdx.internal.PdxField) DiskRegionView(org.apache.geode.internal.cache.persistence.DiskRegionView)

Example 3 with PdxField

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

the class AutoSerializableJUnitTest method testPrimitiveObjects.

@Test
public void testPrimitiveObjects() throws Exception {
    setupSerializer(new PrimitiveObjectsAutoSerializer(true, "org.apache.geode.pdx.AutoSerializableJUnitTest.PrimitiveObjectHolder"), true);
    PrimitiveObjectHolder nullHolder = new PrimitiveObjectHolder();
    PrimitiveObjectHolder defaultHolder = new PrimitiveObjectHolder();
    defaultHolder.bool = false;
    defaultHolder.b = 0;
    defaultHolder.c = 0;
    defaultHolder.s = 0;
    defaultHolder.i = 0;
    defaultHolder.l = 0L;
    defaultHolder.f = 0.0f;
    defaultHolder.d = 0.0;
    HeapDataOutputStream out = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeObject(nullHolder, out);
    PdxInstance pi = DataSerializer.readObject(new DataInputStream(new ByteArrayInputStream(out.toByteArray())));
    PdxField pf = ((PdxInstanceImpl) pi).getPdxField("f");
    assertEquals(FieldType.FLOAT, pf.getFieldType());
    Object dObj = pi.getObject();
    assertFalse(nullHolder.equals(dObj));
    assertEquals(defaultHolder, dObj);
    out = new HeapDataOutputStream(Version.CURRENT);
    DataSerializer.writeObject(defaultHolder, out);
    pi = DataSerializer.readObject(new DataInputStream(new ByteArrayInputStream(out.toByteArray())));
    dObj = pi.getObject();
    assertEquals(defaultHolder, dObj);
}
Also used : PdxInstanceImpl(org.apache.geode.pdx.internal.PdxInstanceImpl) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) PdxField(org.apache.geode.pdx.internal.PdxField) PdxSerializerObject(org.apache.geode.internal.PdxSerializerObject) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

PdxField (org.apache.geode.pdx.internal.PdxField)3 PdxInstanceImpl (org.apache.geode.pdx.internal.PdxInstanceImpl)2 PdxType (org.apache.geode.pdx.internal.PdxType)2 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 Cache (org.apache.geode.cache.Cache)1 CacheFactory (org.apache.geode.cache.CacheFactory)1 HeapDataOutputStream (org.apache.geode.internal.HeapDataOutputStream)1 PdxSerializerObject (org.apache.geode.internal.PdxSerializerObject)1 GemFireCacheImpl (org.apache.geode.internal.cache.GemFireCacheImpl)1 DiskRegionView (org.apache.geode.internal.cache.persistence.DiskRegionView)1 PdxInstance (org.apache.geode.pdx.PdxInstance)1 PdxInstanceFactory (org.apache.geode.pdx.PdxInstanceFactory)1 EnumInfo (org.apache.geode.pdx.internal.EnumInfo)1 TypeRegistry (org.apache.geode.pdx.internal.TypeRegistry)1 SerializationTest (org.apache.geode.test.junit.categories.SerializationTest)1