aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/query/test/PredicateQueryItemTestCase.java
blob: a27a11b8f1538f441d270cc71b2a77ae4d042729 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.query.test;

import com.yahoo.prelude.query.Item;
import com.yahoo.prelude.query.PredicateQueryItem;
import org.junit.jupiter.api.Test;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Iterator;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * @author Magnar Nedland
 */
public class PredicateQueryItemTestCase {

    @Test
    void requireThatItemConstantsAreSet() {
        PredicateQueryItem item = new PredicateQueryItem();
        assertEquals(Item.ItemType.PREDICATE_QUERY, item.getItemType());
        assertEquals("PREDICATE_QUERY_ITEM", item.getName());
        assertEquals(1, item.getTermCount());
        assertEquals("predicate", item.getIndexName());
        item.setIndexName("foobar");
        assertEquals("foobar", item.getIndexName());
    }

    @Test
    void requireThatFeaturesCanBeAdded() {
        PredicateQueryItem item = new PredicateQueryItem();
        assertEquals(0, item.getFeatures().size());
        item.addFeature("foo", "bar");
        item.addFeature("foo", "baz", 0xffff);
        item.addFeature(new PredicateQueryItem.Entry("qux", "quux"));
        item.addFeature(new PredicateQueryItem.Entry("corge", "grault", 0xf00ba));
        assertEquals(4, item.getFeatures().size());
        Iterator<PredicateQueryItem.Entry> it = item.getFeatures().iterator();
        assertEquals(-1, it.next().getSubQueryBitmap());
        assertEquals(0xffffL, it.next().getSubQueryBitmap());
        assertEquals(-1, it.next().getSubQueryBitmap());
        assertEquals(0xf00baL, it.next().getSubQueryBitmap());
    }

    @Test
    void requireThatRangeFeaturesCanBeAdded() {
        PredicateQueryItem item = new PredicateQueryItem();
        assertEquals(0, item.getRangeFeatures().size());
        item.addRangeFeature("foo", 23);
        item.addRangeFeature("foo", 34, 0x12345678L);
        item.addRangeFeature(new PredicateQueryItem.RangeEntry("qux", 43));
        item.addRangeFeature(new PredicateQueryItem.RangeEntry("corge", 54, 0xf00ba));
        assertEquals(4, item.getRangeFeatures().size());
        Iterator<PredicateQueryItem.RangeEntry> it = item.getRangeFeatures().iterator();
        assertEquals(-1, it.next().getSubQueryBitmap());
        assertEquals(0x12345678L, it.next().getSubQueryBitmap());
        assertEquals(-1, it.next().getSubQueryBitmap());
        assertEquals(0xf00baL, it.next().getSubQueryBitmap());
    }

    @Test
    void requireThatToStringWorks() {
        PredicateQueryItem item = new PredicateQueryItem();
        assertEquals("PREDICATE_QUERY_ITEM ", item.toString());
        item.addFeature("foo", "bar");
        item.addFeature("foo", "baz", 0xffffL);
        assertEquals("PREDICATE_QUERY_ITEM foo=bar, foo=baz[0xffff]", item.toString());
        item.addRangeFeature("foo", 23);
        item.addRangeFeature("foo", 34, 0xfffffffffffffffeL);
        assertEquals("PREDICATE_QUERY_ITEM foo=bar, foo=baz[0xffff], foo:23, foo:34[0xfffffffffffffffe]", item.toString());
    }

    @Test
    void requireThatPredicateQueryItemCanBeEncoded() {
        PredicateQueryItem item = new PredicateQueryItem();
        assertEquals("PREDICATE_QUERY_ITEM ", item.toString());
        item.addFeature("foo", "bar");
        item.addFeature("foo", "baz", 0xffffL);
        ByteBuffer buffer = ByteBuffer.allocate(1000);
        item.encode(buffer);
        buffer.flip();
        byte[] actual = new byte[buffer.remaining()];
        buffer.get(actual);
        assertArrayEquals(new byte[]{
                        23,  // PREDICATE_QUERY code 23
                        9, 'p', 'r', 'e', 'd', 'i', 'c', 'a', 't', 'e',
                        2,  // 2 features
                        3, 'f', 'o', 'o', 3, 'b', 'a', 'r', -1, -1, -1, -1, -1, -1, -1, -1,  // key, value, subquery
                        3, 'f', 'o', 'o', 3, 'b', 'a', 'z', 0, 0, 0, 0, 0, 0, -1, -1,  // key, value, subquery
                        0},  // no range features
                actual);

        item.addRangeFeature("foo", 23);
        item.addRangeFeature("foo", 34, 0xfffffffffffffffeL);
        buffer.clear();
        item.encode(buffer);
        buffer.flip();
        actual = new byte[buffer.remaining()];
        buffer.get(actual);
        assertArrayEquals(new byte[]{
                        23,  // PREDICATE_QUERY code 23
                        9, 'p', 'r', 'e', 'd', 'i', 'c', 'a', 't', 'e',
                        2,  // 2 features
                        3, 'f', 'o', 'o',  3, 'b', 'a', 'r',  -1, -1, -1, -1, -1, -1, -1, -1,  // key, value, subquery
                        3, 'f', 'o', 'o',  3, 'b', 'a', 'z',  0, 0, 0, 0, 0, 0, -1, -1,  // key, value, subquery
                        2,  // 2 range features
                        3, 'f', 'o', 'o',  0, 0, 0, 0, 0, 0, 0, 23,  -1, -1, -1, -1, -1, -1, -1, -1,  // key, value, subquery
                        3, 'f', 'o', 'o',  0, 0, 0, 0, 0, 0, 0, 34,  -1, -1, -1, -1, -1, -1, -1, -2},  // key, value, subquery
                actual);
    }

    @Test
    void requireThatPredicateQueryItemWithManyAttributesCanBeEncoded() {
        PredicateQueryItem item = new PredicateQueryItem();
        assertEquals("PREDICATE_QUERY_ITEM ", item.toString());
        for (int i = 0; i < 200; ++i) {
            item.addFeature("foo", "bar");
        }
        ByteBuffer buffer = ByteBuffer.allocate(10000);
        item.encode(buffer);
        buffer.flip();
        byte[] actual = new byte[buffer.remaining()];
        buffer.get(actual);
        byte [] expectedPrefix = new byte[]{
                23,  // PREDICATE_QUERY code 23
                9, 'p', 'r', 'e', 'd', 'i', 'c', 'a', 't', 'e',
                (byte) 0x80, (byte) 0xc8,  // 200 features (0x80c8 => 0xc8 == 200)
                3, 'f', 'o', 'o',  3, 'b', 'a', 'r',  -1, -1, -1, -1, -1, -1, -1, -1,  // key, value, subquery
                3, 'f', 'o', 'o',  3, 'b', 'a', 'r',  -1, -1, -1, -1, -1, -1, -1, -1,  // key, value, subquery
                3, 'f', 'o', 'o',  3, 'b', 'a', 'r',  -1, -1, -1, -1, -1, -1, -1, -1,  // key, value, subquery
        };  // ...
        assertArrayEquals(expectedPrefix, Arrays.copyOfRange(actual, 0, expectedPrefix.length));

    }

}