aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/expression/StringResultNode.java
blob: ffd73bf29441d4496ca09b5e7e0b5400517752d7 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// 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.text.Utf8;
import com.yahoo.vespa.objects.Deserializer;
import com.yahoo.vespa.objects.ObjectVisitor;
import com.yahoo.vespa.objects.Serializer;

import java.util.Arrays;

/**
 * This result holds a string.
 *
 * @author baldersheim
 * @author Simon Thoresen Hult
 */
public class StringResultNode extends SingleResultNode {

    // The global class identifier shared with C++.
    public static final int classId = registerClass(0x4000 + 53, StringResultNode.class, StringResultNode::new);
    private static final StringResultNode negativeInfinity = new StringResultNode("");
    private static final PositiveInfinityResultNode positiveInfinity = new PositiveInfinityResultNode();

    private static final byte[] EMPTY_UTF8_ARRAY = new byte[0];

    // The string value of this node, in raw UTF-8 octets.
    private byte[] utf8Value;

    /**
     * Constructs an empty result node. <b>NOTE:</b> This instance is broken until non-optional member data is set.
     */
    public StringResultNode() {
        super();
        utf8Value = EMPTY_UTF8_ARRAY;
    }

    /**
     * Constructs an instance of this class with given value.
     *
     * @param value The value to assign to this.
     */
    public StringResultNode(String value) {
        super();
        setValue(value);
    }

    private StringResultNode(byte[] rawUtf8Value) {
        super();
        utf8Value = rawUtf8Value;
    }

    /**
     * Creates a new StringResultNode backed by an underlying byte array. The input is
     * presumed to be in valid UTF-8 format, but is _not_ checked for validity.
     */
    protected static StringResultNode ofUncheckedUtf8Array(byte[] rawUtf8Value) {
        return new StringResultNode(rawUtf8Value);
    }

    /**
     * Sets the value of this result.
     *
     * @param value The value to set.
     * @return This, to allow chaining.
     */
    public StringResultNode setValue(String value) {
        if (value == null) {
            throw new IllegalArgumentException("Value can not be null.");
        }
        this.utf8Value = Utf8.toBytes(value);
        return this;
    }

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

    @Override
    protected void onSerialize(Serializer buf) {
        byte[] raw = getRaw();
        buf.putInt(null, raw.length);
        buf.put(null, raw);
    }

    @Override
    protected void onDeserialize(Deserializer buf) {
        // We expect the UTF-8 we get from the backend to be pre-checked and valid.
        utf8Value = getRawUtf8Bytes(buf);
    }

    @Override
    public long getInteger() {
        try {
            return Integer.parseInt(getString());
        } catch (java.lang.NumberFormatException e) {
            return 0;
        }
    }

    @Override
    public double getFloat() {
        try {
            return Double.parseDouble(getString());
        } catch (java.lang.NumberFormatException e) {
            return 0;
        }
    }

    @Override
    public String getString() {
        return Utf8.toString(utf8Value);
    }

    @Override
    public byte[] getRaw() {
        return utf8Value;
    }

    @Override
    protected int onCmp(ResultNode rhs) {
        return (rhs instanceof PositiveInfinityResultNode)
               ? -1
               : internalNonPositiveInfinityCompareTo(rhs);
    }

    @Override
    public int hashCode() {
        return super.hashCode() + Arrays.hashCode(utf8Value);
    }

    @Override
    public void visitMembers(ObjectVisitor visitor) {
        super.visitMembers(visitor);
        visitor.visit("value", getString());
    }

    @Override
    public void add(ResultNode rhs) {
        setValue(getString() + rhs.getString());
    }

    @Override
    public void min(ResultNode rhs) {
        if (internalNonPositiveInfinityCompareTo(rhs) > 0) {
            set(rhs);
        }
    }

    @Override
    public void max(ResultNode rhs) {
        if (internalNonPositiveInfinityCompareTo(rhs) < 0) {
            set(rhs);
        }
    }

    public void append(ResultNode rhs) {
        setValue(getString() + rhs.getString());
    }

    @Override
    public Object getValue() {
        return getString();
    }

    @Override
    public void set(ResultNode rhs) {
        if (rhs instanceof StringResultNode) {
            utf8Value = ((StringResultNode) rhs).utf8Value;
        } else {
            setValue(rhs.getString());
        }
    }

    @Override
    public void negate() {
        char[] a = getString().toCharArray();
        for (int i = 0; i < a.length; i++) {
            a[i] = (char)-a[i];
        }
        setValue(new String(a));
    }

    private int internalNonPositiveInfinityCompareTo(ResultNode rhs) {
        // Note: this may not necessarily be well-defined _semantically_ unless rhs is
        // also a StringResultNode. The C++ implementation explicitly expects rhs to be
        // such an instance, but this depends on a classId check that is _not_ done in
        // the Java implementation...
        // We use getString() instead of getRaw() to support implicit stringification
        // (legacy Java implementation behavior), but it's not given that this is always
        // the desired outcome.
        var rhsAsUtf8 = (rhs instanceof StringResultNode)
                        ? ((StringResultNode)rhs).utf8Value
                        : Utf8.toBytes(rhs.getString());
        return Arrays.compareUnsigned(utf8Value, rhsAsUtf8);
    }

    /**
     * Will provide the smallest possible value
     *
     * @return the smallest possible IntegerResultNode
     */
    public static StringResultNode getNegativeInfinity() {
        return negativeInfinity;
    }

    /**
     * Will provide the largest possible value
     *
     * @return the smallest largest IntegerResultNode
     */
    public static PositiveInfinityResultNode getPositiveInfinity() {
        return positiveInfinity;
    }
}