Search in sources :

Example 1 with BinaryObjectEx

use of org.apache.ignite.internal.binary.BinaryObjectEx in project ignite by apache.

the class CacheObjectBinaryProcessorImpl method affinityKey.

/**
     * @param po Binary object.
     * @return Affinity key.
     */
public Object affinityKey(BinaryObject po) {
    // Fast path for already cached field.
    if (po instanceof BinaryObjectEx) {
        int typeId = ((BinaryObjectEx) po).typeId();
        T1<BinaryField> fieldHolder = affKeyFields.get(typeId);
        if (fieldHolder != null) {
            BinaryField field = fieldHolder.get();
            return field != null ? field.value(po) : po;
        }
    }
    // Slow path if affinity field is not cached yet.
    try {
        BinaryType meta = po instanceof BinaryObjectEx ? ((BinaryObjectEx) po).rawType() : po.type();
        if (meta != null) {
            String name = meta.affinityKeyFieldName();
            if (name != null) {
                BinaryField field = meta.field(name);
                affKeyFields.putIfAbsent(meta.typeId(), new T1<>(field));
                return field.value(po);
            } else
                affKeyFields.putIfAbsent(meta.typeId(), new T1<BinaryField>(null));
        } else if (po instanceof BinaryObjectEx) {
            int typeId = ((BinaryObjectEx) po).typeId();
            String name = binaryCtx.affinityKeyFieldName(typeId);
            if (name != null)
                return po.field(name);
        }
    } catch (BinaryObjectException e) {
        U.error(log, "Failed to get affinity field from binary object: " + po, e);
    }
    return po;
}
Also used : BinaryField(org.apache.ignite.binary.BinaryField) BinaryType(org.apache.ignite.binary.BinaryType) BinaryObjectEx(org.apache.ignite.internal.binary.BinaryObjectEx) T1(org.apache.ignite.internal.util.typedef.T1) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Example 2 with BinaryObjectEx

use of org.apache.ignite.internal.binary.BinaryObjectEx in project ignite by apache.

the class VisorQueryUtils method binaryToString.

/**
 * Convert Binary object to string.
 *
 * @param obj Binary object.
 * @return String representation of Binary object.
 */
public static String binaryToString(BinaryObject obj) {
    int hash = obj.hashCode();
    if (obj instanceof BinaryObjectEx) {
        BinaryObjectEx objEx = (BinaryObjectEx) obj;
        BinaryType meta;
        try {
            meta = ((BinaryObjectEx) obj).rawType();
        } catch (BinaryObjectException ignore) {
            meta = null;
        }
        if (meta != null) {
            if (meta.isEnum()) {
                try {
                    return obj.deserialize().toString();
                } catch (BinaryObjectException ignore) {
                // NO-op.
                }
            }
            SB buf = new SB(meta.typeName());
            if (meta.fieldNames() != null) {
                buf.a(" [hash=").a(hash);
                for (String name : meta.fieldNames()) {
                    Object val = objEx.field(name);
                    buf.a(", ").a(name).a('=').a(val);
                }
                buf.a(']');
                return buf.toString();
            }
        }
    }
    return S.toString(obj.getClass().getSimpleName(), "hash", hash, false, "typeId", obj.type().typeId(), true);
}
Also used : BinaryType(org.apache.ignite.binary.BinaryType) BinaryObjectEx(org.apache.ignite.internal.binary.BinaryObjectEx) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) SB(org.apache.ignite.internal.util.typedef.internal.SB)

Example 3 with BinaryObjectEx

use of org.apache.ignite.internal.binary.BinaryObjectEx in project ignite by apache.

the class QueryBinaryProperty method binaryField.

/**
 * Get binary field for the property.
 *
 * @param obj Target object.
 * @return Binary field.
 */
private BinaryField binaryField(BinaryObject obj) {
    if (ctx.query().skipFieldLookup())
        return null;
    BinaryField field0 = field;
    if (field0 == null && !fieldTaken) {
        BinaryType type = obj instanceof BinaryObjectEx ? ((BinaryObjectEx) obj).rawType() : obj.type();
        if (type != null) {
            field0 = type.field(propName);
            assert field0 != null;
            field = field0;
        }
        fieldTaken = true;
    }
    return field0;
}
Also used : BinaryField(org.apache.ignite.binary.BinaryField) BinaryType(org.apache.ignite.binary.BinaryType) BinaryObjectEx(org.apache.ignite.internal.binary.BinaryObjectEx)

Example 4 with BinaryObjectEx

use of org.apache.ignite.internal.binary.BinaryObjectEx in project ignite by apache.

the class CacheDefaultBinaryAffinityKeyMapper method affinityKey.

/**
 * {@inheritDoc}
 */
@Override
public Object affinityKey(Object key) {
    try {
        key = proc.toBinary(key, false);
    } catch (IgniteException e) {
        U.error(log, "Failed to marshal key to binary: " + key, e);
    }
    if (key instanceof BinaryObject) {
        assert key instanceof BinaryObjectEx : "All BinaryObject implementations must implement " + BinaryObjectEx.class.getName() + ": " + key.getClass().getName();
        BinaryObjectEx key0 = (BinaryObjectEx) key;
        BinaryField affField = affinityKeyField(key0.typeId());
        if (affField != null) {
            Object res = affField.value(key0);
            if (res != null)
                return res;
        }
        return key;
    } else
        return super.affinityKey(key);
}
Also used : BinaryField(org.apache.ignite.binary.BinaryField) BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteException(org.apache.ignite.IgniteException) BinaryObjectEx(org.apache.ignite.internal.binary.BinaryObjectEx) BinaryObject(org.apache.ignite.binary.BinaryObject)

Aggregations

BinaryObjectEx (org.apache.ignite.internal.binary.BinaryObjectEx)4 BinaryField (org.apache.ignite.binary.BinaryField)3 BinaryType (org.apache.ignite.binary.BinaryType)3 BinaryObject (org.apache.ignite.binary.BinaryObject)2 BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)2 IgniteException (org.apache.ignite.IgniteException)1 T1 (org.apache.ignite.internal.util.typedef.T1)1 SB (org.apache.ignite.internal.util.typedef.internal.SB)1