1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
77 for (int i = 0; i < total; i++) {
78 this.kvsls.add(new KeyValue(bytes, bytes, Bytes.toBytes("" + i), value2));
79 }
80
81
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
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
109 for (int i = 0; i < total; i++) {
110 this.kvsls.add(new KeyValue(bytes, bytes, Bytes.toBytes("" + i), value2));
111 }
112
113
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
140
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