Skip to content

Commit

Permalink
Remove singleton usage of SerializationConstants to avoid thread safe…
Browse files Browse the repository at this point in the history
…ty issues
  • Loading branch information
sancar committed Feb 3, 2016
1 parent a77f7e9 commit 1e8fdea
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,12 @@ namespace hazelcast {
template<typename T>
boost::shared_ptr<T> readObject() {
int typeId = readInt();
pimpl::SerializationConstants& constants = pimpl::SerializationConstants::getInstance();
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);
pimpl::SerializationConstants::checkClassType(getTypeId(*result) , typeId);
constants.checkClassType(getTypeId(*result) , typeId);
if (constants.CONSTANT_TYPE_DATA == typeId) {
readDataSerializable(reinterpret_cast<IdentifiedDataSerializable *>(result.get()));
} else if (constants.CONSTANT_TYPE_PORTABLE == typeId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ namespace hazelcast {
if (isEmpty) return;

if (NULL == object) {
writeInt(constants.CONSTANT_TYPE_NULL);
writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
} else {
writeInt(constants.CONSTANT_TYPE_PORTABLE);
writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_PORTABLE);

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

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

if (NULL == serializable) {
writeInt(constants.CONSTANT_TYPE_NULL);
writeInt(pimpl::SerializationConstants::CONSTANT_TYPE_NULL);
} else {
const T *object = static_cast<const T *>(serializable);
int type = getTypeId(*object);
Expand All @@ -231,7 +231,6 @@ namespace hazelcast {
pimpl::DataOutput *dataOutput;
pimpl::PortableContext *context;
pimpl::SerializerHolder *serializerHolder;
pimpl::SerializationConstants& constants;
bool isEmpty;

size_t position();
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,43 +39,40 @@ namespace hazelcast {
namespace pimpl {
class HAZELCAST_API SerializationConstants {
public:
int const CONSTANT_TYPE_NULL;
int const CONSTANT_TYPE_PORTABLE;
int const CONSTANT_TYPE_DATA;
int const CONSTANT_TYPE_BYTE;
int const CONSTANT_TYPE_BOOLEAN;
int const CONSTANT_TYPE_CHAR;
int const CONSTANT_TYPE_SHORT;
int const CONSTANT_TYPE_INTEGER;
int const CONSTANT_TYPE_LONG;
int const CONSTANT_TYPE_FLOAT;
int const CONSTANT_TYPE_DOUBLE;
int const CONSTANT_TYPE_STRING;
int const CONSTANT_TYPE_BYTE_ARRAY;
int const CONSTANT_TYPE_BOOLEAN_ARRAY;
int const CONSTANT_TYPE_CHAR_ARRAY;
int const CONSTANT_TYPE_SHORT_ARRAY;
int const CONSTANT_TYPE_INTEGER_ARRAY;
int const CONSTANT_TYPE_LONG_ARRAY;
int const CONSTANT_TYPE_FLOAT_ARRAY;
int const CONSTANT_TYPE_DOUBLE_ARRAY;
int const CONSTANT_TYPE_STRING_ARRAY;
// ------------------------------------------------------------
SerializationConstants();

std::string typeIdToName(int typeId);
static int const CONSTANT_TYPE_NULL;
static int const CONSTANT_TYPE_PORTABLE;
static int const CONSTANT_TYPE_DATA;
static int const CONSTANT_TYPE_BYTE;
static int const CONSTANT_TYPE_BOOLEAN;
static int const CONSTANT_TYPE_CHAR;
static int const CONSTANT_TYPE_SHORT;
static int const CONSTANT_TYPE_INTEGER;
static int const CONSTANT_TYPE_LONG;
static int const CONSTANT_TYPE_FLOAT;
static int const CONSTANT_TYPE_DOUBLE;
static int const CONSTANT_TYPE_STRING;
static int const CONSTANT_TYPE_BYTE_ARRAY;
static int const CONSTANT_TYPE_BOOLEAN_ARRAY;
static int const CONSTANT_TYPE_CHAR_ARRAY;
static int const CONSTANT_TYPE_SHORT_ARRAY;
static int const CONSTANT_TYPE_INTEGER_ARRAY;
static int const CONSTANT_TYPE_LONG_ARRAY;
static int const CONSTANT_TYPE_FLOAT_ARRAY;
static int const CONSTANT_TYPE_DOUBLE_ARRAY;
static int const CONSTANT_TYPE_STRING_ARRAY;

// ------------------------------------------------------------

static void checkClassType(int expectedType, int currentType);
void checkClassType(int expectedType, int currentType) const;

static SerializationConstants& getInstance();
private:
const int size;
std::vector<std::string> typeIdNameVector;

static SerializationConstants *instance;

SerializationConstants();

int idToIndex(int id);
int idToIndex(int id) const;
std::string typeIdToName(int typeId) const;
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ namespace hazelcast {

SerializationService &operator = (const SerializationService &);

SerializationConstants constants;
PortableContext portableContext;
SerializationConstants& constants;
const SerializationConfig& serializationConfig;

bool isNullData(const Data &data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ namespace hazelcast {
: dataOutput(&dataOutput)
, context(&portableContext)
, serializerHolder(&portableContext.getSerializerHolder())
, constants(pimpl::SerializationConstants::getInstance())
, isEmpty(false) {

}
Expand All @@ -40,7 +39,6 @@ namespace hazelcast {
: dataOutput(NULL)
, context(NULL)
, serializerHolder(NULL)
, constants(pimpl::SerializationConstants::getInstance())
, isEmpty(true) {

}
Expand Down
4 changes: 2 additions & 2 deletions hazelcast/src/hazelcast/client/serialization/TypeIDS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ namespace hazelcast {
namespace client {
namespace serialization {
int getTypeId(const Portable& portable) {
return pimpl::SerializationConstants::getInstance().CONSTANT_TYPE_PORTABLE;
return pimpl::SerializationConstants::CONSTANT_TYPE_PORTABLE;
}

int getTypeId(const IdentifiedDataSerializable& identifiedDataSerializable) {
return pimpl::SerializationConstants::getInstance().CONSTANT_TYPE_DATA;
return pimpl::SerializationConstants::CONSTANT_TYPE_DATA;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ namespace hazelcast {

int Data::getType() const {
if (totalSize() == 0) {
return SerializationConstants::getInstance().CONSTANT_TYPE_NULL;
return SerializationConstants::CONSTANT_TYPE_NULL;
}
return Bits::readIntB(*data, Data::TYPE_OFFSET);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ namespace hazelcast {
namespace client {
namespace serialization {
namespace pimpl {
PortableContext::PortableContext(int version)
: contextVersion(version), serializerHolder(*this){
PortableContext::PortableContext(int version, const SerializationConstants& constants)
: contextVersion(version), serializerHolder(*this) , constants(constants){
}


Expand Down Expand Up @@ -144,6 +144,11 @@ namespace hazelcast {
return serializerHolder;
}


SerializationConstants const& PortableContext::getConstants() const {
return constants;
}

ClassDefinitionContext& PortableContext::getClassDefinitionContext(int factoryId) {
boost::shared_ptr<ClassDefinitionContext> value = classDefContextMap.get(factoryId);
if (value == NULL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,30 @@ namespace hazelcast {
namespace client {
namespace serialization {
namespace pimpl {
SerializationConstants *SerializationConstants::instance = &getInstance();
int const SerializationConstants::CONSTANT_TYPE_NULL = 0;
int const SerializationConstants::CONSTANT_TYPE_PORTABLE=-1;
int const SerializationConstants::CONSTANT_TYPE_DATA=-2;
int const SerializationConstants::CONSTANT_TYPE_BYTE=-3;
int const SerializationConstants::CONSTANT_TYPE_BOOLEAN=-4;
int const SerializationConstants::CONSTANT_TYPE_CHAR=-5;
int const SerializationConstants::CONSTANT_TYPE_SHORT=-6;
int const SerializationConstants::CONSTANT_TYPE_INTEGER=-7;
int const SerializationConstants::CONSTANT_TYPE_LONG=-8;
int const SerializationConstants::CONSTANT_TYPE_FLOAT=-9;
int const SerializationConstants::CONSTANT_TYPE_DOUBLE=-10;
int const SerializationConstants::CONSTANT_TYPE_STRING=-11;
int const SerializationConstants::CONSTANT_TYPE_BYTE_ARRAY=-12;
int const SerializationConstants::CONSTANT_TYPE_BOOLEAN_ARRAY=-13;
int const SerializationConstants::CONSTANT_TYPE_CHAR_ARRAY=-14;
int const SerializationConstants::CONSTANT_TYPE_SHORT_ARRAY=-15;
int const SerializationConstants::CONSTANT_TYPE_INTEGER_ARRAY=-16;
int const SerializationConstants::CONSTANT_TYPE_LONG_ARRAY=-17;
int const SerializationConstants::CONSTANT_TYPE_FLOAT_ARRAY=-18;
int const SerializationConstants::CONSTANT_TYPE_DOUBLE_ARRAY=-19;
int const SerializationConstants::CONSTANT_TYPE_STRING_ARRAY=-20;

SerializationConstants::SerializationConstants()
: CONSTANT_TYPE_NULL(0)
, CONSTANT_TYPE_PORTABLE(-1)
, CONSTANT_TYPE_DATA(-2)
, CONSTANT_TYPE_BYTE(-3)
, CONSTANT_TYPE_BOOLEAN(-4)
, CONSTANT_TYPE_CHAR(-5)
, CONSTANT_TYPE_SHORT(-6)
, CONSTANT_TYPE_INTEGER(-7)
, CONSTANT_TYPE_LONG(-8)
, CONSTANT_TYPE_FLOAT(-9)
, CONSTANT_TYPE_DOUBLE(-10)
, CONSTANT_TYPE_STRING(-11)
, CONSTANT_TYPE_BYTE_ARRAY(-12)
, CONSTANT_TYPE_BOOLEAN_ARRAY(-13)
, CONSTANT_TYPE_CHAR_ARRAY(-14)
, CONSTANT_TYPE_SHORT_ARRAY(-15)
, CONSTANT_TYPE_INTEGER_ARRAY(-16)
, CONSTANT_TYPE_LONG_ARRAY(-17)
, CONSTANT_TYPE_FLOAT_ARRAY(-18)
, CONSTANT_TYPE_DOUBLE_ARRAY(-19)
, CONSTANT_TYPE_STRING_ARRAY(-20)
, size(21)
: size(21)
, typeIdNameVector(size){
typeIdNameVector[idToIndex(CONSTANT_TYPE_NULL)] = "null";
typeIdNameVector[idToIndex(CONSTANT_TYPE_PORTABLE)] = "portable";
Expand All @@ -75,36 +74,29 @@ namespace hazelcast {
typeIdNameVector[idToIndex(CONSTANT_TYPE_STRING_ARRAY)] = "stringArray";
}

std::string SerializationConstants::typeIdToName(int typeId) {
std::string SerializationConstants::typeIdToName(int typeId) const{
int i = idToIndex(typeId);
if (i < 0 || i >= size)
return std::string("custom");
return typeIdNameVector[i];
}

void SerializationConstants::checkClassType(int expectedType, int currentType) {
void SerializationConstants::checkClassType(int expectedType, int currentType) const{
if (expectedType != currentType) {
char message[200];
util::snprintf(message, 200, "Received data of type %s(%d) but expected data type %s(%d)",

instance->typeIdToName(currentType).c_str(), currentType,
instance->typeIdToName(expectedType).c_str(), expectedType);
typeIdToName(currentType).c_str(), currentType,
typeIdToName(expectedType).c_str(), expectedType);

util::ILogger::getLogger().severe(message);
throw exception::IClassCastException("SerializationConstants::checkClassType",message);
}
}

int SerializationConstants::idToIndex(int id) {
int SerializationConstants::idToIndex(int id) const{
return id + size - 1;
}

SerializationConstants& SerializationConstants::getInstance() {
if (NULL == instance) {
instance = new SerializationConstants();
}
return *instance;
}
}
}
}
Expand Down
Loading

0 comments on commit 1e8fdea

Please sign in to comment.