aboutsummaryrefslogtreecommitdiff
path: root/icing/schema-builder.h
blob: c702afe4316ee20117887ce658f0db27413420ac (plain)
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
// Copyright (C) 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef ICING_SCHEMA_BUILDER_H_
#define ICING_SCHEMA_BUILDER_H_

#include <cstdint>
#include <initializer_list>
#include <string>
#include <string_view>
#include <utility>

#include "icing/proto/schema.pb.h"
#include "icing/proto/term.pb.h"

namespace icing {
namespace lib {

constexpr PropertyConfigProto::Cardinality::Code CARDINALITY_UNKNOWN =
    PropertyConfigProto::Cardinality::UNKNOWN;
constexpr PropertyConfigProto::Cardinality::Code CARDINALITY_REPEATED =
    PropertyConfigProto::Cardinality::REPEATED;
constexpr PropertyConfigProto::Cardinality::Code CARDINALITY_OPTIONAL =
    PropertyConfigProto::Cardinality::OPTIONAL;
constexpr PropertyConfigProto::Cardinality::Code CARDINALITY_REQUIRED =
    PropertyConfigProto::Cardinality::REQUIRED;

constexpr StringIndexingConfig::TokenizerType::Code TOKENIZER_NONE =
    StringIndexingConfig::TokenizerType::NONE;
constexpr StringIndexingConfig::TokenizerType::Code TOKENIZER_PLAIN =
    StringIndexingConfig::TokenizerType::PLAIN;
constexpr StringIndexingConfig::TokenizerType::Code TOKENIZER_VERBATIM =
    StringIndexingConfig::TokenizerType::VERBATIM;
constexpr StringIndexingConfig::TokenizerType::Code TOKENIZER_RFC822 =
    StringIndexingConfig::TokenizerType::RFC822;
constexpr StringIndexingConfig::TokenizerType::Code TOKENIZER_URL =
    StringIndexingConfig::TokenizerType::URL;

constexpr TermMatchType::Code TERM_MATCH_UNKNOWN = TermMatchType::UNKNOWN;
constexpr TermMatchType::Code TERM_MATCH_EXACT = TermMatchType::EXACT_ONLY;
constexpr TermMatchType::Code TERM_MATCH_PREFIX = TermMatchType::PREFIX;

constexpr IntegerIndexingConfig::NumericMatchType::Code NUMERIC_MATCH_UNKNOWN =
    IntegerIndexingConfig::NumericMatchType::UNKNOWN;
constexpr IntegerIndexingConfig::NumericMatchType::Code NUMERIC_MATCH_RANGE =
    IntegerIndexingConfig::NumericMatchType::RANGE;

constexpr EmbeddingIndexingConfig::EmbeddingIndexingType::Code
    EMBEDDING_INDEXING_UNKNOWN =
        EmbeddingIndexingConfig::EmbeddingIndexingType::UNKNOWN;
constexpr EmbeddingIndexingConfig::EmbeddingIndexingType::Code
    EMBEDDING_INDEXING_LINEAR_SEARCH =
        EmbeddingIndexingConfig::EmbeddingIndexingType::LINEAR_SEARCH;

constexpr PropertyConfigProto::DataType::Code TYPE_UNKNOWN =
    PropertyConfigProto::DataType::UNKNOWN;
constexpr PropertyConfigProto::DataType::Code TYPE_STRING =
    PropertyConfigProto::DataType::STRING;
constexpr PropertyConfigProto::DataType::Code TYPE_INT64 =
    PropertyConfigProto::DataType::INT64;
constexpr PropertyConfigProto::DataType::Code TYPE_DOUBLE =
    PropertyConfigProto::DataType::DOUBLE;
constexpr PropertyConfigProto::DataType::Code TYPE_BOOLEAN =
    PropertyConfigProto::DataType::BOOLEAN;
constexpr PropertyConfigProto::DataType::Code TYPE_BYTES =
    PropertyConfigProto::DataType::BYTES;
constexpr PropertyConfigProto::DataType::Code TYPE_DOCUMENT =
    PropertyConfigProto::DataType::DOCUMENT;
constexpr PropertyConfigProto::DataType::Code TYPE_VECTOR =
    PropertyConfigProto::DataType::VECTOR;

constexpr JoinableConfig::ValueType::Code JOINABLE_VALUE_TYPE_NONE =
    JoinableConfig::ValueType::NONE;
constexpr JoinableConfig::ValueType::Code JOINABLE_VALUE_TYPE_QUALIFIED_ID =
    JoinableConfig::ValueType::QUALIFIED_ID;

class PropertyConfigBuilder {
 public:
  PropertyConfigBuilder() = default;
  explicit PropertyConfigBuilder(PropertyConfigProto property)
      : property_(std::move(property)) {}

  PropertyConfigBuilder& SetName(std::string_view name) {
    property_.set_property_name(std::string(name));
    return *this;
  }

  PropertyConfigBuilder& SetDataType(
      PropertyConfigProto::DataType::Code data_type) {
    property_.set_data_type(data_type);
    return *this;
  }

  PropertyConfigBuilder& SetDataTypeString(
      TermMatchType::Code match_type,
      StringIndexingConfig::TokenizerType::Code tokenizer) {
    property_.set_data_type(PropertyConfigProto::DataType::STRING);
    property_.mutable_string_indexing_config()->set_term_match_type(match_type);
    property_.mutable_string_indexing_config()->set_tokenizer_type(tokenizer);
    return *this;
  }

