Search in sources :

Example 6 with CacheObjectContext

use of org.apache.ignite.internal.processors.cache.CacheObjectContext in project ignite by apache.

the class QueryUtils method typeForQueryEntity.

/**
     * Create type candidate for query entity.
     *
     * @param cacheName Cache 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, 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.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.
    Class<?> keyCls = U.classForName(qryEntity.findKeyType(), null);
    Class<?> valCls = U.classForName(qryEntity.findValueType(), null);
    // 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;
    if (valCls == null || (binaryEnabled && !keyOrValMustDeserialize)) {
        processBinaryMeta(ctx, qryEntity, desc);
        typeId = new QueryTypeIdKey(cacheName, ctx.cacheObjects().typeId(qryEntity.findValueType()));
        if (valCls != null)
            altTypeId = new QueryTypeIdKey(cacheName, valCls);
        if (!cctx.customAffinityMapper() && qryEntity.findKeyType() != null) {
            // Need to setup affinity key for distributed joins.
            String affField = ctx.cacheObjects().affinityField(qryEntity.findKeyType());
            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, ctx.cacheObjects().typeId(qryEntity.findValueType()));
    }
    return new QueryTypeCandidate(typeId, altTypeId, desc);
}
Also used : GridKernalContext(org.apache.ignite.internal.GridKernalContext) CacheObjectContext(org.apache.ignite.internal.processors.cache.CacheObjectContext) GridCacheDefaultAffinityKeyMapper(org.apache.ignite.internal.processors.cache.GridCacheDefaultAffinityKeyMapper) GridCacheDefaultAffinityKeyMapper(org.apache.ignite.internal.processors.cache.GridCacheDefaultAffinityKeyMapper) AffinityKeyMapper(org.apache.ignite.cache.affinity.AffinityKeyMapper) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Example 7 with CacheObjectContext

use of org.apache.ignite.internal.processors.cache.CacheObjectContext in project ignite by apache.

the class BinaryMarshallerSelfTest method testKeepDeserialized.

/**
     * @throws Exception If failed.
     */
public void testKeepDeserialized() throws Exception {
    BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(new BinaryTypeConfiguration(SimpleObject.class.getName())));
    BinaryObjectImpl po = marshal(simpleObject(), marsh);
    CacheObjectContext coCtx = new CacheObjectContext(newContext(), null, null, false, true, false);
    assert po.value(coCtx, false) == po.value(coCtx, false);
    po = marshal(simpleObject(), marsh);
    assert po.deserialize() != po.deserialize();
}
Also used : BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) CacheObjectContext(org.apache.ignite.internal.processors.cache.CacheObjectContext)

Example 8 with CacheObjectContext

use of org.apache.ignite.internal.processors.cache.CacheObjectContext in project ignite by apache.

the class CacheObjectBinaryProcessorImpl method contextForCache.

/** {@inheritDoc} */
@Override
public CacheObjectContext contextForCache(CacheConfiguration cfg) throws IgniteCheckedException {
    assert cfg != null;
    boolean binaryEnabled = marsh instanceof BinaryMarshaller && !GridCacheUtils.isSystemCache(cfg.getName()) && !GridCacheUtils.isIgfsCache(ctx.config(), cfg.getName());
    CacheObjectContext ctx0 = super.contextForCache(cfg);
    CacheObjectContext res = new CacheObjectBinaryContext(ctx, cfg.getName(), ctx0.copyOnGet(), ctx0.storeValue(), binaryEnabled, ctx0.addDeploymentInfo());
    ctx.resource().injectGeneric(res.defaultAffMapper());
    return res;
}
Also used : BinaryMarshaller(org.apache.ignite.internal.binary.BinaryMarshaller) GridBinaryMarshaller(org.apache.ignite.internal.binary.GridBinaryMarshaller) CacheObjectContext(org.apache.ignite.internal.processors.cache.CacheObjectContext)

Example 9 with CacheObjectContext

use of org.apache.ignite.internal.processors.cache.CacheObjectContext in project ignite by apache.

the class GridCacheQueryManager method store.

/**
     * Writes key-value pair to index.
     *
     * @param key Key.
     * @param partId Partition.
     * @param prevVal Previous value.
     * @param prevVer Previous version.
     * @param val Value.
     * @param ver Cache entry version.
     * @param expirationTime Expiration time or 0 if never expires.
     * @param link Link.
     * @throws IgniteCheckedException In case of error.
     */
