xref: /aosp_15_r20/external/icing/icing/document-builder.h (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
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_DOCUMENT_BUILDER_H_
16 #define ICING_DOCUMENT_BUILDER_H_
17 
18 #include <cstdint>
19 #include <initializer_list>
20 #include <string>
21 #include <string_view>
22 #include <utility>
23 #include <vector>
24 
25 #include "icing/proto/document.pb.h"
26 
27 namespace icing {
28 namespace lib {
29 
30 class DocumentBuilder {
31  public:
32   DocumentBuilder() = default;
DocumentBuilder(DocumentProto document)33   explicit DocumentBuilder(DocumentProto document)
34       : document_(std::move(document)) {}
35 
SetNamespace(std::string name_space)36   DocumentBuilder& SetNamespace(std::string name_space) {
37     document_.set_namespace_(std::move(name_space));
38     return *this;
39   }
40 
SetUri(std::string uri)41   DocumentBuilder& SetUri(std::string uri) {
42     document_.set_uri(std::move(uri));
43     return *this;
44   }
45 
SetKey(std::string name_space,std::string uri)46   DocumentBuilder& SetKey(std::string name_space, std::string uri) {
47     return SetNamespace(std::move(name_space)).SetUri(std::move(uri));
48   }
49 
SetSchema(std::string schema)50   DocumentBuilder& SetSchema(std::string schema) {
51     document_.set_schema(std::move(schema));
52     return *this;
53   }
54 
SetCreationTimestampMs(uint64_t creation_timestamp_ms)55   DocumentBuilder& SetCreationTimestampMs(uint64_t creation_timestamp_ms) {
56     document_.set_creation_timestamp_ms(creation_timestamp_ms);
57     return *this;
58   }
59 
SetScore(int32_t score)60   DocumentBuilder& SetScore(int32_t score) {
61     document_.set_score(score);
62     return *this;
63   }
64 
SetTtlMs(uint64_t ttl_ms)65   DocumentBuilder& SetTtlMs(uint64_t ttl_ms) {
66     document_.set_ttl_ms(ttl_ms);
67     return *this;
68   }
69 
ClearProperties()70   DocumentBuilder& ClearProperties() {
71     document_.clear_properties();
72     return *this;
73   }
74 
75   // Takes a property name and any number of string values.
76   template <typename... V>
AddStringProperty(std::string property_name,V...string_values)77   DocumentBuilder& AddStringProperty(std::string property_name,
78                                      V... string_values) {
79     return AddStringProperty(std::move(property_name), {string_values...});
80   }
81 
82   // Takes a property name and iterator of int64_t values.
83   template <typename InputIt>
AddInt64Property(std::string property_name,InputIt first,InputIt last)84   DocumentBuilder& AddInt64Property(std::string property_name, InputIt first,
85                                     InputIt last) {
86     auto property = document_.add_properties();
87     property->set_name(std::move(property_name));
88     for (InputIt it = first; it != last; ++it) {
89       property->mutable_int64_values()->Add(*it);
90     }
91     return *this;
92   }
93 
94   // Takes a property name and any number of int64_t values.
95   template <typename... V>
AddInt64Property(std::string property_name,V...int64_values)96   DocumentBuilder& AddInt64Property(std::string property_name,
97                                     V... int64_values) {
98     std::initializer_list<int64_t> int64_values_list = {int64_values...};
99     return AddInt64Property(std::move(property_name), int64_values_list.begin(),
100                             int64_values_list.end());
101   }
102 
103   // Takes a property name and any number of double values.
104   template <typename... V>
AddDoubleProperty(std::string property_name,V...double_values)105   DocumentBuilder& AddDoubleProperty(std::string property_name,
106                                      V... double_values) {
107     return AddDoubleProperty(std::move(property_name), {double_values...});
108   }
109 
110   // Takes a property name and any number of boolean values.
111   template <typename... V>
AddBooleanProperty(std::string property_name,V...boolean_values)112   DocumentBuilder& AddBooleanProperty(std::string property_name,
113                                       V... boolean_values) {
114     return AddBooleanProperty(std::move(property_name), {boolean_values...});
115   }
116 
117   // Takes a property name and any number of blob handle values.
118   template <typename... V>
AddBlobHandleProperty(std::string property_name,V...blob_handle_values)119   DocumentBuilder& AddBlobHandleProperty(std::string property_name,
120                                          V... blob_handle_values) {
121     return AddBlobHandleProperty(std::move(property_name),
122                                  {blob_handle_values...});
123   }
124 
125   // Takes a property name and any number of bytes values.
126   template <typename... V>
AddBytesProperty(std::string property_name,V...bytes_values)127   DocumentBuilder& AddBytesProperty(std::string property_name,
128                                     V... bytes_values) {
129     return AddBytesProperty(std::move(property_name), {bytes_values...});
130   }
131   // Takes a property name and any number of document values.
132   template <typename... V>
AddDocumentProperty(std::string property_name,V &&...document_values)133   DocumentBuilder& AddDocumentProperty(std::string property_name,
134                                        V&&... document_values) {
135     return AddDocumentProperty(std::move(property_name), {document_values...});
136   }
137 
138   // Takes a property name and any number of vector values.
139   template <typename... V>
AddVectorProperty(std::string property_name,V...vector_values)140   DocumentBuilder& AddVectorProperty(std::string property_name,
141                                      V... vector_values) {
142     return AddVectorProperty(std::move(property_name), {vector_values...});
143   }
144 
145   // Takes a property name and a list of vector values.
AddVectorProperty(std::string property_name,std::vector<PropertyProto::VectorProto> vector_values)146   DocumentBuilder& AddVectorProperty(
147       std::string property_name,
148       std::vector<PropertyProto::VectorProto> vector_values) {
149     auto property = document_.add_properties();
150     property->set_name(std::move(property_name));
151     for (PropertyProto::VectorProto vector_value : vector_values) {
152       property->mutable_vector_values()->Add(std::move(vector_value));
153     }
154     return *this;
155   }
156 
Build()157   DocumentProto Build() const { return document_; }
158 
159  private:
160   DocumentProto document_;
161 
AddStringProperty(std::string property_name,std::initializer_list<std::string_view> string_values)162   DocumentBuilder& AddStringProperty(
163       std::string property_name,
164       std::initializer_list<std::string_view> string_values) {
165     auto property = document_.add_properties();
166     property->set_name(std::move(property_name));
167     for (std::string_view string_value : string_values) {
168       property->mutable_string_values()->Add(std::string(string_value));
169     }
170     return *this;
171   }
172 
AddDoubleProperty(std::string property_name,std::initializer_list<double> double_values)173   DocumentBuilder& AddDoubleProperty(
174       std::string property_name, std::initializer_list<double> double_values) {
175     auto property = document_.add_properties();
176     property->set_name(std::move(property_name));
177     for (double double_value : double_values) {
178       property->mutable_double_values()->Add(double_value);
179     }
180     return *this;
181   }
182 
AddBooleanProperty(std::string property_name,std::initializer_list<bool> boolean_values)183   DocumentBuilder& AddBooleanProperty(
184       std::string property_name, std::initializer_list<bool> boolean_values) {
185     auto property = document_.add_properties();
186     property->set_name(std::move(property_name));
187     for (bool boolean_value : boolean_values) {
188       property->mutable_boolean_values()->Add(boolean_value);
189     }
190     return *this;
191   }
192 
AddBlobHandleProperty(std::string property_name,std::initializer_list<PropertyProto::BlobHandleProto> blob_handle_values)193   DocumentBuilder& AddBlobHandleProperty(
194       std::string property_name,
195       std::initializer_list<PropertyProto::BlobHandleProto>
196           blob_handle_values) {
197     auto property = document_.add_properties();
198     property->set_name(std::move(property_name));
199     for (PropertyProto::BlobHandleProto blob_handle_value :
200          blob_handle_values) {
201       property->mutable_blob_handle_values()->Add(std::move(blob_handle_value));
202     }
203     return *this;
204   }
205 
AddBytesProperty(std::string property_name,std::initializer_list<std::string> bytes_values)206   DocumentBuilder& AddBytesProperty(
207       std::string property_name,
208       std::initializer_list<std::string> bytes_values) {
209     auto property = document_.add_properties();
210     property->set_name(std::move(property_name));
211     for (const std::string& bytes_value : bytes_values) {
212       property->mutable_bytes_values()->Add(std::string(bytes_value));
213     }
214     return *this;
215   }
216 
AddDocumentProperty(std::string property_name,std::initializer_list<DocumentProto> document_values)217   DocumentBuilder& AddDocumentProperty(
218       std::string property_name,
219       std::initializer_list<DocumentProto> document_values) {
220     auto property = document_.add_properties();
221     property->set_name(std::move(property_name));
222     for (DocumentProto document_value : document_values) {
223       property->mutable_document_values()->Add(std::move(document_value));
224     }
225     return *this;
226   }
227 
AddVectorProperty(std::string property_name,std::initializer_list<PropertyProto::VectorProto> vector_values)228   DocumentBuilder& AddVectorProperty(
229       std::string property_name,
230       std::initializer_list<PropertyProto::VectorProto> vector_values) {
231     auto property = document_.add_properties();
232     property->set_name(std::move(property_name));
233     for (PropertyProto::VectorProto vector_value : vector_values) {
234       property->mutable_vector_values()->Add(std::move(vector_value));
235     }
236     return *this;
237   }
238 };
239 
240 }  // namespace lib
241 }  // namespace icing
242 
243 #endif  // ICING_DOCUMENT_BUILDER_H_
244