Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/serialization/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ add_subdirectory(custom)
add_subdirectory(globalserializer)
add_subdirectory(json)
add_subdirectory(mixed-type-collection)
add_subdirectory(compact)
16 changes: 16 additions & 0 deletions examples/serialization/compact/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# Copyright (c) 2008-2022, Hazelcast, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
add_executable(compact ./main.cpp)
94 changes: 94 additions & 0 deletions examples/serialization/compact/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2008-2022, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <hazelcast/client/hazelcast_client.h>
#include <hazelcast/client/serialization/serialization.h>

struct PersonDTO
{
std::string name;
std::string surname;
int32_t age;
};

std::ostream&
operator<<(std::ostream& os, const PersonDTO& person)
{
os << "name: " << person.name << " surname: " << person.surname
<< " age: " << person.age;

return os;
}

namespace hazelcast {
namespace client {
namespace serialization {

template<>
struct hz_serializer<PersonDTO> : compact::compact_serializer
{
static void write(const PersonDTO& object, compact::compact_writer& out)
{
out.write_int32("age", object.age);
out.write_string("name", object.name);
out.write_string("surname", object.surname);
}

static PersonDTO read(compact::compact_reader& in)
{
PersonDTO person;

person.age = in.read_int32("age");
boost::optional<std::string> name = in.read_string("name");

if (name) {
person.name = *name;
}

boost::optional<std::string> surname = in.read_string("surname");

if (surname) {
person.surname = *surname;
}

return person;
}

static std::string type_name() { return "person"; }
};

} // namespace serialization
} // namespace client
} // namespace hazelcast

/**
* This example demonstrates how to use a type with compact serialization.
*/
int
main()
{
auto hz = hazelcast::new_client().get();
auto map = hz.get_map("map").get();

map->put("Peter", PersonDTO{ "Peter", "Stone", 45 }).get();
auto person =
map->get<std::string, PersonDTO>(std::string{ "Peter" }).get();

std::cout << person << std::endl;

std::cout << "Finished" << std::endl;

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <boost/uuid/uuid.hpp>
#include "hazelcast/client/member.h"
#include "hazelcast/client/serialization/pimpl/compact/schema.h"
#include "hazelcast/logger.h"

#include "codecs.h"
Expand Down Expand Up @@ -5787,6 +5788,22 @@ sql_fetch_encode(const sql::impl::query_id& query_id,
return msg;
}

ClientMessage
client_sendschema_encode(const serialization::pimpl::schema& schema)
{
size_t initial_frame_size = ClientMessage::REQUEST_HEADER_LEN;
ClientMessage msg(initial_frame_size);
msg.set_retryable(true);
msg.set_operation_name("client.sendschema");

msg.set_message_type(static_cast<int32_t>(4864));
msg.set_partition_id(-1);

msg.set(schema, true);

return msg;
}

} // namespace codec
} // namespace protocol
} // namespace client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3079,6 +3079,12 @@ ClientMessage HAZELCAST_API
sql_fetch_encode(const sql::impl::query_id& query_id,
int32_t cursor_buffer_size);

/**
* Replicates schema on cluster
*/
ClientMessage HAZELCAST_API
client_sendschema_encode(const serialization::pimpl::schema& schema);

} // namespace codec
} // namespace protocol
} // namespace client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include "hazelcast/cp/cp_impl.h"
#include "hazelcast/logger.h"
#include "hazelcast/client/sql/sql_service.h"
#include "hazelcast/client/serialization/pimpl/compact/compact.h"

#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
#pragma warning(push)
Expand Down Expand Up @@ -225,6 +226,7 @@ class HAZELCAST_API hazelcast_client_instance_impl
client_config client_config_;
client_properties client_properties_;
spi::ClientContext client_context_;
serialization::pimpl::default_schema_service schema_service_;
serialization::pimpl::SerializationService serialization_service_;
std::shared_ptr<connection::ClientConnectionManagerImpl>
connection_manager_;
Expand Down
75 changes: 75 additions & 0 deletions hazelcast/include/hazelcast/client/protocol/ClientMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
#include <ostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <iterator>
#include <algorithm>

#include <boost/endian/arithmetic.hpp>
#include <boost/endian/conversion.hpp>
Expand All @@ -47,6 +50,8 @@
#include "hazelcast/client/sql/impl/sql_error.h"
#include "hazelcast/client/sql/sql_column_type.h"
#include "hazelcast/client/protocol/codec/builtin/custom_type_factory.h"
#include "hazelcast/client/serialization/pimpl/compact/schema.h"
#include "hazelcast/client/serialization/pimpl/compact/field_descriptor.h"

