public AbstractBounds(T left, T right) { assert left.getPartitioner() == right.getPartitioner(); this.left = left; this.right = right; }
public static <T extends RingPosition<T>> Boundary<T> minRight(Boundary<T> right1, Boundary<T> right2) { int c = right1.boundary.compareTo(right2.boundary); if (c != 0) return c < 0 ? right1 : right2; // return the exclusive version, if either return right2.inclusive ? right1 : right2; }
/** * Merges the list of tokens for each cluster machine to a single list of token ranges. * * @param tokens the map of tokens for each cluster machine. * @param session the connection to the cluster. * @param p the partitioner used in the cluster. * @return the merged lists of tokens transformed to DeepTokenRange(s). The returned collection is shuffled. */ static List<DeepTokenRange> mergeTokenRanges(Map<String, Iterable<Comparable>> tokens, final Session session, final IPartitioner p) { final Iterable<Comparable> allRanges = Ordering.natural().sortedCopy(concat(tokens.values())); final Comparable maxValue = Ordering.natural().max(allRanges); final Comparable minValue = (Comparable) p.minValue(maxValue.getClass()).getToken().token; Function<Comparable, Set<DeepTokenRange>> map = new MergeTokenRangesFunction(maxValue, minValue, session, p, allRanges); Iterable<DeepTokenRange> concatenated = concat(transform(allRanges, map)); Set<DeepTokenRange> dedup = Sets.newHashSet(concatenated); return Ordering.natural().sortedCopy(dedup); }
public boolean contains(T position) { // Range.contains doesnt work correctly if left == right (unless both // are minimum) because for Range that means a wrapping range that select // the whole ring. So we must explicitely handle this case return left.equals(position) || ((right.isMinimum() || !left.equals(right)) && Range.contains(left, right, position)); }
public List<Range<T>> unwrap() { T minValue = right.minValue(); if (!isWrapAround() || right.equals(minValue)) return Arrays.asList(this); List<Range<T>> unwrapped = new ArrayList<Range<T>>(2); unwrapped.add(new Range<T>(left, minValue)); unwrapped.add(new Range<T>(minValue, right)); return unwrapped; }
public static <T extends RingPosition<T>> boolean isEmpty(Boundary<T> left, Boundary<T> right) { int c = left.boundary.compareTo(right.boundary); return c > 0 || (c == 0 && !(left.inclusive && right.inclusive)); }
/** * get the (possibly cached) endpoints that should store the given Token. * Note that while the endpoints are conceptually a Set (no duplicates will be included), * we return a List to avoid an extra allocation when sorting by proximity later * @param searchPosition the position the natural endpoints are requested for * @return a copy of the natural endpoints for the given token */ public ArrayList<InetAddress> getNaturalEndpoints(RingPosition searchPosition) { Token searchToken = searchPosition.getToken(); Token keyToken = TokenMetadata.firstToken(tokenMetadata.sortedTokens(), searchToken); ArrayList<InetAddress> endpoints = getCachedEndpoints(keyToken); if (endpoints == null) { TokenMetadata tm = tokenMetadata.cachedOnlyTokenMap(); // if our cache got invalidated, it's possible there is a new token to account for too keyToken = TokenMetadata.firstToken(tm.sortedTokens(), searchToken); endpoints = new ArrayList<InetAddress>(calculateNaturalEndpoints(searchToken, tm)); cachedEndpoints.put(keyToken, endpoints); } return new ArrayList<InetAddress>(endpoints); }
public boolean contains(T position) { // Range.contains doesnt work correctly if left == right (unless both // are minimum) because for Range that means a wrapping range that select // the whole ring. So we must explicitely handle this case return left.equals(position) || ((right.isMinimum() || !left.equals(right)) && Range.contains(left, right, position)); }
public List<Range<T>> unwrap() { T minValue = right.minValue(); if (!isWrapAround() || right.equals(minValue)) return Arrays.asList(this); List<Range<T>> unwrapped = new ArrayList<Range<T>>(2); unwrapped.add(new Range<T>(left, minValue)); unwrapped.add(new Range<T>(minValue, right)); return unwrapped; }
/** * Whether {@code left} and {@code right} forms a wrapping interval, that is if unwrapping wouldn't be a no-op. * <p> * Note that the semantic is slightly different from {@link Range#isWrapAround()} in the sense that if both * {@code right} are minimal (for the partitioner), this methods return false (doesn't wrap) while * {@link Range#isWrapAround()} returns true (does wrap). This is confusing and we should fix it by * refactoring/rewriting the whole AbstractBounds hierarchy with cleaner semantics, but we don't want to risk * breaking something by changing {@link Range#isWrapAround()} in the meantime. */ public static <T extends RingPosition<T>> boolean strictlyWrapsAround(T left, T right) { return !(left.compareTo(right) <= 0 || right.isMinimum()); }
public int compare(Bounds<T> o1, Bounds<T> o2) { return o1.left.compareTo(o2.left); } });
/** * get the (possibly cached) endpoints that should store the given Token. * Note that while the endpoints are conceptually a Set (no duplicates will be included), * we return a List to avoid an extra allocation when sorting by proximity later * @param searchPosition the position the natural endpoints are requested for * @return a copy of the natural endpoints for the given token */ public ArrayList<InetAddress> getNaturalEndpoints(RingPosition searchPosition) { Token searchToken = searchPosition.getToken(); Token keyToken = TokenMetadata.firstToken(tokenMetadata.sortedTokens(), searchToken); ArrayList<InetAddress> endpoints = getCachedEndpoints(keyToken); if (endpoints == null) { TokenMetadata tm = tokenMetadata.cachedOnlyTokenMap(); // if our cache got invalidated, it's possible there is a new token to account for too keyToken = TokenMetadata.firstToken(tm.sortedTokens(), searchToken); endpoints = new ArrayList<InetAddress>(calculateNaturalEndpoints(searchToken, tm)); cachedEndpoints.put(keyToken, endpoints); } return new ArrayList<InetAddress>(endpoints); }
public boolean contains(T position) { // Range.contains doesnt work correctly if left == right (unless both // are minimum) because for Range that means a wrapping range that select // the whole ring. So we must explicitely handle this case return left.equals(position) || ((right.isMinimum(partitioner) || !left.equals(right)) && Range.contains(left, right, position)); }