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 @@ -15,3 +15,4 @@
#
add_subdirectory(identified-data-serializable)
add_subdirectory(portable)
add_subdirectory(custom)
17 changes: 17 additions & 0 deletions examples/serialization/custom/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright (c) 2008-2015, 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(custom ./main.cpp)

89 changes: 89 additions & 0 deletions examples/serialization/custom/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2008-2015, 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/HazelcastClient.h>
#include <hazelcast/client/serialization/IdentifiedDataSerializable.h>
#include <hazelcast/client/serialization/ObjectDataInput.h>
#include <hazelcast/client/serialization/ObjectDataOutput.h>

class Person {
public:
Person() {
}

Person(const std::string& n) : name(n) {
}

void setName(const std::string& n) {
name = n;
}


const std::string& getName() const {
return name;
}

private:
std::string name;
};

int getHazelcastTypeId(const Person* p){
return 666;
}

class CustomSerializer : public hazelcast::client::serialization::Serializer<Person> {
public:

void write(hazelcast::client::serialization::ObjectDataOutput & out, const Person& object) {
out.writeInt(666);
out.writeUTF(&(object.getName()));
out.writeInt(666);
}

void read(hazelcast::client::serialization::ObjectDataInput & in, Person& object) {
int i = in.readInt();
assert(i == 666);
object.setName(*(in.readUTF()));
i = in.readInt();
assert(i == 666);
}

int getHazelcastTypeId() const {
return 666;
};
};

std::ostream &operator<<(std::ostream &out, const Person &p) {
const std::string & str = p.getName();
out << str;
return out;
}

