GramKeyPartitioner extends Partitioner<GramKey, Gram> { @Override public int getPartition(GramKey key, Gram value, int numPartitions) { int hash = 1; byte[] bytes = key.getBytes(); int length = key.getPrimaryLength(); for (int i = 1; i < length; i++) { hash = (31 * hash) + bytes[i]; } return (hash & Integer.MAX_VALUE) % numPartitions; }  @Override int getPartition(GramKey key, Gram value, int numPartitions);  }
@Test public void testPartition() { byte[] foo = new byte[1]; foo[0] = 1; foo[0] = 2; byte[] empty = new byte[0]; GramKey a = new GramKey(new Gram("foo", 1, Gram.Type.HEAD), empty); GramKey b = new GramKey(new Gram("foo", 1, Gram.Type.HEAD), foo); byte[] bar = new byte[1]; GramKey c = new GramKey(new Gram("foo", 2, Gram.Type.HEAD), bar); GramKey d = new GramKey(new Gram("foo", 1, Gram.Type.TAIL), empty); GramKey e = new GramKey(new Gram("foo", 2, Gram.Type.TAIL), foo); Partitioner<GramKey, Gram> p = new GramKeyPartitioner(); int numPartitions = 5; int ap = p.getPartition(a, null, numPartitions); int bp = p.getPartition(b, null, numPartitions); int cp = p.getPartition(c, null, numPartitions); int dp = p.getPartition(d, null, numPartitions); int ep = p.getPartition(e, null, numPartitions); assertEquals(ap, bp); assertEquals(ap, cp); assertEquals(dp, ep); }