• 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 <cstdint>
19 #include <initializer_list>
20 #include <string>
21 #include <string_view>
22 #include <utility>
23 
24 #include "icing/proto/schema.pb.h"
25 #include "icing/proto/term.pb.h"
26 
27 namespace icing {
28 namespace lib {
29 
30 constexpr PropertyConfigProto::Cardinality::Code CARDINALITY_UNKNOWN =
31     PropertyConfigProto::Cardinality::UNKNOWN;
32 constexpr PropertyConfigProto::Cardinality::Code CARDINALITY_REPEATED =
33     PropertyConfigProto::Cardinality::REPEATED;
34 constexpr PropertyConfigProto::Cardinality::Code CARDINALITY_OPTIONAL =
35     PropertyConfigProto::Cardinality::OPTIONAL;
36 constexpr PropertyConfigProto::Cardinality::Code CARDINALITY_REQUIRED =
37     PropertyConfigProto::Cardinality::REQUIRED;
38 
39 constexpr StringIndexingConfig::TokenizerType::Code TOKENIZER_NONE =
40     StringIndexingConfig::TokenizerType::NONE;
41 constexpr StringIndexingConfig::TokenizerType::Code TOKENIZER_PLAIN =
42     StringIndexingConfig::TokenizerType::PLAIN;
43 constexpr StringIndexingConfig::TokenizerType::Code TOKENIZER_VERBATIM =
44     StringIndexingConfig::TokenizerType::VERBATIM;
45 constexpr StringIndexingConfig::TokenizerType::Code TOKENIZER_RFC822 =
46     StringIndexingConfig::TokenizerType::RFC822;
47 constexpr StringIndexingConfig::TokenizerType::Code TOKENIZER_URL =
48     StringIndexingConfig::TokenizerType::URL;
49 
50 constexpr TermMatchType::Code TERM_MATCH_UNKNOWN = TermMatchType::UNKNOWN;
51 constexpr TermMatchType::Code TERM_MATCH_EXACT = TermMatchType::EXACT_ONLY;
52 constexpr TermMatchType::Code TERM_MATCH_PREFIX = TermMatchType::PREFIX;
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 
66 constexpr PropertyConfigProto::DataType::Code TYPE_UNKNOWN =
67     PropertyConfigProto::DataType::UNKNOWN;
68 constexpr PropertyConfigProto::DataType::Code TYPE_STRING =
69     PropertyConfigProto::DataType::STRING;
70 constexpr PropertyConfigProto::DataType::Code TYPE_INT64 =
71     PropertyConfigProto::DataType::INT64;
72 constexpr PropertyConfigProto::DataType::Code TYPE_DOUBLE =
73     PropertyConfigProto::DataType::DOUBLE;
74 constexpr PropertyConfigProto::DataType::Code TYPE_BOOLEAN =
75     PropertyConfigProto::DataType::BOOLEAN;
76 constexpr PropertyConfigProto::DataType::Code TYPE_BYTES =
77     PropertyConfigProto::DataType::BYTES;
78 constexpr PropertyConfigProto::DataType::Code TYPE_DOCUMENT =
79     PropertyConfigProto::DataType::DOCUMENT;
80 constexpr PropertyConfigProto::DataType::Code TYPE_VECTOR =
81     PropertyConfigProto::DataType::VECTOR;
82 
83 constexpr JoinableConfig::ValueType::Code JOINABLE_VALUE_TYPE_NONE =
84     JoinableConfig::ValueType::NONE;
85 constexpr JoinableConfig::ValueType::Code JOINABLE_VALUE_TYPE_QUALIFIED_ID =
86     JoinableConfig::ValueType::QUALIFIED_ID;
87 
88 class PropertyConfigBuilder {
89  public:
90   PropertyConfigBuilder() = default;
PropertyConfigBuilder(PropertyConfigProto property)91   explicit PropertyConfigBuilder(PropertyConfigProto property)
92       : property_(std::move(property)) {}
93 
SetName(std::string_view name)94   PropertyConfigBuilder& SetName(std::string_view name) {
95     property_.set_property_name(std::string(name));
96     return *this;
97   }
98 
SetDataType(PropertyConfigProto::DataType::Code data_type)99   PropertyConfigBuilder& SetDataType(
100       PropertyConfigProto::DataType::Code data_type) {
101     property_.set_data_type(data_type);
102     return *this;
103   }
104 
SetDataTypeString(TermMatchType::Code match_type,StringIndexingConfig::TokenizerType::Code tokenizer)105   PropertyConfigBuilder& SetDataTypeString(
106       TermMatchType::Code match_type,
107       StringIndexingConfig::TokenizerType::Code tokenizer) {
108     property_.set_data_type(PropertyConfigProto::DataType::STRING);
109     property_.mutable_string_indexing_config()->set_term_match_type(match_type);
110     property_.mutable_string_indexing_config()->set_tokenizer_type(tokenizer);
111     return *this;
112   }
113 
114   PropertyConfigBuilder& SetDataTypeJoinableString(
115       JoinableConfig::ValueType::Code join_value_type,
116       TermMatchType::Code match_type = TERM_MATCH_UNKNOWN,
117       StringIndexingConfig::TokenizerType::Code tokenizer = TOKENIZER_NONE) {
118     property_.set_data_type(PropertyConfigProto::DataType::STRING);
119     property_.mutable_joinable_config()->set_value_type(join_value_type);
120     property_.mutable_string_indexing_config()->set_term_match_type(match_type);
121     property_.mutable_string_indexing_config()->set_tokenizer_type(tokenizer);
122     return *this;
123   }
124 
SetDataTypeInt64(IntegerIndexingConfig::NumericMatchType::Code numeric_match_type)125   PropertyConfigBuilder& SetDataTypeInt64(
126       IntegerIndexingConfig::NumericMatchType::Code numeric_match_type) {
127     property_.set_data_type(PropertyConfigProto::DataType::INT64);
128     property_.mutable_integer_indexing_config()->set_numeric_match_type(
129         numeric_match_type);
130     return *this;
131   }
132 
SetDataTypeDocument(std::string_view schema_type,bool index_nested_properties)133   PropertyConfigBuilder& SetDataTypeDocument(std::string_view schema_type,
134                                              bool index_nested_properties) {
135     property_.set_data_type(PropertyConfigProto::DataType::DOCUMENT);
136     property_.set_schema_type(std::string(schema_type));
137     property_.mutable_document_indexing_config()->set_index_nested_properties(
138         index_nested_properties);
139     property_.mutable_document_indexing_config()
140         ->clear_indexable_nested_properties_list();
141     return *this;
142   }
143 
SetDataTypeDocument(std::string_view schema_type,std::initializer_list<std::string> indexable_nested_properties_list)144   PropertyConfigBuilder& SetDataTypeDocument(
145       std::string_view schema_type,
146       std::initializer_list<std::string> indexable_nested_properties_list) {
147     property_.set_data_type(PropertyConfigProto::DataType::DOCUMENT);
148     property_.set_schema_type(std::string(schema_type));
149     property_.mutable_document_indexing_config()->set_index_nested_properties(
150         false);
151     for (const std::string& property : indexable_nested_properties_list) {
152       property_.mutable_document_indexing_config()
153           ->add_indexable_nested_properties_list(property);
154     }
155     return *this;
156   }
157 
SetDataTypeVector(EmbeddingIndexingConfig::EmbeddingIndexingType::Code embedding_indexing_type)158   PropertyConfigBuilder& SetDataTypeVector(
159       EmbeddingIndexingConfig::EmbeddingIndexingType::Code
160           embedding_indexing_type) {
161     property_.set_data_type(PropertyConfigProto::DataType::VECTOR);
162     property_.mutable_embedding_indexing_config()->set_embedding_indexing_type(
163         embedding_indexing_type);
164     return *this;
165   }
166 
SetJoinable(JoinableConfig::ValueType::Code join_value_type,bool propagate_delete)167   PropertyConfigBuilder& SetJoinable(
168       JoinableConfig::ValueType::Code join_value_type, bool propagate_delete) {
169     property_.mutable_joinable_config()->set_value_type(join_value_type);
170     property_.mutable_joinable_config()->set_propagate_delete(propagate_delete);
171     return *this;
172   }
173 
SetCardinality(PropertyConfigProto::Cardinality::Code cardinality)174   PropertyConfigBuilder& SetCardinality(
175       PropertyConfigProto::Cardinality::Code cardinality) {
176     property_.set_cardinality(cardinality);
177     return *this;
178   }
179 
SetDescription(std::string description)180   PropertyConfigBuilder& SetDescription(std::string description) {
181     property_.set_description(std::move(description));
182     return *this;
183   }
184 
Build()185   PropertyConfigProto Build() const { return std::move(property_); }
186 
187  private:
188   PropertyConfigProto property_;
189 };
190 
191 class SchemaTypeConfigBuilder {
192  public:
193   SchemaTypeConfigBuilder() = default;
SchemaTypeConfigBuilder(SchemaTypeConfigProto type_config)194   SchemaTypeConfigBuilder(SchemaTypeConfigProto type_config)
195       : type_config_(std::move(type_config)) {}
196 
SetType(std::string_view type)197   SchemaTypeConfigBuilder& SetType(std::string_view type) {
198     type_config_.set_schema_type(std::string(type));
199     return *this;
200   }
201 
AddParentType(std::string_view parent_type)202   SchemaTypeConfigBuilder& AddParentType(std::string_view parent_type) {
203     type_config_.add_parent_types(std::string(parent_type));
204     return *this;
205   }
206 
SetVersion(int version)207   SchemaTypeConfigBuilder& SetVersion(int version) {
208     type_config_.set_version(version);
209     return *this;
210   }
211 
SetDescription(std::string description)212   SchemaTypeConfigBuilder& SetDescription(std::string description) {
213     type_config_.set_description(std::move(description));
214     return *this;
215   }
216 
AddProperty(PropertyConfigProto property)217   SchemaTypeConfigBuilder& AddProperty(PropertyConfigProto property) {
218     *type_config_.add_properties() = std::move(property);
219     return *this;
220   }
AddProperty(PropertyConfigBuilder property_builder)221   SchemaTypeConfigBuilder& AddProperty(PropertyConfigBuilder property_builder) {
222     *type_config_.add_properties() = property_builder.Build();
223     return *this;
224   }
225 
Build()226   SchemaTypeConfigProto Build() { return std::move(type_config_); }
227 
228  private:
229   SchemaTypeConfigProto type_config_;
230 };
231 
232 class SchemaBuilder {
233  public:
234   SchemaBuilder() = default;
SchemaBuilder(SchemaProto schema)235   SchemaBuilder(SchemaProto schema) : schema_(std::move(schema)) {}
236 
AddType(SchemaTypeConfigProto type)237   SchemaBuilder& AddType(SchemaTypeConfigProto type) {
238     *schema_.add_types() = std::move(type);
239     return *this;
240   }
AddType(SchemaTypeConfigBuilder type_builder)241   SchemaBuilder& AddType(SchemaTypeConfigBuilder type_builder) {
242     *schema_.add_types() = type_builder.Build();
243     return *this;
244   }
245 
Build()246   SchemaProto Build() { return std::move(schema_); }
247 
248  private:
249   SchemaProto schema_;
250 };
251 
252 }  // namespace lib
253 }  // namespace icing
254 
255 #endif  // ICING_SCHEMA_BUILDER_H_
256