int main() {
hazelcast::client::ClientConfig config;
hazelcast::client::SerializationConfig serializationConfig;
serializationConfig.registerSerializer(boost::shared_ptr<hazelcast::client::serialization::SerializerBase>(new CustomSerializer()));
config.setSerializationConfig(serializationConfig);
hazelcast::client::HazelcastClient hz(config);

hazelcast::client::IMap<std::string, Person> map = hz.getMap<std::string, Person>("map");
Person testPerson("bar");
map.put("foo", testPerson);
std::cout << *(map.get("foo")) << std::endl;
std::cout << "Finished" << std::endl;

return 0;
}

Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ namespace hazelcast {
/**
* Destructor
*/
virtual ~IdentifiedDataSerializable();
virtual ~IdentifiedDataSerializable(){

}

/**
* @return factory id
Expand All @@ -68,12 +70,6 @@ namespace hazelcast {
*/
virtual void readData(ObjectDataInput &reader) = 0;

/**
* Not public api. Do not override this method.
* @return serializer id
*/
virtual int getSerializerId() const;

};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "hazelcast/client/exception/HazelcastSerializationException.h"
#include "hazelcast/client/serialization/pimpl/SerializationConstants.h"
#include "hazelcast/util/IOUtil.h"
#include "hazelcast/client/serialization/TypeIDS.h"
#include <vector>
#include <boost/shared_ptr.hpp>
#include <string>
Expand Down Expand Up @@ -196,13 +197,15 @@ namespace hazelcast {
template<typename T>
boost::shared_ptr<T> readObject() {
int typeId = readInt();
if (pimpl::SerializationConstants::getInstance()->CONSTANT_TYPE_NULL == typeId) {
const pimpl::SerializationConstants& constants = portableContext.getConstants();
if (constants.CONSTANT_TYPE_NULL == typeId) {
return boost::shared_ptr<T>(static_cast<T *>(NULL));
} else {
std::auto_ptr<T> result(new T);
if (pimpl::SerializationConstants::getInstance()->CONSTANT_TYPE_DATA == typeId) {
constants.checkClassType(getHazelcastTypeId(result.get()) , typeId);
if (constants.CONSTANT_TYPE_DATA == typeId) {
readDataSerializable(reinterpret_cast<IdentifiedDataSerializable *>(result.get()));
} else if (pimpl::SerializationConstants::getInstance()->CONSTANT_TYPE_PORTABLE == typeId) {
} else if (constants.CONSTANT_TYPE_PORTABLE == typeId) {
readPortable(reinterpret_cast<Portable *>(result.get()));
} else {
readInternal<T>(typeId, result.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "hazelcast/client/serialization/pimpl/PortableContext.h"
#include "hazelcast/client/serialization/Serializer.h"
#include "hazelcast/util/IOUtil.h"
#include "hazelcast/client/serialization/TypeIDS.h"

#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
#pragma warning(push)
Expand Down Expand Up @@ -168,9 +169,9 @@ namespace hazelcast {
if (isEmpty) return;

if (NULL == object) {
writeInt(pimpl::SerializationConstants::getInstance()->CONSTANT_TYPE_NULL);
writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
} else {
writeInt(pimpl::SerializationConstants::getInstance()->CONSTANT_TYPE_PORTABLE);
writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_PORTABLE);

writeInt(object->getFactoryId());
writeInt(object->getClassId());
Expand All @@ -189,9 +190,9 @@ namespace hazelcast {
if (isEmpty) return;

if (NULL == object) {
writeInt(pimpl::SerializationConstants::getInstance()->CONSTANT_TYPE_NULL);
writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
} else {
writeInt(pimpl::SerializationConstants::getInstance()->CONSTANT_TYPE_DATA);
writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_DATA);
context->getSerializerHolder().getDataSerializer().write(*this, *object);
}
}
Expand All @@ -206,10 +207,10 @@ namespace hazelcast {
if (isEmpty) return;

if (NULL == serializable) {
writeInt(pimpl::SerializationConstants::getInstance()->CONSTANT_TYPE_NULL);
writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
} else {
const T *object = static_cast<const T *>(serializable);
int type = object->getTypeId();
int type = getHazelcastTypeId(object);
writeInt(type);

boost::shared_ptr<SerializerBase> serializer = context->getSerializerHolder().serializerFor(type);
Expand Down
9 changes: 3 additions & 6 deletions hazelcast/include/hazelcast/client/serialization/Portable.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ namespace hazelcast {
/**
* Destructor
*/
virtual ~Portable();
virtual ~Portable(){

}

/**
* @return factory id
Expand All @@ -78,11 +80,6 @@ namespace hazelcast {
*/
virtual void readPortable(PortableReader& reader) = 0;

/**
* Not public api. Do not override this method.
* @return serializer id
*/
virtual int getSerializerId() const;
};
}
}
Expand Down
24 changes: 15 additions & 9 deletions hazelcast/include/hazelcast/client/serialization/Serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
//
// Created by sancar koyunlu on 6/7/13.




#ifndef HAZELCAST_TYPE_SERIALIZER
#define HAZELCAST_TYPE_SERIALIZER

Expand All @@ -43,13 +40,14 @@ namespace hazelcast {

/**
* unique type id for this serializer. It will be used to decide which serializer needs to be used
* for your classes. Also not that your serialized classes needs to implement
* for your classes. Also not that for your serialized classes you need to implement as free function
* in same namespace with your class
*
* int getTypeId();
* int getHazelcastTypeId(const MyClass*);
*
* which should return same id with its serializer.
*/
virtual int getTypeId() const = 0;
virtual int getHazelcastTypeId() const = 0;
};

/**
Expand All @@ -64,7 +62,7 @@ namespace hazelcast {

void read(serialization::ObjectDataInput & in, ExampleBaseClass& object);

int getTypeId() const;
int getHazelcastTypeId() const;

};
}
Expand All @@ -86,16 +84,24 @@ namespace hazelcast {
//.....
}

int getTypeId() const {
int getHazelcastTypeId() const {
//..
}
};

*
* Along with serializer following function should be provided with same namespace that ExampleBaseClass
* belongs to
*
* int getHazelcastTypeId(const MyClass*);
*
* which should return same id with its serializer.
*
* User than can register serializer via SerializationConfig as follows
*

clientConfig.getSerializationConfig().registerSerializer(new MyCustomSerializer());
clientConfig.getSerializationConfig().registerSerializer(
boost::shared_ptr<hazelcast::client::serialization::SerializerBase>(new MyCustomSerializer());

*/
template <typename Serializable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,25 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Created by sancar koyunlu on 8/12/13.

#ifndef HAZELCAST_TypeIDS
#define HAZELCAST_TypeIDS


#include "hazelcast/client/serialization/Portable.h"
#include "hazelcast/client/serialization/pimpl/SerializationConstants.h"
#include "hazelcast/util/HazelcastDll.h"

namespace hazelcast {
namespace client {
namespace serialization {
Portable::~Portable() {
class Portable;
class IdentifiedDataSerializable;

int HAZELCAST_API getHazelcastTypeId(const Portable* portable);

}
int HAZELCAST_API getHazelcastTypeId(const IdentifiedDataSerializable* identifiedDataSerializable);

int Portable::getSerializerId() const {
return pimpl::SerializationConstants::getInstance()->CONSTANT_TYPE_PORTABLE;
}
}
}
}


#endif //HAZELCAST_TypeIDS
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ namespace hazelcast {

class ClassDefinitionContext;

class SerializationConstants;

class HAZELCAST_API PortableContext {
public:

PortableContext(int);
PortableContext(int version,const SerializationConstants& constants);

int getClassVersion(int factoryId, int classId);

Expand All @@ -69,6 +71,9 @@ namespace hazelcast {

SerializerHolder &getSerializerHolder();


SerializationConstants const& getConstants() const;

private:

PortableContext(const PortableContext &);
Expand All @@ -80,7 +85,7 @@ namespace hazelcast {
int contextVersion;
util::SynchronizedMap<int, ClassDefinitionContext> classDefContextMap;
SerializerHolder serializerHolder;

const SerializationConstants& constants;
};
}
}
Expand Down
Loading