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.regionserver;
019
020import java.util.Iterator;
021import java.util.SortedSet;
022import junit.framework.TestCase;
023import org.apache.hadoop.hbase.Cell;
024import org.apache.hadoop.hbase.CellComparatorImpl;
025import org.apache.hadoop.hbase.CellUtil;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.KeyValue;
028import org.apache.hadoop.hbase.testclassification.RegionServerTests;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.junit.ClassRule;
032import org.junit.experimental.categories.Category;
033
034@Category({RegionServerTests.class, SmallTests.class})
035public class TestCellSkipListSet extends TestCase {
036
037  @ClassRule
038  public static final HBaseClassTestRule CLASS_RULE =
039      HBaseClassTestRule.forClass(TestCellSkipListSet.class);
040
041  private final CellSet csls =
042    new CellSet(CellComparatorImpl.COMPARATOR);
043
044  @Override
045  protected void setUp() throws Exception {
046    super.setUp();
047    this.csls.clear();
048  }
049
050  public void testAdd() throws Exception {
051    byte[] bytes = Bytes.toBytes(getName());
052    KeyValue kv = new KeyValue(bytes, bytes, bytes, bytes);
053    this.csls.add(kv);
054    assertTrue(this.csls.contains(kv));
055    assertEquals(1, this.csls.getDelegatee().size());
056    Cell first = this.csls.first();
057    assertTrue(kv.equals(first));
058    assertTrue(Bytes.equals(kv.getValueArray(), kv.getValueOffset(), kv.getValueLength(),
059      first.getValueArray(), first.getValueOffset(), first.getValueLength()));
060    // Now try overwritting
061    byte[] overwriteValue = Bytes.toBytes("overwrite");
062    KeyValue overwrite = new KeyValue(bytes, bytes, bytes, overwriteValue);
063    this.csls.add(overwrite);
064    assertEquals(1, this.csls.getDelegatee().size());
065    first = this.csls.first();
066    assertTrue(Bytes.equals(overwrite.getValueArray(), overwrite.getValueOffset(),
067      overwrite.getValueLength(), first.getValueArray(), first.getValueOffset(),
068      first.getValueLength()));
069    assertFalse(Bytes.equals(CellUtil.cloneValue(overwrite), CellUtil.cloneValue(kv)));
070  }
071
072  public void testIterator() throws Exception {
073    byte [] bytes = Bytes.toBytes(getName());
074    byte [] value1 = Bytes.toBytes("1");
075    byte [] value2 = Bytes.toBytes("2");
076    final int total = 3;
077    for (int i = 0; i < total; i++) {
078      this.csls.add(new KeyValue(bytes, bytes, Bytes.toBytes("" + i), value1));
079    }
080    // Assert that we added 'total' values and that they are in order
081    int count = 0;
082    for (Cell kv: this.csls) {
083      assertEquals("" + count,
084        Bytes.toString(kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength()));
085      assertTrue(Bytes.equals(kv.getValueArray(), kv.getValueOffset(), kv.getValueLength(), value1,
086        0, value1.length));
087      count++;
088    }
089    assertEquals(total, count);
090    // Now overwrite with a new value.
091    for (int i = 0; i < total; i++) {
092      this.csls.add(new KeyValue(bytes, bytes, Bytes.toBytes("" + i), value2));
093    }
094    // Assert that we added 'total' values and that they are in order and that
095    // we are getting back value2
096    count = 0;
097    for (Cell kv : this.csls) {
098      assertEquals("" + count,
099        Bytes.toString(kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength()));
100      assertTrue(Bytes.equals(kv.getValueArray(), kv.getValueOffset(), kv.getValueLength(), value2,
101        0, value2.length));
102      count++;
103    }
104    assertEquals(total, count);
105  }
106
107  public void testDescendingIterator() throws Exception {
108    byte [] bytes = Bytes.toBytes(getName());
109    byte [] value1 = Bytes.toBytes("1");
110    byte [] value2 = Bytes.toBytes("2");
111    final int total = 3;
112    for (int i = 0; i < total; i++) {
113      this.csls.add(new KeyValue(bytes, bytes, Bytes.toBytes("" + i), value1));
114    }
115    // Assert that we added 'total' values and that they are in order
116    int count = 0;
117    for (Iterator<Cell> i = this.csls.descendingIterator(); i.hasNext();) {
118      Cell kv = i.next();
119      assertEquals("" + (total - (count + 1)),
120        Bytes.toString(kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength()));
121      assertTrue(Bytes.equals(kv.getValueArray(), kv.getValueOffset(), kv.getValueLength(), value1,
122        0, value1.length));
123      count++;
124    }
125    assertEquals(total, count);
126    // Now overwrite with a new value.
127    for (int i = 0; i < total; i++) {
128      this.csls.add(new KeyValue(bytes, bytes, Bytes.toBytes("" + i), value2));
129    }
130    // Assert that we added 'total' values and that they are in order and that
131    // we are getting back value2
132    count = 0;
133    for (Iterator<Cell> i = this.csls.descendingIterator(); i.hasNext();) {
134      Cell kv = i.next();
135      assertEquals("" + (total - (count + 1)),
136        Bytes.toString(kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength()));
137      assertTrue(Bytes.equals(kv.getValueArray(), kv.getValueOffset(), kv.getValueLength(), value2,
138        0, value2.length));
139      count++;
140    }
141    assertEquals(total, count);
142  }
143
144  public void testHeadTail() throws Exception {
145    byte [] bytes = Bytes.toBytes(getName());
146    byte [] value1 = Bytes.toBytes("1");
147    byte [] value2 = Bytes.toBytes("2");
148    final int total = 3;
149    KeyValue splitter = null;
150    for (int i = 0; i < total; i++) {
151      KeyValue kv = new KeyValue(bytes, bytes, Bytes.toBytes("" + i), value1);
152      if (i == 1) splitter = kv;
153      this.csls.add(kv);
154    }
155    SortedSet<Cell> tail = this.csls.tailSet(splitter);
156    assertEquals(2, tail.size());
157    SortedSet<Cell> head = this.csls.headSet(splitter);
158    assertEquals(1, head.size());
159    // Now ensure that we get back right answer even when we do tail or head.
160    // Now overwrite with a new value.
161    for (int i = 0; i < total; i++) {
162      this.csls.add(new KeyValue(bytes, bytes, Bytes.toBytes("" + i), value2));
163    }
164    tail = this.csls.tailSet(splitter);
165    assertTrue(Bytes.equals(tail.first().getValueArray(), tail.first().getValueOffset(),
166      tail.first().getValueLength(), value2, 0, value2.length));
167    head = this.csls.headSet(splitter);
168    assertTrue(Bytes.equals(head.first().getValueArray(), head.first().getValueOffset(),
169      head.first().getValueLength(), value2, 0, value2.length));
170  }
171}