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 
20 package org.apache.hadoop.hbase.regionserver;
21 
22 import org.apache.hadoop.hbase.HBaseTestCase;
23 import org.apache.hadoop.hbase.KeyValue;
24 import org.apache.hadoop.hbase.testclassification.SmallTests;
25 import org.apache.hadoop.hbase.regionserver.DeleteTracker.DeleteResult;
26 import org.apache.hadoop.hbase.util.Bytes;
27 import org.junit.experimental.categories.Category;
28 
29 
30 @Category(SmallTests.class)
31 public class TestScanDeleteTracker extends HBaseTestCase {
32 
33   private ScanDeleteTracker sdt;
34   private long timestamp = 10L;
35   private byte deleteType = 0;
36 
setUp()37   public void setUp() throws Exception {
38     super.setUp();
39     sdt = new ScanDeleteTracker();
40   }
41 
testDeletedBy_Delete()42   public void testDeletedBy_Delete() {
43     KeyValue kv = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("f"),
44         Bytes.toBytes("qualifier"), timestamp, KeyValue.Type.Delete);
45     sdt.add(kv);
46     DeleteResult ret = sdt.isDeleted(kv);
47     assertEquals(DeleteResult.VERSION_DELETED, ret);
48   }
49 
testDeletedBy_DeleteColumn()50   public void testDeletedBy_DeleteColumn() {
51     KeyValue kv = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("f"),
52         Bytes.toBytes("qualifier"), timestamp, KeyValue.Type.DeleteColumn);
53     sdt.add(kv);
54     timestamp -= 5;
55     kv = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("f"),
56         Bytes.toBytes("qualifier"), timestamp , KeyValue.Type.DeleteColumn);
57     DeleteResult ret = sdt.isDeleted(kv);
58     assertEquals(DeleteResult.COLUMN_DELETED, ret);
59   }
60 
testDeletedBy_DeleteFamily()61   public void testDeletedBy_DeleteFamily() {
62     KeyValue kv = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("f"),
63         Bytes.toBytes("qualifier"), timestamp, KeyValue.Type.DeleteFamily);
64     sdt.add(kv);
65     timestamp -= 5;
66     kv = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("f"),
67         Bytes.toBytes("qualifier"), timestamp , KeyValue.Type.DeleteColumn);
68     DeleteResult ret = sdt.isDeleted(kv);
69     assertEquals(DeleteResult.FAMILY_DELETED, ret);
70   }
71 
testDeletedBy_DeleteFamilyVersion()72   public void testDeletedBy_DeleteFamilyVersion() {
73     byte [] qualifier1 = Bytes.toBytes("qualifier1");
74     byte [] qualifier2 = Bytes.toBytes("qualifier2");
75     byte [] qualifier3 = Bytes.toBytes("qualifier3");
76     byte [] qualifier4 = Bytes.toBytes("qualifier4");
77     deleteType = KeyValue.Type.DeleteFamilyVersion.getCode();
78     KeyValue kv = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("f"),
79         null, timestamp, KeyValue.Type.DeleteFamilyVersion);
80     sdt.add(kv);
81     kv = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("f"),
82         qualifier1, timestamp, KeyValue.Type.DeleteFamilyVersion);
83     DeleteResult ret = sdt.isDeleted(kv);
84     assertEquals(DeleteResult.FAMILY_VERSION_DELETED, ret);
85     kv = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("f"),
86         qualifier2, timestamp, KeyValue.Type.DeleteFamilyVersion);
87     ret = sdt.isDeleted(kv);
88     assertEquals(DeleteResult.FAMILY_VERSION_DELETED, ret);
89     kv = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("f"),
90         qualifier3, timestamp, KeyValue.Type.DeleteFamilyVersion);
91     ret = sdt.isDeleted(kv);
92     assertEquals(DeleteResult.FAMILY_VERSION_DELETED, ret);
93     kv = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("f"),
94         qualifier4, timestamp, KeyValue.Type.DeleteFamilyVersion);
95     ret = sdt.isDeleted(kv);
96     assertEquals(DeleteResult.FAMILY_VERSION_DELETED, ret);
97     kv = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("f"),
98         qualifier1, timestamp + 3, KeyValue.Type.DeleteFamilyVersion);
99     ret = sdt.isDeleted(kv);
100     assertEquals(DeleteResult.NOT_DELETED, ret);
101     kv = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("f"),
102         qualifier2, timestamp - 2, KeyValue.Type.DeleteFamilyVersion);
103     ret = sdt.isDeleted(kv);
104     assertEquals(DeleteResult.NOT_DELETED, ret);
105     kv = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("f"),
106         qualifier3, timestamp - 5, KeyValue.Type.DeleteFamilyVersion);
107     ret = sdt.isDeleted(kv);
108     assertEquals(DeleteResult.NOT_DELETED, ret);
109     kv = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("f"),
110         qualifier4, timestamp + 8, KeyValue.Type.DeleteFamilyVersion);
111     ret = sdt.isDeleted(kv);
112     assertEquals(DeleteResult.NOT_DELETED, ret);
113   }
114 
115 
testDelete_DeleteColumn()116   public void testDelete_DeleteColumn() {
117     byte [] qualifier = Bytes.toBytes("qualifier");
118     deleteType = KeyValue.Type.Delete.getCode();
119     KeyValue kv = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("f"),
120         qualifier, timestamp, KeyValue.Type.Delete);
121     sdt.add(kv);
122 
123     timestamp -= 5;
124     kv = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("f"),
125         qualifier, timestamp, KeyValue.Type.DeleteColumn);
126     deleteType = KeyValue.Type.DeleteColumn.getCode();
127     sdt.add(kv);
128 
129     timestamp -= 5;
130     kv = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("f"),
131         qualifier, timestamp, KeyValue.Type.DeleteColumn);
132     DeleteResult ret = sdt.isDeleted(kv);
133     assertEquals(DeleteResult.COLUMN_DELETED, ret);
134   }
135 
136 
testDeleteColumn_Delete()137   public void testDeleteColumn_Delete() {
138     byte [] qualifier = Bytes.toBytes("qualifier");
139     deleteType = KeyValue.Type.DeleteColumn.getCode();
140     KeyValue kv = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("f"),
141         qualifier, timestamp, KeyValue.Type.DeleteColumn);
142     sdt.add(kv);
143 
144     qualifier = Bytes.toBytes("qualifier1");
145     deleteType = KeyValue.Type.Delete.getCode();
146     kv = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("f"),
147         qualifier, timestamp, KeyValue.Type.Delete);
148     sdt.add(kv);
149 
150     DeleteResult ret = sdt.isDeleted(kv);
151     assertEquals( DeleteResult.VERSION_DELETED, ret);
152   }
153 
154   //Testing new way where we save the Delete in case of a Delete for specific
155   //ts, could have just added the last line to the first test, but rather keep
156   //them separated
testDelete_KeepDelete()157   public void testDelete_KeepDelete(){
158     byte [] qualifier = Bytes.toBytes("qualifier");
159     deleteType = KeyValue.Type.Delete.getCode();
160     KeyValue kv = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("f"),
161         qualifier, timestamp, KeyValue.Type.Delete);
162     sdt.add(kv);
163     sdt.isDeleted(kv);
164     assertEquals(false ,sdt.isEmpty());
165   }
166 
testDelete_KeepVersionZero()167   public void testDelete_KeepVersionZero(){
168     byte [] qualifier = Bytes.toBytes("qualifier");
169     deleteType = KeyValue.Type.Delete.getCode();
170 
171     long deleteTimestamp = 10;
172     long valueTimestamp = 0;
173 
174     sdt.reset();
175     KeyValue kv = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("f"),
176         qualifier, deleteTimestamp, KeyValue.Type.Delete);
177     sdt.add(kv);
178     kv = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("f"),
179         qualifier, valueTimestamp, KeyValue.Type.Delete);
180     DeleteResult ret = sdt.isDeleted(kv);
181     assertEquals(DeleteResult.NOT_DELETED, ret);
182   }
183 
184 
185 }
186 
187