Search in sources :

Example 41 with CacheObjectContext

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

the class GridBinaryAffinityKeySelfTest method checkAffinity.

/**
 * @param ignite Ignite.
 * @throws Exception If failed.
 */
private void checkAffinity(Ignite ignite) throws Exception {
    Affinity<Object> aff = ignite.affinity(DEFAULT_CACHE_NAME);
    GridAffinityProcessor affProc = ((IgniteKernal) ignite).context().affinity();
    IgniteCacheObjectProcessor cacheObjProc = ((IgniteKernal) ignite).context().cacheObjects();
    CacheObjectContext cacheObjCtx = cacheObjProc.contextForCache(ignite.cache(DEFAULT_CACHE_NAME).getConfiguration(CacheConfiguration.class));
    for (int i = 0; i < 1000; i++) {
        assertEquals(i, aff.affinityKey(i));
        assertEquals(i, aff.affinityKey(new TestObject(i)));
        assertEquals(i, aff.affinityKey(ignite.binary().toBinary(new TestObject(i))));
        assertEquals(i, aff.affinityKey(new AffinityKey(0, i)));
        BinaryObjectBuilder bldr = ignite.binary().builder("TestObject2");
        bldr.setField("affKey", i);
        assertEquals(i, aff.affinityKey(bldr.build()));
        CacheObject cacheObj = cacheObjProc.toCacheObject(cacheObjCtx, new TestObject(i), true);
        assertEquals(i, aff.affinityKey(cacheObj));
        assertEquals(aff.mapKeyToNode(i), aff.mapKeyToNode(new TestObject(i)));
        assertEquals(aff.mapKeyToNode(i), aff.mapKeyToNode(cacheObj));
        assertEquals(i, affProc.affinityKey(DEFAULT_CACHE_NAME, i));
        assertEquals(i, affProc.affinityKey(DEFAULT_CACHE_NAME, new TestObject(i)));
        assertEquals(i, affProc.affinityKey(DEFAULT_CACHE_NAME, cacheObj));
        assertEquals(affProc.mapKeyToNode(DEFAULT_CACHE_NAME, i), affProc.mapKeyToNode(DEFAULT_CACHE_NAME, new TestObject(i)));
        assertEquals(affProc.mapKeyToNode(DEFAULT_CACHE_NAME, i), affProc.mapKeyToNode(DEFAULT_CACHE_NAME, cacheObj));
        assertEquals(affProc.mapKeyToNode(DEFAULT_CACHE_NAME, new AffinityKey(0, i)), affProc.mapKeyToNode(DEFAULT_CACHE_NAME, i));
    }
}
Also used : GridAffinityProcessor(org.apache.ignite.internal.processors.affinity.GridAffinityProcessor) IgniteCacheObjectProcessor(org.apache.ignite.internal.processors.cacheobject.IgniteCacheObjectProcessor) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) CacheObjectContext(org.apache.ignite.internal.processors.cache.CacheObjectContext) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 42 with CacheObjectContext

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

the class IgniteSnapshotManagerSelfTest method testSnapshotIteratorRandomizedLoader.

/**
 * @throws Exception If fails.
 */
