Search in sources :

Example 51 with PdxInstance

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

the class CopyJUnitTest method testIsWellKnownImmutableInstance.

@Test
public void testIsWellKnownImmutableInstance() {
    assertEquals(true, CopyHelper.isWellKnownImmutableInstance("abc"));
    assertEquals(true, CopyHelper.isWellKnownImmutableInstance(Integer.valueOf(0)));
    assertEquals(true, CopyHelper.isWellKnownImmutableInstance(Long.valueOf(0)));
    assertEquals(true, CopyHelper.isWellKnownImmutableInstance(Byte.valueOf((byte) 0)));
    assertEquals(true, CopyHelper.isWellKnownImmutableInstance(Short.valueOf((short) 0)));
    assertEquals(true, CopyHelper.isWellKnownImmutableInstance(Float.valueOf((float) 1.2)));
    assertEquals(true, CopyHelper.isWellKnownImmutableInstance(Double.valueOf(1.2)));
    assertEquals(true, CopyHelper.isWellKnownImmutableInstance(Character.valueOf((char) 0)));
    assertEquals(true, CopyHelper.isWellKnownImmutableInstance(new BigInteger("1234")));
    assertEquals(true, CopyHelper.isWellKnownImmutableInstance(new BigDecimal("123.4556")));
    assertEquals(true, CopyHelper.isWellKnownImmutableInstance(new UUID(1L, 2L)));
    PdxInstance pi = new PdxInstance() {

        public Object getObject() {
            return null;
        }

        public Object getObject(Object pdxObject) {
            return null;
        }

        public boolean hasField(String fieldName) {
            return false;
        }

        public List<String> getFieldNames() {
            return null;
        }

        public boolean isIdentityField(String fieldName) {
            return false;
        }

        public Object getField(String fieldName) {
            return null;
        }

        public WritablePdxInstance createWriter() {
            return null;
        }

        public String getClassName() {
            return null;
        }

        public boolean isEnum() {
            return false;
        }
    };
    WritablePdxInstance wpi = new WritablePdxInstance() {

        public Object getObject() {
            return null;
        }

        public Object getObject(Object pdxObject) {
            return null;
        }

        public boolean hasField(String fieldName) {
            return false;
        }

        public List<String> getFieldNames() {
            return null;
        }

        public boolean isIdentityField(String fieldName) {
            return false;
        }

        public Object getField(String fieldName) {
            return null;
        }

        public WritablePdxInstance createWriter() {
            return null;
        }

        public void setField(String fieldName, Object value) {
        }

        public String getClassName() {
            return null;
        }

        public boolean isEnum() {
            return false;
        }
    };
    assertEquals(true, CopyHelper.isWellKnownImmutableInstance(pi));
    assertEquals(false, CopyHelper.isWellKnownImmutableInstance(wpi));
    assertEquals(false, CopyHelper.isWellKnownImmutableInstance(new Object()));
}
Also used : PdxInstance(org.apache.geode.pdx.PdxInstance) WritablePdxInstance(org.apache.geode.pdx.WritablePdxInstance) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) WritablePdxInstance(org.apache.geode.pdx.WritablePdxInstance) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 52 with PdxInstance

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

the class AbstractRegionEntry method checkOffHeapEquals.

private static boolean checkOffHeapEquals(@Unretained StoredObject ohVal, @Unretained Object obj) {
    if (ohVal.isSerializedPdxInstance()) {
        PdxInstance pi = InternalDataSerializer.readPdxInstance(ohVal.getSerializedValue(), GemFireCacheImpl.getForPdx("Could not check value equality"));
        return checkPdxEquals(pi, obj);
    }
    if (obj instanceof StoredObject) {
        return ohVal.checkDataEquals((StoredObject) obj);
    } else {
        byte[] serializedObj;
        if (obj instanceof CachedDeserializable) {
            CachedDeserializable cdObj = (CachedDeserializable) obj;
            if (!ohVal.isSerialized()) {
                assert cdObj.isSerialized();
                return false;
            }
            serializedObj = cdObj.getSerializedValue();
        } else if (obj instanceof byte[]) {
            if (ohVal.isSerialized()) {
                return false;
            }
            serializedObj = (byte[]) obj;
        } else {
            if (!ohVal.isSerialized()) {
                return false;
            }
            if (obj == null || obj == Token.NOT_AVAILABLE || Token.isInvalidOrRemoved(obj)) {
                return false;
            }
            serializedObj = EntryEventImpl.serialize(obj);
        }
        return ohVal.checkDataEquals(serializedObj);
    }
}
Also used : PdxInstance(org.apache.geode.pdx.PdxInstance) StoredObject(org.apache.geode.internal.offheap.StoredObject)

