Search in sources :

Example 1 with BinaryField

use of org.apache.ignite.binary.BinaryField in project ignite by apache.

the class QueryUtils method typeForQueryEntity.

/**
 * Create type candidate for query entity.
 *
 * @param cacheName Cache name.
 * @param schemaName Schema name.
 * @param cctx Cache context.
 * @param qryEntity Query entity.
 * @param mustDeserializeClss Classes which must be deserialized.
 * @param escape Escape flag.
 * @return Type candidate.
 * @throws IgniteCheckedException If failed.
 */
public static QueryTypeCandidate typeForQueryEntity(String cacheName, String schemaName, GridCacheContext cctx, QueryEntity qryEntity, List<Class<?>> mustDeserializeClss, boolean escape) throws IgniteCheckedException {
    GridKernalContext ctx = cctx.kernalContext();
    CacheConfiguration<?, ?> ccfg = cctx.config();
    boolean binaryEnabled = ctx.cacheObjects().isBinaryEnabled(ccfg);
    CacheObjectContext coCtx = binaryEnabled ? ctx.cacheObjects().contextForCache(ccfg) : null;
    QueryTypeDescriptorImpl desc = new QueryTypeDescriptorImpl(cacheName);
    desc.schemaName(schemaName);
    desc.aliases(qryEntity.getAliases());
    // Key and value classes still can be available if they are primitive or JDK part.
    // We need that to set correct types for _key and _val columns.
    // We better box these types - otherwise, if user provides, say, raw 'byte' for
    // key or value (which they could), we'll deem key or value as Object which clearly is not right.
    Class<?> keyCls = U.box(U.classForName(qryEntity.findKeyType(), null, true));
    Class<?> valCls = U.box(U.classForName(qryEntity.findValueType(), null, true));
    // If local node has the classes and they are externalizable, we must use reflection properties.
    boolean keyMustDeserialize = mustDeserializeBinary(ctx, keyCls);
    boolean valMustDeserialize = mustDeserializeBinary(ctx, valCls);
    boolean keyOrValMustDeserialize = keyMustDeserialize || valMustDeserialize;
    if (keyCls == null)
        keyCls = Object.class;
    String simpleValType = ((valCls == null) ? typeName(qryEntity.findValueType()) : typeName(valCls));
    desc.name(simpleValType);
    desc.tableName(qryEntity.getTableName());
    if (binaryEnabled && !keyOrValMustDeserialize) {
        // Safe to check null.
        if (SQL_TYPES.contains(valCls))
            desc.valueClass(valCls);
        else
            desc.valueClass(Object.class);
        if (SQL_TYPES.contains(keyCls))
            desc.keyClass(keyCls);
        else
            desc.keyClass(Object.class);
    } else {
        if (valCls == null)
            throw new IgniteCheckedException("Failed to find value class in the node classpath " + "(use default marshaller to enable binary objects) : " + qryEntity.findValueType());
        desc.valueClass(valCls);
        desc.keyClass(keyCls);
    }
    desc.keyTypeName(qryEntity.findKeyType());
    desc.valueTypeName(qryEntity.findValueType());
    desc.keyFieldName(qryEntity.getKeyFieldName());
    desc.valueFieldName(qryEntity.getValueFieldName());
    if (binaryEnabled && keyOrValMustDeserialize) {
        if (keyMustDeserialize)
            mustDeserializeClss.add(keyCls);
        if (valMustDeserialize)
            mustDeserializeClss.add(valCls);
    }
    QueryTypeIdKey typeId;
    QueryTypeIdKey altTypeId = null;
    int valTypeId = ctx.cacheObjects().typeId(qryEntity.findValueType());
    if (valCls == null || (binaryEnabled && !keyOrValMustDeserialize)) {
        processBinaryMeta(ctx, qryEntity, desc);
        typeId = new QueryTypeIdKey(cacheName, valTypeId);
        if (valCls != null)
            altTypeId = new QueryTypeIdKey(cacheName, valCls);
        String affField = null;
        // Need to setup affinity key for distributed joins.
        String keyType = qryEntity.getKeyType();
        if (!cctx.customAffinityMapper() && keyType != null) {
            if (coCtx != null) {
                CacheDefaultBinaryAffinityKeyMapper mapper = (CacheDefaultBinaryAffinityKeyMapper) coCtx.defaultAffMapper();
                BinaryField field = mapper.affinityKeyField(keyType);
                if (field != null)
                    affField = field.name();
            }
        }
        if (affField != null) {
            if (!escape)
                affField = normalizeObjectName(affField, false);
            desc.affinityKey(affField);
        }
    } else {
        processClassMeta(qryEntity, desc, coCtx);
        AffinityKeyMapper keyMapper = cctx.config().getAffinityMapper();
        if (keyMapper instanceof GridCacheDefaultAffinityKeyMapper) {
            String affField = ((GridCacheDefaultAffinityKeyMapper) keyMapper).affinityKeyPropertyName(desc.keyClass());
            if (affField != null) {
                if (!escape)
                    affField = normalizeObjectName(affField, false);
                desc.affinityKey(affField);
            }
        }
        typeId = new QueryTypeIdKey(cacheName, valCls);
        altTypeId = new QueryTypeIdKey(cacheName, valTypeId);
    }
    desc.typeId(valTypeId);
    return new QueryTypeCandidate(typeId, altTypeId, desc);
}
Also used : CacheDefaultBinaryAffinityKeyMapper(org.apache.ignite.internal.processors.cache.CacheDefaultBinaryAffinityKeyMapper) BinaryField(org.apache.ignite.binary.BinaryField) GridKernalContext(org.apache.ignite.internal.GridKernalContext) CacheObjectContext(org.apache.ignite.internal.processors.cache.CacheObjectContext) GridCacheDefaultAffinityKeyMapper(org.apache.ignite.internal.processors.cache.GridCacheDefaultAffinityKeyMapper) CacheDefaultBinaryAffinityKeyMapper(org.apache.ignite.internal.processors.cache.CacheDefaultBinaryAffinityKeyMapper) GridCacheDefaultAffinityKeyMapper(org.apache.ignite.internal.processors.cache.GridCacheDefaultAffinityKeyMapper) AffinityKeyMapper(org.apache.ignite.cache.affinity.AffinityKeyMapper) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Example 2 with BinaryField

