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