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