| 1 | package net.spy.memcached; |
| 2 | |
| 3 | import java.util.Arrays; |
| 4 | import java.util.Collection; |
| 5 | import java.util.Iterator; |
| 6 | import java.util.List; |
| 7 | |
| 8 | /** |
| 9 | * NodeLocator implementation for dealing with simple array lookups using a |
| 10 | * modulus of the hash code and node list length. |
| 11 | */ |
| 12 | public final class ArrayModNodeLocator implements NodeLocator { |
| 13 | |
| 14 | final MemcachedNode[] nodes; |
| 15 | |
| 16 | private final HashAlgorithm hashAlg; |
| 17 | |
| 18 | /** |
| 19 | * Construct an ArraymodNodeLocator over the given array of nodes and |
| 20 | * using the given hash algorithm. |
| 21 | * |
| 22 | * @param n the array of nodes |
| 23 | * @param alg the hash algorithm |
| 24 | */ |
| 25 | public ArrayModNodeLocator(List<MemcachedNode> n, HashAlgorithm alg) { |
| 26 | super(); |
| 27 | nodes=n.toArray(new MemcachedNode[n.size()]); |
| 28 | hashAlg=alg; |
| 29 | } |
| 30 | |
| 31 | private ArrayModNodeLocator(MemcachedNode[] n, HashAlgorithm alg) { |
| 32 | super(); |
| 33 | nodes=n; |
| 34 | hashAlg=alg; |
| 35 | } |
| 36 | |
| 37 | public Collection<MemcachedNode> getAll() { |
| 38 | return Arrays.asList(nodes); |
| 39 | } |
| 40 | |
| 41 | public MemcachedNode getPrimary(String k) { |
| 42 | return nodes[getServerForKey(k)]; |
| 43 | } |
| 44 | |
| 45 | public Iterator<MemcachedNode> getSequence(String k) { |
| 46 | return new NodeIterator(getServerForKey(k)); |
| 47 | } |
| 48 | |
| 49 | public NodeLocator getReadonlyCopy() { |
| 50 | MemcachedNode[] n=new MemcachedNode[nodes.length]; |
| 51 | for(int i=0; i<nodes.length; i++) { |
| 52 | n[i] = new MemcachedNodeROImpl(nodes[i]); |
| 53 | } |
| 54 | return new ArrayModNodeLocator(n, hashAlg); |
| 55 | } |
| 56 | |
| 57 | private int getServerForKey(String key) { |
| 58 | int rv=(int)(hashAlg.hash(key) % nodes.length); |
| 59 | assert rv >= 0 : "Returned negative key for key " + key; |
| 60 | assert rv < nodes.length |
| 61 | : "Invalid server number " + rv + " for key " + key; |
| 62 | return rv; |
| 63 | } |
| 64 | |
| 65 | class NodeIterator implements Iterator<MemcachedNode> { |
| 66 | |
| 67 | private final int start; |
| 68 | private int next=0; |
| 69 | |
| 70 | public NodeIterator(int keyStart) { |
| 71 | start=keyStart; |
| 72 | next=start; |
| 73 | computeNext(); |
| 74 | assert next >= 0 || nodes.length == 1 |
| 75 | : "Starting sequence at " + start + " of " |
| 76 | + nodes.length + " next is " + next; |
| 77 | } |
| 78 | |
| 79 | public boolean hasNext() { |
| 80 | return next >= 0; |
| 81 | } |
| 82 | |
| 83 | private void computeNext() { |
| 84 | if(++next >= nodes.length) { |
| 85 | next=0; |
| 86 | } |
| 87 | if(next == start) { |
| 88 | next=-1; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | public MemcachedNode next() { |
| 93 | try { |
| 94 | return nodes[next]; |
| 95 | } finally { |
| 96 | computeNext(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | public void remove() { |
| 101 | throw new UnsupportedOperationException("Can't remove a node"); |
| 102 | } |
| 103 | |
| 104 | } |
| 105 | } |