Example 53 with PdxInstance

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

the class AbstractRegionEntry method checkPdxEquals.

/**
   * This method fixes bug 43643
   */
private static boolean checkPdxEquals(PdxInstance pdx, Object obj) {
    if (!(obj instanceof PdxInstance)) {
        // if we are not readSerialized.
        if (obj instanceof CachedDeserializable) {
            CachedDeserializable cdObj = (CachedDeserializable) obj;
            if (!cdObj.isSerialized()) {
                // obj is actually a byte[] which will never be equal to a PdxInstance
                return false;
            }
            Object cdVal = cdObj.getValue();
            if (cdVal instanceof byte[]) {
                byte[] cdValBytes = (byte[]) cdVal;
                PdxInstance pi = InternalDataSerializer.readPdxInstance(cdValBytes, GemFireCacheImpl.getForPdx("Could not check value equality"));
                if (pi != null) {
                    return pi.equals(pdx);
                } else {
                    // since obj is serialized as something other than pdx it must not equal our pdx
                    return false;
                }
            } else {
                // remove the cd wrapper so that obj is the actual value we want to compare.
                obj = cdVal;
            }
        }
        if (obj != null && obj.getClass().getName().equals(pdx.getClassName())) {
            InternalCache internalCache = GemFireCacheImpl.getForPdx("Could not access Pdx registry");
            if (internalCache != null) {
                PdxSerializer pdxSerializer;
                if (obj instanceof PdxSerializable) {
                    pdxSerializer = null;
                } else {
                    pdxSerializer = internalCache.getPdxSerializer();
                }
                if (pdxSerializer != null || obj instanceof PdxSerializable) {
                    // try to convert obj to a PdxInstance
                    HeapDataOutputStream hdos = new HeapDataOutputStream(Version.CURRENT);
                    try {
                        if (InternalDataSerializer.autoSerialized(obj, hdos) || InternalDataSerializer.writePdx(hdos, internalCache, obj, pdxSerializer)) {
                            PdxInstance pi = InternalDataSerializer.readPdxInstance(hdos.toByteArray(), internalCache);
                            if (pi != null) {
                                obj = pi;
                            }
                        }
                    } catch (IOException | PdxSerializationException ignore) {
                    // we are not able to convert it so just fall through
                    }
                }
            }
        }
    }
    return basicEquals(obj, pdx);
}
Also used : PdxSerializer(org.apache.geode.pdx.PdxSerializer) PdxInstance(org.apache.geode.pdx.PdxInstance) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) StoredObject(org.apache.geode.internal.offheap.StoredObject) IOException(java.io.IOException) PdxSerializationException(org.apache.geode.pdx.PdxSerializationException) PdxSerializable(org.apache.geode.pdx.PdxSerializable)

Example 54 with PdxInstance

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

the class TypedJson method visitSpecialObjects.

