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 constexpr TermMatchType::Code TERM_MATCH_STEMMING = TermMatchType::STEMMING; 54 55 constexpr IntegerIndexingConfig::NumericMatchType::Code NUMERIC_MATCH_UNKNOWN = 56 IntegerIndexingConfig::NumericMatchType::UNKNOWN; 57 constexpr IntegerIndexingConfig::NumericMatchType::Code NUMERIC_MATCH_RANGE = 58 IntegerIndexingConfig::NumericMatchType::RANGE; 59 60 constexpr EmbeddingIndexingConfig::EmbeddingIndexingType::Code 61 EMBEDDING_INDEXING_UNKNOWN = 62 EmbeddingIndexingConfig::EmbeddingIndexingType::UNKNOWN; 63 constexpr EmbeddingIndexingConfig::EmbeddingIndexingType::Code 64 EMBEDDING_INDEXING_LINEAR_SEARCH = 65 EmbeddingIndexingConfig::EmbeddingIndexingType::LINEAR_SEARCH; 66 constexpr EmbeddingIndexingConfig::QuantizationType::Code 67 QUANTIZATION_TYPE_NONE = EmbeddingIndexingConfig::QuantizationType::NONE; 68 constexpr EmbeddingIndexingConfig::QuantizationType::Code 69 QUANTIZATION_TYPE_QUANTIZE_8_BIT = 70 EmbeddingIndexingConfig::QuantizationType::QUANTIZE_8_BIT; 71 72 constexpr PropertyConfigProto::DataType::Code TYPE_UNKNOWN = 73 PropertyConfigProto::DataType::UNKNOWN; 74 constexpr PropertyConfigProto::DataType::Code TYPE_STRING = 75 PropertyConfigProto::DataType::STRING; 76 constexpr PropertyConfigProto::DataType::Code TYPE_INT64 = 77 PropertyConfigProto::DataType::INT64; 78 constexpr PropertyConfigProto::DataType::Code TYPE_DOUBLE = 79 PropertyConfigProto::DataType::DOUBLE; 80 constexpr PropertyConfigProto::DataType::Code TYPE_BOOLEAN = 81 PropertyConfigProto::DataType::BOOLEAN; 82 constexpr PropertyConfigProto::DataType::Code TYPE_BYTES = 83 PropertyConfigProto::DataType::BYTES; 84 constexpr PropertyConfigProto::DataType::Code TYPE_DOCUMENT = 85 PropertyConfigProto::DataType::DOCUMENT; 86 constexpr PropertyConfigProto::DataType::Code TYPE_VECTOR = 87 PropertyConfigProto::DataType::VECTOR; 88 constexpr PropertyConfigProto::DataType::Code TYPE_BLOB_HANDLE = 89 PropertyConfigProto::DataType::BLOB_HANDLE; 90 91 constexpr JoinableConfig::ValueType::Code JOINABLE_VALUE_TYPE_NONE = 92 JoinableConfig::ValueType::NONE; 93 constexpr JoinableConfig::ValueType::Code JOINABLE_VALUE_TYPE_QUALIFIED_ID = 94 JoinableConfig::ValueType::QUALIFIED_ID; 95 96 constexpr JoinableConfig::DeletePropagationType::Code 97 DELETE_PROPAGATION_TYPE_NONE = JoinableConfig::DeletePropagationType::NONE; 98 constexpr JoinableConfig::DeletePropagationType::Code 99 DELETE_PROPAGATION_TYPE_PROPAGATE_FROM = 100 JoinableConfig::DeletePropagationType::PROPAGATE_FROM; 101 102 constexpr PropertyConfigProto::ScorableType::Code SCORABLE_TYPE_ENABLED = 103 PropertyConfigProto::ScorableType::ENABLED; 104 constexpr PropertyConfigProto::ScorableType::Code SCORABLE_TYPE_DISABLED = 105 PropertyConfigProto::ScorableType::DISABLED; 106 constexpr PropertyConfigProto::ScorableType::Code SCORABLE_TYPE_UNKNOWN = 107 PropertyConfigProto::ScorableType::UNKNOWN; 108 109 class PropertyConfigBuilder { 110 public: 111 PropertyConfigBuilder() = default; PropertyConfigBuilder(PropertyConfigProto property)112 explicit PropertyConfigBuilder(PropertyConfigProto property) 113 : property_(std::move(property)) {} 114 SetName(std::string_view name)115 PropertyConfigBuilder& SetName(std::string_view name) { 116 property_.set_property_name(std::string(name)); 117 return *this; 118 } 119 SetDataType(PropertyConfigProto::DataType::Code data_type)120 PropertyConfigBuilder& SetDataType( 121 PropertyConfigProto::DataType::Code data_type) { 122 property_.set_data_type(data_type); 123 return *this; 124 } 125 SetDataTypeString(TermMatchType::Code match_type,StringIndexingConfig::TokenizerType::Code tokenizer)126 PropertyConfigBuilder& SetDataTypeString( 127 TermMatchType::Code match_type, 128 StringIndexingConfig::TokenizerType::Code tokenizer) { 129 property_.set_data_type(PropertyConfigProto::DataType::STRING); 130 property_.mutable_string_indexing_config()->set_term_match_type(match_type); 131 property_.mutable_string_indexing_config()->set_tokenizer_type(tokenizer); 132 return *this; 133 } 134 135 PropertyConfigBuilder& SetDataTypeJoinableString( 136 JoinableConfig::ValueType::Code join_value_type, 137 JoinableConfig::DeletePropagationType::Code delete_propagation_type = 138 DELETE_PROPAGATION_TYPE_NONE) { 139 property_.set_data_type(PropertyConfigProto::DataType::STRING); 140 property_.mutable_joinable_config()->set_value_type(join_value_type); 141 property_.mutable_joinable_config()->set_delete_propagation_type( 142 delete_propagation_type); 143 return *this; 144 } 145 SetDataTypeInt64(IntegerIndexingConfig::NumericMatchType::Code numeric_match_type)146 PropertyConfigBuilder& SetDataTypeInt64( 147 IntegerIndexingConfig::NumericMatchType::Code numeric_match_type) { 148 property_.set_data_type(PropertyConfigProto::DataType::INT64); 149 property_.mutable_integer_indexing_config()->set_numeric_match_type( 150 numeric_match_type); 151 return *this; 152 } 153 SetDataTypeDocument(std::string_view schema_type,bool index_nested_properties)154 PropertyConfigBuilder& SetDataTypeDocument(std::string_view schema_type, 155 bool index_nested_properties) { 156 property_.set_data_type(PropertyConfigProto::DataType::DOCUMENT); 157 property_.set_schema_type(std::string(schema_type)); 158 property_.mutable_document_indexing_config()->set_index_nested_properties( 159 index_nested_properties); 160 property_.mutable_document_indexing_config() 161 ->clear_indexable_nested_properties_list(); 162 return *this; 163 } 164 SetDataTypeDocument(std::string_view schema_type,std::initializer_list<std::string> indexable_nested_properties_list)165 PropertyConfigBuilder& SetDataTypeDocument( 166 std::string_view schema_type, 167 std::initializer_list<std::string> indexable_nested_properties_list) { 168 property_.set_data_type(PropertyConfigProto::DataType::DOCUMENT); 169 property_.set_schema_type(std::string(schema_type)); 170 property_.mutable_document_indexing_config()->set_index_nested_properties( 171 false); 172 for (const std::string& property : indexable_nested_properties_list) { 173 property_.mutable_document_indexing_config() 174 ->add_indexable_nested_properties_list(property); 175 } 176 return *this; 177 } 178 179 PropertyConfigBuilder& SetDataTypeVector( 180 EmbeddingIndexingConfig::EmbeddingIndexingType::Code 181 embedding_indexing_type, 182 EmbeddingIndexingConfig::QuantizationType::Code quantization_type = 183 EmbeddingIndexingConfig::QuantizationType::NONE) { 184 property_.set_data_type(PropertyConfigProto::DataType::VECTOR); 185 EmbeddingIndexingConfig* embedding_indexing_config = 186 property_.mutable_embedding_indexing_config(); 187 embedding_indexing_config->set_embedding_indexing_type( 188 embedding_indexing_type); 189 embedding_indexing_config->set_quantization_type(quantization_type); 190 return *this; 191 } 192 SetJoinable(JoinableConfig::ValueType::Code join_value_type,JoinableConfig::DeletePropagationType::Code delete_propagation_type)193 PropertyConfigBuilder& SetJoinable( 194 JoinableConfig::ValueType::Code join_value_type, 195 JoinableConfig::DeletePropagationType::Code delete_propagation_type) { 196 property_.mutable_joinable_config()->set_value_type(join_value_type); 197 property_.mutable_joinable_config()->set_delete_propagation_type( 198 delete_propagation_type); 199 return *this; 200 } 201 SetCardinality(PropertyConfigProto::Cardinality::Code cardinality)202 PropertyConfigBuilder& SetCardinality( 203 PropertyConfigProto::Cardinality::Code cardinality) { 204 property_.set_cardinality(cardinality); 205 return *this; 206 } 207 SetDescription(std::string description)208 PropertyConfigBuilder& SetDescription(std::string description) { 209 property_.set_description(std::move(description)); 210 return *this; 211 } 212 SetScorableType(PropertyConfigProto::ScorableType::Code scorable_type)213 PropertyConfigBuilder& SetScorableType( 214 PropertyConfigProto::ScorableType::Code scorable_type) { 215 property_.set_scorable_type(scorable_type); 216 return *this; 217 } 218 Build()219 PropertyConfigProto Build() const { return std::move(property_); } 220 221 private: 222 PropertyConfigProto property_; 223 }; 224 225 class SchemaTypeConfigBuilder { 226 public: 227 SchemaTypeConfigBuilder() = default; SchemaTypeConfigBuilder(SchemaTypeConfigProto type_config)228 SchemaTypeConfigBuilder(SchemaTypeConfigProto type_config) 229 : type_config_(std::move(type_config)) {} 230 SetType(std::string_view type)231 SchemaTypeConfigBuilder& SetType(std::string_view type) { 232 type_config_.set_schema_type(std::string(type)); 233 return *this; 234 } 235 AddParentType(std::string_view parent_type)236 SchemaTypeConfigBuilder& AddParentType(std::string_view parent_type) { 237 type_config_.add_parent_types(std::string(parent_type)); 238 return *this; 239 } 240 SetVersion(int version)241 SchemaTypeConfigBuilder& SetVersion(int version) { 242 type_config_.set_version(version); 243 return *this; 244 } 245 SetDescription(std::string description)246 SchemaTypeConfigBuilder& SetDescription(std::string description) { 247 type_config_.set_description(std::move(description)); 248 return *this; 249 } 250 SetDatabase(std::string database)251 SchemaTypeConfigBuilder& SetDatabase(std::string database) { 252 type_config_.set_database(std::move(database)); 253 return *this; 254 } 255 AddProperty(PropertyConfigProto property)256 SchemaTypeConfigBuilder& AddProperty(PropertyConfigProto property) { 257 *type_config_.add_properties() = std::move(property); 258 return *this; 259 } AddProperty(PropertyConfigBuilder property_builder)260 SchemaTypeConfigBuilder& AddProperty(PropertyConfigBuilder property_builder) { 261 *type_config_.add_properties() = property_builder.Build(); 262 return *this; 263 } 264 Build()265 SchemaTypeConfigProto Build() { return std::move(type_config_); } 266 267 private: 268 SchemaTypeConfigProto type_config_; 269 }; 270 271 class SchemaBuilder { 272 public: 273 SchemaBuilder() = default; SchemaBuilder(SchemaProto schema)274 SchemaBuilder(SchemaProto schema) : schema_(std::move(schema)) {} 275 AddType(SchemaTypeConfigProto type)276 SchemaBuilder& AddType(SchemaTypeConfigProto type) { 277 *schema_.add_types() = std::move(type); 278 return *this; 279 } AddType(SchemaTypeConfigBuilder type_builder)280 SchemaBuilder& AddType(SchemaTypeConfigBuilder type_builder) { 281 *schema_.add_types() = type_builder.Build(); 282 return *this; 283 } 284 Build()285 SchemaProto Build() { return std::move(schema_); } 286 287 private: 288 SchemaProto schema_; 289 }; 290 291 } // namespace lib 292 } // namespace icing 293 294 #endif // ICING_SCHEMA_BUILDER_H_ 295