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 Build()129 DocumentProto Build() const { return document_; } 130 131 private: 132 DocumentProto document_; 133 AddStringProperty(std::string property_name,std::initializer_list<std::string_view> string_values)134 DocumentBuilder& AddStringProperty( 135 std::string property_name, 136 std::initializer_list<std::string_view> string_values) { 137 auto property = document_.add_properties(); 138 property->set_name(std::move(property_name)); 139 for (std::string_view string_value : string_values) { 140 property->mutable_string_values()->Add(std::string(string_value)); 141 } 142 return *this; 143 } 144 AddDoubleProperty(std::string property_name,std::initializer_list<double> double_values)145 DocumentBuilder& AddDoubleProperty( 146 std::string property_name, std::initializer_list<double> double_values) { 147 auto property = document_.add_properties(); 148 property->set_name(std::move(property_name)); 149 for (double double_value : double_values) { 150 property->mutable_double_values()->Add(double_value); 151 } 152 return *this; 153 } 154 AddBooleanProperty(std::string property_name,std::initializer_list<bool> boolean_values)155 DocumentBuilder& AddBooleanProperty( 156 std::string property_name, std::initializer_list<bool> boolean_values) { 157 auto property = document_.add_properties(); 158 property->set_name(std::move(property_name)); 159 for (bool boolean_value : boolean_values) { 160 property->mutable_boolean_values()->Add(boolean_value); 161 } 162 return *this; 163 } 164 AddBytesProperty(std::string property_name,std::initializer_list<std::string> bytes_values)165 DocumentBuilder& AddBytesProperty( 166 std::string property_name, 167 std::initializer_list<std::string> bytes_values) { 168 auto property = document_.add_properties(); 169 property->set_name(std::move(property_name)); 170 for (const std::string& bytes_value : bytes_values) { 171 property->mutable_bytes_values()->Add(std::string(bytes_value)); 172 } 173 return *this; 174 } 175 AddDocumentProperty(std::string property_name,std::initializer_list<DocumentProto> document_values)176 DocumentBuilder& AddDocumentProperty( 177 std::string property_name, 178 std::initializer_list<DocumentProto> document_values) { 179 auto property = document_.add_properties(); 180 property->set_name(std::move(property_name)); 181 for (DocumentProto document_value : document_values) { 182 property->mutable_document_values()->Add(std::move(document_value)); 183 } 184 return *this; 185 } 186 }; 187 188 } // namespace lib 189 } // namespace icing 190 191 #endif // ICING_DOCUMENT_BUILDER_H_ 192