aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/bucket/bucketselector.cpp
blob: b9dc6a9cfa97875cb9831014fe01b8245e196f9b (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "bucketselector.h"
#include "bucketidfactory.h"
#include <vespa/document/base/documentid.h>
#include <vespa/document/select/node.h>
#include <vespa/document/select/valuenodes.h>
#include <vespa/document/select/visitor.h>
#include <vespa/document/select/branch.h>
#include <vespa/document/select/compare.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <algorithm>

namespace document {

using namespace document::select;

//namespace {
    /**
     * Visitor class that is used for visiting a node tree generated by a
     * document selection expression.
     *
     * The visitor class contains the set of buckets expression can match.
     */
    struct BucketVisitor : public document::select::Visitor {
        const BucketIdFactory& _factory;
        std::vector<BucketId> _buckets;
            // If set to false, the buckets to visit is set in _buckets.
        bool _unknown;

        BucketVisitor(const BucketIdFactory& factory)
            : _factory(factory), _buckets(), _unknown(true) {}

        ~BucketVisitor() override;

        void visitAndBranch(const document::select::And& node) override {
            BucketVisitor left(_factory);
            node.getLeft().visit(left);
            node.getRight().visit(*this);
                // If either part is unknown we can just return other part.
            if (left._unknown) {
                return;
            }
                // If only left part is known return that part.
            if (_unknown) {
                _buckets.swap(left._buckets);
                _unknown = false;
                return;
            }
            std::vector<BucketId> result;
            std::set_intersection(left._buckets.begin(), left._buckets.end(),
                                  _buckets.begin(), _buckets.end(),
                                  back_inserter(result));
            _buckets.swap(result);
        }

        void visitOrBranch(const document::select::Or& node) override {
            BucketVisitor left(_factory);
            node.getLeft().visit(left);
            node.getRight().visit(*this);
                // If one part is unknown we have to keep unknown status
            if (left._unknown || _unknown) {
                _unknown = true;
                return;
            }
                // If both parts are known return both sets
            std::vector<BucketId> result;
            std::set_union(left._buckets.begin(), left._buckets.end(),
                           _buckets.begin(), _buckets.end(),
                           back_inserter(result));
            _buckets.swap(result);
        }

        void visitNotBranch(const document::select::Not&) override {
            // Since selected locations doesn't include everything at that
            // location, we can't reverse the selection. Any NOT branch must
            // end up specifying all
        }

        void compare(const select::IdValueNode& node,
                     const select::ValueNode& valnode,
                     const select::Operator& op)
        {
            if (node.getType() == IdValueNode::ALL) {
                auto val = dynamic_cast<const StringValueNode*>(&valnode);
                if (!val) return;
                vespalib::string docId(val->getValue());
                if (op == FunctionOperator::EQ || !GlobOperator::containsVariables(docId)) {
                    _buckets.emplace_back(58, IdString(docId).getLocation());
                    _unknown = false;
                }
            } else if (node.getType() == IdValueNode::USER) {
                auto val = dynamic_cast<const IntegerValueNode*>(&valnode);
                if (!val) return;
                IdString id(vespalib::make_string("id::test:n=%" PRIu64 ":", val->getValue()));
                _buckets.emplace_back(32, id.getNumber());
                _unknown = false;
            } else if (node.getType() == IdValueNode::GROUP) {
                auto val = dynamic_cast<const StringValueNode*>(&valnode);
                if (!val) return;
                vespalib::string group(val->getValue());
                if (op == FunctionOperator::EQ || !GlobOperator::containsVariables(group)) {
                    _buckets.emplace_back(32, IdString::makeLocation(group));
                    _unknown = false;
                }
            } else if (node.getType() == IdValueNode::GID) {
                auto val = dynamic_cast<const StringValueNode*>(&valnode);

                vespalib::string gid(val->getValue());
                if (op == FunctionOperator::EQ || !GlobOperator::containsVariables(gid)) {
                    BucketId bid = document::GlobalId::parse(gid).convertToBucketId();
                    _buckets.emplace_back(32, bid.getRawId());
                    _unknown = false;
                }
            } else if (node.getType() == IdValueNode::BUCKET) {
                auto val = dynamic_cast<const IntegerValueNode*>(&valnode);
                if (!val) return;

                BucketId bid(val->getValue());
                if (!bid.getUsedBits()) {
                    bid.setUsedBits(32);
                }
                _buckets.push_back(bid);
                _unknown = false;
            }
        }

        void visitComparison(const document::select::Compare& node) override {
            if (node.getOperator() != document::select::FunctionOperator::EQ &&
                node.getOperator() != document::select::GlobOperator::GLOB)
            {
                return;
            }
            auto lid = dynamic_cast<const IdValueNode*>(&node.getLeft());
            if (lid) {
                compare(*lid, node.getRight(), node.getOperator());
            } else {
                auto rid = dynamic_cast<const IdValueNode*>(&node.getRight());
                if (rid) {
                    compare(*rid, node.getLeft(), node.getOperator());
                }
            }
        }

        void visitConstant(const document::select::Constant&) override {}
        void visitInvalidConstant(const document::select::InvalidConstant &) override {}
        void visitDocumentType(const document::select::DocType&) override {}
        void visitArithmeticValueNode(const ArithmeticValueNode &) override {}
        void visitFunctionValueNode(const FunctionValueNode &) override {}
        void visitIdValueNode(const IdValueNode &) override {}
        void visitFieldValueNode(const FieldValueNode &) override {}
        void visitFloatValueNode(const FloatValueNode &) override {}
        void visitVariableValueNode(const VariableValueNode &) override {}
        void visitIntegerValueNode(const IntegerValueNode &) override {}
        void visitBoolValueNode(const BoolValueNode &) override {}
        void visitCurrentTimeValueNode(const CurrentTimeValueNode &) override {}
        void visitStringValueNode(const StringValueNode &) override {}
        void visitNullValueNode(const NullValueNode &) override {}
        void visitInvalidValueNode(const InvalidValueNode &) override {}
    };

BucketVisitor::~BucketVisitor() = default;

//}

BucketSelector::BucketSelector(const document::BucketIdFactory& factory)
    : _factory(factory)
{
}

std::unique_ptr<BucketSelector::BucketVector>
BucketSelector::select(const document::select::Node& expression) const
{
    BucketVisitor v(_factory);
    expression.visit(v);
    return std::unique_ptr<BucketVector>(v._unknown
            ? 0 : new BucketVector(v._buckets));
}

} // document