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