aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/repo/configbuilder.h
blob: 4ef17425c1b1167dc5bc22ffe47a03063806f14e (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/document/base/field.h>
#include <vespa/document/config/config-documenttypes.h>
#include <vespa/document/config/documenttypes_config_fwd.h>
#include <vespa/vespalib/stllike/string.h>
#include <cassert>

namespace document::config_builder {

struct TypeOrId;

struct DatatypeConfig : DocumenttypesConfig::Documenttype::Datatype {
    static int32_t id_counter;
    std::vector<DatatypeConfig> nested_types;

    DatatypeConfig();

    DatatypeConfig(const DatatypeConfig&);
    DatatypeConfig& operator=(const DatatypeConfig&);

    DatatypeConfig &setId(int32_t i) { id = i; return *this; }
    void addNestedType(const TypeOrId &t);
};

int32_t createFieldId(const vespalib::string &name, int32_t type);

struct TypeOrId {
    int32_t id;
    bool has_type;
    DatatypeConfig type;

    TypeOrId(int32_t i) : id(i), has_type(false), type() {}
    TypeOrId(const DatatypeConfig &t) : id(t.id), has_type(true), type(t) {}
};

struct Struct : DatatypeConfig {
    explicit Struct(vespalib::string name) {
        type = Type::STRUCT;
        sstruct.name = std::move(name);
    }
    Struct &setCompression(Sstruct::Compression::Type t, int32_t level,
                           int32_t threshold, int32_t min_size) {
        sstruct.compression.type = t;
        sstruct.compression.level = level;
        sstruct.compression.threshold = threshold;
        sstruct.compression.minsize = min_size;
        return *this;
    }
    Struct &addField(const vespalib::string &name, TypeOrId data_type) {
        addNestedType(data_type);
        sstruct.field.resize(sstruct.field.size() + 1);
        sstruct.field.back().name = name;
        sstruct.field.back().id = createFieldId(name, data_type.id);
        sstruct.field.back().datatype = data_type.id;
        return *this;
    }
    Struct &addTensorField(const vespalib::string &name, const vespalib::string &spec);
    Struct &setId(int32_t i) { DatatypeConfig::setId(i); return *this; }
};

struct Array : DatatypeConfig {
    explicit Array(TypeOrId nested_type) {
        addNestedType(nested_type);
        type = Type::ARRAY;
        array.element.id = nested_type.id;
    }
};

struct Wset : DatatypeConfig {
    explicit Wset(TypeOrId nested_type) {
        addNestedType(nested_type);
        type = Type::WSET;
        wset.key.id = nested_type.id;
    }
    Wset &removeIfZero() { wset.removeifzero = true; return *this; }
    Wset &createIfNonExistent() {
        wset.createifnonexistent = true;
        return *this;
    }
};

struct Map : DatatypeConfig {
    Map(TypeOrId key_type, TypeOrId value_type) {
        addNestedType(key_type);
        addNestedType(value_type);
        type = Type::MAP;
        map.key.id = key_type.id;
        map.value.id = value_type.id;
    }
};

struct AnnotationRef : DatatypeConfig {
    explicit AnnotationRef(int32_t annotation_type_id) {
        type = Type::ANNOTATIONREF;
        annotationref.annotation.id = annotation_type_id;
    }
};

struct DocTypeRep {
    DocumenttypesConfig::Documenttype &doc_type;

    explicit DocTypeRep(DocumenttypesConfig::Documenttype &type)
        : doc_type(type) {}
    DocTypeRep &inherit(int32_t id) {
        doc_type.inherits.resize(doc_type.inherits.size() + 1);
        doc_type.inherits.back().id = id;
        return *this;
    }
    DocTypeRep &annotationType(int32_t id, const vespalib::string &name,
                               int32_t datatype) {
        doc_type.annotationtype.resize(doc_type.annotationtype.size() + 1);
        doc_type.annotationtype.back().id = id;
        doc_type.annotationtype.back().name = name;
        doc_type.annotationtype.back().datatype = datatype;
        return *this;
    }
    DocTypeRep &annotationType(int32_t id, const vespalib::string &name,
                               const DatatypeConfig &type);

    DocTypeRep& referenceType(int32_t id, int32_t target_type_id) {
        doc_type.referencetype.resize(doc_type.referencetype.size() + 1);
        doc_type.referencetype.back().id = id;
        doc_type.referencetype.back().targetTypeId = target_type_id;
        return *this;
    }

    DocTypeRep& imported_field(vespalib::string field_name) {
        doc_type.importedfield.resize(doc_type.importedfield.size() + 1);
        doc_type.importedfield.back().name = std::move(field_name);
        return *this;
    }
};

class DocumenttypesConfigBuilderHelper {
    ::document::config::DocumenttypesConfigBuilder _config;

public:
    DocumenttypesConfigBuilderHelper() {}
    DocumenttypesConfigBuilderHelper(const DocumenttypesConfig &c)
        : _config(c) {}

    DocTypeRep document(int32_t id, const vespalib::string &name,
                        const DatatypeConfig &header,
                        const DatatypeConfig &body);

    ::document::config::DocumenttypesConfigBuilder &config() { return _config; }
};

}