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.util;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.HBaseConfiguration;
26  import org.apache.hadoop.hbase.testclassification.MiscTests;
27  import org.apache.hadoop.hbase.testclassification.SmallTests;
28  
29  import org.junit.Test;
30  import org.junit.experimental.categories.Category;
31  
32  @Category({MiscTests.class, SmallTests.class})
33  public class TestLossyCounting {
34  
35    private final Configuration conf = HBaseConfiguration.create();
36  
37    @Test
38    public void testBucketSize() {
39      LossyCounting lossyCounting = new LossyCounting(0.01);
40      assertEquals(100L, lossyCounting.getBucketSize());
41      LossyCounting lossyCounting2 = new LossyCounting(conf);
42      assertEquals(50L, lossyCounting2.getBucketSize());
43    }
44  
45    @Test
46    public void testAddByOne() {
47      LossyCounting lossyCounting = new LossyCounting(0.01);
48      for(int i = 0; i < 100; i++){
49        String key = "" + i;
50        lossyCounting.add(key);
51      }
52      assertEquals(100L, lossyCounting.getDataSize());
53      for (int i = 0; i < 100; i++) {
54        String key = "" + i;
55        assertTrue(lossyCounting.contains(key));
56      }
57    }
58  
59    @Test
60    public void testSweep1() {
61      LossyCounting lossyCounting = new LossyCounting(0.01);
62      for(int i = 0; i < 400; i++){
63        String key = "" + i;
64        lossyCounting.add(key);
65      }
66      assertEquals(4L, lossyCounting.getCurrentTerm());
67      //if total rows added are proportional to bucket size
68      assertEquals(lossyCounting.getBucketSize() - 1, lossyCounting.getDataSize());
69    }
70  
71    @Test
72    public void testSweep2() {
73      LossyCounting lossyCounting = new LossyCounting(0.1);
74      for(int i = 0; i < 10; i++){
75        String key = "" + i;
76        lossyCounting.add(key);
77      }
78      assertEquals(10L, lossyCounting.getDataSize());
79      for(int i = 0; i < 10; i++){
80        String key = "1";
81        lossyCounting.add(key);
82      }
83      assertEquals(1L, lossyCounting.getDataSize());
84    }
85  }