namespace hazelcast {
namespace util {
Expand Down Expand Up @@ -535,6 +540,29 @@ class HAZELCAST_API ClientMessage
return result;
}

template<typename T>
typename std::enable_if<
std::is_same<
T,
std::unordered_set<typename T::value_type, typename T::hasher>>::value,
T>::type
get()
{
T result;

auto f = reinterpret_cast<frame_header_type*>(
rd_ptr(SIZE_OF_FRAME_LENGTH_AND_FLAGS));
auto content_length =
static_cast<int32_t>(f->frame_len) - SIZE_OF_FRAME_LENGTH_AND_FLAGS;
size_t item_count =
content_length / ClientMessage::get_sizeof<typename T::value_type>();
for (size_t i = 0; i < item_count; ++i) {
result.emplace(get<typename T::value_type>());
}

return result;
}

template<typename T>
typename std::enable_if<
std::is_same<T, std::vector<typename T::value_type>>::value &&
Expand Down Expand Up @@ -1207,6 +1235,12 @@ class HAZELCAST_API ClientMessage
header->flags = is_final ? IS_FINAL_FLAG : DEFAULT_FLAGS;
std::memcpy(
fp + SIZE_OF_FRAME_LENGTH_AND_FLAGS, &bytes[0], bytes.size());

const auto& replicated_schemas = value.schemas_will_be_replicated();

copy(begin(replicated_schemas),
end(replicated_schemas),
back_inserter(schemas_will_be_replicated_));
}

inline void set(const serialization::pimpl::data* value,
Expand Down Expand Up @@ -1284,6 +1318,44 @@ class HAZELCAST_API ClientMessage
add_end_frame(is_final);
}

void set(const serialization::pimpl::field_descriptor& descriptor,
const std::string& field_name,
bool is_final = false)
{
add_begin_frame();

set(frame_header_type{ SIZE_OF_FRAME_LENGTH_AND_FLAGS + INT32_SIZE,
DEFAULT_FLAGS });
set(int32_t(descriptor.kind));
set(field_name);

add_end_frame(is_final);
}

void set(const serialization::pimpl::schema& s, bool is_final = false)
{
add_begin_frame();

set(s.type_name());

{ // Fields list
add_begin_frame();

for (const auto& p : s.fields()) {
const std::string& field_name{ p.first };
const serialization::pimpl::field_descriptor& descriptor{
p.second
};

set(descriptor, field_name, false);
}

add_end_frame(false);
}

add_end_frame(is_final);
}

//----- Setter methods end ---------------------

//----- utility methods -------------------
Expand Down Expand Up @@ -1342,6 +1414,8 @@ class HAZELCAST_API ClientMessage
}

void fast_forward_to_end_frame();
const std::vector<serialization::pimpl::schema>&
schemas_will_be_replicated() const;

static const frame_header_type& null_frame();
static const frame_header_type& begin_frame();
Expand Down Expand Up @@ -1446,6 +1520,7 @@ class HAZELCAST_API ClientMessage
std::vector<std::vector<byte>> data_buffer_;
size_t buffer_index_{ 0 };
size_t offset_{ 0 };
std::vector<serialization::pimpl::schema> schemas_will_be_replicated_;
};

template<>
Expand Down
76 changes: 76 additions & 0 deletions hazelcast/include/hazelcast/client/serialization/field_kind.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2008-2022, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <ostream>

namespace hazelcast {
namespace client {
namespace serialization {

enum class HAZELCAST_API field_kind
{
BOOLEAN = 1,
ARRAY_OF_BOOLEAN = 2,
INT8 = 3,
ARRAY_OF_INT8 = 4,
INT16 = 7,
ARRAY_OF_INT16 = 8,
INT32 = 9,
ARRAY_OF_INT32 = 10,
INT64 = 11,
ARRAY_OF_INT64 = 12,
FLOAT32 = 13,
ARRAY_OF_FLOAT32 = 14,
FLOAT64 = 15,
ARRAY_OF_FLOAT64 = 16,
STRING = 17,
ARRAY_OF_STRING = 18,
DECIMAL = 19,
ARRAY_OF_DECIMAL = 20,
TIME = 21,
ARRAY_OF_TIME = 22,
DATE = 23,
ARRAY_OF_DATE = 24,
TIMESTAMP = 25,
ARRAY_OF_TIMESTAMP = 26,
TIMESTAMP_WITH_TIMEZONE = 27,
ARRAY_OF_TIMESTAMP_WITH_TIMEZONE = 28,
COMPACT = 29,
ARRAY_OF_COMPACT = 30,
NULLABLE_BOOLEAN = 33,
ARRAY_OF_NULLABLE_BOOLEAN = 34,
NULLABLE_INT8 = 35,
ARRAY_OF_NULLABLE_INT8 = 36,
NULLABLE_INT16 = 37,
ARRAY_OF_NULLABLE_INT16 = 38,
NULLABLE_INT32 = 39,
ARRAY_OF_NULLABLE_INT32 = 40,
NULLABLE_INT64 = 41,
ARRAY_OF_NULLABLE_INT64 = 42,
NULLABLE_FLOAT32 = 43,
ARRAY_OF_NULLABLE_FLOAT32 = 44,
NULLABLE_FLOAT64 = 45,
ARRAY_OF_NULLABLE_FLOAT64 = 46
};

std::ostream HAZELCAST_API&
operator<<(std::ostream&, field_kind);

} // namespace serialization
} // namespace client
} // namespace hazelcast
Loading