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 {
}
}
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;
}
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);
}
Aggregations