aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/test/doc_builder.cpp
blob: 3950a28bea78cb3bee68983c22c079a3a679b828 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "doc_builder.h"
#include <vespa/document/datatype/documenttype.h>
#include <vespa/document/fieldvalue/arrayfieldvalue.h>
#include <vespa/document/fieldvalue/document.h>
#include <vespa/document/fieldvalue/mapfieldvalue.h>
#include <vespa/document/fieldvalue/structfieldvalue.h>
#include <vespa/document/fieldvalue/weightedsetfieldvalue.h>
#include <vespa/document/repo/documenttyperepo.h>
#include <vespa/document/repo/document_type_repo_factory.h>
#include <vespa/document/repo/configbuilder.h>
#include <cassert>

using document::ArrayFieldValue;
using document::DataType;
using document::Document;
using document::DocumentId;
using document::DocumentTypeRepo;
using document::DocumentTypeRepoFactory;
using document::MapFieldValue;
using document::StructFieldValue;
using document::WeightedSetFieldValue;

namespace search::test {

namespace {

DocumenttypesConfig
get_document_types_config(DocBuilder::AddFieldsType add_fields)
{
    using namespace document::config_builder;
    DocumenttypesConfigBuilderHelper builder;
    Struct header("searchdocument.header");
    add_fields(header);
    builder.document(42, "searchdocument",
                     header,
                     Struct("searchdocument.body"));
    return builder.config();
}

}

DocBuilder::DocBuilder()
    : DocBuilder([](auto&) noexcept {})
{
}

DocBuilder::DocBuilder(AddFieldsType add_fields)
    : _document_types_config(std::make_shared<const DocumenttypesConfig>(get_document_types_config(add_fields))),
      _repo(DocumentTypeRepoFactory::make(*_document_types_config)),
      _document_type(_repo->getDocumentType("searchdocument"))
{
}

DocBuilder::~DocBuilder() = default;


std::unique_ptr<Document>
DocBuilder::make_document(vespalib::string document_id)
{
    auto doc = std::make_unique<Document>(get_repo(), get_document_type(), DocumentId(document_id));
    return doc;
}

const DataType&
DocBuilder::get_data_type(const vespalib::string &name) const
{
    const DataType *type = _repo->getDataType(*_document_type, name);
    assert(type);
    return *type;
}

ArrayFieldValue
DocBuilder::make_array(vespalib::stringref field_name)
{
    auto& field = _document_type->getField(field_name);
    auto& field_type = field.getDataType();
    assert(field_type.isArray());
    return {field_type};
}

WeightedSetFieldValue
DocBuilder::make_wset(vespalib::stringref field_name)
{
    auto& field = _document_type->getField(field_name);
    auto& field_type = field.getDataType();
    assert(field_type.isWeightedSet());
    return {field_type};
}

}