Hazelcast C++ Client
Hazelcast C++ Client Library
Loading...
Searching...
No Matches
generic_record_builder.h
1/*
2 * Copyright (c) 2008-2025, Hazelcast, Inc. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <unordered_set>
20
21#include <boost/optional.hpp>
22#include <boost/any.hpp>
23#include <boost/variant.hpp>
24
25#include "hazelcast/client/serialization/pimpl/compact/schema_writer.h"
26#include "hazelcast/client/serialization/generic_record.h"
27#include "hazelcast/client/big_decimal.h"
28#include "hazelcast/client/local_time.h"
29#include "hazelcast/client/local_date.h"
30#include "hazelcast/client/local_date_time.h"
31#include "hazelcast/client/offset_date_time.h"
32#include "hazelcast/util/byte.h"
33
34namespace hazelcast {
35namespace client {
36namespace serialization {
37namespace pimpl {
38class compact_stream_serializer;
39}
40namespace generic_record {
41
47class HAZELCAST_API generic_record_builder
48{
49public:
63 explicit generic_record_builder(std::string type_name);
64
70 generic_record build();
71
88 generic_record_builder& set_boolean(std::string field_name, bool value);
89
106 generic_record_builder& set_int8(std::string field_name, int8_t value);
107
123 generic_record_builder& set_int16(std::string field_name, int16_t value);
124
140 generic_record_builder& set_int32(std::string field_name, int32_t value);
141
158 generic_record_builder& set_int64(std::string field_name, int64_t value);
159
176 generic_record_builder& set_float32(std::string field_name, float value);
177
194 generic_record_builder& set_float64(std::string field_name, double value);
195
211 generic_record_builder& set_nullable_boolean(std::string field_name,
212 boost::optional<bool> value);
213
229 generic_record_builder& set_nullable_int8(std::string field_name,
230 boost::optional<int8_t> value);
231
247 generic_record_builder& set_nullable_int16(std::string field_name,
248 boost::optional<int16_t> value);
249
265 generic_record_builder& set_nullable_int32(std::string field_name,
266 boost::optional<int32_t> value);
267
283 generic_record_builder& set_nullable_int64(std::string field_name,
284 boost::optional<int64_t> value);
285
301 generic_record_builder& set_nullable_float32(std::string field_name,
302 boost::optional<float> value);
303
319 generic_record_builder& set_nullable_float64(std::string field_name,
320 boost::optional<double> value);
321
337 generic_record_builder& set_string(std::string field_name,
338 boost::optional<std::string> value);
339
356 generic_record_builder& set_string(std::string field_name,
357 const char* cstr);
358
377 std::string field_name,
378 boost::optional<generic_record> value);
379
398 generic_record_builder& set_decimal(std::string field_name,
399 boost::optional<big_decimal> value);
400
419 generic_record_builder& set_time(std::string field_name,
420 boost::optional<local_time> value);
421
441 generic_record_builder& set_date(std::string field_name,
442 boost::optional<local_date> value);
443
464 std::string field_name,
465 boost::optional<local_date_time> value);
466
488 std::string field_name,
489 boost::optional<offset_date_time> value);
490
508 std::string field_name,
509 boost::optional<std::vector<bool>> value);
510
527 generic_record_builder& set_array_of_boolean(std::string field_name,
528 std::vector<bool> value);
529
546 generic_record_builder& set_array_of_boolean(std::string field_name,
547 std::initializer_list<bool> value);
548
566 std::string field_name,
567 boost::optional<std::vector<int8_t>> value);
568
585 generic_record_builder& set_array_of_int8(std::string field_name,
586 std::vector<int8_t> value);
587
604 generic_record_builder& set_array_of_int8(std::string field_name,
605 std::initializer_list<int8_t> value);
606
624 std::string field_name,
625 boost::optional<std::vector<int16_t>> value);
626
643 generic_record_builder& set_array_of_int16(std::string field_name,
644 std::vector<int16_t> value);
645
662 generic_record_builder& set_array_of_int16(std::string field_name,
663 std::initializer_list<int16_t> value);
664
682 std::string field_name,
683 boost::optional<std::vector<int32_t>> value);
684
701 generic_record_builder& set_array_of_int32(std::string field_name,
702 std::vector<int32_t> value);
703
720 generic_record_builder& set_array_of_int32(std::string field_name,
721 std::initializer_list<int32_t> value);
722
740 std::string field_name,
741 boost::optional<std::vector<int64_t>> value);
742
759 generic_record_builder& set_array_of_int64(std::string field_name,
760 std::vector<int64_t> value);
761
778 generic_record_builder& set_array_of_int64(std::string field_name,
779 std::initializer_list<int64_t> value);
780
798 std::string field_name,
799 boost::optional<std::vector<float>> value);
800
817 generic_record_builder& set_array_of_float32(std::string field_name,
818 std::vector<float> value);
819
836 generic_record_builder& set_array_of_float32(std::string field_name,
837 std::initializer_list<float> value);
838
856 std::string field_name,
857 boost::optional<std::vector<double>> value);
858
875 generic_record_builder& set_array_of_float64(std::string field_name,
876 std::vector<double> value);
877
894 generic_record_builder& set_array_of_float64(std::string field_name,
895 std::initializer_list<double> value);
896
913 std::string field_name,
914 boost::optional<std::vector<boost::optional<bool>>> value);
915
933 std::string field_name,
934 std::vector<bool> value);
935
953 std::string field_name,
954 std::initializer_list<bool> value);
955
972 std::string field_name,
973 boost::optional<std::vector<boost::optional<int8_t>>> value);
974
992 std::string field_name,
993 std::vector<int8_t> value);
994
1012 std::string field_name,
1013 std::initializer_list<int8_t> value);
1014
1031 std::string field_name,
1032 boost::optional<std::vector<boost::optional<int16_t>>> value);
1033
1051 std::string field_name,
1052 std::vector<int16_t> value);
1053
1071 std::string field_name,
1072 std::initializer_list<int16_t> value);
1073
1090 std::string field_name,
1091 boost::optional<std::vector<boost::optional<int32_t>>> value);
1092
1110 std::string field_name,
1111 std::vector<int32_t> value);
1112
1130 std::string field_name,
1131 std::initializer_list<int32_t> value);
1132
1149 std::string field_name,
1150 boost::optional<std::vector<boost::optional<int64_t>>> value);
1151
1169 std::string field_name,
1170 std::vector<int64_t> value);
1171
1189 std::string field_name,
1190 std::initializer_list<int64_t> value);
1191
1208 std::string field_name,
1209 boost::optional<std::vector<boost::optional<float>>> value);
1210
1228 std::string field_name,
1229 std::vector<float> value);
1230
1248 std::string field_name,
1249 std::initializer_list<float> value);
1250
1267 std::string field_name,
1268 boost::optional<std::vector<boost::optional<double>>> value);
1269
1287 std::string field_name,
1288 std::vector<double> value);
1289
1307 std::string field_name,
1308 std::initializer_list<double> value);
1309
1327 std::string field_name,
1328 boost::optional<std::vector<boost::optional<std::string>>> value);
1329
1348 std::string field_name,
1349 boost::optional<std::vector<boost::optional<big_decimal>>> value);
1350
1369 std::string field_name,
1370 boost::optional<std::vector<boost::optional<local_time>>> value);
1371
1390 std::string field_name,
1391 boost::optional<std::vector<boost::optional<local_date>>> value);
1392
1411 std::string field_name,
1412 boost::optional<std::vector<boost::optional<local_date_time>>> value);
1413
1432 std::string field_name,
1433 boost::optional<std::vector<boost::optional<offset_date_time>>> value);
1434
1458 std::string field_name,
1459 boost::optional<std::vector<boost::optional<generic_record>>> value);
1460
1461private:
1462 enum class strategy
1463 {
1464 default_builder,
1465 cloner,
1466 schema_bounded
1467 };
1468
1469 friend class generic_record;
1470 friend class pimpl::compact_stream_serializer;
1472 std::shared_ptr<pimpl::schema>); // DeserializedSchemaBoundGenericRecordBuilder
1474 std::shared_ptr<pimpl::schema>,
1475 std::unordered_map<std::string,
1476 boost::any>); // DeserializedGenericRecordCloner
1477
1478 template<typename T>
1479 generic_record_builder& write(std::string field_name,
1480 T value,
1481 field_kind kind)
1482 {
1483 if (already_built_) {
1484 BOOST_THROW_EXCEPTION(exception::hazelcast_serialization{
1485 boost::str(boost::format("Illegal to write after record is "
1486 "built. {field : %1%, kind : %2%}") %
1487 field_name % kind) });
1488 }
1489
1490 if (strategy_ == strategy::cloner ||
1491 strategy_ == strategy::schema_bounded) {
1492 const auto& schema =
1493 *boost::get<std::shared_ptr<pimpl::schema>>(writer_or_schema_);
1494
1495 check_type_with_schema(schema, field_name, kind);
1496 }
1497
1498 if (strategy_ == strategy::cloner) {
1499 bool inserted = overwritten_fields_.insert(field_name).second;
1500
1501 if (!inserted) {
1502 BOOST_THROW_EXCEPTION(exception::hazelcast_serialization{
1503 "Field can only be written once." });
1504 }
1505 } else if (objects_.find(field_name) != end(objects_)) {
1506 BOOST_THROW_EXCEPTION(exception::hazelcast_serialization{
1507 "Field can only be written once" });
1508 }
1509
1510 if (strategy_ == strategy::cloner) {
1511 auto existing = objects_.find(field_name);
1512
1513 assert(existing != end(objects_));
1514 existing->second = std::move(value);
1515 } else {
1516 (void)objects_.emplace(field_name, std::move(value));
1517 }
1518
1519 if (strategy_ == strategy::default_builder) {
1520 pimpl::schema_writer& writer =
1521 boost::get<pimpl::schema_writer>(writer_or_schema_);
1522
1523 writer.add_field(std::move(field_name), kind);
1524 }
1525
1526 return *this;
1527 }
1528
1529 void check_type_with_schema(const pimpl::schema&,
1530 const std::string& field_name,
1531 field_kind) const;
1532
1533 strategy strategy_;
1534 bool already_built_;
1535 std::unordered_set<std::string> overwritten_fields_;
1536 std::unordered_map<std::string, boost::any> objects_;
1537 boost::variant<pimpl::schema_writer, std::shared_ptr<pimpl::schema>> writer_or_schema_;
1538};
1539
1540} // namespace generic_record
1541} // namespace serialization
1542} // namespace client
1543} // namespace hazelcast
generic_record_builder & set_array_of_int8(std::string field_name, boost::optional< std::vector< int8_t > > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:328
generic_record_builder & set_time(std::string field_name, boost::optional< local_time > value)
Sets a time field consisting of hour, minute, seconds, and nanos parts.
Definition compact.cpp:267
generic_record_builder & set_timestamp(std::string field_name, boost::optional< local_date_time > value)
Sets a timestamp field consisting of year, month of the year, and day of the month,...
Definition compact.cpp:281
generic_record_builder & set_generic_record(std::string field_name, boost::optional< generic_record > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:252
generic_record_builder & set_array_of_boolean(std::string field_name, boost::optional< std::vector< bool > > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:303
generic_record_builder & set_array_of_string(std::string field_name, boost::optional< std::vector< boost::optional< std::string > > > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:670
generic_record_builder & set_array_of_nullable_int64(std::string field_name, boost::optional< std::vector< boost::optional< int64_t > > > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:593
generic_record_builder & set_array_of_nullable_float64(std::string field_name, boost::optional< std::vector< boost::optional< double > > > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:644
generic_record_builder & set_array_of_nullable_boolean(std::string field_name, boost::optional< std::vector< boost::optional< bool > > > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:492
generic_record_builder & set_int64(std::string field_name, int64_t value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:185
generic_record_builder & set_int8(std::string field_name, int8_t value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:167
generic_record_builder & set_array_of_decimal(std::string field_name, boost::optional< std::vector< boost::optional< big_decimal > > > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:679
generic_record_builder & set_decimal(std::string field_name, boost::optional< big_decimal > value)
Sets a decimal which is arbitrary precision and scale floating-point number.
Definition compact.cpp:260
generic_record_builder & set_array_of_nullable_int8(std::string field_name, boost::optional< std::vector< boost::optional< int8_t > > > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:518
generic_record_builder & set_nullable_int8(std::string field_name, boost::optional< int8_t > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:210
generic_record_builder & set_int16(std::string field_name, int16_t value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:173
generic_record_builder & set_array_of_nullable_int32(std::string field_name, boost::optional< std::vector< boost::optional< int32_t > > > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:568
generic_record_builder & set_nullable_float32(std::string field_name, boost::optional< float > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:238
generic_record_builder & set_array_of_nullable_int16(std::string field_name, boost::optional< std::vector< boost::optional< int16_t > > > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:543
generic_record_builder & set_array_of_nullable_float32(std::string field_name, boost::optional< std::vector< boost::optional< float > > > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:618
generic_record_builder & set_array_of_float64(std::string field_name, boost::optional< std::vector< double > > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:452
generic_record_builder & set_array_of_int64(std::string field_name, boost::optional< std::vector< int64_t > > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:402
generic_record_builder & set_array_of_timestamp_with_timezone(std::string field_name, boost::optional< std::vector< boost::optional< offset_date_time > > > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:713
generic_record_builder & set_nullable_int64(std::string field_name, boost::optional< int64_t > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:231
generic_record_builder & set_nullable_boolean(std::string field_name, boost::optional< bool > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:203
generic_record_builder & set_float32(std::string field_name, float value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:191
generic_record_builder & set_array_of_date(std::string field_name, boost::optional< std::vector< boost::optional< local_date > > > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:696
generic_record_builder & set_timestamp_with_timezone(std::string field_name, boost::optional< offset_date_time > value)
Sets a timestamp with timezone field consisting of year, month of the year and day of the month,...
Definition compact.cpp:288
generic_record_builder & set_nullable_float64(std::string field_name, boost::optional< double > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:245
generic_record_builder & set_array_of_int16(std::string field_name, boost::optional< std::vector< int16_t > > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:352
generic_record_builder & set_boolean(std::string field_name, bool value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:161
generic_record_builder & set_array_of_float32(std::string field_name, boost::optional< std::vector< float > > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:427
generic_record_builder & set_date(std::string field_name, boost::optional< local_date > value)
Sets a date field consisting of year, month of the year, and day of the month.
Definition compact.cpp:274
generic_record_builder & set_array_of_int32(std::string field_name, boost::optional< std::vector< int32_t > > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:377
generic_record_builder & set_array_of_time(std::string field_name, boost::optional< std::vector< boost::optional< local_time > > > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:688
generic_record_builder & set_float64(std::string field_name, double value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:197
generic_record_builder & set_int32(std::string field_name, int32_t value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:179
generic_record_builder(std::string type_name)
Creates a Builder that will build a generic_record in Compact format with the given type name and sch...
Definition compact.cpp:84
generic_record_builder & set_string(std::string field_name, boost::optional< std::string > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:296
generic_record_builder & set_nullable_int32(std::string field_name, boost::optional< int32_t > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:224
generic_record_builder & set_nullable_int16(std::string field_name, boost::optional< int16_t > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:217
generic_record_builder & set_array_of_timestamp(std::string field_name, boost::optional< std::vector< boost::optional< local_date_time > > > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:704
generic_record_builder & set_array_of_generic_record(std::string field_name, boost::optional< std::vector< boost::optional< generic_record > > > value)
It is legal to set the field again only when Builder is created with generic_record::new_builder_with...
Definition compact.cpp:723