use of org.apache.ignite.binary.BinaryField in project ignite by apache.

the class CacheDefaultBinaryAffinityKeyMapper method affinityKeyField.

/**
 * Get affinity field override for type.
 *
 * @param typeId Type ID.
 * @return Affinity field override if any.
 */
@Nullable
public BinaryField affinityKeyField(int typeId) {
    Map<Integer, BinaryField> typeIdAffFields0 = typeIdAffFields;
    if (typeIdAffFields0 == null) {
        typeIdAffFields0 = new HashMap<>();
        for (Map.Entry<String, String> entry : typeNameAffFields.entrySet()) {
            String typeName = entry.getKey();
            String affFieldName = entry.getValue();
            int curTypeId = proc.typeId(typeName);
            BinaryField field = proc.binaryContext().createField(curTypeId, affFieldName);
            typeIdAffFields0.put(curTypeId, field);
        }
        typeIdAffFields = typeIdAffFields0;
    }
    BinaryField res = typeIdAffFields0.get(typeId);
    if (res == null)
        res = proc.affinityKeyField(typeId);
    return res;
}
Also used : BinaryField(org.apache.ignite.binary.BinaryField) HashMap(java.util.HashMap) Map(java.util.Map) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with BinaryField

use of org.apache.ignite.binary.BinaryField in project ignite by apache.

the class QueryUtils method typeForQueryEntity.

/**
 * Create type candidate for query entity.
 *
 * @param cacheName Cache name.
 * @param schemaName Schema name.
 * @param cacheInfo Cache context info.
 * @param qryEntity Query entity.
 * @param mustDeserializeClss Classes which must be deserialized.
 * @param escape Escape flag.
 * @return Type candidate.
 * @throws IgniteCheckedException If failed.
 */
