/** * @param key Key. * @return Affinity key. */ private Object affinityKey(Object key) { if (key instanceof CacheObject && !(key instanceof BinaryObject)) key = ((CacheObject)key).value(cacheObjCtx, false); return mapper.affinityKey(key); }
/** * If Key is {@link GridCacheInternal GridCacheInternal} entry when won't passed into user's mapper and * will use {@link GridCacheDefaultAffinityKeyMapper default}. * * @param key Key. * @return Affinity key. */ public Object affinityKey(Object key) { CacheObjectContext coCtx = cctx.cacheObjectContext(); if (key instanceof CacheObject && !(key instanceof BinaryObject)) key = ((CacheObject)key).value(coCtx, false); return (key instanceof GridCacheInternal ? coCtx.defaultAffMapper() : affMapper).affinityKey(key); }
/** {@inheritDoc} */ @Override public Object affinityKey(K key) { A.notNull(key, "key"); if (key instanceof CacheObject && !(key instanceof BinaryObject)) { CacheObjectContext ctx = cctx.cacheObjectContext(); if (ctx == null) throw new IgniteException(FAILED_TO_FIND_CACHE_ERR_MSG + cctx.name()); key = ((CacheObject)key).value(ctx, false); } CacheConfiguration ccfg = cctx.config(); if (ccfg == null) throw new IgniteException(FAILED_TO_FIND_CACHE_ERR_MSG + cctx.name()); return ccfg.getAffinityMapper().affinityKey(key); }
/** * */ @Test public void testFieldAffinityMapper() { AffinityKeyMapper mapper = new GridCacheDefaultAffinityKeyMapper(); GridTestUtils.setFieldValue(mapper, "ignite", grid()); List<FieldAffinityKey<Integer>> keys = new ArrayList<>(); for (int i = 1; i <= 10; i++) keys.add(new FieldAffinityKey<>(i, Integer.toString(i))); for (int i = 1; i <= 10; i++) { FieldAffinityKey<Integer> key = keys.get(i - 1); Object mapped = mapper.affinityKey(key); info("Mapped key: " + mapped); assertNotNull(mapped); assertSame(key.affinityKey(), mapped); } }
/** * */ @Test public void testMethodAffinityMapper() { AffinityKeyMapper mapper = new GridCacheDefaultAffinityKeyMapper(); GridTestUtils.setFieldValue(mapper, "ignite", grid()); List<AffinityKey<Integer>> keys = new ArrayList<>(); for (int i = 1; i <= 10; i++) keys.add(new AffinityKey<>(i, Integer.toString(i))); for (int i = 1; i <= 10; i++) { AffinityKey<Integer> key = keys.get(i - 1); Object mapped = mapper.affinityKey(key); info("Mapped key: " + mapped); assertNotNull(mapped); assertSame(key.affinityKey(), mapped); } }
/** * */ @Test public void testFieldAffinityMapperWithWrongClass() { AffinityKeyMapper mapper = new GridCacheDefaultAffinityKeyMapper(); GridTestUtils.setFieldValue(mapper, "ignite", grid()); FieldNoAffinityKey key = new FieldNoAffinityKey(); Object mapped = mapper.affinityKey(key); assertEquals(key, mapped); }
/** * @throws Exception If failed. */ @Test public void testPartitionWithAffinityMapper() throws Exception { AffinityKey<Integer> key = new AffinityKey<>(1, 2); int expPart = affinity().partition(affinityMapper().affinityKey(key)); for (int i = 0; i < gridCount(); i++) assertEquals(expPart, grid(i).affinity(DEFAULT_CACHE_NAME).partition(key)); } }
/** * @param key Key. * @return Affinity key. */ private Object affinityKey(Object key) { if (key instanceof CacheObject && !(key instanceof BinaryObject)) key = ((CacheObject)key).value(cacheObjCtx, false); return mapper.affinityKey(key); }
/** * If Key is {@link GridCacheInternal GridCacheInternal} entry when won't passed into user's mapper and * will use {@link GridCacheDefaultAffinityKeyMapper default}. * * @param key Key. * @return Affinity key. */ public Object affinityKey(Object key) { if (key instanceof CacheObject && !(key instanceof BinaryObject)) key = ((CacheObject)key).value(cctx.cacheObjectContext(), false); return (key instanceof GridCacheInternal ? cctx.defaultAffMapper() : affMapper).affinityKey(key); }
/** {@inheritDoc} */ @Override public Object affinityKey(K key) { A.notNull(key, "key"); if (key instanceof CacheObject && !(key instanceof BinaryObject)) { CacheObjectContext ctx = cctx.cacheObjectContext(); if (ctx == null) throw new IgniteException(FAILED_TO_FIND_CACHE_ERR_MSG + cctx.name()); key = ((CacheObject)key).value(ctx, false); } CacheConfiguration ccfg = cctx.config(); if (ccfg == null) throw new IgniteException(FAILED_TO_FIND_CACHE_ERR_MSG + cctx.name()); return ccfg.getAffinityMapper().affinityKey(key); }