aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/index/schemautil.h
blob: fc61c10de0a1abc678936a23134ecae87c9d26de (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/searchcommon/common/schema.h>

namespace search::index {

class SchemaUtil {
public:

    class IndexSettings {
        schema::DataType _dataType;
        bool _error;        // Schema is bad.

    public:
        const schema::DataType & getDataType() const {
            return _dataType;
        }

        bool hasError() const { return _error; }

        IndexSettings()
            : _dataType(schema::DataType::STRING),
              _error(false)
        { }

        IndexSettings(const IndexSettings &rhs)
            : _dataType(rhs._dataType),
              _error(rhs._error)
        { }

        IndexSettings(schema::DataType dataType,
                      bool error)
            : _dataType(dataType),
              _error(error)
        { }

        IndexSettings & operator=(const IndexSettings &rhs) {
            IndexSettings tmp(rhs);
            swap(tmp);
            return *this;
        }

        void swap(IndexSettings &rhs) {
            std::swap(_dataType, rhs._dataType);
            std::swap(_error, rhs._error);
        }
    };

    class IndexIterator {
        const Schema &_schema;
        uint32_t _index;

    public:
        IndexIterator(const Schema &schema)
            : _schema(schema),
              _index(0u)
        { }

        IndexIterator(const Schema &schema, uint32_t index)
            : _schema(schema),
              _index(index)
        { }

        IndexIterator(const Schema &schema, const IndexIterator &rhs)
            : _schema(schema),
              _index(Schema::UNKNOWN_FIELD_ID)
        {
            const vespalib::string &name = rhs.getName();
            _index = schema.getIndexFieldId(name);
        }

        const Schema & getSchema() const {
            return _schema;
        }

        uint32_t getIndex() const {
            return _index;
        }

        const vespalib::string &getName() const {
            return _schema.getIndexField(_index).getName();
        }

        bool use_interleaved_features() const {
            return _schema.getIndexField(_index).use_interleaved_features();
        }

        IndexIterator &operator++() {
            if (_index < _schema.getNumIndexFields()) {
                ++_index;
            }
            return *this;
        }

        bool isValid() const {
            return _index < _schema.getNumIndexFields();
        }

        IndexSettings getIndexSettings() const {
            return SchemaUtil::getIndexSettings(_schema, _index);
        }

        /**
         * Return if old schema has at least one usable input field
         * with matching data type.
         *
         * @param oldSchema old schema, present in an input index
         */
        bool hasOldFields(const Schema &oldSchema) const;

        /**
         * Return if fields in old schema matches fields in new
         * schema, allowing for slightly faster fusion operations.
         * Field collections must have same set of fields which must
         * also match between new and old schema.
         *
         * @param oldSchema old schema, present in an input index
         */
        bool hasMatchingOldFields(const Schema &oldSchema) const;

        bool has_matching_use_interleaved_features(const Schema &oldSchema) const;
    };

    static IndexSettings getIndexSettings(const Schema &schema, const uint32_t index);


    static bool validateIndexFieldType(schema::DataType dataType) {
        switch (dataType) {
        case schema::DataType::STRING:
        case schema::DataType::INT32:
            return true;
        default:
            ;
        }
        return false;
    }

    static bool validateIndexField(const Schema::IndexField &field);
    static bool addIndexField(Schema &schema, const Schema::IndexField &field);
    static bool validateSchema(const Schema &schema);
    static bool getIndexIds(const Schema &schema, schema::DataType dataType, std::vector<uint32_t> &indexes);
};

}