1   /*
2    * Copyright 2009 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.regionserver;
22  
23  import org.apache.hadoop.hbase.*;
24  import org.apache.hadoop.hbase.regionserver.DeleteTracker.DeleteResult;
25  import org.apache.hadoop.hbase.util.Bytes;
26  import org.junit.experimental.categories.Category;
27  
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      byte [] qualifier = Bytes.toBytes("qualifier");
43      deleteType = KeyValue.Type.Delete.getCode();
44  
45      sdt.add(qualifier, 0, qualifier.length, timestamp, deleteType);
46      DeleteResult ret = sdt.isDeleted(qualifier, 0, qualifier.length, timestamp);
47      assertEquals(DeleteResult.VERSION_DELETED, ret);
48    }
49  
50    public void testDeletedBy_DeleteColumn() {
51      byte [] qualifier = Bytes.toBytes("qualifier");
52      deleteType = KeyValue.Type.DeleteColumn.getCode();
53  
54      sdt.add(qualifier, 0, qualifier.length, timestamp, deleteType);
55      timestamp -= 5;
56      DeleteResult ret = sdt.isDeleted(qualifier, 0, qualifier.length, timestamp);
57      assertEquals(DeleteResult.COLUMN_DELETED, ret);
58    }
59  
60    public void testDeletedBy_DeleteFamily() {
61      byte [] qualifier = Bytes.toBytes("qualifier");
62      deleteType = KeyValue.Type.DeleteFamily.getCode();
63  
64      sdt.add(qualifier, 0, qualifier.length, timestamp, deleteType);
65  
66      timestamp -= 5;
67      DeleteResult ret = sdt.isDeleted(qualifier, 0, qualifier.length, timestamp);
68      assertEquals(DeleteResult.FAMILY_DELETED, ret);
69    }
70  
71    public void testDelete_DeleteColumn() {
72      byte [] qualifier = Bytes.toBytes("qualifier");
73      deleteType = KeyValue.Type.Delete.getCode();
74  
75      sdt.add(qualifier, 0, qualifier.length, timestamp, deleteType);
76  
77      timestamp -= 5;
78      deleteType = KeyValue.Type.DeleteColumn.getCode();
79      sdt.add(qualifier, 0, qualifier.length, timestamp, deleteType);
80  
81      timestamp -= 5;
82      DeleteResult ret = sdt.isDeleted(qualifier, 0, qualifier.length, timestamp);
83      assertEquals(DeleteResult.COLUMN_DELETED, ret);
84    }
85  
86  
87    public void testDeleteColumn_Delete() {
88      byte [] qualifier = Bytes.toBytes("qualifier");
89      deleteType = KeyValue.Type.DeleteColumn.getCode();
90  
91      sdt.add(qualifier, 0, qualifier.length, timestamp, deleteType);
92  
93      qualifier = Bytes.toBytes("qualifier1");
94      deleteType = KeyValue.Type.Delete.getCode();
95      sdt.add(qualifier, 0, qualifier.length, timestamp, deleteType);
96  
97      DeleteResult ret = sdt.isDeleted(qualifier, 0, qualifier.length, timestamp);
98      assertEquals( DeleteResult.VERSION_DELETED, ret);
99    }
100 
101   //Testing new way where we save the Delete in case of a Delete for specific
102   //ts, could have just added the last line to the first test, but rather keep
103   //them separated
104   public void testDelete_KeepDelete(){
105     byte [] qualifier = Bytes.toBytes("qualifier");
106     deleteType = KeyValue.Type.Delete.getCode();
107 
108     sdt.add(qualifier, 0, qualifier.length, timestamp, deleteType);
109     sdt.isDeleted(qualifier, 0, qualifier.length, timestamp);
110     assertEquals(false ,sdt.isEmpty());
111   }
112 
113   public void testDelete_KeepVersionZero(){
114     byte [] qualifier = Bytes.toBytes("qualifier");
115     deleteType = KeyValue.Type.Delete.getCode();
116 
117     long deleteTimestamp = 10;
118     long valueTimestamp = 0;
119 
120     sdt.reset();
121     sdt.add(qualifier, 0, qualifier.length, deleteTimestamp, deleteType);
122     DeleteResult ret = sdt.isDeleted(qualifier, 0, qualifier.length, valueTimestamp);
123     assertEquals(DeleteResult.NOT_DELETED, ret);
124   }
125 
126 
127   @org.junit.Rule
128   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
129     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
130 }
131