/** * Returns a new consistent hash of the same type with the given address removed. * * @param ch consistent hash to start with * @param toRemove address to remove * @param c configuration * @return a new consistent hash instance of the same type */ public static ConsistentHash removeAddress(ConsistentHash ch, Address toRemove, Configuration c) { if (ch instanceof UnionConsistentHash) return removeAddressFromUnionConsistentHash((UnionConsistentHash) ch, toRemove, c); else { ConsistentHash newCH = constructConsistentHashInstance(c); Set<Address> caches = new HashSet<Address>(ch.getCaches()); caches.remove(toRemove); newCH.setCaches(caches); return newCH; } }
/** * Creates a new consistent hash instance based on the type specified, and populates the * consistent hash with the collection of addresses passed in. * * @param template An older consistent hash instance to clone * @param addresses with which to populate the consistent hash * @return a new consistent hash instance */ public static ConsistentHash createConsistentHash( ConsistentHash template, Collection<Address> addresses) { Hash hf = null; HashSeed hs = null; int numVirtualNodes = 1; GroupManager groupManager = null; if (template instanceof AbstractWheelConsistentHash) { AbstractWheelConsistentHash wTemplate = (AbstractWheelConsistentHash) template; hf = wTemplate.hashFunction; hs = wTemplate.hashSeed; numVirtualNodes = wTemplate.numVirtualNodes; groupManager = wTemplate.groupManager; } ConsistentHash ch = constructConsistentHashInstance(template.getClass(), hf, hs, numVirtualNodes, groupManager); if (addresses != null && !addresses.isEmpty()) ch.setCaches(toSet(addresses)); return ch; }
/** * Creates a new consistent hash instance based on the user's configuration, and populates the * consistent hash with the collection of addresses passed in. * * @param c configuration * @param addresses with which to populate the consistent hash * @return a new consistent hash instance */ public static ConsistentHash createConsistentHash( Configuration c, Collection<Address> addresses) { ConsistentHash ch = constructConsistentHashInstance(c); ch.setCaches(toSet(addresses)); return ch; }