• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ICING_SCHEMA_BUILDER_H_
16 #define ICING_SCHEMA_BUILDER_H_
17 
18 #include <initializer_list>
19 #include <string>
20 #include <string_view>
21 #include <utility>
22 
23 #include "icing/proto/schema.pb.h"
24 #include "icing/proto/term.pb.h"
25 
26 namespace icing {
27 namespace lib {
28 
29 constexpr PropertyConfigProto::Cardinality::Code CARDINALITY_UNKNOWN =
30     PropertyConfigProto::Cardinality::UNKNOWN;
31 constexpr PropertyConfigProto::Cardinality::Code CARDINALITY_REPEATED =
32     PropertyConfigProto::Cardinality::REPEATED;
33 constexpr PropertyConfigProto::Cardinality::Code CARDINALITY_OPTIONAL =
34     PropertyConfigProto::Cardinality::OPTIONAL;
35 constexpr PropertyConfigProto::Cardinality::Code CARDINALITY_REQUIRED =
36     PropertyConfigProto::Cardinality::REQUIRED;
37 
38 constexpr StringIndexingConfig::TokenizerType::Code TOKENIZER_NONE =
39     StringIndexingConfig::TokenizerType::NONE;
40 constexpr StringIndexingConfig::TokenizerType::Code TOKENIZER_PLAIN =
41     StringIndexingConfig::TokenizerType::PLAIN;
42 constexpr StringIndexingConfig::TokenizerType::Code TOKENIZER_VERBATIM =
43     StringIndexingConfig::TokenizerType::VERBATIM;
44 constexpr StringIndexingConfig::TokenizerType::Code TOKENIZER_RFC822 =
45     StringIndexingConfig::TokenizerType::RFC822;
46 constexpr StringIndexingConfig::TokenizerType::Code TOKENIZER_URL =
47     StringIndexingConfig::TokenizerType::URL;
48 
49 constexpr TermMatchType::Code TERM_MATCH_UNKNOWN = TermMatchType::UNKNOWN;
50 constexpr TermMatchType::Code TERM_MATCH_EXACT = TermMatchType::EXACT_ONLY;
51 constexpr TermMatchType::Code TERM_MATCH_PREFIX = TermMatchType::PREFIX;
52 constexpr TermMatchType::Code TERM_MATCH_STEMMING = TermMatchType::STEMMING;
53 
54 constexpr IntegerIndexingConfig::NumericMatchType::Code NUMERIC_MATCH_UNKNOWN =
55     IntegerIndexingConfig::NumericMatchType::UNKNOWN;
56 constexpr IntegerIndexingConfig::NumericMatchType::Code NUMERIC_MATCH_RANGE =
57     IntegerIndexingConfig::NumericMatchType::RANGE;
58 
59 constexpr EmbeddingIndexingConfig::EmbeddingIndexingType::Code
60     EMBEDDING_INDEXING_UNKNOWN =
61         EmbeddingIndexingConfig::EmbeddingIndexingType::UNKNOWN;
62 constexpr EmbeddingIndexingConfig::EmbeddingIndexingType::Code
63     EMBEDDING_INDEXING_LINEAR_SEARCH =
64         EmbeddingIndexingConfig::EmbeddingIndexingType::LINEAR_SEARCH;
65 constexpr EmbeddingIndexingConfig::QuantizationType::Code
66     QUANTIZATION_TYPE_NONE = EmbeddingIndexingConfig::QuantizationType::NONE;
67 constexpr EmbeddingIndexingConfig::QuantizationType::Code
68     QUANTIZATION_TYPE_QUANTIZE_8_BIT =
69         EmbeddingIndexingConfig::QuantizationType::QUANTIZE_8_BIT;
70 
71 constexpr PropertyConfigProto::DataType::Code TYPE_UNKNOWN =
72     PropertyConfigProto::DataType::UNKNOWN;
73 constexpr PropertyConfigProto::DataType::Code TYPE_STRING =
74     PropertyConfigProto::DataType::STRING;
75 constexpr PropertyConfigProto::DataType::Code TYPE_INT64 =
76     PropertyConfigProto::DataType::INT64;
77 constexpr PropertyConfigProto::DataType::Code TYPE_DOUBLE =
78     PropertyConfigProto::DataType::DOUBLE;
79 constexpr PropertyConfigProto::DataType::Code TYPE_BOOLEAN =
80     PropertyConfigProto::DataType::BOOLEAN;
81 constexpr PropertyConfigProto::DataType::Code TYPE_BYTES =
82     PropertyConfigProto::DataType::BYTES;
83 constexpr PropertyConfigProto::DataType::Code TYPE_DOCUMENT =
84     PropertyConfigProto::DataType::DOCUMENT;
85 constexpr PropertyConfigProto::DataType::Code TYPE_VECTOR =
86     PropertyConfigProto::DataType::VECTOR;
87 constexpr PropertyConfigProto::DataType::Code TYPE_BLOB_HANDLE =
88     PropertyConfigProto::DataType::BLOB_HANDLE;
89 
90 constexpr JoinableConfig::ValueType::Code JOINABLE_VALUE_TYPE_NONE =
91     JoinableConfig::ValueType::NONE;
92 constexpr JoinableConfig::ValueType::Code JOINABLE_VALUE_TYPE_QUALIFIED_ID =
93     JoinableConfig::ValueType::QUALIFIED_ID;
94 
95 constexpr JoinableConfig::DeletePropagationType::Code
96     DELETE_PROPAGATION_TYPE_NONE = JoinableConfig::DeletePropagationType::NONE;
97 constexpr JoinableConfig::DeletePropagationType::Code
98     DELETE_PROPAGATION_TYPE_PROPAGATE_FROM =
99         JoinableConfig::DeletePropagationType::PROPAGATE_FROM;
100 
101 constexpr PropertyConfigProto::ScorableType::Code SCORABLE_TYPE_ENABLED =
102     PropertyConfigProto::ScorableType::ENABLED;
103 constexpr PropertyConfigProto::ScorableType::Code SCORABLE_TYPE_DISABLED =
104     PropertyConfigProto::ScorableType::DISABLED;
105 constexpr PropertyConfigProto::ScorableType::Code SCORABLE_TYPE_UNKNOWN =
106     PropertyConfigProto::ScorableType::UNKNOWN;
107 
108 class PropertyConfigBuilder {
109  public:
110   PropertyConfigBuilder() = default;
PropertyConfigBuilder(PropertyConfigProto property)111   explicit PropertyConfigBuilder(PropertyConfigProto property)
112       : property_(std::move(property)) {}
113 
SetName(std::string_view name)114   PropertyConfigBuilder& SetName(std::string_view name) {
115     property_.set_property_name(std::string(name));
116     return *this;
117   }
118 
SetDataType(PropertyConfigProto::DataType::Code data_type)119   PropertyConfigBuilder& SetDataType(
120       PropertyConfigProto::DataType::Code data_type) {
121     property_.set_data_type(data_type);
122     return *this;
123   }
124 
SetDataTypeString(TermMatchType::Code match_type,StringIndexingConfig::TokenizerType::Code tokenizer)125   PropertyConfigBuilder& SetDataTypeString(
126       TermMatchType::Code match_type,
127       StringIndexingConfig::TokenizerType::Code tokenizer) {
128     property_.set_data_type(PropertyConfigProto::DataType::STRING);
129     property_.mutable_string_indexing_config()->set_term_match_type(match_type);
130     property_.mutable_string_indexing_config()->set_tokenizer_type(tokenizer);
131     return *this;
132   }
133 
134   PropertyConfigBuilder& SetDataTypeJoinableString(
135       JoinableConfig::ValueType::Code join_value_type,
136       JoinableConfig::DeletePropagationType::Code delete_propagation_type =
137           DELETE_PROPAGATION_TYPE_NONE) {
138     property_.set_data_type(PropertyConfigProto::DataType::STRING);
139     property_.mutable_joinable_config()->set_value_type(join_value_type);
140     property_.mutable_joinable_config()->set_delete_propagation_type(
141         delete_propagation_type);
142     return *this;
143   }
144 
SetDataTypeInt64(IntegerIndexingConfig::NumericMatchType::Code numeric_match_type)145   PropertyConfigBuilder& SetDataTypeInt64(
146       IntegerIndexingConfig::NumericMatchType::Code numeric_match_type) {
147     property_.set_data_type(PropertyConfigProto::DataType::INT64);
148     property_.mutable_integer_indexing_config()->set_numeric_match_type(
149         numeric_match_type);
150     return *this;
151   }
152 
SetDataTypeDocument(std::string_view schema_type,bool index_nested_properties)153   PropertyConfigBuilder& SetDataTypeDocument(std::string_view schema_type,
154                                              bool index_nested_properties) {
155     property_.set_data_type(PropertyConfigProto::DataType::DOCUMENT);
156     property_.set_schema_type(std::string(schema_type));
157     property_.mutable_document_indexing_config()->set_index_nested_properties(
158         index_nested_properties);
159     property_.mutable_document_indexing_config()
160         ->clear_indexable_nested_properties_list();
161     return *this;
162   }
163 
SetDataTypeDocument(std::string_view schema_type,std::initializer_list<std::string> indexable_nested_properties_list)164   PropertyConfigBuilder& SetDataTypeDocument(
165       std::string_view schema_type,
166       std::initializer_list<std::string> indexable_nested_properties_list) {
167     property_.set_data_type(PropertyConfigProto::DataType::DOCUMENT);
168     property_.set_schema_type(std::string(schema_type));
169     property_.mutable_document_indexing_config()->set_index_nested_properties(
170         false);
171     for (const std::string& property : indexable_nested_properties_list) {
172       property_.mutable_document_indexing_config()
173           ->add_indexable_nested_properties_list(property);
174     }
175     return *this;
176   }
177 
178   PropertyConfigBuilder& SetDataTypeVector(
179       EmbeddingIndexingConfig::EmbeddingIndexingType::Code
180           embedding_indexing_type,
181       EmbeddingIndexingConfig::QuantizationType::Code quantization_type =
182           EmbeddingIndexingConfig::QuantizationType::NONE) {
183     property_.set_data_type(PropertyConfigProto::DataType::VECTOR);
184     EmbeddingIndexingConfig* embedding_indexing_config =
185         property_.mutable_embedding_indexing_config();
186     embedding_indexing_config->set_embedding_indexing_type(
187         embedding_indexing_type);
188     embedding_indexing_config->set_quantization_type(quantization_type);
189     return *this;
190   }
191 
SetJoinable(JoinableConfig::ValueType::Code join_value_type,JoinableConfig::DeletePropagationType::Code delete_propagation_type)192   PropertyConfigBuilder& SetJoinable(
193       JoinableConfig::ValueType::Code join_value_type,
194       JoinableConfig::DeletePropagationType::Code delete_propagation_type) {
195     property_.mutable_joinable_config()->set_value_type(join_value_type);
196     property_.mutable_joinable_config()->set_delete_propagation_type(
197         delete_propagation_type);
198     return *this;
199   }
200 
SetCardinality(PropertyConfigProto::Cardinality::Code cardinality)201   PropertyConfigBuilder& SetCardinality(
202       PropertyConfigProto::Cardinality::Code cardinality) {
203     property_.set_cardinality(cardinality);
204     return *this;
205   }
206 
SetDescription(std::string description)207   PropertyConfigBuilder& SetDescription(std::string description) {
208     property_.set_description(std::move(description));
209     return *this;
210   }
211 
SetScorableType(PropertyConfigProto::ScorableType::Code scorable_type)212   PropertyConfigBuilder& SetScorableType(
213       PropertyConfigProto::ScorableType::Code scorable_type) {
214     property_.set_scorable_type(scorable_type);
215     return *this;
216   }
217 
Build()218   PropertyConfigProto Build() const { return std::move(property_); }
219 
220  private:
221   PropertyConfigProto property_;
222 };
223 
224 class SchemaTypeConfigBuilder {
225  public:
226   SchemaTypeConfigBuilder() = default;
SchemaTypeConfigBuilder(SchemaTypeConfigProto type_config)227   SchemaTypeConfigBuilder(SchemaTypeConfigProto type_config)
228       : type_config_(std::move(type_config)) {}
229 
SetType(std::string_view type)230   SchemaTypeConfigBuilder& SetType(std::string_view type) {
231     type_config_.set_schema_type(std::string(type));
232     return *this;
233   }
234 
AddParentType(std::string_view parent_type)235   SchemaTypeConfigBuilder& AddParentType(std::string_view parent_type) {
236     type_config_.add_parent_types(std::string(parent_type));
237     return *this;
238   }
239 
SetVersion(int version)240   SchemaTypeConfigBuilder& SetVersion(int version) {
241     type_config_.set_version(version);
242     return *this;
243   }
244 
SetDescription(std::string description)245   SchemaTypeConfigBuilder& SetDescription(std::string description) {
246     type_config_.set_description(std::move(description));
247     return *this;
248   }
249 
SetDatabase(std::string database)250   SchemaTypeConfigBuilder& SetDatabase(std::string database) {
251     type_config_.set_database(std::move(database));
252     return *this;
253   }
254 
AddProperty(PropertyConfigProto property)255   SchemaTypeConfigBuilder& AddProperty(PropertyConfigProto property) {
256     *type_config_.add_properties() = std::move(property);
257     return *this;
258   }
AddProperty(PropertyConfigBuilder property_builder)259   SchemaTypeConfigBuilder& AddProperty(PropertyConfigBuilder property_builder) {
260     *type_config_.add_properties() = property_builder.Build();
261     return *this;
262   }
263 
Build()264   SchemaTypeConfigProto Build() { return std::move(type_config_); }
265 
266  private:
267   SchemaTypeConfigProto type_config_;
268 };
269 
270 class SchemaBuilder {
271  public:
272   SchemaBuilder() = default;
SchemaBuilder(SchemaProto schema)273   SchemaBuilder(SchemaProto schema) : schema_(std::move(schema)) {}
274 
AddType(SchemaTypeConfigProto type)275   SchemaBuilder& AddType(SchemaTypeConfigProto type) {
276     *schema_.add_types() = std::move(type);
277     return *this;
278   }
AddType(SchemaTypeConfigBuilder type_builder)279   SchemaBuilder& AddType(SchemaTypeConfigBuilder type_builder) {
280     *schema_.add_types() = type_builder.Build();
281     return *this;
282   }
283 
Build()284   SchemaProto Build() { return std::move(schema_); }
285 
286  private:
287   SchemaProto schema_;
288 };
289 
290 }  // namespace lib
291 }  // namespace icing
292 
293 #endif  // ICING_SCHEMA_BUILDER_H_
294