• 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 PropertyConfigProto::DataType::Code TYPE_UNKNOWN =
60     PropertyConfigProto::DataType::UNKNOWN;
61 constexpr PropertyConfigProto::DataType::Code TYPE_STRING =
62     PropertyConfigProto::DataType::STRING;
63 constexpr PropertyConfigProto::DataType::Code TYPE_INT64 =
64     PropertyConfigProto::DataType::INT64;
65 constexpr PropertyConfigProto::DataType::Code TYPE_DOUBLE =
66     PropertyConfigProto::DataType::DOUBLE;
67 constexpr PropertyConfigProto::DataType::Code TYPE_BOOLEAN =
68     PropertyConfigProto::DataType::BOOLEAN;
69 constexpr PropertyConfigProto::DataType::Code TYPE_BYTES =
70     PropertyConfigProto::DataType::BYTES;
71 constexpr PropertyConfigProto::DataType::Code TYPE_DOCUMENT =
72     PropertyConfigProto::DataType::DOCUMENT;
73 
74 constexpr JoinableConfig::ValueType::Code JOINABLE_VALUE_TYPE_NONE =
75     JoinableConfig::ValueType::NONE;
76 constexpr JoinableConfig::ValueType::Code JOINABLE_VALUE_TYPE_QUALIFIED_ID =
77     JoinableConfig::ValueType::QUALIFIED_ID;
78 
79 class PropertyConfigBuilder {
80  public:
81   PropertyConfigBuilder() = default;
PropertyConfigBuilder(PropertyConfigProto property)82   explicit PropertyConfigBuilder(PropertyConfigProto property)
83       : property_(std::move(property)) {}
84 
SetName(std::string_view name)85   PropertyConfigBuilder& SetName(std::string_view name) {
86     property_.set_property_name(std::string(name));
87     return *this;
88   }
89 
SetDataType(PropertyConfigProto::DataType::Code data_type)90   PropertyConfigBuilder& SetDataType(
91       PropertyConfigProto::DataType::Code data_type) {
92     property_.set_data_type(data_type);
93     return *this;
94   }
95 
SetDataTypeString(TermMatchType::Code match_type,StringIndexingConfig::TokenizerType::Code tokenizer)96   PropertyConfigBuilder& SetDataTypeString(
97       TermMatchType::Code match_type,
98       StringIndexingConfig::TokenizerType::Code tokenizer) {
99     property_.set_data_type(PropertyConfigProto::DataType::STRING);
100     property_.mutable_string_indexing_config()->set_term_match_type(match_type);
101     property_.mutable_string_indexing_config()->set_tokenizer_type(tokenizer);
102     return *this;
103   }
104 
105   PropertyConfigBuilder& SetDataTypeJoinableString(
106       JoinableConfig::ValueType::Code join_value_type,
107       TermMatchType::Code match_type = TERM_MATCH_UNKNOWN,
108       StringIndexingConfig::TokenizerType::Code tokenizer = TOKENIZER_NONE) {
109     property_.set_data_type(PropertyConfigProto::DataType::STRING);
110     property_.mutable_joinable_config()->set_value_type(join_value_type);
111     property_.mutable_string_indexing_config()->set_term_match_type(match_type);
112     property_.mutable_string_indexing_config()->set_tokenizer_type(tokenizer);
113     return *this;
114   }
115 
SetDataTypeInt64(IntegerIndexingConfig::NumericMatchType::Code numeric_match_type)116   PropertyConfigBuilder& SetDataTypeInt64(
117       IntegerIndexingConfig::NumericMatchType::Code numeric_match_type) {
118     property_.set_data_type(PropertyConfigProto::DataType::INT64);
119     property_.mutable_integer_indexing_config()->set_numeric_match_type(
120         numeric_match_type);
121     return *this;
122   }
123 
SetDataTypeDocument(std::string_view schema_type,bool index_nested_properties)124   PropertyConfigBuilder& SetDataTypeDocument(std::string_view schema_type,
125                                              bool index_nested_properties) {
126     property_.set_data_type(PropertyConfigProto::DataType::DOCUMENT);
127     property_.set_schema_type(std::string(schema_type));
128     property_.mutable_document_indexing_config()->set_index_nested_properties(
129         index_nested_properties);
130     return *this;
131   }
132 
SetJoinable(JoinableConfig::ValueType::Code join_value_type,bool propagate_delete)133   PropertyConfigBuilder& SetJoinable(
134       JoinableConfig::ValueType::Code join_value_type, bool propagate_delete) {
135     property_.mutable_joinable_config()->set_value_type(join_value_type);
136     property_.mutable_joinable_config()->set_propagate_delete(propagate_delete);
137     return *this;
138   }
139 
SetCardinality(PropertyConfigProto::Cardinality::Code cardinality)140   PropertyConfigBuilder& SetCardinality(
141       PropertyConfigProto::Cardinality::Code cardinality) {
142     property_.set_cardinality(cardinality);
143     return *this;
144   }
145 
Build()146   PropertyConfigProto Build() const { return std::move(property_); }
147 
148  private:
149   PropertyConfigProto property_;
150 };
151 
152 class SchemaTypeConfigBuilder {
153  public:
154   SchemaTypeConfigBuilder() = default;
SchemaTypeConfigBuilder(SchemaTypeConfigProto type_config)155   SchemaTypeConfigBuilder(SchemaTypeConfigProto type_config)
156       : type_config_(std::move(type_config)) {}
157 
SetType(std::string_view type)158   SchemaTypeConfigBuilder& SetType(std::string_view type) {
159     type_config_.set_schema_type(std::string(type));
160     return *this;
161   }
162 
AddParentType(std::string_view parent_type)163   SchemaTypeConfigBuilder& AddParentType(std::string_view parent_type) {
164     type_config_.add_parent_types(std::string(parent_type));
165     return *this;
166   }
167 
SetVersion(int version)168   SchemaTypeConfigBuilder& SetVersion(int version) {
169     type_config_.set_version(version);
170     return *this;
171   }
172 
AddProperty(PropertyConfigProto property)173   SchemaTypeConfigBuilder& AddProperty(PropertyConfigProto property) {
174     *type_config_.add_properties() = std::move(property);
175     return *this;
176   }
AddProperty(PropertyConfigBuilder property_builder)177   SchemaTypeConfigBuilder& AddProperty(PropertyConfigBuilder property_builder) {
178     *type_config_.add_properties() = property_builder.Build();
179     return *this;
180   }
181 
Build()182   SchemaTypeConfigProto Build() { return std::move(type_config_); }
183 
184  private:
185   SchemaTypeConfigProto type_config_;
186 };
187 
188 class SchemaBuilder {
189  public:
190   SchemaBuilder() = default;
SchemaBuilder(SchemaProto schema)191   SchemaBuilder(SchemaProto schema) : schema_(std::move(schema)) {}
192 
AddType(SchemaTypeConfigProto type)193   SchemaBuilder& AddType(SchemaTypeConfigProto type) {
194     *schema_.add_types() = std::move(type);
195     return *this;
196   }
AddType(SchemaTypeConfigBuilder type_builder)197   SchemaBuilder& AddType(SchemaTypeConfigBuilder type_builder) {
198     *schema_.add_types() = type_builder.Build();
199     return *this;
200   }
201 
Build()202   SchemaProto Build() { return std::move(schema_); }
203 
204  private:
205   SchemaProto schema_;
206 };
207 
208 }  // namespace lib
209 }  // namespace icing
210 
211 #endif  // ICING_SCHEMA_BUILDER_H_
212