/** * NOTE: Use this method always when you need to calculate partition id for * a key provided by user. It's required since we should apply affinity mapper * logic in order to find a key that will eventually be passed to affinity function. * * @param key Key. * @param useKeyPart If {@code true} can use pre-calculated partition stored in KeyCacheObject. * @return Partition. */ public int partition(Object key, boolean useKeyPart) { GridAffinityAssignmentCache aff0 = aff; if (aff0 == null) throw new IgniteException(FAILED_TO_FIND_CACHE_ERR_MSG + cctx.name()); if (useKeyPart && (key instanceof KeyCacheObject)) { int part = ((KeyCacheObject)key).partition(); if (part != -1) return part; } return affFunction.partition(affinityKey(key)); }
return null; Object pkAffKeyFirst = cctx.affinity().affinityKey(pkFirst.getObject()); Object pkAffKeyLast = cctx.affinity().affinityKey(pkLast.getObject());
/** * NOTE: Use this method always when you need to calculate partition id for * a key provided by user. It's required since we should apply affinity mapper * logic in order to find a key that will eventually be passed to affinity function. * * @param key Key. * @param useKeyPart If {@code true} can use pre-calculated partition stored in KeyCacheObject. * @return Partition. */ public int partition(Object key, boolean useKeyPart) { GridAffinityAssignmentCache aff0 = aff; if (aff0 == null) throw new IgniteException(FAILED_TO_FIND_CACHE_ERR_MSG + cctx.name()); if (useKeyPart && (key instanceof KeyCacheObject)) { int part = ((KeyCacheObject)key).partition(); if (part != -1) return part; } return affFunction.partition(affinityKey(key)); }
/** * @param firstRow First row. * @param lastRow Last row. * @return Affinity key or {@code null}. */ private Object getAffinityKey(SearchRow firstRow, SearchRow lastRow) { if (firstRow == null || lastRow == null) return null; Value affKeyFirst = firstRow.getValue(affColId); Value affKeyLast = lastRow.getValue(affColId); if (affKeyFirst != null && equal(affKeyFirst, affKeyLast)) return affKeyFirst == ValueNull.INSTANCE ? EXPLICIT_NULL : affKeyFirst.getObject(); if (getTable().rowDescriptor().isKeyColumn(affColId)) return null; // Try to extract affinity key from primary key. Value pkFirst = firstRow.getValue(KEY_COL); Value pkLast = lastRow.getValue(KEY_COL); if (pkFirst == ValueNull.INSTANCE || pkLast == ValueNull.INSTANCE) return EXPLICIT_NULL; if (pkFirst == null || pkLast == null || !equal(pkFirst, pkLast)) return null; Object pkAffKeyFirst = cctx.affinity().affinityKey(pkFirst.getObject()); Object pkAffKeyLast = cctx.affinity().affinityKey(pkLast.getObject()); if (pkAffKeyFirst == null || pkAffKeyLast == null) throw new CacheException("Cache key without affinity key."); if (pkAffKeyFirst.equals(pkAffKeyLast)) return pkAffKeyFirst; return null; }