1   /**
2    * Copyright 2009 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.mapreduce;
21  
22  import org.apache.hadoop.hbase.HBaseTestCase;
23  import org.apache.hadoop.hbase.HConstants;
24  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
25  import org.apache.hadoop.hbase.util.Bytes;
26  
27  /**
28   * Test of simple partitioner.
29   */
30  public class TestSimpleTotalOrderPartitioner extends HBaseTestCase {
31    public void testSplit() throws Exception {
32      String start = "a";
33      String end = "{";
34      SimpleTotalOrderPartitioner<byte []> p =
35        new SimpleTotalOrderPartitioner<byte []>();
36      this.conf.set(SimpleTotalOrderPartitioner.START, start);
37      this.conf.set(SimpleTotalOrderPartitioner.END, end);
38      p.setConf(this.conf);
39      ImmutableBytesWritable c = new ImmutableBytesWritable(Bytes.toBytes("c"));
40      // If one reduce, partition should be 0.
41      int partition = p.getPartition(c, HConstants.EMPTY_BYTE_ARRAY, 1);
42      assertEquals(0, partition);
43      // If two reduces, partition should be 0.
44      partition = p.getPartition(c, HConstants.EMPTY_BYTE_ARRAY, 2);
45      assertEquals(0, partition);
46      // Divide in 3.
47      partition = p.getPartition(c, HConstants.EMPTY_BYTE_ARRAY, 3);
48      assertEquals(0, partition);
49      ImmutableBytesWritable q = new ImmutableBytesWritable(Bytes.toBytes("q"));
50      partition = p.getPartition(q, HConstants.EMPTY_BYTE_ARRAY, 2);
51      assertEquals(1, partition);
52      partition = p.getPartition(q, HConstants.EMPTY_BYTE_ARRAY, 3);
53      assertEquals(2, partition);
54      // What about end and start keys.
55      ImmutableBytesWritable startBytes =
56        new ImmutableBytesWritable(Bytes.toBytes(start));
57      partition = p.getPartition(startBytes, HConstants.EMPTY_BYTE_ARRAY, 2);
58      assertEquals(0, partition);
59      partition = p.getPartition(startBytes, HConstants.EMPTY_BYTE_ARRAY, 3);
60      assertEquals(0, partition);
61      ImmutableBytesWritable endBytes =
62        new ImmutableBytesWritable(Bytes.toBytes("z"));
63      partition = p.getPartition(endBytes, HConstants.EMPTY_BYTE_ARRAY, 2);
64      assertEquals(1, partition);
65      partition = p.getPartition(endBytes, HConstants.EMPTY_BYTE_ARRAY, 3);
66      assertEquals(2, partition);
67    }
68  }