public void store(KeyCacheObject key, int partId, @Nullable CacheObject prevVal, @Nullable GridCacheVersion prevVer, CacheObject val, GridCacheVersion ver, long expirationTime, long link) throws IgniteCheckedException {
    assert key != null;
    assert val != null;
    assert enabled();
    if (key instanceof GridCacheInternal)
        // No-op.
        return;
    if (!enterBusy())
        throw new NodeStoppingException("Operation has been cancelled (node is stopping).");
    try {
        if (isIndexingSpiEnabled()) {
            CacheObjectContext coctx = cctx.cacheObjectContext();
            Object key0 = unwrapIfNeeded(key, coctx);
            Object val0 = unwrapIfNeeded(val, coctx);
            cctx.kernalContext().indexing().store(cacheName, key0, val0, expirationTime);
        }
        if (qryProcEnabled)
            qryProc.store(cacheName, key, partId, prevVal, prevVer, val, ver, expirationTime, link);
    } finally {
        invalidateResultCache();
        leaveBusy();
    }
}
Also used : NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) GridCacheInternal(org.apache.ignite.internal.processors.cache.GridCacheInternal) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) CacheObjectContext(org.apache.ignite.internal.processors.cache.CacheObjectContext)

Example 10 with CacheObjectContext

use of org.apache.ignite.internal.processors.cache.CacheObjectContext in project ignite by apache.

the class GridLuceneIndex method store.

/**
     * Stores given data in this fulltext index.
     *
     * @param k Key.
     * @param v Value.
     * @param ver Version.
     * @param expires Expiration time.
     * @throws IgniteCheckedException If failed.
     */
public void store(CacheObject k, CacheObject v, GridCacheVersion ver, long expires) throws IgniteCheckedException {
    CacheObjectContext coctx = objectContext();
    Object key = k.isPlatformType() ? k.value(coctx, false) : k;
    Object val = v.isPlatformType() ? v.value(coctx, false) : v;
    Document doc = new Document();
    boolean stringsFound = false;
    if (type.valueTextIndex() || type.valueClass() == String.class) {
        doc.add(new Field(VAL_STR_FIELD_NAME, val.toString(), Field.Store.YES, Field.Index.ANALYZED));
        stringsFound = true;
    }
    for (int i = 0, last = idxdFields.length - 1; i < last; i++) {
        Object fieldVal = type.value(idxdFields[i], key, val);
        if (fieldVal != null) {
            doc.add(new Field(idxdFields[i], fieldVal.toString(), Field.Store.YES, Field.Index.ANALYZED));
            stringsFound = true;
        }
    }
    String keyStr = org.apache.commons.codec.binary.Base64.encodeBase64String(k.valueBytes(coctx));
    try {
        // Delete first to avoid duplicates.
        writer.deleteDocuments(new Term(KEY_FIELD_NAME, keyStr));
        if (!stringsFound)
            // We did not find any strings to be indexed, will not store data at all.
            return;
        doc.add(new Field(KEY_FIELD_NAME, keyStr, Field.Store.YES, Field.Index.NOT_ANALYZED));
        if (type.valueClass() != String.class)
            doc.add(new Field(VAL_FIELD_NAME, v.valueBytes(coctx)));
        doc.add(new Field(VER_FIELD_NAME, ver.toString().getBytes()));
        doc.add(new Field(EXPIRATION_TIME_FIELD_NAME, DateTools.timeToString(expires, DateTools.Resolution.MILLISECOND), Field.Store.YES, Field.Index.NOT_ANALYZED));
        writer.addDocument(doc);
    } catch (IOException e) {
        throw new IgniteCheckedException(e);
    } finally {
        updateCntr.incrementAndGet();
    }
}
Also used : Field(org.apache.lucene.document.Field) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) CacheObjectContext(org.apache.ignite.internal.processors.cache.CacheObjectContext) Document(org.apache.lucene.document.Document)

Aggregations

CacheObjectContext (org.apache.ignite.internal.processors.cache.CacheObjectContext)15 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)3 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)3 GridKernalContext (org.apache.ignite.internal.GridKernalContext)2 IgniteKernal (org.apache.ignite.internal.IgniteKernal)2 NodeStoppingException (org.apache.ignite.internal.NodeStoppingException)2 GridCacheDefaultAffinityKeyMapper (org.apache.ignite.internal.processors.cache.GridCacheDefaultAffinityKeyMapper)2 IgniteCacheObjectProcessor (org.apache.ignite.internal.processors.cacheobject.IgniteCacheObjectProcessor)2 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 CreatedExpiryPolicy (javax.cache.expiry.CreatedExpiryPolicy)1 Duration (javax.cache.expiry.Duration)1 Ignite (org.apache.ignite.Ignite)1 IgniteException (org.apache.ignite.IgniteException)1 IgniteLogger (org.apache.ignite.IgniteLogger)1 BinaryObject (org.apache.ignite.binary.BinaryObject)1