aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/queryeval/iterator_benchmark/intermediate_blueprint_factory.cpp
blob: 8591ec1415d24db67205ff8ae1e97be44ee4f9b0 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "intermediate_blueprint_factory.h"
#include <vespa/searchlib/queryeval/intermediate_blueprints.h>
#include <iomanip>
#include <sstream>

namespace search::queryeval::test {

template <typename BlueprintType>
char
IntermediateBlueprintFactory<BlueprintType>::child_name(void* blueprint) const
{
    auto itr = _child_names.find(blueprint);
    if (itr != _child_names.end()) {
        return itr->second;
    }
    return '?';
}

template <typename BlueprintType>
IntermediateBlueprintFactory<BlueprintType>::IntermediateBlueprintFactory(vespalib::stringref name)
    : _name(name),
      _children(),
      _child_names()
{
}

template <typename BlueprintType>
IntermediateBlueprintFactory<BlueprintType>::~IntermediateBlueprintFactory() = default;

template <typename BlueprintType>
std::unique_ptr<Blueprint>
IntermediateBlueprintFactory<BlueprintType>::make_blueprint()
{
    auto res = std::make_unique<BlueprintType>();
    _child_names.clear();
    char name = 'A';
    for (const auto& factory : _children) {
        auto child = factory->make_blueprint();
        _child_names[child.get()] = name++;
        res->addChild(std::move(child));
    }
    return res;
}

template <typename BlueprintType>
vespalib::string
IntermediateBlueprintFactory<BlueprintType>::get_name(Blueprint& blueprint) const
{
    auto* intermediate = blueprint.asIntermediate();
    if (intermediate != nullptr) {
        std::ostringstream oss;
        bool first = true;
        oss << _name << "[";
        for (size_t i = 0; i < intermediate->childCnt(); ++i) {
            auto* child = &intermediate->getChild(i);
            oss << (first ? "" : ",") << child_name(child) << ".";
            if (child->strict()) {
                oss << "s(" << std::setw(6) << std::setprecision(3) << child->strict_cost() << ")";
            } else {
                oss << "n(" << std::setw(6) << std::setprecision(3) << child->cost() << ")";
            }
            first = false;
        }
        oss << "]";
        return oss.str();
    }
    return get_class_name(blueprint);
}

template class IntermediateBlueprintFactory<AndBlueprint>;

AndBlueprintFactory::AndBlueprintFactory()
    : IntermediateBlueprintFactory<AndBlueprint>("AND")
{}

}