  PropertyConfigBuilder& SetDataTypeJoinableString(
      JoinableConfig::ValueType::Code join_value_type,
      TermMatchType::Code match_type = TERM_MATCH_UNKNOWN,
      StringIndexingConfig::TokenizerType::Code tokenizer = TOKENIZER_NONE) {
    property_.set_data_type(PropertyConfigProto::DataType::STRING);
    property_.mutable_joinable_config()->set_value_type(join_value_type);
    property_.mutable_string_indexing_config()->set_term_match_type(match_type);
    property_.mutable_string_indexing_config()->set_tokenizer_type(tokenizer);
    return *this;
  }

  PropertyConfigBuilder& SetDataTypeInt64(
      IntegerIndexingConfig::NumericMatchType::Code numeric_match_type) {
    property_.set_data_type(PropertyConfigProto::DataType::INT64);
    property_.mutable_integer_indexing_config()->set_numeric_match_type(
        numeric_match_type);
    return *this;
  }

  PropertyConfigBuilder& SetDataTypeDocument(std::string_view schema_type,
                                             bool index_nested_properties) {
    property_.set_data_type(PropertyConfigProto::DataType::DOCUMENT);
    property_.set_schema_type(std::string(schema_type));
    property_.mutable_document_indexing_config()->set_index_nested_properties(
        index_nested_properties);
    property_.mutable_document_indexing_config()
        ->clear_indexable_nested_properties_list();
    return *this;
  }

  PropertyConfigBuilder& SetDataTypeDocument(
      std::string_view schema_type,
      std::initializer_list<std::string> indexable_nested_properties_list) {
    property_.set_data_type(PropertyConfigProto::DataType::DOCUMENT);
    property_.set_schema_type(std::string(schema_type));
    property_.mutable_document_indexing_config()->set_index_nested_properties(
        false);
    for (const std::string& property : indexable_nested_properties_list) {
      property_.mutable_document_indexing_config()
          ->add_indexable_nested_properties_list(property);
    }
    return *this;
  }

  PropertyConfigBuilder& SetDataTypeVector(
      EmbeddingIndexingConfig::EmbeddingIndexingType::Code
          embedding_indexing_type) {
    property_.set_data_type(PropertyConfigProto::DataType::VECTOR);
    property_.mutable_embedding_indexing_config()->set_embedding_indexing_type(
        embedding_indexing_type);
    return *this;
  }

  PropertyConfigBuilder& SetJoinable(
      JoinableConfig::ValueType::Code join_value_type, bool propagate_delete) {
    property_.mutable_joinable_config()->set_value_type(join_value_type);
    property_.mutable_joinable_config()->set_propagate_delete(propagate_delete);
    return *this;
  }

  PropertyConfigBuilder& SetCardinality(
      PropertyConfigProto::Cardinality::Code cardinality) {
    property_.set_cardinality(cardinality);
    return *this;
  }

  PropertyConfigBuilder& SetDescription(std::string description) {
    property_.set_description(std::move(description));
    return *this;
  }

  PropertyConfigProto Build() const { return std::move(property_); }

 private:
  PropertyConfigProto property_;
};

class SchemaTypeConfigBuilder {
 public:
  SchemaTypeConfigBuilder() = default;
  SchemaTypeConfigBuilder(SchemaTypeConfigProto type_config)
      : type_config_(std::move(type_config)) {}

  SchemaTypeConfigBuilder& SetType(std::string_view type) {
    type_config_.set_schema_type(std::string(type));
    return *this;
  }

  SchemaTypeConfigBuilder& AddParentType(std::string_view parent_type) {
    type_config_.add_parent_types(std::string(parent_type));
    return *this;
  }

  SchemaTypeConfigBuilder& SetVersion(int version) {
    type_config_.set_version(version);
    return *this;
  }

  SchemaTypeConfigBuilder& SetDescription(std::string description) {
    type_config_.set_description(std::move(description));
    return *this;
  }

  SchemaTypeConfigBuilder& AddProperty(PropertyConfigProto property) {
    *type_config_.add_properties() = std::move(property);
    return *this;
  }
  SchemaTypeConfigBuilder& AddProperty(PropertyConfigBuilder property_builder) {
    *type_config_.add_properties() = property_builder.Build();
    return *this;
  }

  SchemaTypeConfigProto Build() { return std::move(type_config_); }

 private:
  SchemaTypeConfigProto type_config_;
};

class SchemaBuilder {
 public:
  SchemaBuilder() = default;
  SchemaBuilder(SchemaProto schema) : schema_(std::move(schema)) {}

  SchemaBuilder& AddType(SchemaTypeConfigProto type) {
    *schema_.add_types() = std::move(type);
    return *this;
  }
  SchemaBuilder& AddType(SchemaTypeConfigBuilder type_builder) {
    *schema_.add_types() = type_builder.Build();
    return *this;
  }

  SchemaProto Build() { return std::move(schema_); }

 private:
  SchemaProto schema_;
};

}  // namespace lib
}  // namespace icing

#endif  // ICING_SCHEMA_BUILDER_H_