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 org.apache.hadoop.hbase.KeyValue;
25  import org.apache.hadoop.hbase.SmallTests;
26  import org.apache.hadoop.hbase.util.Bytes;
27  
28  import junit.framework.TestCase;
29  import org.junit.experimental.categories.Category;
30  
31  @Category(SmallTests.class)
32  public class TestKeyValueSkipListSet extends TestCase {
33    private final KeyValueSkipListSet kvsls =
34      new KeyValueSkipListSet(KeyValue.COMPARATOR);
35  
36    protected void setUp() throws Exception {
37      super.setUp();
38      this.kvsls.clear();
39    }
40  
41    public void testAdd() throws Exception {
42      byte [] bytes = Bytes.toBytes(getName());
43      KeyValue kv = new KeyValue(bytes, bytes, bytes, bytes);
44      this.kvsls.add(kv);
45      assertTrue(this.kvsls.contains(kv));
46      assertEquals(1, this.kvsls.size());
47      KeyValue first = this.kvsls.first();
48      assertTrue(kv.equals(first));
49      assertTrue(Bytes.equals(kv.getValue(), first.getValue()));
50      // Now try overwritting
51      byte [] overwriteValue = Bytes.toBytes("overwrite");
52      KeyValue overwrite = new KeyValue(bytes, bytes, bytes, overwriteValue);
53      this.kvsls.add(overwrite);
54      assertEquals(1, this.kvsls.size());
55      first = this.kvsls.first();
56      assertTrue(Bytes.equals(overwrite.getValue(), first.getValue()));
57      assertFalse(Bytes.equals(overwrite.getValue(), kv.getValue()));
58    }
59  
60    public void testIterator() throws Exception {
61      byte [] bytes = Bytes.toBytes(getName());
62      byte [] value1 = Bytes.toBytes("1");
63      byte [] value2 = Bytes.toBytes("2");
64      final int total = 3;
65      for (int i = 0; i < total; i++) {
66        this.kvsls.add(new KeyValue(bytes, bytes, Bytes.toBytes("" + i), value1));
67      }
68      // Assert that we added 'total' values and that they are in order
69      int count = 0;
70      for (KeyValue kv: this.kvsls) {
71        assertEquals("" + count, Bytes.toString(kv.getQualifier()));
72        assertTrue(Bytes.equals(kv.getValue(), value1));
73        count++;
74      }
75      assertEquals(total, count);
76      // Now overwrite with a new value.
77      for (int i = 0; i < total; i++) {
78        this.kvsls.add(new KeyValue(bytes, bytes, Bytes.toBytes("" + i), value2));
79      }
80      // Assert that we added 'total' values and that they are in order and that
81      // we are getting back value2
82      count = 0;
83      for (KeyValue kv: this.kvsls) {
84        assertEquals("" + count, Bytes.toString(kv.getQualifier()));
85        assertTrue(Bytes.equals(kv.getValue(), value2));
86        count++;
87      }
88      assertEquals(total, count);
89    }
90  
91    public void testDescendingIterator() throws Exception {
92      byte [] bytes = Bytes.toBytes(getName());
93      byte [] value1 = Bytes.toBytes("1");
94      byte [] value2 = Bytes.toBytes("2");
95      final int total = 3;
96      for (int i = 0; i < total; i++) {
97        this.kvsls.add(new KeyValue(bytes, bytes, Bytes.toBytes("" + i), value1));
98      }
99      // Assert that we added 'total' values and that they are in order
100     int count = 0;
101     for (Iterator<KeyValue> i = this.kvsls.descendingIterator(); i.hasNext();) {
102       KeyValue kv = i.next();
103       assertEquals("" + (total - (count + 1)), Bytes.toString(kv.getQualifier()));
104       assertTrue(Bytes.equals(kv.getValue(), value1));
105       count++;
106     }
107     assertEquals(total, count);
108     // Now overwrite with a new value.
109     for (int i = 0; i < total; i++) {
110       this.kvsls.add(new KeyValue(bytes, bytes, Bytes.toBytes("" + i), value2));
111     }
112     // Assert that we added 'total' values and that they are in order and that
113     // we are getting back value2
114     count = 0;
115     for (Iterator<KeyValue> i = this.kvsls.descendingIterator(); i.hasNext();) {
116       KeyValue kv = i.next();
117       assertEquals("" + (total - (count + 1)), Bytes.toString(kv.getQualifier()));
118       assertTrue(Bytes.equals(kv.getValue(), value2));
119       count++;
120     }
121     assertEquals(total, count);
122   }
123 
124   public void testHeadTail() throws Exception {
125     byte [] bytes = Bytes.toBytes(getName());
126     byte [] value1 = Bytes.toBytes("1");
127     byte [] value2 = Bytes.toBytes("2");
128     final int total = 3;
129     KeyValue splitter = null;
130     for (int i = 0; i < total; i++) {
131       KeyValue kv = new KeyValue(bytes, bytes, Bytes.toBytes("" + i), value1);
132       if (i == 1) splitter = kv;
133       this.kvsls.add(kv);
134     }
135     SortedSet<KeyValue> tail = this.kvsls.tailSet(splitter);
136     assertEquals(2, tail.size());
137     SortedSet<KeyValue> head = this.kvsls.headSet(splitter);
138     assertEquals(1, head.size());
139     // Now ensure that we get back right answer even when we do tail or head.
140     // Now overwrite with a new value.
141     for (int i = 0; i < total; i++) {
142       this.kvsls.add(new KeyValue(bytes, bytes, Bytes.toBytes("" + i), value2));
143     }
144     tail = this.kvsls.tailSet(splitter);
145     assertTrue(Bytes.equals(tail.first().getValue(), value2));
146     head = this.kvsls.headSet(splitter);
147     assertTrue(Bytes.equals(head.first().getValue(), value2));
148   }
149 
150 }
151