Skip to content

Commit 628abe2

Browse files
authored
GenericRecord Feature has implemented. [API-1164][API-1853] (#1086)
1 parent 7298549 commit 628abe2

20 files changed

+8217
-90
lines changed

hazelcast/include/hazelcast/client/serialization/field_kind.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace serialization {
2424

2525
enum class HAZELCAST_API field_kind
2626
{
27+
NOT_AVAILABLE = 0,
2728
BOOLEAN = 1,
2829
ARRAY_OF_BOOLEAN = 2,
2930
INT8 = 3,

hazelcast/include/hazelcast/client/serialization/generic_record.h

Lines changed: 1297 additions & 0 deletions
Large diffs are not rendered by default.

hazelcast/include/hazelcast/client/serialization/generic_record_builder.h

Lines changed: 1543 additions & 0 deletions
Large diffs are not rendered by default.

hazelcast/include/hazelcast/client/serialization/pimpl/compact/compact.h

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
// serialization.h not in the beginning but later to avoid cyclic dependency.
2121

2222
#include <boost/thread/future.hpp>
23+
#include <boost/property_tree/ptree.hpp>
2324
#include <utility>
2425
#include "hazelcast/util/export.h"
2526
#include "hazelcast/client/serialization/serialization.h"
2627
#include "hazelcast/util/SynchronizedMap.h"
2728
#include "hazelcast/client/serialization/field_kind.h"
2829
#include "hazelcast/client/serialization/pimpl/compact/default_schema_service.h"
30+
#include "hazelcast/client/serialization/pimpl/compact/schema_writer.h"
2931

3032
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
3133
#pragma warning(push)
@@ -649,6 +651,11 @@ class HAZELCAST_API compact_reader
649651
typename boost::optional<T>>::type
650652
read();
651653
template<typename T>
654+
typename std::enable_if<
655+
std::is_same<generic_record::generic_record, T>::value,
656+
typename boost::optional<T>>::type
657+
read();
658+
template<typename T>
652659
typename std::enable_if<
653660
std::is_same<std::vector<bool>, typename std::remove_cv<T>::type>::value,
654661
typename boost::optional<T>>::type
@@ -1335,6 +1342,12 @@ class HAZELCAST_API default_compact_writer
13351342
void>::type
13361343
write(const T& value);
13371344

1345+
template<typename T>
1346+
typename std::enable_if<
1347+
std::is_same<generic_record::generic_record, T>::value,
1348+
void>::type
1349+
write(const T& value);
1350+
13381351
template<typename T>
13391352
typename std::enable_if<std::is_same<std::vector<bool>, T>::value,
13401353
void>::type
@@ -1369,17 +1382,6 @@ class HAZELCAST_API default_compact_writer
13691382
} // namespace pimpl
13701383

13711384
namespace pimpl {
1372-
class HAZELCAST_API schema_writer
1373-
{
1374-
public:
1375-
explicit schema_writer(std::string type_name);
1376-
void add_field(std::string field_name, enum field_kind kind);
1377-
schema build() &&;
1378-
1379-
private:
1380-
std::unordered_map<std::string, field_descriptor> field_definition_map;
1381-
std::string type_name;
1382-
};
13831385

13841386
class HAZELCAST_API compact_stream_serializer
13851387
{
@@ -1389,33 +1391,57 @@ class HAZELCAST_API compact_stream_serializer
13891391
template<typename T>
13901392
T read(object_data_input& in);
13911393

1394+
generic_record::generic_record read_generic_record(object_data_input& in);
1395+
13921396
template<typename T>
13931397
void write(const T& object, object_data_output& out);
13941398

13951399
template<typename T>
13961400
static schema build_schema(const T& object);
13971401

1402+
void write_generic_record(const generic_record::generic_record& record,
1403+
object_data_output& out);
1404+
13981405
private:
13991406
default_schema_service& schema_service;
14001407
};
14011408

14021409
struct HAZELCAST_API field_kind_based_operations
14031410
{
1411+
using kind_size_in_bytes_fn = std::function<int()>;
1412+
using write_field_from_record_to_writer_fn =
1413+
std::function<void(default_compact_writer&,
1414+
const generic_record::generic_record&,
1415+
const std::string&)>;
1416+
using read_generic_record_or_primitive_fn =
1417+
std::function<void(compact::compact_reader&,
1418+
generic_record::generic_record_builder&,
1419+
const std::string&)>;
1420+
using write_json_formatted_field_fn =
1421+
std::function<void(boost::property_tree::ptree&,
1422+
const generic_record::generic_record&,
1423+
const std::string&)>;
1424+
14041425
static constexpr int VARIABLE_SIZE = -1;
14051426

14061427
static constexpr int DEFAULT_KIND_SIZE_IN_BYTES() { return VARIABLE_SIZE; }
14071428

14081429
field_kind_based_operations();
14091430

1410-
explicit field_kind_based_operations(
1411-
std::function<int()> kind_size_in_byte_func);
1431+
explicit field_kind_based_operations(kind_size_in_bytes_fn,
1432+
write_field_from_record_to_writer_fn,
1433+
read_generic_record_or_primitive_fn,
1434+
write_json_formatted_field_fn);
14121435

1413-
std::function<int()> kind_size_in_byte_func;
1436+
kind_size_in_bytes_fn kind_size_in_byte_func;
1437+
write_field_from_record_to_writer_fn write_field_from_record_to_writer;
1438+
read_generic_record_or_primitive_fn read_generic_record_or_primitive;
1439+
write_json_formatted_field_fn write_json_formatted_field;
14141440
};
14151441

14161442
struct HAZELCAST_API field_operations
14171443
{
1418-
static field_kind_based_operations get(enum field_kind field_kind);
1444+
static const field_kind_based_operations& get(enum field_kind field_kind);
14191445
};
14201446

14211447
/**

hazelcast/include/hazelcast/client/serialization/pimpl/compact/compact_impl.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#pragma once
1717

1818
#include "hazelcast/client/serialization/pimpl/compact/compact.h"
19+
#include "hazelcast/client/serialization/generic_record_builder.h"
1920
#include "hazelcast/util/finally.h"
2021
#include "hazelcast/util/IOUtil.h"
2122
#include <type_traits>
@@ -173,6 +174,14 @@ compact_reader::read()
173174
return compact_stream_serializer.template read<T>(object_data_input);
174175
}
175176

177+
template<typename T>
178+
typename std::enable_if<std::is_same<generic_record::generic_record, T>::value,
179+
typename boost::optional<T>>::type
180+
compact_reader::read()
181+
{
182+
return compact_stream_serializer.read_generic_record(object_data_input);
183+
}
184+
176185
template<typename T>
177186
typename std::enable_if<
178187
std::is_same<std::vector<bool>, typename std::remove_cv<T>::type>::value,
@@ -522,6 +531,14 @@ default_compact_writer::write(const T& value)
522531
compact_stream_serializer_.template write<T>(value, object_data_output_);
523532
}
524533

534+
template<typename T>
535+
typename std::enable_if<std::is_same<generic_record::generic_record, T>::value,
536+
void>::type
537+
default_compact_writer::write(const T& value)
538+
{
539+
compact_stream_serializer_.write_generic_record(value, object_data_output_);
540+
}
541+
525542
template<typename T>
526543
typename std::enable_if<std::is_same<std::vector<bool>, T>::value, void>::type
527544
default_compact_writer::write(const T& value)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2008-2023, 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+
#pragma once
17+
18+
#include "hazelcast/client/exception/protocol_exceptions.h"
19+
20+
namespace hazelcast {
21+
namespace client {
22+
namespace serialization {
23+
namespace pimpl {
24+
25+
struct compact_util
26+
{
27+
compact_util() = delete;
28+
29+
static exception::hazelcast_serialization
30+
exception_for_unexpected_null_value(const std::string& field_name,
31+
const std::string& method_prefix,
32+
const std::string& method_suffix);
33+
34+
static exception::hazelcast_serialization
35+
exception_for_unexpected_null_value_in_array(
36+
const std::string& field_name,
37+
const std::string& method_prefix,
38+
const std::string& method_suffix);
39+
};
40+
41+
} // namespace pimpl
42+
} // namespace serialization
43+
} // namespace client
44+
} // namespace hazelcast

hazelcast/include/hazelcast/client/serialization/pimpl/compact/schema.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class HAZELCAST_API schema
4141
size_t fixed_size_fields_length() const;
4242
const std::string& type_name() const;
4343
const std::unordered_map<std::string, field_descriptor>& fields() const;
44+
boost::optional<field_descriptor> get_field(
45+
const std::string& field_name) const;
4446

4547
private:
4648
std::string type_name_;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2008-2023, 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 "hazelcast/client/serialization/pimpl/compact/schema.h"
20+
21+
namespace hazelcast {
22+
namespace client {
23+
namespace serialization {
24+
namespace pimpl {
25+
26+
class HAZELCAST_API schema_writer
27+
{
28+
public:
29+
explicit schema_writer(std::string type_name);
30+
void add_field(std::string field_name, enum field_kind kind);
31+
schema build() &&;
32+
33+
private:
34+
std::unordered_map<std::string, field_descriptor> field_definition_map_;
35+
std::string type_name_;
36+
};
37+
38+
} // namespace pimpl
39+
} // namespace serialization
40+
} // namespace client
41+
} // namespace hazelcast

hazelcast/include/hazelcast/client/serialization/serialization.h

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "hazelcast/client/serialization/pimpl/data_input.h"
2929
#include "hazelcast/client/serialization/pimpl/data.h"
3030
#include "hazelcast/client/serialization/pimpl/data_output.h"
31+
#include "hazelcast/client/serialization/generic_record.h"
3132
#include "hazelcast/client/serialization_config.h"
3233
#include "hazelcast/client/partition_aware.h"
3334
#include "hazelcast/util/SynchronizedMap.h"
@@ -818,6 +819,12 @@ class HAZELCAST_API object_data_input
818819
std::is_base_of<custom_serializer, hz_serializer<T>>::value,
819820
boost::optional<T>>::type inline read_object(int32_t type_id);
820821

822+
template<typename T>
823+
typename std::enable_if<
824+
std::is_same<generic_record::generic_record, T>::value,
825+
boost::optional<T>>::type
826+
read_object(int32_t type_id);
827+
821828
/**
822829
* Global serialization
823830
* @tparam T The type to be deserialized to
@@ -830,7 +837,8 @@ class HAZELCAST_API object_data_input
830837
std::is_base_of<portable_serializer, hz_serializer<T>>::value ||
831838
std::is_base_of<compact::compact_serializer, hz_serializer<T>>::value ||
832839
std::is_base_of<builtin_serializer, hz_serializer<T>>::value ||
833-
std::is_base_of<custom_serializer, hz_serializer<T>>::value),
840+
std::is_base_of<custom_serializer, hz_serializer<T>>::value ||
841+
std::is_same<generic_record::generic_record, T>::value),
834842
boost::optional<T>>::type inline read_object(int32_t type_id);
835843

836844
private:
@@ -890,6 +898,11 @@ class HAZELCAST_API object_data_output : public pimpl::data_output
890898
std::is_base_of<portable_serializer, hz_serializer<T>>::value,
891899
void>::type inline write_object(const T& object);
892900

901+
template<typename T>
902+
typename std::enable_if<
903+
std::is_same<generic_record::generic_record, T>::value>::type
904+
write_object(const T& object);
905+
893906
template<typename T>
894907
typename std::enable_if<
895908
std::is_base_of<compact::compact_serializer, hz_serializer<T>>::value,
@@ -907,6 +920,7 @@ class HAZELCAST_API object_data_output : public pimpl::data_output
907920
std::is_base_of<portable_serializer, hz_serializer<T>>::value ||
908921
std::is_base_of<compact::compact_serializer, hz_serializer<T>>::value ||
909922
std::is_base_of<custom_serializer, hz_serializer<T>>::value ||
923+
std::is_same<generic_record::generic_record, T>::value ||
910924
(std::is_array<T>::value &&
911925
std::is_same<typename std::remove_all_extents<T>::type, char>::value)),
912926
void>::type inline write_object(const T object);
@@ -2359,6 +2373,24 @@ typename std::enable_if<
23592373
hz_serializer<T>::write(object, *this);
23602374
}
23612375

2376+
template<typename T>
2377+
typename std::enable_if<
2378+
std::is_same<generic_record::generic_record, T>::value>::type
2379+
object_data_output::write_object(const T& object)
2380+
{
2381+
if (is_no_write_) {
2382+
return;
2383+
}
2384+
2385+
const auto& record =
2386+
static_cast<const generic_record::generic_record&>(object);
2387+
2388+
write(static_cast<int32_t>(
2389+
pimpl::serialization_constants::CONSTANT_TYPE_COMPACT),
2390+
boost::endian::order::big);
2391+
compact_serializer_->write_generic_record(record, *this);
2392+
}
2393+
23622394
/**
23632395
* Global serialization if configured
23642396
* @tparam T
@@ -2372,6 +2404,7 @@ typename std::enable_if<
23722404
std::is_base_of<portable_serializer, hz_serializer<T>>::value ||
23732405
std::is_base_of<compact::compact_serializer, hz_serializer<T>>::value ||
23742406
std::is_base_of<custom_serializer, hz_serializer<T>>::value ||
2407+
std::is_same<generic_record::generic_record, T>::value ||
23752408
(std::is_array<T>::value &&
23762409
std::is_same<typename std::remove_all_extents<T>::type, char>::value)),
23772410
void>::type inline object_data_output::write_object(const T object)
@@ -2493,13 +2526,22 @@ typename std::enable_if<
24932526
return boost::optional<T>(read<T>());
24942527
}
24952528

2529+
template<typename T>
2530+
typename std::enable_if<std::is_same<generic_record::generic_record, T>::value,
2531+
boost::optional<T>>::type
2532+
object_data_input::read_object(int32_t type_id)
2533+
{
2534+
return compact_serializer_.read_generic_record(*this);
2535+
}
2536+
24962537
template<typename T>
24972538
typename std::enable_if<
24982539
!(std::is_base_of<identified_data_serializer, hz_serializer<T>>::value ||
24992540
std::is_base_of<portable_serializer, hz_serializer<T>>::value ||
25002541
std::is_base_of<compact::compact_serializer, hz_serializer<T>>::value ||
25012542
std::is_base_of<builtin_serializer, hz_serializer<T>>::value ||
2502-
std::is_base_of<custom_serializer, hz_serializer<T>>::value),
2543+
std::is_base_of<custom_serializer, hz_serializer<T>>::value ||
2544+
std::is_same<generic_record::generic_record, T>::value),
25032545
boost::optional<T>>::type inline object_data_input::read_object(int32_t
25042546
type_id)
25052547
{

0 commit comments

Comments
 (0)