aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/test/java/com/yahoo/searchlib/aggregation/hll/SketchMergerTest.java
blob: 3fbced70969ec91c35718d3ccd7101a13fc03cf7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.aggregation.hll;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class SketchMergerTest {

    private final SketchMerger merger = new SketchMerger();

    @Test
    public void requireThatMergingTwoSmallSparseSketchesReturnsSparseSketch() {
        SparseSketch s1 = SketchUtils.createSparseSketch(1);
        SparseSketch s2 = SketchUtils.createSparseSketch(2);

        Sketch<?> result = merger.merge(s1, s2);
        assertEquals(result.getClass(), SparseSketch.class);
        assertTrue("Should return the instance given by first argument.", result == s1);
        SketchUtils.assertSketchContains(result, 1, 2);
    }

    @Test
    public void requireThatMergingTwoThresholdSizeSparseSketchesReturnsNormalSketch() {
        SparseSketch s1 = SketchUtils.createSparseSketch();
        SparseSketch s2 = SketchUtils.createSparseSketch();

        // Fill sketches with disjoint data.
        for (int i = 0; i < HyperLogLog.SPARSE_SKETCH_CONVERSION_THRESHOLD; i++) {
            s1.aggregate(i);
            s2.aggregate(i + HyperLogLog.SPARSE_SKETCH_CONVERSION_THRESHOLD);
        }

        Sketch<?> result = merger.merge(s1, s2);
        assertEquals(result.getClass(), NormalSketch.class);

        List<Integer> unionOfSketchData = new ArrayList<>();
        unionOfSketchData.addAll(s1.data());
        unionOfSketchData.addAll(s2.data());
        Integer[] expectedValues = unionOfSketchData.toArray(new Integer[unionOfSketchData.size()]);
        SketchUtils.assertSketchContains(result, expectedValues);
    }

    @Test
    public void requireThatMergingTwoNormalSketchesReturnsNormalSketch() {
        NormalSketch s1 = SketchUtils.createNormalSketch(1);
        NormalSketch s2 = SketchUtils.createNormalSketch(2);

        Sketch<?> result = merger.merge(s1, s2);
        assertEquals(result.getClass(), NormalSketch.class);
        assertTrue("Should return the instance given by first argument.", result == s1);
        SketchUtils.assertSketchContains(result, 1, 2);
    }

    @Test
    public void requireThatMergingNormalAndSparseSketchReturnsNormalSketch() {
        SparseSketch s1 = SketchUtils.createSparseSketch(1);
        NormalSketch s2 = SketchUtils.createNormalSketch(2);

        Sketch<?> result = merger.merge(s1, s2);
        assertEquals(result.getClass(), NormalSketch.class);
        assertTrue("Should return the NormalSketch instance given by the arguments.", result == s2);
        SketchUtils.assertSketchContains(result, 1, 2);
    }
}