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

#include "doctype.h"
#include "visitor.h"

#include <vespa/document/update/documentupdate.h>
#include <vespa/document/fieldvalue/document.h>
#include <vespa/document/datatype/documenttype.h>
#include <ostream>

namespace document::select {

namespace {
    bool documentTypeEqualsName(const DocumentType& type,
                                vespalib::stringref name)
    {
        return (type.getName() == name);
    }
}

DocType::DocType(vespalib::stringref doctype)
    : Node("DocType"),
      _doctype(doctype)
{
}

ResultList
DocType::contains(const Context &context) const
{
    if (context._doc != NULL) {
        const Document &doc = *context._doc;
        return
            ResultList(Result::get(
                documentTypeEqualsName(doc.getType(),
                                       _doctype)));
    }
    if (context._docId != NULL) {
        return ResultList(Result::get((context._docId->getDocType() == _doctype)));
    }
    const DocumentUpdate &upd(*context._docUpdate);
    return ResultList(Result::get(
                documentTypeEqualsName(upd.getType(), _doctype)));
}

ResultList
DocType::trace(const Context& context, std::ostream& out) const
{
    ResultList result = contains(context);
    if (context._doc != NULL) {
        const Document &doc = *context._doc;
        out << "DocType - Doc is type " << doc.getType()
            << ", wanted " << _doctype << ", returning "
            << result << ".\n";
    } else if (context._docId != NULL) {
        out << "DocType - Doc is type (document id -- unknown type)"
            << ", wanted " << _doctype << ", returning "
            << result << ".\n";
    } else {
        const DocumentUpdate &update(*context._docUpdate);
        out << "DocType - Doc is type " << update.getType()
            << ", wanted " << _doctype << ", returning "
            << result << ".\n";
    }
    return result;
}


void
DocType::visit(Visitor &v) const
{
    v.visitDocumentType(*this);
}


void
DocType::print(std::ostream& out, bool verbose,
                const std::string& indent) const
{
    (void) verbose; (void) indent;
    if (_parentheses) out << '(';
    out << _doctype;
    if (_parentheses) out << ')';
}

}