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));
}
}
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());
}
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);
}
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);
}
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();
}
}
Aggregations