List<Object> visitSpecialObjects(Writer w, Object object, boolean write) throws IOException {
    List<Object> elements = new ArrayList<Object>();
    Class clazz = object.getClass();
    if (clazz.isArray()) {
        if (write) {
            writeArray(w, object);
        } else {
            return getArrayChildren(object);
        }
    }
    if (clazz.isEnum()) {
        if (write) {
            writeEnum(w, object);
        } else {
            elements.add(object);
        }
        return elements;
    }
    if (object instanceof TypedJson) {
        this.writeTypedJson(w, (TypedJson) object);
        return elements;
    }
    if (object instanceof Collection) {
        Collection collection = (Collection) object;
        Iterator iter = collection.iterator();
        int i = 0;
        if (write)
            w.write('{');
        while (iter.hasNext() && i < queryCollectionsDepth) {
            Object item = iter.next();
            if (write) {
                writeKeyValue(w, i, item, item != null ? item.getClass() : null);
            } else {
                elements.add(item);
            }
            i++;
        }
        if (write)
            w.write('}');
        return elements;
    }
    if (object instanceof Map) {
        Map map = (Map) object;
        Iterator it = map.entrySet().iterator();
        int i = 0;
        if (write)
            w.write('{');
        while (it.hasNext() && i < queryCollectionsDepth) {
            Map.Entry e = (Map.Entry) it.next();
            Object value = e.getValue();
            if (write) {
                writeKeyValue(w, e.getKey(), value, value != null ? value.getClass() : null);
            } else {
                elements.add(value);
            }
            i++;
        }
        if (write)
            w.write('}');
        return elements;
    }
    if (object instanceof PdxInstance) {
        PdxInstance pdxInstance = (PdxInstance) object;
        if (write)
            w.write('{');
        for (String field : pdxInstance.getFieldNames()) {
            Object fieldValue = pdxInstance.getField(field);
            if (write) {
                writeKeyValue(w, field, fieldValue, fieldValue != null ? fieldValue.getClass() : null);
            } else {
                elements.add(fieldValue);
            }
        }
        if (write)
            w.write('}');
        return elements;
    }
    if (object instanceof Struct) {
        StructImpl impl = (StructImpl) object;
        String[] fields = impl.getFieldNames();
        Object[] values = impl.getFieldValues();
        if (write)
            w.write('{');
        for (int i = 0; i < fields.length; i++) {
            Object fieldValue = values[i];
            if (write) {
                writeKeyValue(w, fields[i], fieldValue, fieldValue != null ? fieldValue.getClass() : null);
            } else {
                elements.add(fieldValue);
            }
        }
        if (write)
            w.write('}');
        return elements;
    }
    if (object instanceof Region.Entry) {
        Region.Entry entry = (Region.Entry) object;
        Object key = entry.getKey();
        Object value = entry.getValue();
        if (write) {
            w.write('{');
            writeKeyValue(w, key, value, value != null ? value.getClass() : null);
            w.write('}');
        } else {
            elements.add(value);
        }
        return elements;
    }
    return elements;
}
Also used : ArrayList(java.util.ArrayList) Struct(org.apache.geode.cache.query.Struct) StructImpl(org.apache.geode.cache.query.internal.StructImpl) PdxInstance(org.apache.geode.pdx.PdxInstance) Iterator(java.util.Iterator) Collection(java.util.Collection) Region(org.apache.geode.cache.Region) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 55 with PdxInstance

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

the class DataCommandFunction method getJSONForNonPrimitiveObject.

@SuppressWarnings({ "rawtypes" })
public static Object[] getJSONForNonPrimitiveObject(Object obj) {
    Object[] array = new Object[2];
    if (obj == null) {
        array[0] = null;
        array[1] = "<NULL>";
        return array;
    } else {
        array[0] = obj.getClass().getCanonicalName();
        Class klass = obj.getClass();
        if (JsonUtil.isPrimitiveOrWrapper(klass)) {
            array[1] = obj;
        } else if (obj instanceof PdxInstance) {
            String str = pdxToJson((PdxInstance) obj);
            array[1] = str;
        } else {
            GfJsonObject object = new GfJsonObject(obj, true);
            Iterator keysIterator = object.keys();
            while (keysIterator.hasNext()) {
                String key = (String) keysIterator.next();
                Object value = object.get(key);
                if (GfJsonObject.isJSONKind(value)) {
                    GfJsonObject jsonVal = new GfJsonObject(value);
                    // System.out.println("Re-wrote inner object");
                    try {
                        if (jsonVal.has("type-class")) {
                            object.put(key, jsonVal.get("type-class"));
                        } else {
                            // Its Map Value
                            object.put(key, "a Map");
                        }
                    } catch (GfJsonException e) {
                        throw new RuntimeException(e);
                    }
                } else if (value instanceof JSONArray) {
                    // Its a collection either a set or list
                    try {
                        object.put(key, "a Collection");
                    } catch (GfJsonException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
            String str = object.toString();
            array[1] = str;
        }
        return array;
    }
}
Also used : PdxInstance(org.apache.geode.pdx.PdxInstance) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) Iterator(java.util.Iterator) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) JSONArray(org.json.JSONArray) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject)

Aggregations

PdxInstance (org.apache.geode.pdx.PdxInstance)63 Test (org.junit.Test)33 Region (org.apache.geode.cache.Region)25 PdxInstanceFactory (org.apache.geode.pdx.PdxInstanceFactory)22 SelectResults (org.apache.geode.cache.query.SelectResults)18 Host (org.apache.geode.test.dunit.Host)18 VM (org.apache.geode.test.dunit.VM)18 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)18 QueryService (org.apache.geode.cache.query.QueryService)16 CacheException (org.apache.geode.cache.CacheException)14 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)13 ClientCache (org.apache.geode.cache.client.ClientCache)11 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)11 ClientCacheFactory (org.apache.geode.cache.client.ClientCacheFactory)10 GemFireCacheImpl (org.apache.geode.internal.cache.GemFireCacheImpl)10 PdxString (org.apache.geode.pdx.internal.PdxString)10 CacheServer (org.apache.geode.cache.server.CacheServer)8 Query (org.apache.geode.cache.query.Query)7 Collection (java.util.Collection)6 Struct (org.apache.geode.cache.query.Struct)6