@Test
public void testSnapshotIteratorRandomizedLoader() throws Exception {
    Random rnd = new Random();
    int maxKey = 15_000;
    int maxValSize = 32_768;
    int loadingTimeMs = 30_000;
    CacheConfiguration<Integer, Value> ccfg = txCacheConfig(new CacheConfiguration<Integer, Value>("tx1")).setAffinity(new RendezvousAffinityFunction(false, 1));
    IgniteEx ignite = startGridsWithCache(1, CACHE_KEYS_RANGE, k -> new Value(new byte[1024]), ccfg);
    IgniteCache<Integer, Value> cache = ignite.cache(ccfg.getName());
    long startTime = U.currentTimeMillis();
    IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(() -> {
        while (!Thread.currentThread().isInterrupted() && startTime + loadingTimeMs > U.currentTimeMillis()) {
            if (rnd.nextBoolean())
                cache.put(rnd.nextInt(maxKey), new Value(new byte[rnd.nextInt(maxValSize)]));
            else
                cache.remove(rnd.nextInt(maxKey));
        }
    }, 10, "change-loader-");
    fut.get();
    ignite.snapshot().createSnapshot(SNAPSHOT_NAME).get();
    Map<Integer, Value> iterated = new HashMap<>();
    try (GridCloseableIterator<CacheDataRow> iter = snp(ignite).partitionRowIterator(ignite.context(), SNAPSHOT_NAME, ignite.context().pdsFolderResolver().resolveFolders().folderName(), ccfg.getName(), 0)) {
        CacheObjectContext coctx = ignite.cachex(ccfg.getName()).context().cacheObjectContext();
        while (iter.hasNext()) {
            CacheDataRow row = iter.next();
            iterated.put(row.key().value(coctx, true), row.value().value(coctx, true));
        }
    }
    stopAllGrids();
    IgniteEx snpIgnite = startGridsFromSnapshot(1, SNAPSHOT_NAME);
    IgniteCache<Integer, Value> snpCache = snpIgnite.cache(ccfg.getName());
    assertEquals(snpCache.size(CachePeekMode.PRIMARY), iterated.size());
    snpCache.forEach(e -> {
        Value val = iterated.remove(e.getKey());
        assertNotNull(val);
        assertEquals(val.arr().length, e.getValue().arr().length);
    });
    assertTrue(iterated.isEmpty());
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) HashMap(java.util.HashMap) CacheObjectContext(org.apache.ignite.internal.processors.cache.CacheObjectContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Random(java.util.Random) IgniteEx(org.apache.ignite.internal.IgniteEx) GridTestUtils.setFieldValue(org.apache.ignite.testframework.GridTestUtils.setFieldValue) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Test(org.junit.Test)

Example 43 with CacheObjectContext

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

the class IgniteSnapshotManagerSelfTest method testSnapshotIteratorLargeRows.

/**
 * @throws Exception If fails.
 */
@Test
public void testSnapshotIteratorLargeRows() throws Exception {
    int keys = 2;
    CacheConfiguration<Integer, Value> ccfg = txCacheConfig(new CacheConfiguration<Integer, Value>(DEFAULT_CACHE_NAME)).setAffinity(new RendezvousAffinityFunction(false, 1));
    IgniteEx ignite = startGridsWithoutCache(2);
    assertEquals(DFLT_PAGE_SIZE, ignite.configuration().getDataStorageConfiguration().getPageSize());
    for (int i = 0; i < keys; i++) ignite.getOrCreateCache(ccfg).put(i, new Value(new byte[SIZE_FOR_FIT_3_PAGES]));
    forceCheckpoint();
    ignite.snapshot().createSnapshot(SNAPSHOT_NAME).get();
    int rows = 0;
    try (GridCloseableIterator<CacheDataRow> iter = snp(ignite).partitionRowIterator(ignite.context(), SNAPSHOT_NAME, ignite.context().pdsFolderResolver().resolveFolders().folderName(), dfltCacheCfg.getName(), 0)) {
        CacheObjectContext coctx = ignite.cachex(dfltCacheCfg.getName()).context().cacheObjectContext();
        while (iter.hasNext()) {
            CacheDataRow row = iter.next();
            assertEquals(SIZE_FOR_FIT_3_PAGES, ((Value) row.value().value(coctx, false)).arr().length);
            assertTrue((Integer) row.key().value(coctx, false, null) < 2);
            rows++;
        }
    }
    assertEquals("Invalid number of rows: " + rows, keys, rows);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) IgniteEx(org.apache.ignite.internal.IgniteEx) GridTestUtils.setFieldValue(org.apache.ignite.testframework.GridTestUtils.setFieldValue) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) CacheObjectContext(org.apache.ignite.internal.processors.cache.CacheObjectContext) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Test(org.junit.Test)

Example 44 with CacheObjectContext

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

the class QueryIndexRowHandler method key.

/**
 * @return Cache key for specified cache row.
 */
public Object key(CacheDataRow row) {
    KeyCacheObject key = row.key();
    Object o = getBinaryObject(key);
    if (o != null)
        return o;
    CacheObjectContext coctx = cacheDesc.context().cacheObjectContext();
    return key.value(coctx, false);
}
Also used : CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) CacheObjectContext(org.apache.ignite.internal.processors.cache.CacheObjectContext) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 45 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.
 */
@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 LongPoint(EXPIRATION_TIME_FIELD_NAME, expires));
        // 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) LongPoint(org.apache.lucene.document.LongPoint) IOException(java.io.IOException) CacheObjectContext(org.apache.ignite.internal.processors.cache.CacheObjectContext) Document(org.apache.lucene.document.Document) LongPoint(org.apache.lucene.document.LongPoint) 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

CacheObjectContext (org.apache.ignite.internal.processors.cache.CacheObjectContext)45 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)21 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)17 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)8 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)7 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)6 IgniteEx (org.apache.ignite.internal.IgniteEx)6 ArrayList (java.util.ArrayList)5 DataEntry (org.apache.ignite.internal.pagemem.wal.record.DataEntry)5 Test (org.junit.Test)5 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)4 MvccDataEntry (org.apache.ignite.internal.pagemem.wal.record.MvccDataEntry)4 GridCacheOperation (org.apache.ignite.internal.processors.cache.GridCacheOperation)4 CacheDataRow (org.apache.ignite.internal.processors.cache.persistence.CacheDataRow)4 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)4 AffinityKeyMapper (org.apache.ignite.cache.affinity.AffinityKeyMapper)3 NodeStoppingException (org.apache.ignite.internal.NodeStoppingException)3 GridCacheDefaultAffinityKeyMapper (org.apache.ignite.internal.processors.cache.GridCacheDefaultAffinityKeyMapper)3 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)3 Message (org.apache.ignite.plugin.extensions.communication.Message)3