View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import java.util.Iterator;
22  import java.util.SortedSet;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.hadoop.hbase.Cell;
27  import org.apache.hadoop.hbase.KeyValue;
28  import org.apache.hadoop.hbase.testclassification.SmallTests;
29  import org.apache.hadoop.hbase.util.Bytes;
30  import org.junit.experimental.categories.Category;
31  
32  @Category(SmallTests.class)
33  public class TestCellSkipListSet extends TestCase {
34    private final CellSkipListSet csls =
35      new CellSkipListSet(KeyValue.COMPARATOR);
36  
37    protected void setUp() throws Exception {
38      super.setUp();
39      this.csls.clear();
40    }
41  
42    public void testAdd() throws Exception {
43      byte [] bytes = Bytes.toBytes(getName());
44      KeyValue kv = new KeyValue(bytes, bytes, bytes, bytes);
45      this.csls.add(kv);
46      assertTrue(this.csls.contains(kv));
47      assertEquals(1, this.csls.size());
48      Cell first = this.csls.first();
49      assertTrue(kv.equals(first));
50      assertTrue(Bytes.equals(kv.getValue(), first.getValue()));
51      // Now try overwritting
52      byte [] overwriteValue = Bytes.toBytes("overwrite");
53      KeyValue overwrite = new KeyValue(bytes, bytes, bytes, overwriteValue);
54      this.csls.add(overwrite);
55      assertEquals(1, this.csls.size());
56      first = this.csls.first();
57      assertTrue(Bytes.equals(overwrite.getValue(), first.getValue()));
58      assertFalse(Bytes.equals(overwrite.getValue(), kv.getValue()));
59    }
60  
61    public void testIterator() throws Exception {
62      byte [] bytes = Bytes.toBytes(getName());
63      byte [] value1 = Bytes.toBytes("1");
64      byte [] value2 = Bytes.toBytes("2");
65      final int total = 3;
66      for (int i = 0; i < total; i++) {
67        this.csls.add(new KeyValue(bytes, bytes, Bytes.toBytes("" + i), value1));
68      }
69      // Assert that we added 'total' values and that they are in order
70      int count = 0;
71      for (Cell kv: this.csls) {
72        assertEquals("" + count, Bytes.toString(kv.getQualifier()));
73        assertTrue(Bytes.equals(kv.getValue(), value1));
74        count++;
75      }
76      assertEquals(total, count);
77      // Now overwrite with a new value.
78      for (int i = 0; i < total; i++) {
79        this.csls.add(new KeyValue(bytes, bytes, Bytes.toBytes("" + i), value2));
80      }
81      // Assert that we added 'total' values and that they are in order and that
82      // we are getting back value2
83      count = 0;
84      for (Cell kv: this.csls) {
85        assertEquals("" + count, Bytes.toString(kv.getQualifier()));
86        assertTrue(Bytes.equals(kv.getValue(), value2));
87        count++;
88      }
89      assertEquals(total, count);
90    }
91  
92    public void testDescendingIterator() throws Exception {
93      byte [] bytes = Bytes.toBytes(getName());
94      byte [] value1 = Bytes.toBytes("1");
95      byte [] value2 = Bytes.toBytes("2");
96      final int total = 3;
97      for (int i = 0; i < total; i++) {
98        this.csls.add(new KeyValue(bytes, bytes, Bytes.toBytes("" + i), value1));
99      }
100     // Assert that we added 'total' values and that they are in order
101     int count = 0;
102     for (Iterator<Cell> i = this.csls.descendingIterator(); i.hasNext();) {
103       Cell kv = i.next();
104       assertEquals("" + (total - (count + 1)), Bytes.toString(kv.getQualifier()));
105       assertTrue(Bytes.equals(kv.getValue(), value1));
106       count++;
107     }
108     assertEquals(total, count);
109     // Now overwrite with a new value.
110     for (int i = 0; i < total; i++) {
111       this.csls.add(new KeyValue(bytes, bytes, Bytes.toBytes("" + i), value2));
112     }
113     // Assert that we added 'total' values and that they are in order and that
114     // we are getting back value2
115     count = 0;
116     for (Iterator<Cell> i = this.csls.descendingIterator(); i.hasNext();) {
117       Cell kv = i.next();
118       assertEquals("" + (total - (count + 1)), Bytes.toString(kv.getQualifier()));
119       assertTrue(Bytes.equals(kv.getValue(), value2));
120       count++;
121     }
122     assertEquals(total, count);
123   }
124 
125   public void testHeadTail() throws Exception {
126     byte [] bytes = Bytes.toBytes(getName());
127     byte [] value1 = Bytes.toBytes("1");
128     byte [] value2 = Bytes.toBytes("2");
129     final int total = 3;
130     KeyValue splitter = null;
131     for (int i = 0; i < total; i++) {
132       KeyValue kv = new KeyValue(bytes, bytes, Bytes.toBytes("" + i), value1);
133       if (i == 1) splitter = kv;
134       this.csls.add(kv);
135     }
136     SortedSet<Cell> tail = this.csls.tailSet(splitter);
137     assertEquals(2, tail.size());
138     SortedSet<Cell> head = this.csls.headSet(splitter);
139     assertEquals(1, head.size());
140     // Now ensure that we get back right answer even when we do tail or head.
141     // Now overwrite with a new value.
142     for (int i = 0; i < total; i++) {
143       this.csls.add(new KeyValue(bytes, bytes, Bytes.toBytes("" + i), value2));
144     }
145     tail = this.csls.tailSet(splitter);
146     assertTrue(Bytes.equals(tail.first().getValue(), value2));
147     head = this.csls.headSet(splitter);
148     assertTrue(Bytes.equals(head.first().getValue(), value2));
149   }
150 }