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 any number of int64_t values. 82 template <typename... V> AddInt64Property(std::string property_name,V...int64_values)83 DocumentBuilder& AddInt64Property(std::string property_name, 84 V... int64_values) { 85 return AddInt64Property(std::move(property_name), {int64_values...}); 86 } 87 88 // Takes a property name and any number of double values. 89 template <typename... V> AddDoubleProperty(std::string property_name,V...double_values)90 DocumentBuilder& AddDoubleProperty(std::string property_name, 91 V... double_values) { 92 return AddDoubleProperty(std::move(property_name), {double_values...}); 93 } 94 95 // Takes a property name and any number of boolean values. 96 template <typename... V> AddBooleanProperty(std::string property_name,V...boolean_values)97 DocumentBuilder& AddBooleanProperty(std::string property_name, 98 V... boolean_values) { 99 return AddBooleanProperty(std::move(property_name), {boolean_values...}); 100 } 101 102 // Takes a property name and any number of bytes values. 103 template <typename... V> AddBytesProperty(std::string property_name,V...bytes_values)104 DocumentBuilder& AddBytesProperty(std::string property_name, 105 V... bytes_values) { 106 return AddBytesProperty(std::move(property_name), {bytes_values...}); 107 } 108 // Takes a property name and any number of document values. 109 template <typename... V> AddDocumentProperty(std::string property_name,V &&...document_values)110 DocumentBuilder& AddDocumentProperty(std::string property_name, 111 V&&... document_values) { 112 return AddDocumentProperty(std::move(property_name), {document_values...}); 113 } 114 Build()115 DocumentProto Build() const { return document_; } 116 117 private: 118 DocumentProto document_; 119 AddStringProperty(std::string property_name,std::initializer_list<std::string_view> string_values)120 DocumentBuilder& AddStringProperty( 121 std::string property_name, 122 std::initializer_list<std::string_view> string_values) { 123 auto property = document_.add_properties(); 124 property->set_name(std::move(property_name)); 125 for (std::string_view string_value : string_values) { 126 property->mutable_string_values()->Add(std::string(string_value)); 127 } 128 return *this; 129 } 130 AddInt64Property(std::string property_name,std::initializer_list<int64_t> int64_values)131 DocumentBuilder& AddInt64Property( 132 std::string property_name, std::initializer_list<int64_t> int64_values) { 133 auto property = document_.add_properties(); 134 property->set_name(std::move(property_name)); 135 for (int64_t int64_value : int64_values) { 136 property->mutable_int64_values()->Add(int64_value); 137 } 138 return *this; 139 } 140 AddDoubleProperty(std::string property_name,std::initializer_list<double> double_values)141 DocumentBuilder& AddDoubleProperty( 142 std::string property_name, std::initializer_list<double> double_values) { 143 auto property = document_.add_properties(); 144 property->set_name(std::move(property_name)); 145 for (double double_value : double_values) { 146 property->mutable_double_values()->Add(double_value); 147 } 148 return *this; 149 } 150 AddBooleanProperty(std::string property_name,std::initializer_list<bool> boolean_values)151 DocumentBuilder& AddBooleanProperty( 152 std::string property_name, std::initializer_list<bool> boolean_values) { 153 auto property = document_.add_properties(); 154 property->set_name(std::move(property_name)); 155 for (bool boolean_value : boolean_values) { 156 property->mutable_boolean_values()->Add(boolean_value); 157 } 158 return *this; 159 } 160 AddBytesProperty(std::string property_name,std::initializer_list<std::string> bytes_values)161 DocumentBuilder& AddBytesProperty( 162 std::string property_name, 163 std::initializer_list<std::string> bytes_values) { 164 auto property = document_.add_properties(); 165 property->set_name(std::move(property_name)); 166 for (const std::string& bytes_value : bytes_values) { 167 property->mutable_bytes_values()->Add(std::string(bytes_value)); 168 } 169 return *this; 170 } 171 AddDocumentProperty(std::string property_name,std::initializer_list<DocumentProto> document_values)172 DocumentBuilder& AddDocumentProperty( 173 std::string property_name, 174 std::initializer_list<DocumentProto> document_values) { 175 auto property = document_.add_properties(); 176 property->set_name(std::move(property_name)); 177 for (DocumentProto document_value : document_values) { 178 property->mutable_document_values()->Add(std::move(document_value)); 179 } 180 return *this; 181 } 182 }; 183 184 } // namespace lib 185 } // namespace icing 186 187 #endif // ICING_DOCUMENT_BUILDER_H_ 188