summaryrefslogtreecommitdiffstats
path: root/searchcommon/src/vespa/searchcommon/common/schema.h
blob: e17d219d7e8d50b92ad9919438561355491b4e21 (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "datatype.h"
#include <vespa/vespalib/stllike/string.h>
#include <vespa/vespalib/stllike/hash_map.h>
#include <vespa/vespalib/util/ptrholder.h>
#include <vector>

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 PH = vespalib::PtrHolder<Schema>;

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

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

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

        /**
         * Create this field based on the given config lines.
         **/
        Field(const std::vector<vespalib::string> & lines);
        Field(const Field &);
        Field & operator = (const Field &);
        Field(Field &&) noexcept;
        Field & operator = (Field &&) noexcept;

        virtual ~Field();

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

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

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

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


    /**
     * 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);
        IndexField(vespalib::stringref name, DataType dt, CollectionType ct);
        IndexField(const IndexField &);
        IndexField & operator = (const IndexField &);
        IndexField(IndexField &&) noexcept;
        IndexField & operator = (IndexField &&) noexcept;
        /**
         * Create this index field based on the given config lines.
         **/
        IndexField(const std::vector<vespalib::string> &lines);

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

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

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

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

    using AttributeField = Field;
    using SummaryField = 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:
        FieldSet(vespalib::stringref n) : _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.
         **/
        FieldSet(const std::vector<vespalib::string> & lines);

        ~FieldSet();

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

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

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

    static const uint32_t UNKNOWN_FIELD_ID;

private:
    std::vector<IndexField>      _indexFields;
    std::vector<AttributeField>  _attributeFields;
    std::vector<SummaryField>    _summaryFields;
    std::vector<FieldSet> _fieldSets;
    std::vector<ImportedAttributeField> _importedAttributeFields;
    using Name2IdMap = vespalib::hash_map<vespalib::string, uint32_t>;
    Name2IdMap _indexIds;
    Name2IdMap _attributeIds;
    Name2IdMap _summaryIds;
    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);
    Schema & operator=(Schema && rhs);
    ~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 summary field to this schema
     *
     * @param field the field to add
     **/
    Schema &
    addSummaryField(const SummaryField &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 { return _indexFields.size(); }

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

    /**
     * Obtain the number of summary fields in this schema.
     *
     * @return number of fields
     **/
    uint32_t getNumSummaryFields() const { return _summaryFields.size(); }

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

    size_t getNumImportedAttributeFields() const { 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
    {
        return _indexFields[fieldId];
    }

    /**
     * Returns const view of the index fields.
     */
    const std::vector<IndexField> &getIndexFields() const {
        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;

    /**
     * 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;

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

    /**
     * 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;

    /**
     * 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
    {
        return _attributeFields[fieldId];
    }

    /**
     * Returns const view of the attribute fields.
     */
    const std::vector<AttributeField> &getAttributeFields() const {
        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;

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

    /**
     * Returns const view of the summary fields.
     */
    const std::vector<SummaryField> &getSummaryFields() const {
        return _summaryFields;
    }

    /**
     * Get the field id for the summary 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 getSummaryFieldId(vespalib::stringref name) const;

    /**
     * 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
    {
        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;

    const std::vector<ImportedAttributeField> &getImportedAttributeFields() const {
        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;
    bool operator!=(const Schema &rhs) const;

    bool empty() const;
};

}