aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/expression/AttributeMapLookupNode.java
blob: 719ebcad6e1cc0c2218401ca8b155fe6442882f7 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.expression;

import com.yahoo.vespa.objects.Deserializer;
import com.yahoo.vespa.objects.ObjectVisitor;
import com.yahoo.vespa.objects.Serializer;

import java.util.Objects;

/**
 * This function is an instruction to do a lookup in a map attribute, returning the value.
 *
 * The key is either specified explicitly or found via a key source attribute.
 * Two underlying attributes are used to represent the map attribute (the key and value attributes).
 *
 * @author geirst
 */
public class AttributeMapLookupNode extends AttributeNode {

    public static final int classId = registerClass(0x4000 + 145, AttributeMapLookupNode.class, AttributeMapLookupNode::new);
    private String keyAttribute = "";
    private String valueAttribute = "";
    private String key = "";
    private String keySourceAttribute = "";

    private AttributeMapLookupNode(String attributeExpression, String keyAttribute, String valueAttribute,
                                   String key, String keySourceAttribute) {
        super(attributeExpression);
        this.keyAttribute = keyAttribute;
        this.valueAttribute = valueAttribute;
        this.key = key;
        this.keySourceAttribute = keySourceAttribute;
    }

    public AttributeMapLookupNode() {
    }

    public static AttributeMapLookupNode fromKey(String attributeExpression, String keyAttribute, String valueAttribute, String key) {
        return new AttributeMapLookupNode(attributeExpression, keyAttribute, valueAttribute, key, "");
    }

    public static AttributeMapLookupNode fromKeySourceAttribute(String attributeExpression, String keyAttribute, String valueAttribute, String keySourceAttribute) {
        return new AttributeMapLookupNode(attributeExpression, keyAttribute, valueAttribute, "", keySourceAttribute);
    }

    @Override
    protected int onGetClassId() {
        return classId;
    }

    @Override
    protected void onSerialize(Serializer buf) {
        super.onSerialize(buf);
        putUtf8(buf, keyAttribute);
        putUtf8(buf, valueAttribute);
        putUtf8(buf, key);
        putUtf8(buf, keySourceAttribute);
    }

    @Override
    protected void onDeserialize(Deserializer buf) {
        super.onDeserialize(buf);
        keyAttribute = getUtf8(buf);
        valueAttribute = getUtf8(buf);
        key = getUtf8(buf);
        keySourceAttribute = getUtf8(buf);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), keyAttribute, valueAttribute, key, keySourceAttribute);
    }

    @Override
    protected boolean equalsFunction(FunctionNode obj) {
        AttributeMapLookupNode that = (AttributeMapLookupNode) obj;
        return super.equalsFunction(obj) &&
                Objects.equals(keyAttribute, that.keyAttribute) &&
                Objects.equals(valueAttribute, that.valueAttribute) &&
                Objects.equals(key, that.key) &&
                Objects.equals(keySourceAttribute, that.keySourceAttribute);
    }

    @Override
    public void visitMembers(ObjectVisitor visitor) {
        super.visitMembers(visitor);
        visitor.visit("keyAttribute", keyAttribute);
        visitor.visit("valueAttribute", valueAttribute);
        visitor.visit("key", key);
        visitor.visit("keySourceAttribute", keySourceAttribute);
    }
}