001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.mapreduce;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021
022import java.nio.charset.StandardCharsets;
023import java.util.Base64;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
028import org.apache.hadoop.hbase.testclassification.MapReduceTests;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.junit.jupiter.api.Tag;
032import org.junit.jupiter.api.Test;
033
034/**
035 * Test of simple partitioner.
036 */
037@Tag(MapReduceTests.TAG)
038@Tag(SmallTests.TAG)
039public class TestSimpleTotalOrderPartitioner {
040
041  protected final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
042  Configuration conf = TEST_UTIL.getConfiguration();
043
044  @Test
045  public void testSplit() throws Exception {
046    String start = "a";
047    String end = "{";
048    SimpleTotalOrderPartitioner<byte[]> p = new SimpleTotalOrderPartitioner<>();
049
050    this.conf.set(SimpleTotalOrderPartitioner.START_BASE64,
051      Base64.getEncoder().encodeToString(start.getBytes(StandardCharsets.UTF_8)));
052    this.conf.set(SimpleTotalOrderPartitioner.END_BASE64,
053      Base64.getEncoder().encodeToString(end.getBytes(StandardCharsets.UTF_8)));
054    p.setConf(this.conf);
055    ImmutableBytesWritable c = new ImmutableBytesWritable(Bytes.toBytes("c"));
056    // If one reduce, partition should be 0.
057    int partition = p.getPartition(c, HConstants.EMPTY_BYTE_ARRAY, 1);
058    assertEquals(0, partition);
059    // If two reduces, partition should be 0.
060    partition = p.getPartition(c, HConstants.EMPTY_BYTE_ARRAY, 2);
061    assertEquals(0, partition);
062    // Divide in 3.
063    partition = p.getPartition(c, HConstants.EMPTY_BYTE_ARRAY, 3);
064    assertEquals(0, partition);
065    ImmutableBytesWritable q = new ImmutableBytesWritable(Bytes.toBytes("q"));
066    partition = p.getPartition(q, HConstants.EMPTY_BYTE_ARRAY, 2);
067    assertEquals(1, partition);
068    partition = p.getPartition(q, HConstants.EMPTY_BYTE_ARRAY, 3);
069    assertEquals(2, partition);
070    // What about end and start keys.
071    ImmutableBytesWritable startBytes = new ImmutableBytesWritable(Bytes.toBytes(start));
072    partition = p.getPartition(startBytes, HConstants.EMPTY_BYTE_ARRAY, 2);
073    assertEquals(0, partition);
074    partition = p.getPartition(startBytes, HConstants.EMPTY_BYTE_ARRAY, 3);
075    assertEquals(0, partition);
076    ImmutableBytesWritable endBytes = new ImmutableBytesWritable(Bytes.toBytes("z"));
077    partition = p.getPartition(endBytes, HConstants.EMPTY_BYTE_ARRAY, 2);
078    assertEquals(1, partition);
079    partition = p.getPartition(endBytes, HConstants.EMPTY_BYTE_ARRAY, 3);
080    assertEquals(2, partition);
081  }
082
083}