aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchcommon/common/schema.h
blob: a2eb1dacd650593b82f34f23ee7909cd5bc6b9ad (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "datatype.h"
#include <vespa/config/common/types.h>
#include <vespa/vespalib/stllike/hash_map.h>

namespace vespalib { class asciistream; }
namespace search::index {

/**
 * Schema class used to give a high-level description of the content
 * of an index.
 **/
class Schema
{
public:
    using UP = std::unique_ptr<Schema>;
    using SP = std::shared_ptr<Schema>;

    using DataType = schema::DataType;
    using CollectionType = schema::CollectionType;

    /**
     * A single field has a name, data type and collection
     * type. Various aspects (index/attribute) may have
     * limitations on what types are supported in the back-end.
     **/
    class Field
    {
        vespalib::string  _name;
        DataType          _dataType;
        CollectionType    _collectionType;
        vespalib::string  _tensor_spec;

    public:
        Field(vespalib::stringref n, DataType dt) noexcept;
        Field(vespalib::stringref n, DataType dt, CollectionType ct) noexcept;
        Field(vespalib::stringref n, DataType dt, CollectionType ct, vespalib::stringref tensor_spec) noexcept;

        /**
         * Create this field based on the given config lines.
         **/
        Field(const config::StringVector & lines);
        Field(const Field &) noexcept;
        Field & operator = (const Field &) noexcept;
        Field(Field &&) noexcept;
        Field & operator = (Field &&) noexcept;

        virtual ~Field();

        virtual void write(vespalib::asciistream & os, vespalib::stringref prefix) const;

        const vespalib::string &getName() const noexcept { return _name; }
        DataType getDataType() const noexcept { return _dataType; }
        CollectionType getCollectionType() const noexcept { return _collectionType; }
        const vespalib::string& get_tensor_spec() const noexcept { return _tensor_spec; }

        bool matchingTypes(const Field &rhs) const noexcept {
            return getDataType() == rhs.getDataType() &&
             getCollectionType() == rhs.getCollectionType();
        }

        bool operator==(const Field &rhs) const noexcept;
        bool operator!=(const Field &rhs) const noexcept;
    };


    /**
     * A representation of an index field with extra information on
     * how the index should be generated.
     **/
    class IndexField : public Field {
    private:
        uint32_t _avgElemLen;
        // TODO: Remove when posting list format with interleaved features is made default
        bool _interleaved_features;

    public:
        IndexField(vespalib::stringref name, DataType dt) noexcept;
        IndexField(vespalib::stringref name, DataType dt, CollectionType ct) noexcept;
        IndexField(const IndexField &) noexcept;
        IndexField & operator = (const IndexField &) noexcept;
        IndexField(IndexField &&) noexcept;
        IndexField & operator = (IndexField &&) noexcept;
        /**
         * Create this index field based on the given config lines.
         **/
        explicit IndexField(const config::StringVector &lines);

        IndexField &setAvgElemLen(uint32_t avgElemLen) noexcept { _avgElemLen = avgElemLen; return *this; }
        IndexField &set_interleaved_features(bool value) noexcept {
            _interleaved_features = value;
            return *this;
        }

        void write(vespalib::asciistream &os,
                   vespalib::stringref prefix) const override;

        uint32_t getAvgElemLen() const noexcept { return _avgElemLen; }
        bool use_interleaved_features() const noexcept { return _interleaved_features; }

        bool operator==(const IndexField &rhs) const noexcept;
        bool operator!=(const IndexField &rhs) const noexcept;
    };

    using AttributeField = Field;
    using ImportedAttributeField = Field;

    /**
     * A field collection has a name and a list of index field names,
     * and is a named physical view over the list of index fields.
     **/
    class FieldSet
    {
        vespalib::string _name;
        std::vector<vespalib::string> _fields;

    public:
        explicit FieldSet(vespalib::stringref n) noexcept : _name(n), _fields() {}
        FieldSet(const FieldSet &);
        FieldSet & operator =(const FieldSet &);
        FieldSet(FieldSet &&) noexcept = default;
        FieldSet & operator =(FieldSet &&) noexcept = default;

        /**
         * Create this field collection based on the given config lines.
         **/
        explicit FieldSet(const config::StringVector & lines);

        ~FieldSet();

        FieldSet &addField(vespalib::stringref fieldName) {
            _fields.emplace_back(fieldName);
            return *this;
        }

        const vespalib::string &getName() const noexcept { return _name; }
        const std::vector<vespalib::string> &getFields() const noexcept { return _fields; }

        bool operator==(const FieldSet &rhs) const noexcept;
        bool operator!=(const FieldSet &rhs) const noexcept;
    };

    static const uint32_t UNKNOWN_FIELD_ID;

private:
    std::vector<IndexField>      _indexFields;
    std::vector<AttributeField>  _attributeFields;
    std::vector<FieldSet> _fieldSets;
    std::vector<ImportedAttributeField> _importedAttributeFields;
    using Name2IdMap = vespalib::hash_map<vespalib::string, uint32_t>;
    Name2IdMap _indexIds;
    Name2IdMap _attributeIds;
    Name2IdMap _fieldSetIds;
    Name2IdMap _importedAttributeIds;

    void writeToStream(vespalib::asciistream &os, bool saveToDisk) const;

public:
    /**
     * Create an initially empty schema
     **/
    Schema();
    Schema(const Schema & rhs);
    Schema & operator=(const Schema & rhs);
    Schema(Schema && rhs) noexcept;
    Schema & operator=(Schema && rhs) noexcept;
    ~Schema();

    /**
     * Load this schema from the file with the given name.
     *
     * @param fileName the name of the file.
     * @return true if the schema could be loaded.
     **/
    bool loadFromFile(const vespalib::string & fileName);

    /**
     * Save this schema to the file with the given name.
     *
     * @param fileName the name of the file.
     * @return true if the schema could be saved.
     **/
    bool saveToFile(const vespalib::string & fileName) const;

    vespalib::string toString() const;

    /**
     * Add an index field to this schema
     *
     * @param field the field to add
     **/
    Schema & addIndexField(const IndexField &field);

    // Only used by tests.
    Schema & addUriIndexFields(const IndexField &field);

    /**
     * Add an attribute field to this schema
     *
     * @param field the field to add
     **/
    Schema & addAttributeField(const AttributeField &field);

    /**
     * Add a field set to this schema.
     *
     * @param collection the field set to add.
     **/
    Schema & addFieldSet(const FieldSet &collection);

    Schema &addImportedAttributeField(const ImportedAttributeField &field);

    /**
     * Obtain the number of index fields in this schema.
     *
     * @return number of fields
     **/
    uint32_t getNumIndexFields() const noexcept { return _indexFields.size(); }

    /**
     * Obtain the number of attribute fields in this schema.
     *
     * @return number of fields
     **/
    uint32_t getNumAttributeFields() const noexcept { return _attributeFields.size(); }

    /**
     * Obtain the number of field sets in this schema.
     *
     * @return number of field sets.
     **/
    uint32_t getNumFieldSets() const noexcept { return _fieldSets.size(); }

    size_t getNumImportedAttributeFields() const noexcept { return _importedAttributeFields.size(); }

    /**
     * Get information about a specific index field using the given fieldId.
     *
     * @return the field
     * @param idx an index in the range [0, size - 1].
     **/
    const IndexField & getIndexField(uint32_t fieldId) const noexcept { return _indexFields[fieldId]; }

    /**
     * Returns const view of the index fields.
     */
    const std::vector<IndexField> &getIndexFields() const noexcept { return _indexFields; }

    /**
     * Get the field id for the index field with the given name.
     *
     * @return the field id or UNKNOWN_FIELD_ID if not found.
     * @param name the name of the field.
     **/
    uint32_t getIndexFieldId(vespalib::stringref name) const noexcept;

    /**
     * Check if a field is an index
     *
     * @return true if field is an index field.
     * @param name the name of the field.
     **/
    bool isIndexField(vespalib::stringref name) const noexcept;

    /**
     * Check if a field is a attribute field
     *
     * @return true if field is an attribute field.
     * @param name the name of the field.
     **/
    bool isAttributeField(vespalib::stringref name) const noexcept;

    /**
     * Get information about a specific attribute field using the given fieldId.
     *
     * @return the field
     * @param idx an index in the range [0, size - 1].
     **/
    const AttributeField & getAttributeField(uint32_t fieldId) const noexcept { return _attributeFields[fieldId]; }

    /**
     * Returns const view of the attribute fields.
     */
    const std::vector<AttributeField> &getAttributeFields() const noexcept { return _attributeFields; }

    /**
     * Get the field id for the attribute field with the given name.
     *
     * @return the field id or UNKNOWN_FIELD_ID if not found.
     * @param name the name of the field.
     **/
    uint32_t getAttributeFieldId(vespalib::stringref name) const noexcept;

    /**
     * Get information about a specific field set
     *
     * @return the field set.
     * @param idx an index in the range [0, size - 1].
     **/
    const FieldSet & getFieldSet(uint32_t idx) const noexcept { return _fieldSets[idx]; }

    /**
     * Get the field id for the field set with the given name.
     *
     * @return the field id or UNKNOWN_FIELD_ID if not found.
     * @param name the name of the field set.
     **/
    uint32_t getFieldSetId(vespalib::stringref name) const noexcept;

    const std::vector<ImportedAttributeField> &getImportedAttributeFields() const noexcept {
        return _importedAttributeFields;
    }

    void swap(Schema &rhs);
    void clear();

    static Schema::UP intersect(const Schema &lhs, const Schema &rhs);
    static Schema::UP make_union(const Schema &lhs, const Schema &rhs);
    static Schema::UP set_difference(const Schema &lhs, const Schema &rhs);

    bool operator==(const Schema &rhs) const noexcept ;
    bool operator!=(const Schema &rhs) const noexcept;

    bool empty() const noexcept;
};

}