Search in sources :

Example 96 with CacheObject

use of org.apache.ignite.internal.processors.cache.CacheObject 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.
 */
@SuppressWarnings("ConstantConditions")
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 TextField(VAL_STR_FIELD_NAME, val.toString(), Field.Store.YES));
        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 TextField(idxdFields[i], fieldVal.toString(), Field.Store.YES));
            stringsFound = true;
        }
    }
    BytesRef keyByteRef = new BytesRef(k.valueBytes(coctx));
    try {
        final Term term = new Term(KEY_FIELD_NAME, keyByteRef);
        if (!stringsFound) {
            writer.deleteDocuments(term);
            // We did not find any strings to be indexed, will not store data at all.
            return;
        }
        doc.add(new StringField(KEY_FIELD_NAME, keyByteRef, Field.Store.YES));
        if (type.valueClass() != String.class)
            doc.add(new StoredField(VAL_FIELD_NAME, v.valueBytes(coctx)));
        doc.add(new StoredField(VER_FIELD_NAME, ver.toString().getBytes()));
        doc.add(new LongField(EXPIRATION_TIME_FIELD_NAME, expires, Field.Store.YES));
        // Next implies remove than add atomically operation.
        writer.updateDocument(term, doc);
    } catch (IOException e) {
        throw new IgniteCheckedException(e);
    } finally {
        updateCntr.incrementAndGet();
    }
}
Also used : Term(org.apache.lucene.index.Term) IOException(java.io.IOException) CacheObjectContext(org.apache.ignite.internal.processors.cache.CacheObjectContext) Document(org.apache.lucene.document.Document) LongField(org.apache.lucene.document.LongField) StoredField(org.apache.lucene.document.StoredField) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) StringField(org.apache.lucene.document.StringField) TextField(org.apache.lucene.document.TextField) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)96 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)86 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)46 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)44 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)38 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)26 CacheLockCandidates (org.apache.ignite.internal.processors.cache.CacheLockCandidates)17 GridCacheMvcc (org.apache.ignite.internal.processors.cache.GridCacheMvcc)17 ArrayList (java.util.ArrayList)15 EntryGetResult (org.apache.ignite.internal.processors.cache.EntryGetResult)15 GridCacheOperation (org.apache.ignite.internal.processors.cache.GridCacheOperation)15 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)13 Nullable (org.jetbrains.annotations.Nullable)13 UUID (java.util.UUID)11 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)11 GridCacheMvccCandidate (org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate)11 GridTimeoutObject (org.apache.ignite.internal.processors.timeout.GridTimeoutObject)11 IgniteException (org.apache.ignite.IgniteException)10 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)10 LinkedHashMap (java.util.LinkedHashMap)9