aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/select/value.h
blob: 5d94af128330a28e991a97088e1a92badfe2be00 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

/**
 * @class document::select::Value
 * @ingroup select
 *
 * @brief Values are entities that can be compared.
 *
 * To be able to cope with field specifications that can end up in values of
 * multiple types we need an abstraction.
 *
 * @author H�kon Humberset
 * @date 2007-20-05
 * @version $Id$
 */

#pragma once

#include <memory>
#include <map>
#include <string>
#include <vector>
#include <iosfwd>
#include "resultlist.h"

namespace document::select {

class Value : public document::Printable
{
public:
    using SP = std::shared_ptr<Value>;
    using UP = std::unique_ptr<Value>;
    enum Type { Invalid, Null, String, Integer, Float, Array, Struct, Bucket };

    Value(Type t) : _type(t) {}
    virtual ~Value() = default;

    Type getType() const { return _type; }

    virtual ResultList operator<(const Value& value) const = 0;
    virtual ResultList operator==(const Value& value) const = 0;

    virtual ResultList operator!=(const Value& value) const {
        return !(this->operator==(value));
    }
    virtual ResultList operator>(const Value& value) const {
        return (!(this->operator<(value)) && !(this->operator==(value)));
    }
    virtual ResultList operator>=(const Value& value) const {
        return !(this->operator<(value));
    }
    virtual ResultList operator<=(const Value& value) const {
        return ((this->operator<(value)) || (this->operator==(value)));
    }

    virtual ResultList globCompare(const Value& value) const;
    virtual ResultList regexCompare(const Value& value) const;
    virtual ResultList globTrace(const Value& value, std::ostream& trace) const;
    virtual ResultList regexTrace(const Value& value, std::ostream& trace) const;
private:
    Type _type;
};

class InvalidValue : public Value
{
public:
    InvalidValue() : Value(Invalid) {}

    ResultList operator<(const Value&) const override;
    ResultList operator==(const Value&) const override;
    void print(std::ostream& out, bool verbose, const std::string& indent) const override;
};

class NullValue : public Value
{
public:
    NullValue() : Value(Null) {}

    ResultList operator<(const Value&) const override;
    ResultList operator==(const Value&) const override;
    ResultList operator>(const Value &) const override;
    ResultList operator>=(const Value &) const override;
    ResultList operator<=(const Value &) const override;
    void print(std::ostream& out, bool verbose, const std::string& indent) const override;
};

class StringValue : public Value
{
    vespalib::string _value;

public:
    StringValue(vespalib::stringref val);

    const vespalib::string& getValue() const { return _value; }
    ResultList operator<(const Value& value) const override;
    ResultList operator==(const Value& value) const override;
    void print(std::ostream& out, bool verbose, const std::string& indent) const override;
};

class IntegerValue;
class FloatValue;

class NumberValue : public Value
{
public:
    using CommonValueType = double;

    NumberValue(Type t) : Value(t) {}

    virtual CommonValueType getCommonValue() const = 0;

    virtual ResultList operator<(const Value& value) const override = 0;
    virtual ResultList operator>(const IntegerValue& value) const = 0;
    virtual ResultList operator>(const FloatValue& value) const = 0;
    virtual ResultList operator==(const Value& value) const override = 0;
    virtual ResultList operator==(const IntegerValue& value) const = 0;
    virtual ResultList operator==(const FloatValue& value) const = 0;
};

class IntegerValue : public NumberValue
{
public:
    using ValueType = int64_t;

    IntegerValue(ValueType value, bool isBucketValue);

    ValueType getValue() const { return _value; }
    CommonValueType getCommonValue() const override { return _value; }

    ResultList operator<(const Value& value) const override;
    ResultList operator==(const Value& value) const override;

    ResultList operator>(const IntegerValue& value) const override;
    ResultList operator>(const FloatValue& value) const override;
    ResultList operator==(const IntegerValue& value) const override;
    ResultList operator==(const FloatValue& value) const override;
    void print(std::ostream& out, bool verbose, const std::string& indent) const override;
private:
    ValueType _value;
};

class FloatValue : public NumberValue
{
public:
    using ValueType = double;

    FloatValue(ValueType val);

    ValueType getValue() const { return _value; }
    CommonValueType getCommonValue() const override { return _value; }

    ResultList operator<(const Value& value) const override;
    ResultList operator==(const Value& value) const override;

    ResultList operator>(const IntegerValue& value) const override;
    ResultList operator>(const FloatValue& value) const override;
    ResultList operator==(const IntegerValue& value) const override;
    ResultList operator==(const FloatValue& value) const override;
    void print(std::ostream& out, bool verbose, const std::string& indent) const override;
private:
    ValueType _value;
};

inline ResultList IntegerValue::operator>(const IntegerValue& value) const {
    return ResultList(Result::get(_value > value.getValue()));
}
inline ResultList IntegerValue::operator>(const FloatValue& value) const {
    return ResultList(Result::get(_value > value.getValue()));
}
inline ResultList IntegerValue::operator==(const IntegerValue& value) const {
    return ResultList(Result::get(_value == value.getValue()));
}
inline ResultList IntegerValue::operator==(const FloatValue& value) const {
    return ResultList(Result::get(_value == value.getValue()));
}

inline ResultList FloatValue::operator>(const IntegerValue& value) const {
    return ResultList(Result::get(_value > value.getValue()));
}
inline ResultList FloatValue::operator>(const FloatValue& value) const {
    return ResultList(Result::get(_value > value.getValue()));
}
inline ResultList FloatValue::operator==(const IntegerValue& value) const {
    return ResultList(Result::get(_value == value.getValue()));
}
inline ResultList FloatValue::operator==(const FloatValue& value) const {
    return ResultList(Result::get(_value == value.getValue()));
}

class ArrayValue : public Value
{
public:
    using VariableValue = std::pair<fieldvalue::VariableMap, Value::SP>;

    ArrayValue(std::vector<VariableValue> values);
    ArrayValue(const ArrayValue &) = delete;
    ArrayValue & operator =(const ArrayValue &) = delete;
    ~ArrayValue() override;

    ResultList operator<(const Value& value) const override;
    ResultList operator>(const Value& value) const override;
    ResultList operator==(const Value& value) const override;
    ResultList operator!=(const Value& value) const override;
    ResultList operator>=(const Value& value) const override;
    ResultList operator<=(const Value& value) const override;
    void print(std::ostream& out, bool verbose, const std::string& indent) const override;

    ResultList globCompare(const Value& value) const override;
    ResultList regexCompare(const Value& value) const override;
    ResultList globTrace(const Value& value, std::ostream& trace) const override;
    ResultList regexTrace(const Value& value, std::ostream& trace) const override;

    template <typename Predicate>
    ResultList doCompare(const Value& value, const Predicate& cmp) const;
private:
    struct EqualsComparator;
    struct NotEqualsComparator;
    struct LessThanComparator;
    struct GreaterThanComparator;
    struct LessThanOrEqualsComparator;
    struct GreaterThanOrEqualsComparator;
    struct GlobComparator;
    struct RegexComparator;

    std::vector<VariableValue> _values;
};

class StructValue : public Value
{
public:
    using ValueMap = std::map<vespalib::string, Value::SP>;
    StructValue(ValueMap values);
    StructValue(const StructValue &) = delete;
    StructValue & operator = (const StructValue &) = delete;
    ~StructValue() override;

    ResultList operator<(const Value& value) const override;
    ResultList operator==(const Value& value) const override;
    void print(std::ostream& out, bool verbose, const std::string& indent) const override;
private:
    ValueMap _values;
};

}