public static QueryTypeCandidate typeForQueryEntity(GridKernalContext ctx, String cacheName, String schemaName, GridCacheContextInfo cacheInfo, QueryEntity qryEntity, List<Class<?>> mustDeserializeClss, boolean escape) throws IgniteCheckedException {
    CacheConfiguration<?, ?> ccfg = cacheInfo.config();
    boolean binaryEnabled = ctx.cacheObjects().isBinaryEnabled(ccfg);
    CacheObjectContext coCtx = ctx.cacheObjects().contextForCache(ccfg);
    QueryTypeDescriptorImpl desc = new QueryTypeDescriptorImpl(cacheName, coCtx);
    desc.schemaName(schemaName);
    desc.aliases(qryEntity.getAliases());
    if (qryEntity instanceof QueryEntityEx)
        desc.setFillAbsentPKsWithDefaults(((QueryEntityEx) qryEntity).fillAbsentPKsWithDefaults());
    // Key and value classes still can be available if they are primitive or JDK part.
    // We need that to set correct types for _key and _val columns.
    // We better box these types - otherwise, if user provides, say, raw 'byte' for
    // key or value (which they could), we'll deem key or value as Object which clearly is not right.
    Class<?> keyCls = U.box(U.classForName(qryEntity.findKeyType(), null, true));
    Class<?> valCls = U.box(U.classForName(qryEntity.findValueType(), null, true));
    // If local node has the classes and they are externalizable, we must use reflection properties.
    boolean keyMustDeserialize = mustDeserializeBinary(ctx, keyCls);
    boolean valMustDeserialize = mustDeserializeBinary(ctx, valCls);
    boolean keyOrValMustDeserialize = keyMustDeserialize || valMustDeserialize;
    if (keyCls == null)
        keyCls = Object.class;
    String simpleValType = ((valCls == null) ? typeName(qryEntity.findValueType()) : typeName(valCls));
    desc.name(simpleValType);
    desc.tableName(qryEntity.getTableName());
    if (binaryEnabled && !keyOrValMustDeserialize) {
        // Safe to check null.
        if (SQL_TYPES.contains(valCls))
            desc.valueClass(valCls);
        else
            desc.valueClass(Object.class);
        if (SQL_TYPES.contains(keyCls))
            desc.keyClass(keyCls);
        else
            desc.keyClass(Object.class);
    } else {
        if (valCls == null)
            throw new IgniteCheckedException("Failed to find value class in the node classpath " + "(use default marshaller to enable binary objects) : " + qryEntity.findValueType());
        desc.valueClass(valCls);
        desc.keyClass(keyCls);
    }
    desc.keyTypeName(qryEntity.findKeyType());
    desc.valueTypeName(qryEntity.findValueType());
    desc.keyFieldName(qryEntity.getKeyFieldName());
    desc.valueFieldName(qryEntity.getValueFieldName());
    if (binaryEnabled && keyOrValMustDeserialize) {
        if (keyMustDeserialize)
            mustDeserializeClss.add(keyCls);
        if (valMustDeserialize)
            mustDeserializeClss.add(valCls);
    }
    QueryTypeIdKey typeId;
    QueryTypeIdKey altTypeId = null;
    int valTypeId = ctx.cacheObjects().typeId(qryEntity.findValueType());
    if (valCls == null || (binaryEnabled && !keyOrValMustDeserialize)) {
        processBinaryMeta(ctx, qryEntity, desc);
        typeId = new QueryTypeIdKey(cacheName, valTypeId);
        if (valCls != null)
            altTypeId = new QueryTypeIdKey(cacheName, valCls);
        String affField = null;
        if (!coCtx.customAffinityMapper()) {
            String keyType = qryEntity.getKeyType();
            if (keyType != null) {
                CacheDefaultBinaryAffinityKeyMapper mapper = (CacheDefaultBinaryAffinityKeyMapper) coCtx.defaultAffMapper();
                BinaryField field = mapper.affinityKeyField(keyType);
                if (field != null) {
                    String affField0 = field.name();
                    if (!F.isEmpty(qryEntity.getKeyFields()) && qryEntity.getKeyFields().contains(affField0)) {
                        affField = desc.aliases().getOrDefault(affField0, affField0);
                        if (!escape)
                            affField = normalizeObjectName(affField, false);
                    }
                }
            }
        } else
            desc.customAffinityKeyMapper(true);
        desc.affinityKey(affField);
    } else {
        processClassMeta(qryEntity, desc, coCtx);
        AffinityKeyMapper keyMapper = cacheInfo.config().getAffinityMapper();
        if (keyMapper instanceof GridCacheDefaultAffinityKeyMapper) {
            String affField = ((GridCacheDefaultAffinityKeyMapper) keyMapper).affinityKeyPropertyName(desc.keyClass());
            if (affField != null) {
                affField = desc.aliases().getOrDefault(affField, affField);
                if (!escape)
                    affField = normalizeObjectName(affField, false);
                desc.affinityKey(affField);
            }
        } else
            desc.customAffinityKeyMapper(true);
        typeId = new QueryTypeIdKey(cacheName, valCls);
        altTypeId = new QueryTypeIdKey(cacheName, valTypeId);
    }
    desc.typeId(valTypeId);
    if (qryEntity instanceof QueryEntityEx) {
        QueryEntityEx qe = (QueryEntityEx) qryEntity;
        desc.primaryKeyInlineSize(qe.getPrimaryKeyInlineSize() != null ? qe.getPrimaryKeyInlineSize() : -1);
        desc.affinityFieldInlineSize(qe.getAffinityKeyInlineSize() != null ? qe.getAffinityKeyInlineSize() : -1);
    }
    return new QueryTypeCandidate(typeId, altTypeId, desc);
}
Also used : CacheDefaultBinaryAffinityKeyMapper(org.apache.ignite.internal.processors.cache.CacheDefaultBinaryAffinityKeyMapper) BinaryField(org.apache.ignite.binary.BinaryField) CacheObjectContext(org.apache.ignite.internal.processors.cache.CacheObjectContext) GridCacheDefaultAffinityKeyMapper(org.apache.ignite.internal.processors.cache.GridCacheDefaultAffinityKeyMapper) AffinityKeyMapper(org.apache.ignite.cache.affinity.AffinityKeyMapper) CacheDefaultBinaryAffinityKeyMapper(org.apache.ignite.internal.processors.cache.CacheDefaultBinaryAffinityKeyMapper) GridCacheDefaultAffinityKeyMapper(org.apache.ignite.internal.processors.cache.GridCacheDefaultAffinityKeyMapper) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Example 4 with BinaryField

use of org.apache.ignite.binary.BinaryField in project ignite by apache.

the class BinaryMarshallerSelfTest method testDuplicateFields.

/**
 * Test duplicate fields.
 *
 * @throws Exception If failed.
 */
@Test
public void testDuplicateFields() throws Exception {
    BinaryMarshaller marsh = binaryMarshaller();
    DuplicateFieldsB obj = new DuplicateFieldsB(1, 2);
    BinaryObjectImpl objBin = marshal(obj, marsh);
    String fieldName = "x";
    String fieldNameA = DuplicateFieldsA.class.getName() + "." + fieldName;
    String fieldNameB = DuplicateFieldsB.class.getName() + "." + fieldName;
    // Check "hasField".
    assert !objBin.hasField(fieldName);
    assert objBin.hasField(fieldNameA);
    assert objBin.hasField(fieldNameB);
    // Check direct field access.
    assertNull(objBin.field(fieldName));
    assertEquals(Integer.valueOf(1), objBin.field(fieldNameA));
    assertEquals(Integer.valueOf(2), objBin.field(fieldNameB));
    // Check metadata.
    BinaryType type = objBin.type();
    Collection<String> fieldNames = type.fieldNames();
    assertEquals(2, fieldNames.size());
    assert !fieldNames.contains(fieldName);
    assert fieldNames.contains(fieldNameA);
    assert fieldNames.contains(fieldNameB);
    // Check field access through type.
    BinaryField field = type.field(fieldName);
    BinaryField fieldA = type.field(fieldNameA);
    BinaryField fieldB = type.field(fieldNameB);
    assert !field.exists(objBin);
    assert fieldA.exists(objBin);
    assert fieldB.exists(objBin);
    assertNull(field.value(objBin));
    assertEquals(Integer.valueOf(1), fieldA.value(objBin));
    assertEquals(Integer.valueOf(2), fieldB.value(objBin));
    // Check object deserialization.
    DuplicateFieldsB deserialized = objBin.deserialize();
    assertEquals(obj.xA(), deserialized.xA());
    assertEquals(obj.xB(), deserialized.xB());
}
Also used : BinaryField(org.apache.ignite.binary.BinaryField) BinaryType(org.apache.ignite.binary.BinaryType) Test(org.junit.Test)

Example 5 with BinaryField

use of org.apache.ignite.binary.BinaryField 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)

Aggregations

BinaryField (org.apache.ignite.binary.BinaryField)14 BinaryObject (org.apache.ignite.binary.BinaryObject)3 BinaryType (org.apache.ignite.binary.BinaryType)3 BinaryObjectEx (org.apache.ignite.internal.binary.BinaryObjectEx)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 AffinityKeyMapper (org.apache.ignite.cache.affinity.AffinityKeyMapper)2 CacheDefaultBinaryAffinityKeyMapper (org.apache.ignite.internal.processors.cache.CacheDefaultBinaryAffinityKeyMapper)2 CacheObjectContext (org.apache.ignite.internal.processors.cache.CacheObjectContext)2 GridCacheDefaultAffinityKeyMapper (org.apache.ignite.internal.processors.cache.GridCacheDefaultAffinityKeyMapper)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 EntryProcessorException (javax.cache.processor.EntryProcessorException)1 Ignite (org.apache.ignite.Ignite)1 IgniteException (org.apache.ignite.IgniteException)1 BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)1 GridKernalContext (org.apache.ignite.internal.GridKernalContext)1 BinaryObjectBuilderImpl (org.apache.ignite.internal.binary.builder.BinaryObjectBuilderImpl)1 T1 (org.apache.ignite.internal.util.typedef.T1)1 Nullable (org.jetbrains.annotations.Nullable)1 Test (org.junit.Test)1