http://git-wip-us.apache.org/repos/asf/geode-native/blob/da389793/src/cppcache/src/PdxInstanceImpl.cpp ---------------------------------------------------------------------- diff --git a/src/cppcache/src/PdxInstanceImpl.cpp b/src/cppcache/src/PdxInstanceImpl.cpp index 401db41..5ce5e43 100644 --- a/src/cppcache/src/PdxInstanceImpl.cpp +++ b/src/cppcache/src/PdxInstanceImpl.cpp @@ -23,7 +23,6 @@ #include <geode/PdxReader.hpp> #include "CacheRegionHelper.hpp" #include <geode/Cache.hpp> -#include "CacheImpl.hpp" #include "Utils.hpp" #include <algorithm> @@ -78,23 +77,21 @@ PdxInstanceImpl::~PdxInstanceImpl() { GF_SAFE_DELETE_ARRAY(m_buffer); } PdxInstanceImpl::PdxInstanceImpl( apache::geode::client::FieldVsValues fieldVsValue, - apache::geode::client::PdxTypePtr pdxType) { - m_pdxType = pdxType; - m_updatedFields = fieldVsValue; - m_buffer = nullptr; - m_bufferLength = 0; - m_typeId = 0; - + apache::geode::client::PdxTypePtr pdxType, CachePerfStats* cacheStats, + PdxTypeRegistryPtr pdxTypeRegistry, const Cache* cache, + bool enableTimeStatistics) + : m_pdxType(pdxType), + m_updatedFields(fieldVsValue), + m_buffer(nullptr), + m_bufferLength(0), + m_typeId(0), + m_cacheStats(cacheStats), + m_pdxTypeRegistry(pdxTypeRegistry), + m_cache(cache), + m_enableTimeStatistics(enableTimeStatistics) { m_pdxType->InitializeType(); // to generate static position map } -PdxInstanceImpl::PdxInstanceImpl() { - m_pdxType = nullptr; - m_buffer = nullptr; - m_bufferLength = 0; - m_typeId = 0; -} - void PdxInstanceImpl::writeField(PdxWriterPtr writer, const char* fieldName, int typeId, CacheablePtr value) { switch (typeId) { @@ -302,8 +299,9 @@ WritablePdxInstancePtr PdxInstanceImpl::createWriter() { LOGDEBUG("PdxInstanceImpl::createWriter m_bufferLength = %d m_typeId = %d ", m_bufferLength, m_typeId); return std::make_shared<PdxInstanceImpl>( - m_buffer, m_bufferLength, - m_typeId); // need to create duplicate byte stream); + m_buffer, m_bufferLength, m_typeId, m_cacheStats, m_pdxTypeRegistry, + m_cache, + m_enableTimeStatistics); // need to create duplicate byte stream); } bool PdxInstanceImpl::enumerateObjectArrayEquals( @@ -702,7 +700,7 @@ int32_t PdxInstanceImpl::hashcode() const { std::vector<PdxFieldTypePtr> pdxIdentityFieldList = getIdentityPdxFields(pt); - DataInput dataInput(m_buffer, m_bufferLength); + auto dataInput = m_cache->createDataInput(m_buffer, m_bufferLength); for (uint32_t i = 0; i < pdxIdentityFieldList.size(); i++) { PdxFieldTypePtr pField = pdxIdentityFieldList.at(i); @@ -730,23 +728,23 @@ int32_t PdxInstanceImpl::hashcode() const { case PdxFieldTypes::DOUBLE_ARRAY: case PdxFieldTypes::STRING_ARRAY: case PdxFieldTypes::ARRAY_OF_BYTE_ARRAYS: { - int retH = getRawHashCode(pt, pField, dataInput); + int retH = getRawHashCode(pt, pField, *dataInput); if (retH != 0) hashCode = 31 * hashCode + retH; break; } case PdxFieldTypes::OBJECT: { - setOffsetForObject(dataInput, pt, pField->getSequenceId()); + setOffsetForObject(*dataInput, pt, pField->getSequenceId()); CacheablePtr object = nullptr; - dataInput.readObject(object); + dataInput->readObject(object); if (object != nullptr) { hashCode = 31 * hashCode + deepArrayHashCode(object); } break; } case PdxFieldTypes::OBJECT_ARRAY: { - setOffsetForObject(dataInput, pt, pField->getSequenceId()); + setOffsetForObject(*dataInput, pt, pField->getSequenceId()); CacheableObjectArrayPtr objectArray = CacheableObjectArray::create(); - objectArray->fromData(dataInput); + objectArray->fromData(*dataInput); hashCode = 31 * hashCode + ((objectArray != nullptr) ? deepArrayHashCode(objectArray) : 0); @@ -785,7 +783,7 @@ PdxTypePtr PdxInstanceImpl::getPdxType() const { } return m_pdxType; } - PdxTypePtr pType = PdxTypeRegistry::getPdxType(m_typeId); + auto pType = getPdxTypeRegistry()->getPdxType(m_typeId); return pType; } @@ -805,358 +803,180 @@ bool PdxInstanceImpl::hasField(const char* fieldname) { } void PdxInstanceImpl::getField(const char* fieldname, bool& value) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - - VERIFY_PDX_INSTANCE_FIELD_THROW; - - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - - dataInput.reset(); - dataInput.advanceCursor(pos); - dataInput.readBoolean(&value); + auto dataInput = getDataInputForField(fieldname); + dataInput->readBoolean(&value); } void PdxInstanceImpl::getField(const char* fieldname, signed char& value) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - - VERIFY_PDX_INSTANCE_FIELD_THROW; - - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - - dataInput.reset(); - dataInput.advanceCursor(pos); + auto dataInput = getDataInputForField(fieldname); int8_t tmp = 0; - dataInput.read(&tmp); + dataInput->read(&tmp); value = (signed char)tmp; } void PdxInstanceImpl::getField(const char* fieldname, unsigned char& value) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - - VERIFY_PDX_INSTANCE_FIELD_THROW; - - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - - dataInput.reset(); - dataInput.advanceCursor(pos); + auto dataInput = getDataInputForField(fieldname); int8_t tmp = 0; - dataInput.read(&tmp); + dataInput->read(&tmp); value = static_cast<unsigned char>(tmp); } void PdxInstanceImpl::getField(const char* fieldname, int16_t& value) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); - dataInput.readInt(&value); + auto dataInput = getDataInputForField(fieldname); + dataInput->readInt(&value); } void PdxInstanceImpl::getField(const char* fieldname, int32_t& value) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - - dataInput.reset(); - dataInput.advanceCursor(pos); - dataInput.readInt(&value); + auto dataInput = getDataInputForField(fieldname); + dataInput->readInt(&value); } void PdxInstanceImpl::getField(const char* fieldname, int64_t& value) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); - dataInput.readInt(&value); + auto dataInput = getDataInputForField(fieldname); + dataInput->readInt(&value); } void PdxInstanceImpl::getField(const char* fieldname, float& value) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); - dataInput.readFloat(&value); + auto dataInput = getDataInputForField(fieldname); + dataInput->readFloat(&value); } void PdxInstanceImpl::getField(const char* fieldname, double& value) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); - dataInput.readDouble(&value); + auto dataInput = getDataInputForField(fieldname); + dataInput->readDouble(&value); } void PdxInstanceImpl::getField(const char* fieldname, wchar_t& value) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); + auto dataInput = getDataInputForField(fieldname); uint16_t temp = 0; - dataInput.readInt(&temp); + dataInput->readInt(&temp); value = static_cast<wchar_t>(temp); } void PdxInstanceImpl::getField(const char* fieldname, char& value) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); + auto dataInput = getDataInputForField(fieldname); uint16_t temp = 0; - dataInput.readInt(&temp); + dataInput->readInt(&temp); value = static_cast<char>(temp); } void PdxInstanceImpl::getField(const char* fieldname, bool** value, int32_t& length) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); - dataInput.readBooleanArray(value, length); + auto dataInput = getDataInputForField(fieldname); + dataInput->readBooleanArray(value, length); } void PdxInstanceImpl::getField(const char* fieldname, signed char** value, int32_t& length) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); + auto dataInput = getDataInputForField(fieldname); int8_t* temp = nullptr; - dataInput.readByteArray(&temp, length); + dataInput->readByteArray(&temp, length); *value = (signed char*)temp; } void PdxInstanceImpl::getField(const char* fieldname, unsigned char** value, int32_t& length) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); + auto dataInput = getDataInputForField(fieldname); int8_t* temp = nullptr; - dataInput.readByteArray(&temp, length); + dataInput->readByteArray(&temp, length); *value = reinterpret_cast<unsigned char*>(temp); } void PdxInstanceImpl::getField(const char* fieldname, int16_t** value, int32_t& length) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); - dataInput.readShortArray(value, length); + auto dataInput = getDataInputForField(fieldname); + dataInput->readShortArray(value, length); } void PdxInstanceImpl::getField(const char* fieldname, int32_t** value, int32_t& length) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); - dataInput.readIntArray(value, length); + auto dataInput = getDataInputForField(fieldname); + dataInput->readIntArray(value, length); } void PdxInstanceImpl::getField(const char* fieldname, int64_t** value, int32_t& length) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); - dataInput.readLongArray(value, length); + auto dataInput = getDataInputForField(fieldname); + dataInput->readLongArray(value, length); } void PdxInstanceImpl::getField(const char* fieldname, float** value, int32_t& length) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); - dataInput.readFloatArray(value, length); + auto dataInput = getDataInputForField(fieldname); + dataInput->readFloatArray(value, length); } void PdxInstanceImpl::getField(const char* fieldname, double** value, int32_t& length) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); - dataInput.readDoubleArray(value, length); + auto dataInput = getDataInputForField(fieldname); + dataInput->readDoubleArray(value, length); } void PdxInstanceImpl::getField(const char* fieldname, wchar_t** value, int32_t& length) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); - dataInput.readWideCharArray(value, length); + auto dataInput = getDataInputForField(fieldname); + dataInput->readWideCharArray(value, length); } void PdxInstanceImpl::getField(const char* fieldname, char** value, int32_t& length) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); - dataInput.readCharArray(value, length); + auto dataInput = getDataInputForField(fieldname); + dataInput->readCharArray(value, length); } void PdxInstanceImpl::getField(const char* fieldname, wchar_t** value) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); + auto dataInput = getDataInputForField(fieldname); wchar_t* temp = nullptr; - dataInput.readWideString(&temp); + dataInput->readWideString(&temp); *value = temp; } void PdxInstanceImpl::getField(const char* fieldname, char** value) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); + auto dataInput = getDataInputForField(fieldname); char* temp = nullptr; - dataInput.readString(&temp); + dataInput->readString(&temp); *value = temp; } void PdxInstanceImpl::getField(const char* fieldname, wchar_t*** value, int32_t& length) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); - dataInput.readWideStringArray(value, length); + auto dataInput = getDataInputForField(fieldname); + dataInput->readWideStringArray(value, length); } void PdxInstanceImpl::getField(const char* fieldname, char*** value, int32_t& length) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); - dataInput.readStringArray(value, length); + auto dataInput = getDataInputForField(fieldname); + dataInput->readStringArray(value, length); } void PdxInstanceImpl::getField(const char* fieldname, CacheableDatePtr& value) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); + auto dataInput = getDataInputForField(fieldname); value = CacheableDate::create(); - value->fromData(dataInput); + value->fromData(*dataInput); } void PdxInstanceImpl::getField(const char* fieldname, CacheablePtr& value) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); - dataInput.readObject(value); + auto dataInput = getDataInputForField(fieldname); + dataInput->readObject(value); } void PdxInstanceImpl::getField(const char* fieldname, CacheableObjectArrayPtr& value) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); + auto dataInput = getDataInputForField(fieldname); value = CacheableObjectArray::create(); - value->fromData(dataInput); + value->fromData(*dataInput); } void PdxInstanceImpl::getField(const char* fieldname, int8_t*** value, int32_t& arrayLength, int32_t*& elementLength) const { - PdxTypePtr pt = getPdxType(); - PdxFieldTypePtr pft = pt->getPdxField(fieldname); - VERIFY_PDX_INSTANCE_FIELD_THROW; - DataInput dataInput(m_buffer, m_bufferLength); - int pos = getOffset(dataInput, pt, pft->getSequenceId()); - dataInput.reset(); - dataInput.advanceCursor(pos); - dataInput.readArrayOfByteArrays(value, arrayLength, &elementLength); + auto dataInput = getDataInputForField(fieldname); + dataInput->readArrayOfByteArrays(value, arrayLength, &elementLength); } CacheableStringPtr PdxInstanceImpl::toString() const { @@ -1418,26 +1238,21 @@ CacheableStringPtr PdxInstanceImpl::toString() const { } PdxSerializablePtr PdxInstanceImpl::getObject() { - DataInput dataInput(m_buffer, m_bufferLength); - int64_t sampleStartNanos = Utils::startStatOpTime(); + auto dataInput = m_cache->createDataInput(m_buffer, m_bufferLength); + int64_t sampleStartNanos = + m_enableTimeStatistics ? Utils::startStatOpTime() : 0; //[ToDo] do we have to call incPdxDeSerialization here? PdxSerializablePtr ret = - PdxHelper::deserializePdx(dataInput, true, m_typeId, m_bufferLength); - CachePtr cache = CacheFactory::getAnyInstance(); - if (cache == nullptr) { - throw IllegalStateException("cache has not been created yet."); - ; - } - if (cache->isClosed()) { - throw IllegalStateException("cache has been closed. "); - } - CacheImpl* cacheImpl = CacheRegionHelper::getCacheImpl(cache.get()); - if (cacheImpl != nullptr) { - Utils::updateStatOpTime( - cacheImpl->m_cacheStats->getStat(), - cacheImpl->m_cacheStats->getPdxInstanceDeserializationTimeId(), - sampleStartNanos); - cacheImpl->m_cacheStats->incPdxInstanceDeserializations(); + PdxHelper::deserializePdx(*dataInput, true, m_typeId, m_bufferLength); + + if (m_cacheStats != nullptr) { + if (m_enableTimeStatistics) { + Utils::updateStatOpTime( + m_cacheStats->getStat(), + m_cacheStats->getPdxInstanceDeserializationTimeId(), + sampleStartNanos); + } + m_cacheStats->incPdxInstanceDeserializations(); } return ret; } @@ -1502,8 +1317,8 @@ bool PdxInstanceImpl::operator==(const CacheableKey& other) const { equatePdxFields(myPdxIdentityFieldList, otherPdxIdentityFieldList); equatePdxFields(otherPdxIdentityFieldList, myPdxIdentityFieldList); - DataInput myDataInput(m_buffer, m_bufferLength); - DataInput otherDataInput(otherPdx->m_buffer, otherPdx->m_bufferLength); + auto myDataInput = m_cache->createDataInput(m_buffer, m_bufferLength); + auto otherDataInput = m_cache->createDataInput(otherPdx->m_buffer, otherPdx->m_bufferLength); int fieldTypeId = -1; for (size_t i = 0; i < myPdxIdentityFieldList.size(); i++) { @@ -1542,8 +1357,8 @@ bool PdxInstanceImpl::operator==(const CacheableKey& other) const { case PdxFieldTypes::DOUBLE_ARRAY: case PdxFieldTypes::STRING_ARRAY: case PdxFieldTypes::ARRAY_OF_BYTE_ARRAYS: { - if (!compareRawBytes(*otherPdx, myPdxType, myPFT, myDataInput, - otherPdxType, otherPFT, otherDataInput)) { + if (!compareRawBytes(*otherPdx, myPdxType, myPFT, *myDataInput, + otherPdxType, otherPFT, *otherDataInput)) { return false; } break; @@ -1552,14 +1367,14 @@ bool PdxInstanceImpl::operator==(const CacheableKey& other) const { CacheablePtr object = nullptr; CacheablePtr otherObject = nullptr; if (!myPFT->equals(m_DefaultPdxFieldType)) { - setOffsetForObject(myDataInput, myPdxType, myPFT->getSequenceId()); - myDataInput.readObject(object); + setOffsetForObject(*myDataInput, myPdxType, myPFT->getSequenceId()); + myDataInput->readObject(object); } if (!otherPFT->equals(m_DefaultPdxFieldType)) { - otherPdx->setOffsetForObject(otherDataInput, otherPdxType, + otherPdx->setOffsetForObject(*otherDataInput, otherPdxType, otherPFT->getSequenceId()); - otherDataInput.readObject(otherObject); + otherDataInput->readObject(otherObject); } if (object != nullptr) { @@ -1577,14 +1392,14 @@ bool PdxInstanceImpl::operator==(const CacheableKey& other) const { CacheableObjectArrayPtr objectArray = CacheableObjectArray::create(); if (!myPFT->equals(m_DefaultPdxFieldType)) { - setOffsetForObject(myDataInput, myPdxType, myPFT->getSequenceId()); - objectArray->fromData(myDataInput); + setOffsetForObject(*myDataInput, myPdxType, myPFT->getSequenceId()); + objectArray->fromData(*myDataInput); } if (!otherPFT->equals(m_DefaultPdxFieldType)) { - otherPdx->setOffsetForObject(otherDataInput, otherPdxType, + otherPdx->setOffsetForObject(*otherDataInput, otherPdxType, otherPFT->getSequenceId()); - otherObjectArray->fromData(otherDataInput); + otherObjectArray->fromData(*otherDataInput); } if (!deepArrayEquals(objectArray, otherObjectArray)) { return false; @@ -1711,7 +1526,7 @@ void PdxInstanceImpl::toData(PdxWriterPtr writer) /*const*/ { if (m_buffer != nullptr) { uint8_t* copy = apache::geode::client::DataInput::getBufferCopy( m_buffer, m_bufferLength); - DataInput dataInput(copy, m_bufferLength); // this will delete buffer + auto dataInput = m_cache->createDataInput(copy, m_bufferLength); for (size_t i = 0; i < pdxFieldList->size(); i++) { PdxFieldTypePtr currPf = pdxFieldList->at(i); LOGDEBUG("toData filedname = %s , isVarLengthType = %d ", @@ -1727,7 +1542,7 @@ void PdxInstanceImpl::toData(PdxWriterPtr writer) /*const*/ { } if (value != nullptr) { writeField(writer, currPf->getFieldName(), currPf->getTypeId(), value); - position = getNextFieldPosition(dataInput, static_cast<int>(i) + 1, pt); + position = getNextFieldPosition(*dataInput, static_cast<int>(i) + 1, pt); } else { if (currPf->IsVariableLengthType()) { // need to add offset @@ -1735,8 +1550,8 @@ void PdxInstanceImpl::toData(PdxWriterPtr writer) /*const*/ { } // write raw byte array... nextFieldPosition = - getNextFieldPosition(dataInput, static_cast<int>(i) + 1, pt); - writeUnmodifieldField(dataInput, position, nextFieldPosition, + getNextFieldPosition(*dataInput, static_cast<int>(i) + 1, pt); + writeUnmodifieldField(*dataInput, position, nextFieldPosition, std::static_pointer_cast<PdxLocalWriter>(writer)); position = nextFieldPosition; // mark next field; } @@ -1761,7 +1576,7 @@ void PdxInstanceImpl::fromData(PdxReaderPtr input) { const char* PdxInstanceImpl::getClassName() const { if (m_typeId != 0) { - PdxTypePtr pdxtype = PdxTypeRegistry::getPdxType(m_typeId); + auto pdxtype = getPdxTypeRegistry()->getPdxType(m_typeId); if (pdxtype == nullptr) { char excpStr[256] = {0}; ACE_OS::snprintf(excpStr, 256, @@ -2753,6 +2568,26 @@ uint32_t PdxInstanceImpl::objectSize() const { } return size; } + +PdxTypeRegistryPtr PdxInstanceImpl::getPdxTypeRegistry() const { + return m_pdxTypeRegistry; +} + +std::unique_ptr<DataInput> PdxInstanceImpl::getDataInputForField(const char* fieldname) const { + auto pt = getPdxType(); + auto pft = pt->getPdxField(fieldname); + + VERIFY_PDX_INSTANCE_FIELD_THROW; + + auto dataInput = m_cache->createDataInput(m_buffer, m_bufferLength); + auto pos = getOffset(*dataInput, pt, pft->getSequenceId()); + + dataInput->reset(); + dataInput->advanceCursor(pos); + + return dataInput; +} + } // namespace client } // namespace geode } // namespace apache
http://git-wip-us.apache.org/repos/asf/geode-native/blob/da389793/src/cppcache/src/PdxInstanceImpl.hpp ---------------------------------------------------------------------- diff --git a/src/cppcache/src/PdxInstanceImpl.hpp b/src/cppcache/src/PdxInstanceImpl.hpp index 825c89a..643400d 100644 --- a/src/cppcache/src/PdxInstanceImpl.hpp +++ b/src/cppcache/src/PdxInstanceImpl.hpp @@ -1,8 +1,3 @@ -#pragma once - -#ifndef GEODE_PDXINSTANCEIMPL_H_ -#define GEODE_PDXINSTANCEIMPL_H_ - /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -20,14 +15,22 @@ * limitations under the License. */ +#pragma once + +#ifndef GEODE_PDXINSTANCEIMPL_H_ +#define GEODE_PDXINSTANCEIMPL_H_ + +#include <vector> +#include <map> + #include <geode/PdxInstance.hpp> #include <geode/WritablePdxInstance.hpp> #include <geode/PdxSerializable.hpp> +#include <geode/PdxFieldTypes.hpp> + #include "PdxType.hpp" #include "PdxLocalWriter.hpp" -#include <geode/PdxFieldTypes.hpp> -#include <vector> -#include <map> +#include "PdxTypeRegistry.hpp" namespace apache { namespace geode { @@ -48,7 +51,7 @@ class CPPCACHE_EXPORT PdxInstanceImpl : public WritablePdxInstance { * registered. * @return the deserialized domain object. * - * @see Serializable::registerPdxType + * @see serializationRegistry->addPdxType */ virtual PdxSerializablePtr getObject(); @@ -402,7 +405,7 @@ class CPPCACHE_EXPORT PdxInstanceImpl : public WritablePdxInstance { * For deserialization C++ Native Client requires the domain class to be * registered. * - * @see Serializable::registerPdxType + * @see serializationRegistry->addPdxType * @see PdxInstance#hasField */ virtual void getField(const char* fieldname, CacheablePtr& value) const; @@ -418,7 +421,7 @@ class CPPCACHE_EXPORT PdxInstanceImpl : public WritablePdxInstance { * type. * @throws IllegalStateException if PdxInstance doesn't has the named field. * - * @see Serializable::registerPdxType + * @see serializationRegistry->addPdxType * @see PdxInstance#hasField */ virtual void getField(const char* fieldname, @@ -930,7 +933,7 @@ class CPPCACHE_EXPORT PdxInstanceImpl : public WritablePdxInstance { * @throws IllegalStateException if the field contains an element that is not * of CacheableKey derived type. * - * @see Serializable::registerPdxType + * @see serializationRegistry->addPdxType */ virtual int32_t hashcode() const; @@ -943,7 +946,7 @@ class CPPCACHE_EXPORT PdxInstanceImpl : public WritablePdxInstance { * For deserialization C++ Native Client requires the domain class to be * registered. * - * @see Serializable::registerPdxType + * @see serializationRegistry->addPdxType */ virtual CacheableStringPtr toString() const; @@ -1000,7 +1003,7 @@ class CPPCACHE_EXPORT PdxInstanceImpl : public WritablePdxInstance { * @throws IllegalStateException if the field contains an element that is not * of CacheableKey derived type. * - * @see Serializable::registerPdxType + * @see serializationRegistry->addPdxType */ virtual bool operator==(const CacheableKey& other) const; @@ -1047,32 +1050,46 @@ class CPPCACHE_EXPORT PdxInstanceImpl : public WritablePdxInstance { /** * @brief constructors */ - PdxInstanceImpl(); - PdxInstanceImpl(uint8_t* buffer, int length, int typeId) { - m_buffer = DataInput::getBufferCopy(buffer, length); - m_bufferLength = length; + PdxInstanceImpl(uint8_t* buffer, int length, int typeId, + CachePerfStats* cacheStats, + PdxTypeRegistryPtr pdxTypeRegistry, const Cache* cache, + bool enableTimeStatistics) + : m_buffer(DataInput::getBufferCopy(buffer, length)), + m_bufferLength(length), + m_typeId(typeId), + m_pdxType(nullptr), + m_cacheStats(cacheStats), + m_pdxTypeRegistry(pdxTypeRegistry), + m_cache(cache), + m_enableTimeStatistics(enableTimeStatistics) { LOGDEBUG("PdxInstanceImpl::m_bufferLength = %d ", m_bufferLength); - m_typeId = typeId; - m_pdxType = nullptr; } - PdxInstanceImpl(FieldVsValues fieldVsValue, PdxTypePtr pdxType); + PdxInstanceImpl(FieldVsValues fieldVsValue, PdxTypePtr pdxType, + CachePerfStats* cacheStats, + PdxTypeRegistryPtr pdxTypeRegistry, const Cache* cache, + bool enableTimeStatistics); + + PdxInstanceImpl(const PdxInstanceImpl& other) = delete; + + void operator=(const PdxInstanceImpl& other) = delete; PdxTypePtr getPdxType() const; void updatePdxStream(uint8_t* newPdxStream, int len); private: - // never implemented. - PdxInstanceImpl(const PdxInstanceImpl& other); - void operator=(const PdxInstanceImpl& other); - uint8_t* m_buffer; int m_bufferLength; int m_typeId; PdxTypePtr m_pdxType; FieldVsValues m_updatedFields; + CachePerfStats* m_cacheStats; + + PdxTypeRegistryPtr m_pdxTypeRegistry; + const Cache* m_cache; + bool m_enableTimeStatistics; std::vector<PdxFieldTypePtr> getIdentityPdxFields(PdxTypePtr pt) const; @@ -1109,6 +1126,8 @@ class CPPCACHE_EXPORT PdxInstanceImpl : public WritablePdxInstance { void equatePdxFields(std::vector<PdxFieldTypePtr>& my, std::vector<PdxFieldTypePtr>& other) const; + PdxTypeRegistryPtr getPdxTypeRegistry() const; + static int deepArrayHashCode(CacheablePtr obj); static int enumerateMapHashCode(CacheableHashMapPtr map); @@ -1150,6 +1169,8 @@ class CPPCACHE_EXPORT PdxInstanceImpl : public WritablePdxInstance { static bool enumerateHashTableEquals(CacheableHashTablePtr Obj, CacheableHashTablePtr OtherObj); + std::unique_ptr<DataInput> getDataInputForField(const char* fieldname) const; + static int8_t m_BooleanDefaultBytes[]; static int8_t m_ByteDefaultBytes[]; static int8_t m_CharDefaultBytes[]; http://git-wip-us.apache.org/repos/asf/geode-native/blob/da389793/src/cppcache/src/PdxLocalReader.cpp ---------------------------------------------------------------------- diff --git a/src/cppcache/src/PdxLocalReader.cpp b/src/cppcache/src/PdxLocalReader.cpp index 425f629..e32cf66 100644 --- a/src/cppcache/src/PdxLocalReader.cpp +++ b/src/cppcache/src/PdxLocalReader.cpp @@ -27,7 +27,7 @@ namespace apache { namespace geode { namespace client { -PdxLocalReader::PdxLocalReader() +PdxLocalReader::PdxLocalReader(PdxTypeRegistryPtr pdxTypeRegistry) : m_dataInput(nullptr), m_startBuffer(nullptr), m_startPosition(0), @@ -38,20 +38,21 @@ PdxLocalReader::PdxLocalReader() m_isDataNeedToPreserve(false), m_localToRemoteMap(nullptr), m_remoteToLocalMap(nullptr), - m_remoteToLocalMapSize(0) {} + m_remoteToLocalMapSize(0), + m_pdxTypeRegistry(pdxTypeRegistry) {} PdxLocalReader::PdxLocalReader(DataInput& input, PdxTypePtr remoteType, - int32_t pdxLen) { - m_dataInput = &input; - m_pdxType = remoteType; - m_serializedLengthWithOffsets = pdxLen; - - m_localToRemoteMap = remoteType->getLocalToRemoteMap(); - m_remoteToLocalMap = remoteType->getRemoteToLocalMap(); - m_remoteToLocalMapSize = remoteType->getTotalFields(); - - m_pdxRemotePreserveData = std::make_shared<PdxRemotePreservedData>(); - m_isDataNeedToPreserve = true; + int32_t pdxLen, + PdxTypeRegistryPtr pdxTypeRegistry) + : m_dataInput(&input), + m_pdxType(remoteType), + m_serializedLengthWithOffsets(pdxLen), + m_localToRemoteMap(remoteType->getLocalToRemoteMap()), + m_remoteToLocalMap(remoteType->getRemoteToLocalMap()), + m_remoteToLocalMapSize(remoteType->getTotalFields()), + m_pdxRemotePreserveData(std::make_shared<PdxRemotePreservedData>()), + m_isDataNeedToPreserve(true), + m_pdxTypeRegistry(pdxTypeRegistry) { initialize(); } @@ -303,8 +304,9 @@ PdxRemotePreservedDataPtr PdxLocalReader::getPreservedData( LOGDEBUG( "PdxLocalReader::getPreservedData::nFieldExtra = %d AND " "PdxTypeRegistry::getPdxIgnoreUnreadFields = %d ", - nFieldExtra, PdxTypeRegistry::getPdxIgnoreUnreadFields()); - if (nFieldExtra > 0 && PdxTypeRegistry::getPdxIgnoreUnreadFields() == false) { + nFieldExtra, m_pdxTypeRegistry->getPdxIgnoreUnreadFields()); + if (nFieldExtra > 0 && + m_pdxTypeRegistry->getPdxIgnoreUnreadFields() == false) { m_pdxRemotePreserveData->initialize( m_pdxType != nullptr ? m_pdxType->getTypeId() : 0, mergedVersion->getTypeId(), nFieldExtra, pdxObject); @@ -374,11 +376,12 @@ void PdxLocalReader::readCollection(const char* fieldName, PdxUnreadFieldsPtr PdxLocalReader::readUnreadFields() { LOGDEBUG("readUnreadFields:: %d ignore property %d", m_isDataNeedToPreserve, - PdxTypeRegistry::getPdxIgnoreUnreadFields()); - if (PdxTypeRegistry::getPdxIgnoreUnreadFields() == true) return nullptr; + m_pdxTypeRegistry->getPdxIgnoreUnreadFields()); + if (m_pdxTypeRegistry->getPdxIgnoreUnreadFields() == true) return nullptr; m_isDataNeedToPreserve = false; return m_pdxRemotePreserveData; } + } // namespace client } // namespace geode } // namespace apache http://git-wip-us.apache.org/repos/asf/geode-native/blob/da389793/src/cppcache/src/PdxLocalReader.hpp ---------------------------------------------------------------------- diff --git a/src/cppcache/src/PdxLocalReader.hpp b/src/cppcache/src/PdxLocalReader.hpp index 7f0c724..439626f 100644 --- a/src/cppcache/src/PdxLocalReader.hpp +++ b/src/cppcache/src/PdxLocalReader.hpp @@ -51,9 +51,10 @@ class PdxLocalReader : public PdxReader { void checkEmptyFieldName(const char* fieldName); public: - PdxLocalReader(); + PdxLocalReader(PdxTypeRegistryPtr pdxTypeRegistry); - PdxLocalReader(DataInput& input, PdxTypePtr remoteType, int32_t pdxLen); + PdxLocalReader(DataInput& input, PdxTypePtr remoteType, int32_t pdxLen, + PdxTypeRegistryPtr pdxTypeRegistry); virtual ~PdxLocalReader(); @@ -213,6 +214,10 @@ class PdxLocalReader : public PdxReader { CacheableArrayListPtr& collection); virtual PdxUnreadFieldsPtr readUnreadFields(); + + protected: + + PdxTypeRegistryPtr m_pdxTypeRegistry; }; typedef std::shared_ptr<PdxLocalReader> PdxLocalReaderPtr; } // namespace client http://git-wip-us.apache.org/repos/asf/geode-native/blob/da389793/src/cppcache/src/PdxLocalWriter.cpp ---------------------------------------------------------------------- diff --git a/src/cppcache/src/PdxLocalWriter.cpp b/src/cppcache/src/PdxLocalWriter.cpp index b30f31e..7b88893 100644 --- a/src/cppcache/src/PdxLocalWriter.cpp +++ b/src/cppcache/src/PdxLocalWriter.cpp @@ -31,76 +31,29 @@ namespace apache { namespace geode { namespace client { -/* adongre - Coverity II - * Non-static class member "m_currentOffsetIndex" is not initialized in this - * constructor nor in any functions that it calls. - * Non-static class member "m_startPositionOffset" is not initialized in this - * constructor nor in any functions that it calls. - */ -PdxLocalWriter::PdxLocalWriter() - : m_dataOutput(nullptr), - m_pdxType(nullptr), +PdxLocalWriter::PdxLocalWriter(DataOutput& output, PdxTypePtr pdxType, + PdxTypeRegistryPtr pdxTypeRegistry) + : PdxLocalWriter(output, pdxType, + pdxType ? pdxType->getPdxClassName() : nullptr, + pdxTypeRegistry) + +{} + +PdxLocalWriter::PdxLocalWriter(DataOutput& dataOutput, PdxTypePtr pdxType, + const char* pdxClassName, + PdxTypeRegistryPtr pdxTypeRegistry) + : m_dataOutput(&dataOutput), + m_pdxType(pdxType), + m_pdxClassName(pdxClassName), m_startPosition(nullptr), m_startPositionOffset(0), m_domainClassName(nullptr), m_currentOffsetIndex(0), - m_pdxClassName( - nullptr) { // COVERITY --> 29282 Uninitialized pointer field - // m_dataOutput = nullptr; - // m_pdxType =nullptr; -} - -PdxLocalWriter::PdxLocalWriter(DataOutput& output, PdxTypePtr pdxType) { - m_dataOutput = &output; - m_pdxType = pdxType; - m_currentOffsetIndex = 0; - m_preserveData = nullptr; - m_pdxClassName = nullptr; - if (pdxType != nullptr) m_pdxClassName = pdxType->getPdxClassName(); - ; + m_pdxTypeRegistry(pdxTypeRegistry) { initialize(); - /* adongre - Coverity II - * CID 29281: Uninitialized pointer field (UNINIT_CTOR) - * Non-static class member "m_domainClassName" is not initialized in this - * constructor nor in any functions that it calls. - * Fix : - */ - m_domainClassName = nullptr; } -PdxLocalWriter::PdxLocalWriter(DataOutput& dataOutput, PdxTypePtr pdxType, - const char* pdxClassName) { - m_dataOutput = &dataOutput; - m_pdxType = pdxType; - m_currentOffsetIndex = 0; - m_preserveData = nullptr; - m_pdxClassName = pdxClassName; - initialize(); - /* adongre - Coverity II - * CID 29281: Uninitialized pointer field (UNINIT_CTOR) - * Non-static class member "m_domainClassName" is not initialized in this - * constructor nor in any functions that it calls. - * Fix : - */ - m_domainClassName = nullptr; -} - -PdxLocalWriter::~PdxLocalWriter() { - /*if (m_dataOutput != nullptr) { - delete m_dataOutput; - m_dataOutput = nullptr; - } - */ - /*if (m_startPosition != nullptr) { - delete m_startPosition; - m_startPosition = nullptr; - }*/ - - /*if (m_domainClassName != nullptr) { - delete m_domainClassName; - m_domainClassName = nullptr; - }*/ -} +PdxLocalWriter::~PdxLocalWriter() {} void PdxLocalWriter::initialize() { if (m_pdxType != nullptr) { @@ -171,11 +124,11 @@ PdxWriterPtr PdxLocalWriter::writeUnreadFields(PdxUnreadFieldsPtr unread) { m_preserveData = std::dynamic_pointer_cast<PdxRemotePreservedData>(unread); if (m_preserveData != nullptr) { m_pdxType = - PdxTypeRegistry::getPdxType(m_preserveData->getMergedTypeId()); + getPdxTypeRegistry()->getPdxType(m_preserveData->getMergedTypeId()); if (m_pdxType == nullptr) { // its local type // this needs to fix for IPdxTypemapper - m_pdxType = PdxTypeRegistry::getLocalPdxType(m_pdxClassName); + m_pdxType = getPdxTypeRegistry()->getLocalPdxType(m_pdxClassName); } } else { throw IllegalStateException( @@ -526,6 +479,11 @@ uint8_t* PdxLocalWriter::getPdxStream(int& pdxLen) { } void PdxLocalWriter::writeByte(int8_t byte) { m_dataOutput->write(byte); } + +PdxTypeRegistryPtr PdxLocalWriter::getPdxTypeRegistry() const { + return m_pdxTypeRegistry; +} + } // namespace client } // namespace geode } // namespace apache http://git-wip-us.apache.org/repos/asf/geode-native/blob/da389793/src/cppcache/src/PdxLocalWriter.hpp ---------------------------------------------------------------------- diff --git a/src/cppcache/src/PdxLocalWriter.hpp b/src/cppcache/src/PdxLocalWriter.hpp index 230eea3..4c78635 100644 --- a/src/cppcache/src/PdxLocalWriter.hpp +++ b/src/cppcache/src/PdxLocalWriter.hpp @@ -1,8 +1,3 @@ -#pragma once - -#ifndef GEODE_PDXLOCALWRITER_H_ -#define GEODE_PDXLOCALWRITER_H_ - /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -20,13 +15,21 @@ * limitations under the License. */ +#pragma once + +#ifndef GEODE_PDXLOCALWRITER_H_ +#define GEODE_PDXLOCALWRITER_H_ + +#include <vector> + #include <geode/PdxWriter.hpp> -#include "PdxType.hpp" #include <geode/DataOutput.hpp> -#include <vector> -#include "PdxRemotePreservedData.hpp" #include <geode/CacheableObjectArray.hpp> +#include "PdxType.hpp" +#include "PdxRemotePreservedData.hpp" +#include "PdxTypeRegistry.hpp" + namespace apache { namespace geode { namespace client { @@ -43,6 +46,7 @@ class PdxLocalWriter : public PdxWriter, int32_t m_currentOffsetIndex; PdxRemotePreservedDataPtr m_preserveData; + PdxTypeRegistryPtr m_pdxTypeRegistry; const char* m_pdxClassName; PdxWriterPtr writeStringwithoutOffset(const char* value); @@ -50,12 +54,11 @@ class PdxLocalWriter : public PdxWriter, PdxWriterPtr writeWideStringwithoutOffset(const wchar_t* value); public: - PdxLocalWriter(); - - PdxLocalWriter(DataOutput& output, PdxTypePtr pdxType); + PdxLocalWriter(DataOutput& output, PdxTypePtr pdxType, + PdxTypeRegistryPtr pdxTypeRegistry); PdxLocalWriter(DataOutput& output, PdxTypePtr pdxType, - const char* pdxDomainType); + const char* pdxDomainType, PdxTypeRegistryPtr pdxTypeRegistry); virtual ~PdxLocalWriter(); @@ -334,6 +337,9 @@ class PdxLocalWriter : public PdxWriter, void writeByte(int8_t byte); inline int32_t getStartPositionOffset() { return m_startPositionOffset; } + + private: + PdxTypeRegistryPtr getPdxTypeRegistry() const; }; typedef std::shared_ptr<PdxLocalWriter> PdxLocalWriterPtr; } // namespace client http://git-wip-us.apache.org/repos/asf/geode-native/blob/da389793/src/cppcache/src/PdxReaderWithTypeCollector.cpp ---------------------------------------------------------------------- diff --git a/src/cppcache/src/PdxReaderWithTypeCollector.cpp b/src/cppcache/src/PdxReaderWithTypeCollector.cpp index 54fa121..32d4f01 100644 --- a/src/cppcache/src/PdxReaderWithTypeCollector.cpp +++ b/src/cppcache/src/PdxReaderWithTypeCollector.cpp @@ -25,20 +25,19 @@ #include "PdxTypes.hpp" #include <ace/OS_NS_stdio.h> #include <geode/PdxFieldTypes.hpp> +#include "CacheImpl.hpp" namespace apache { namespace geode { namespace client { -PdxReaderWithTypeCollector::PdxReaderWithTypeCollector(DataInput& dataInput, - PdxTypePtr pdxType, - int32_t pdxlen) - : PdxLocalReader(dataInput, pdxType, pdxlen) { - m_newPdxType = std::make_shared<PdxType>(pdxType->getPdxClassName(), true); +PdxReaderWithTypeCollector::PdxReaderWithTypeCollector( + DataInput& dataInput, PdxTypePtr pdxType, int32_t pdxlen, + PdxTypeRegistryPtr pdxTypeRegistry) + : PdxLocalReader(dataInput, pdxType, pdxlen, pdxTypeRegistry) { + m_newPdxType = std::make_shared<PdxType>(m_pdxTypeRegistry ,pdxType->getPdxClassName(), true); } -PdxReaderWithTypeCollector::PdxReaderWithTypeCollector() {} - PdxReaderWithTypeCollector::~PdxReaderWithTypeCollector() {} void PdxReaderWithTypeCollector::checkType(const char* fieldName, int8_t typeId, http://git-wip-us.apache.org/repos/asf/geode-native/blob/da389793/src/cppcache/src/PdxReaderWithTypeCollector.hpp ---------------------------------------------------------------------- diff --git a/src/cppcache/src/PdxReaderWithTypeCollector.hpp b/src/cppcache/src/PdxReaderWithTypeCollector.hpp index 8d89434..fff07a0 100644 --- a/src/cppcache/src/PdxReaderWithTypeCollector.hpp +++ b/src/cppcache/src/PdxReaderWithTypeCollector.hpp @@ -21,6 +21,7 @@ */ #include "PdxLocalReader.hpp" +#include "PdxTypeRegistry.hpp" namespace apache { namespace geode { @@ -33,10 +34,8 @@ class PdxReaderWithTypeCollector : public PdxLocalReader { void checkType(const char* fieldName, int8_t typeId, const char* fieldType); public: - PdxReaderWithTypeCollector(); - PdxReaderWithTypeCollector(DataInput& dataInput, PdxTypePtr pdxType, - int pdxlen); + int pdxlen, PdxTypeRegistryPtr pdxTypeRegistry); virtual ~PdxReaderWithTypeCollector(); http://git-wip-us.apache.org/repos/asf/geode-native/blob/da389793/src/cppcache/src/PdxRemoteReader.hpp ---------------------------------------------------------------------- diff --git a/src/cppcache/src/PdxRemoteReader.hpp b/src/cppcache/src/PdxRemoteReader.hpp index b036513..8459832 100644 --- a/src/cppcache/src/PdxRemoteReader.hpp +++ b/src/cppcache/src/PdxRemoteReader.hpp @@ -31,8 +31,9 @@ class PdxRemoteReader : public PdxLocalReader { int32_t m_currentIndex; public: - PdxRemoteReader(DataInput& dataInput, PdxTypePtr remoteType, int32_t pdxLen) - : PdxLocalReader(dataInput, remoteType, pdxLen) { + PdxRemoteReader(DataInput& dataInput, PdxTypePtr remoteType, int32_t pdxLen, + PdxTypeRegistryPtr pdxTypeRegistry) + : PdxLocalReader(dataInput, remoteType, pdxLen, pdxTypeRegistry) { m_currentIndex = 0; } http://git-wip-us.apache.org/repos/asf/geode-native/blob/da389793/src/cppcache/src/PdxRemoteWriter.cpp ---------------------------------------------------------------------- diff --git a/src/cppcache/src/PdxRemoteWriter.cpp b/src/cppcache/src/PdxRemoteWriter.cpp index dd3f522..7a9b760 100644 --- a/src/cppcache/src/PdxRemoteWriter.cpp +++ b/src/cppcache/src/PdxRemoteWriter.cpp @@ -27,32 +27,15 @@ namespace apache { namespace geode { namespace client { -/* adongre - * Coverity - II - * Non-static class member "m_currentDataIdx" is not initialized in this - * constructor nor in any functions that it calls. - * Non-static class member "m_preserveDataIdx" is not initialized in this - * constructor nor in any functions that it calls. - * Non-static class member "m_remoteTolocalMapLength" is not initialized in this - * constructor nor in any functions that it calls. - * Fix : Initialize the members - */ -PdxRemoteWriter::PdxRemoteWriter() - : m_preserveDataIdx(0), m_currentDataIdx(-1), m_remoteTolocalMapLength(0) { - if (m_pdxType != nullptr) { - m_remoteTolocalMap = - m_pdxType->getRemoteToLocalMap(); // COVERITY --> 29286 Uninitialized - // pointer field - m_remoteTolocalMapLength = m_pdxType->getTotalFields(); - } -} PdxRemoteWriter::PdxRemoteWriter(DataOutput& output, PdxTypePtr pdxType, - PdxRemotePreservedDataPtr preservedData) - : PdxLocalWriter(output, pdxType), + PdxRemotePreservedDataPtr preservedData, + PdxTypeRegistryPtr pdxTypeRegistry) + : PdxLocalWriter(output, pdxType, pdxTypeRegistry), m_preserveDataIdx(0), m_currentDataIdx(-1), - m_remoteTolocalMapLength(0) { + m_remoteTolocalMapLength(0), + m_pdxTypeRegistry(pdxTypeRegistry) { m_preserveData = preservedData; if (m_pdxType != nullptr) { m_remoteTolocalMap = m_pdxType->getRemoteToLocalMap(); @@ -63,11 +46,13 @@ PdxRemoteWriter::PdxRemoteWriter(DataOutput& output, PdxTypePtr pdxType, initialize(); } -PdxRemoteWriter::PdxRemoteWriter(DataOutput& output, const char* pdxClassName) - : PdxLocalWriter(output, nullptr, pdxClassName), +PdxRemoteWriter::PdxRemoteWriter(DataOutput& output, const char* pdxClassName, + PdxTypeRegistryPtr pdxTypeRegistry) + : PdxLocalWriter(output, nullptr, pdxClassName, pdxTypeRegistry), m_preserveDataIdx(0), m_currentDataIdx(-1), - m_remoteTolocalMapLength(0) { + m_remoteTolocalMapLength(0), + m_pdxTypeRegistry(pdxTypeRegistry) { m_preserveData = nullptr; if (m_pdxType != nullptr) { m_remoteTolocalMapLength = m_pdxType->getTotalFields(); @@ -130,7 +115,7 @@ void PdxRemoteWriter::writePreserveData() { void PdxRemoteWriter::initialize() { // this is default case if (m_preserveData == nullptr) { - m_pdxType = PdxTypeRegistry::getLocalPdxType(m_pdxClassName); + m_pdxType = getPdxTypeRegistry()->getLocalPdxType(m_pdxClassName); } } @@ -323,6 +308,11 @@ PdxWriterPtr PdxRemoteWriter::writeArrayOfByteArrays(const char* fieldName, elementLength); return shared_from_this(); } + +PdxTypeRegistryPtr PdxRemoteWriter::getPdxTypeRegistry() const { + return m_pdxTypeRegistry; +} + } // namespace client } // namespace geode } // namespace apache http://git-wip-us.apache.org/repos/asf/geode-native/blob/da389793/src/cppcache/src/PdxRemoteWriter.hpp ---------------------------------------------------------------------- diff --git a/src/cppcache/src/PdxRemoteWriter.hpp b/src/cppcache/src/PdxRemoteWriter.hpp index 809833b..2e7a4b4 100644 --- a/src/cppcache/src/PdxRemoteWriter.hpp +++ b/src/cppcache/src/PdxRemoteWriter.hpp @@ -1,8 +1,3 @@ -#pragma once - -#ifndef GEODE_PDXREMOTEWRITER_H_ -#define GEODE_PDXREMOTEWRITER_H_ - /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -19,12 +14,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* - * PdxRemoteWriter.hpp - * - * Created on: Nov 3, 2011 - * Author: npatel - */ + +#pragma once + +#ifndef GEODE_PDXREMOTEWRITER_H_ +#define GEODE_PDXREMOTEWRITER_H_ #include "PdxLocalWriter.hpp" @@ -40,18 +34,22 @@ class PdxRemoteWriter : public PdxLocalWriter { int32_t m_remoteTolocalMapLength; + PdxTypeRegistryPtr m_pdxTypeRegistry; + void initialize(); void writePreserveData(); - public: - PdxRemoteWriter(); + PdxTypeRegistryPtr getPdxTypeRegistry() const; + public: virtual ~PdxRemoteWriter(); PdxRemoteWriter(DataOutput& output, PdxTypePtr pdxType, - PdxRemotePreservedDataPtr preservedData); + PdxRemotePreservedDataPtr preservedData, + PdxTypeRegistryPtr pdxTypeRegistry); - PdxRemoteWriter(DataOutput& output, const char* pdxClassName); + PdxRemoteWriter(DataOutput& output, const char* pdxClassName, + PdxTypeRegistryPtr pdxTypeRegistry); virtual void endObjectWriting(); http://git-wip-us.apache.org/repos/asf/geode-native/blob/da389793/src/cppcache/src/PdxType.cpp ---------------------------------------------------------------------- diff --git a/src/cppcache/src/PdxType.cpp b/src/cppcache/src/PdxType.cpp index f505be3..50bfa38 100644 --- a/src/cppcache/src/PdxType.cpp +++ b/src/cppcache/src/PdxType.cpp @@ -43,53 +43,22 @@ PdxType::~PdxType() { GF_SAFE_DELETE_ARRAY(m_className); } -PdxType::PdxType() : Serializable() { - // m_lockObj = nullptr; - m_className = nullptr; - m_isLocal = false; - m_numberOfVarLenFields = 0; - m_varLenFieldIdx = 0; // start with 0 - m_isVarLenFieldAdded = false; - // m_fieldNameVsPdxType = CacheableHashMap::create(); - m_noJavaClass = false; - // m_pdxDomainType = nullptr; - m_pdxFieldTypes = new std::vector<PdxFieldTypePtr>(); - m_localToRemoteFieldMap = nullptr; - m_remoteToLocalFieldMap = nullptr; - m_geodeTypeId = 0; - /* adongre - * Coverity - II - * CID 29288: Uninitialized scalar field (UNINIT_CTOR) - * Non-static class member "m_numberOfFieldsExtra" is not - * initialized in this constructor nor in any functions that it calls. - * Fix : Initialized the memeber - */ - m_numberOfFieldsExtra = 0; -} - -PdxType::PdxType(const char* pdxDomainClassName, bool isLocal) - : Serializable() { - // m_lockObj = nullptr; - m_className = Utils::copyString(pdxDomainClassName); - m_isLocal = isLocal; - m_numberOfVarLenFields = 0; - m_varLenFieldIdx = 0; // start with 0 - m_isVarLenFieldAdded = false; - // m_fieldNameVsPdxType = CacheableHashMap::create(); - m_noJavaClass = false; - m_pdxFieldTypes = new std::vector<PdxFieldTypePtr>(); - m_localToRemoteFieldMap = nullptr; - m_remoteToLocalFieldMap = nullptr; - m_geodeTypeId = 0; - /* adongre - * Coverity - II - * CID 29287: Uninitialized scalar field (UNINIT_CTOR) - * Non-static class member "m_numberOfFieldsExtra" is not - * initialized in this constructor nor in any functions that it calls. - * Fix : Initialized the memeber - */ - m_numberOfFieldsExtra = 0; -} +//PdxType::PdxType() : PdxType(nullptr, false) {} + +PdxType::PdxType(PdxTypeRegistryPtr pdxTypeRegistryPtr, const char* pdxDomainClassName, bool isLocal) + : Serializable(), + m_className(Utils::copyString(pdxDomainClassName)), + m_isLocal(isLocal), + m_numberOfVarLenFields(0), + m_varLenFieldIdx(0), + m_isVarLenFieldAdded(false), + m_noJavaClass(false), + m_pdxFieldTypes(new std::vector<PdxFieldTypePtr>()), + m_localToRemoteFieldMap(nullptr), + m_remoteToLocalFieldMap(nullptr), + m_geodeTypeId(0), + m_numberOfFieldsExtra(0), + m_pdxTypeRegistryPtr(pdxTypeRegistryPtr) {} void PdxType::toData(DataOutput& output) const { output.write(static_cast<int8_t>(GeodeTypeIdsImpl::DataSerializable)); // 45 @@ -242,7 +211,7 @@ void PdxType::initRemoteToLocal() { PdxTypePtr localPdxType = nullptr; //[TODO - open this up once PdxTypeRegistry is done] - localPdxType = PdxTypeRegistry::getLocalPdxType(m_className); + localPdxType = m_pdxTypeRegistryPtr->getLocalPdxType(m_className); m_numberOfFieldsExtra = 0; if (localPdxType != nullptr) { @@ -302,7 +271,7 @@ void PdxType::initLocalToRemote() { // 5. else if local field is not in remote type then -1 PdxTypePtr localPdxType = nullptr; - localPdxType = PdxTypeRegistry::getLocalPdxType(m_className); + localPdxType = m_pdxTypeRegistryPtr->getLocalPdxType(m_className); if (localPdxType != nullptr) { std::vector<PdxFieldTypePtr>* localPdxFields = @@ -478,7 +447,7 @@ PdxTypePtr PdxType::isContains(PdxTypePtr first, PdxTypePtr second) { } PdxTypePtr PdxType::clone() { - auto clone = std::make_shared<PdxType>(m_className, false); + auto clone = std::make_shared<PdxType>(m_pdxTypeRegistryPtr, m_className, false); clone->m_geodeTypeId = 0; clone->m_numberOfVarLenFields = m_numberOfVarLenFields; @@ -628,6 +597,8 @@ bool PdxType::Equals(PdxTypePtr otherObj) { bool PdxType::operator<(const PdxType& other) const { return ACE_OS::strcmp(this->m_className, other.m_className) < 0; } + + } // namespace client } // namespace geode } // namespace apache http://git-wip-us.apache.org/repos/asf/geode-native/blob/da389793/src/cppcache/src/PdxType.hpp ---------------------------------------------------------------------- diff --git a/src/cppcache/src/PdxType.hpp b/src/cppcache/src/PdxType.hpp index 3a90a78..5b094b4 100644 --- a/src/cppcache/src/PdxType.hpp +++ b/src/cppcache/src/PdxType.hpp @@ -1,8 +1,3 @@ -#pragma once - -#ifndef GEODE_PDXTYPE_H_ -#define GEODE_PDXTYPE_H_ - /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -20,6 +15,11 @@ * limitations under the License. */ +#pragma once + +#ifndef GEODE_PDXTYPE_H_ +#define GEODE_PDXTYPE_H_ + #include <geode/Serializable.hpp> #include "PdxFieldType.hpp" #include <geode/CacheableBuiltins.hpp> @@ -40,18 +40,9 @@ namespace client { typedef std::map<std::string, PdxFieldTypePtr> NameVsPdxType; class PdxType; typedef std::shared_ptr<PdxType> PdxTypePtr; -/* adongre - * Coverity - II - * CID 29178: Other violation (MISSING_COPY) - * Class "apache::geode::client::PdxType" owns resources that are managed - * in its constructor and destructor but has no user-written copy constructor. - * Fix : Make the class Non Copyable - * - * CID 29173: Other violation (MISSING_ASSIGN) - * Class "apache::geode::client::PdxType" owns resources that are managed in its - * constructor and destructor but has no user-written assignment operator. - * Fix : Make the class Non Assignable - */ +class PdxTypeRegistry; +typedef std::shared_ptr <PdxTypeRegistry> PdxTypeRegistryPtr; + class PdxType : public Serializable, private NonCopyable, private NonAssignable { @@ -86,6 +77,8 @@ class PdxType : public Serializable, bool m_noJavaClass; + PdxTypeRegistryPtr m_pdxTypeRegistryPtr; + void initRemoteToLocal(); void initLocalToRemote(); int32_t fixedLengthFieldPosition(PdxFieldTypePtr fixLenField, @@ -107,9 +100,8 @@ class PdxType : public Serializable, } public: - PdxType(); - PdxType(const char* pdxDomainClassName, bool isLocal); + PdxType(PdxTypeRegistryPtr pdxTypeRegistryPtr, const char* pdxDomainClassName, bool isLocal); virtual ~PdxType(); @@ -119,7 +111,7 @@ class PdxType : public Serializable, virtual int32_t classId() const { return GeodeTypeIds::PdxType; } - static Serializable* CreateDeserializable() { return new PdxType(); } + static Serializable* CreateDeserializable(PdxTypeRegistryPtr pdxTypeRegistryPtr) { return new PdxType(pdxTypeRegistryPtr, nullptr, false); } virtual uint32_t objectSize() const { uint32_t size = sizeof(PdxType); http://git-wip-us.apache.org/repos/asf/geode-native/blob/da389793/src/cppcache/src/PdxTypeRegistry.cpp ---------------------------------------------------------------------- diff --git a/src/cppcache/src/PdxTypeRegistry.cpp b/src/cppcache/src/PdxTypeRegistry.cpp index 713341a..fc9e45d 100644 --- a/src/cppcache/src/PdxTypeRegistry.cpp +++ b/src/cppcache/src/PdxTypeRegistry.cpp @@ -14,82 +14,29 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* - * PdxTypeRegistry.cpp - * - * Created on: Dec 9, 2011 - * Author: npatel - */ + +#include <geode/PoolManager.hpp> #include "PdxTypeRegistry.hpp" -#include "SerializationRegistry.hpp" +#include "CacheRegionHelper.hpp" +#include "ThinClientPoolDM.hpp" namespace apache { namespace geode { namespace client { -TypeIdVsPdxType* PdxTypeRegistry::typeIdToPdxType = nullptr; - -TypeIdVsPdxType* PdxTypeRegistry::remoteTypeIdToMergedPdxType = nullptr; - -TypeNameVsPdxType* PdxTypeRegistry::localTypeToPdxType = nullptr; - -// TODO::Add support for weakhashmap -// std::map<PdxSerializablePtr, PdxRemotePreservedDataPtr> -// *PdxTypeRegistry::preserveData = nullptr; -PreservedHashMap PdxTypeRegistry::preserveData; - -CacheableHashMapPtr PdxTypeRegistry::enumToInt = nullptr; - -CacheableHashMapPtr PdxTypeRegistry::intToEnum = nullptr; - -ACE_RW_Thread_Mutex PdxTypeRegistry::g_readerWriterLock; - -ACE_RW_Thread_Mutex PdxTypeRegistry::g_preservedDataLock; - -PdxTypeToTypeIdMap* PdxTypeRegistry::pdxTypeToTypeIdMap = nullptr; -bool PdxTypeRegistry::pdxReadSerialized; -bool PdxTypeRegistry::pdxIgnoreUnreadFields; - -PdxTypeRegistry::PdxTypeRegistry() /*:pdxIgnoreUnreadFields (false), - pdxReadSerialized(false)*/ -{} +PdxTypeRegistry::PdxTypeRegistry(Cache* cache) + : cache(cache), + typeIdToPdxType(), + remoteTypeIdToMergedPdxType(), + localTypeToPdxType(), + pdxTypeToTypeIdMap(), + enumToInt(CacheableHashMap::create()), + intToEnum(CacheableHashMap::create()) {} PdxTypeRegistry::~PdxTypeRegistry() {} -void PdxTypeRegistry::init() { - // try{ - typeIdToPdxType = new TypeIdVsPdxType(); - remoteTypeIdToMergedPdxType = new TypeIdVsPdxType(); - localTypeToPdxType = new TypeNameVsPdxType(); - // preserveData = CacheableHashMap::create(); - // preserveData = PreservedHashMapPtr(new PreservedHashMap()); - enumToInt = CacheableHashMap::create(); - intToEnum = CacheableHashMap::create(); - pdxTypeToTypeIdMap = new PdxTypeToTypeIdMap(); - /*}catch(const std::bad_alloc&){ - throw apache::geode::client::OutOfMemoryException( "Out of Memory while - executing new in - PdxTypeRegistry::init()"); - }*/ -} - -void PdxTypeRegistry::cleanup() { - clear(); - GF_SAFE_DELETE(typeIdToPdxType); - GF_SAFE_DELETE(remoteTypeIdToMergedPdxType); - GF_SAFE_DELETE(localTypeToPdxType); - GF_SAFE_DELETE(pdxTypeToTypeIdMap); - intToEnum = nullptr; - enumToInt = nullptr; - // GF_SAFE_DELETE(preserveData); -} - -size_t PdxTypeRegistry::testGetNumberOfPdxIds() { - return typeIdToPdxType->size(); -} - -size_t PdxTypeRegistry::testNumberOfPreservedData() { +size_t PdxTypeRegistry::testNumberOfPreservedData() const { return preserveData.size(); } @@ -106,7 +53,10 @@ int32_t PdxTypeRegistry::getPDXIdForType(const char* type, const char* poolname, } } - int typeId = SerializationRegistry::GetPDXIdForType(poolname, nType); + int typeId = + CacheRegionHelper::getCacheImpl(cache) + ->getSerializationRegistry() + ->GetPDXIdForType(cache->getPoolManager().find(poolname), nType); nType->setTypeId(typeId); PdxTypeRegistry::addPdxType(typeId, nType); @@ -115,48 +65,51 @@ int32_t PdxTypeRegistry::getPDXIdForType(const char* type, const char* poolname, int32_t PdxTypeRegistry::getPDXIdForType(PdxTypePtr nType, const char* poolname) { - PdxTypeToTypeIdMap* tmp = pdxTypeToTypeIdMap; int32_t typeId = 0; - PdxTypeToTypeIdMap::iterator iter = tmp->find(nType); - if (iter != tmp->end()) { - typeId = iter->second; - if (typeId != 0) { - return typeId; + { + ReadGuard read(g_readerWriterLock); + PdxTypeToTypeIdMap::iterator iter = pdxTypeToTypeIdMap.find(nType); + if (iter != pdxTypeToTypeIdMap.end()) { + typeId = iter->second; + if (typeId != 0) { + return typeId; + } } } - /*WriteGuard guard(g_readerWriterLock); - tmp = pdxTypeToTypeIdMap; - if(tmp->find(nType) != tmp->end()) { - typeId = tmp->operator[](nType); + + WriteGuard write(g_readerWriterLock); + + PdxTypeToTypeIdMap::iterator iter = pdxTypeToTypeIdMap.find(nType); + if (iter != pdxTypeToTypeIdMap.end()) { + typeId = iter->second; if (typeId != 0) { return typeId; } - }*/ - typeId = SerializationRegistry::GetPDXIdForType(poolname, nType); + } + + typeId = CacheRegionHelper::getCacheImpl(cache) + ->getSerializationRegistry() + ->GetPDXIdForType(cache->getPoolManager().find(poolname), nType); nType->setTypeId(typeId); - tmp = pdxTypeToTypeIdMap; - tmp->insert(std::make_pair(nType, typeId)); - pdxTypeToTypeIdMap = tmp; - PdxTypeRegistry::addPdxType(typeId, nType); + pdxTypeToTypeIdMap.insert(std::make_pair(nType, typeId)); + addPdxType(typeId, nType); return typeId; } void PdxTypeRegistry::clear() { { WriteGuard guard(g_readerWriterLock); - if (typeIdToPdxType != nullptr) typeIdToPdxType->clear(); + typeIdToPdxType.clear(); - if (remoteTypeIdToMergedPdxType != nullptr) { - remoteTypeIdToMergedPdxType->clear(); - } + remoteTypeIdToMergedPdxType.clear(); - if (localTypeToPdxType != nullptr) localTypeToPdxType->clear(); + localTypeToPdxType.clear(); - if (intToEnum != nullptr) intToEnum->clear(); + if (intToEnum) intToEnum->clear(); - if (enumToInt != nullptr) enumToInt->clear(); + if (enumToInt) enumToInt->clear(); - if (pdxTypeToTypeIdMap != nullptr) pdxTypeToTypeIdMap->clear(); + pdxTypeToTypeIdMap.clear(); } { WriteGuard guard(getPreservedDataLock()); @@ -167,15 +120,15 @@ void PdxTypeRegistry::clear() { void PdxTypeRegistry::addPdxType(int32_t typeId, PdxTypePtr pdxType) { WriteGuard guard(g_readerWriterLock); std::pair<int32_t, PdxTypePtr> pc(typeId, pdxType); - typeIdToPdxType->insert(pc); + typeIdToPdxType.insert(pc); } PdxTypePtr PdxTypeRegistry::getPdxType(int32_t typeId) { ReadGuard guard(g_readerWriterLock); PdxTypePtr retValue = nullptr; TypeIdVsPdxType::iterator iter; - iter = typeIdToPdxType->find(typeId); - if (iter != typeIdToPdxType->end()) { + iter = typeIdToPdxType.find(typeId); + if (iter != typeIdToPdxType.end()) { retValue = (*iter).second; return retValue; } @@ -185,7 +138,7 @@ PdxTypePtr PdxTypeRegistry::getPdxType(int32_t typeId) { void PdxTypeRegistry::addLocalPdxType(const char* localType, PdxTypePtr pdxType) { WriteGuard guard(g_readerWriterLock); - localTypeToPdxType->insert( + localTypeToPdxType.insert( std::pair<std::string, PdxTypePtr>(localType, pdxType)); } @@ -193,8 +146,8 @@ PdxTypePtr PdxTypeRegistry::getLocalPdxType(const char* localType) { ReadGuard guard(g_readerWriterLock); PdxTypePtr localTypePtr = nullptr; TypeNameVsPdxType::iterator it; - it = localTypeToPdxType->find(localType); - if (it != localTypeToPdxType->end()) { + it = localTypeToPdxType.find(localType); + if (it != localTypeToPdxType.end()) { localTypePtr = (*it).second; return localTypePtr; } @@ -205,14 +158,14 @@ void PdxTypeRegistry::setMergedType(int32_t remoteTypeId, PdxTypePtr mergedType) { WriteGuard guard(g_readerWriterLock); std::pair<int32_t, PdxTypePtr> mergedTypePair(remoteTypeId, mergedType); - remoteTypeIdToMergedPdxType->insert(mergedTypePair); + remoteTypeIdToMergedPdxType.insert(mergedTypePair); } PdxTypePtr PdxTypeRegistry::getMergedType(int32_t remoteTypeId) { PdxTypePtr retVal = nullptr; TypeIdVsPdxType::iterator it; - it = remoteTypeIdToMergedPdxType->find(remoteTypeId); - if (it != remoteTypeIdToMergedPdxType->end()) { + it = remoteTypeIdToMergedPdxType.find(remoteTypeId); + if (it != remoteTypeIdToMergedPdxType.end()) { retVal = (*it).second; return retVal; } @@ -220,22 +173,22 @@ PdxTypePtr PdxTypeRegistry::getMergedType(int32_t remoteTypeId) { } void PdxTypeRegistry::setPreserveData(PdxSerializablePtr obj, - PdxRemotePreservedDataPtr pData) { + PdxRemotePreservedDataPtr pData, + ExpiryTaskManager& expiryTaskManager) { WriteGuard guard(getPreservedDataLock()); pData->setOwner(obj); if (preserveData.find(obj) != preserveData.end()) { // reset expiry task // TODO: check value for nullptr auto expTaskId = preserveData[obj]->getPreservedDataExpiryTaskId(); - CacheImpl::expiryTaskManager->resetTask(expTaskId, 5); + expiryTaskManager.resetTask(expTaskId, 5); LOGDEBUG("PdxTypeRegistry::setPreserveData Reset expiry task Done"); pData->setPreservedDataExpiryTaskId(expTaskId); preserveData[obj] = pData; } else { // schedule new expiry task - auto handler = new PreservedDataExpiryHandler(obj, 20); - auto id = - CacheImpl::expiryTaskManager->scheduleExpiryTask(handler, 20, 0, false); + auto handler = new PreservedDataExpiryHandler(shared_from_this(), obj, 20); + long id = expiryTaskManager.scheduleExpiryTask(handler, 20, 0, false); pData->setPreservedDataExpiryTaskId(id); LOGDEBUG( "PdxTypeRegistry::setPreserveData Schedule new expirt task with id=%ld", @@ -275,7 +228,11 @@ int32_t PdxTypeRegistry::getEnumValue(EnumInfoPtr ei) { const auto val2 = std::static_pointer_cast<CacheableInt32>(entry2->second); return val2->value(); } - int val = SerializationRegistry::GetEnumValue(ei); + + int val = static_cast<ThinClientPoolDM*>( + cache->getPoolManager().getAll().begin()->second.get()) + ->GetEnumValue(ei); + tmp = enumToInt; tmp->emplace(ei, CacheableInt32::create(val)); enumToInt = tmp; @@ -314,7 +271,9 @@ EnumInfoPtr PdxTypeRegistry::getEnum(int32_t enumVal) { } ret = std::static_pointer_cast<EnumInfo>( - SerializationRegistry::GetEnum(enumVal)); + static_cast<ThinClientPoolDM*>( + cache->getPoolManager().getAll().begin()->second.get()) + ->GetEnum(enumVal)); tmp = intToEnum; (*tmp)[enumValPtr] = ret; intToEnum = tmp; http://git-wip-us.apache.org/repos/asf/geode-native/blob/da389793/src/cppcache/src/PdxTypeRegistry.hpp ---------------------------------------------------------------------- diff --git a/src/cppcache/src/PdxTypeRegistry.hpp b/src/cppcache/src/PdxTypeRegistry.hpp index 2c2177f..491a347 100644 --- a/src/cppcache/src/PdxTypeRegistry.hpp +++ b/src/cppcache/src/PdxTypeRegistry.hpp @@ -28,12 +28,14 @@ #include <geode/utils.hpp> #include <geode/PdxSerializable.hpp> +#include <geode/Cache.hpp> #include "PdxRemotePreservedData.hpp" #include "ReadWriteLock.hpp" #include "PdxType.hpp" #include "EnumInfo.hpp" #include "PreservedDataExpiryHandler.hpp" +#include "ExpiryTaskManager.hpp" namespace apache { namespace geode { @@ -54,95 +56,88 @@ typedef std::unordered_map<PdxSerializablePtr, PdxRemotePreservedDataPtr, PreservedHashMap; typedef std::map<PdxTypePtr, int32_t, PdxTypeLessThan> PdxTypeToTypeIdMap; -class CPPCACHE_EXPORT PdxTypeRegistry { +class CPPCACHE_EXPORT PdxTypeRegistry + : public std::enable_shared_from_this<PdxTypeRegistry> { private: - static TypeIdVsPdxType* typeIdToPdxType; + Cache* cache; - static TypeIdVsPdxType* remoteTypeIdToMergedPdxType; + TypeIdVsPdxType typeIdToPdxType; - static TypeNameVsPdxType* localTypeToPdxType; + TypeIdVsPdxType remoteTypeIdToMergedPdxType; + + TypeNameVsPdxType localTypeToPdxType; + + PdxTypeToTypeIdMap pdxTypeToTypeIdMap; // TODO:: preserveData need to be of type WeakHashMap - // static std::map<PdxSerializablePtr , PdxRemotePreservedDataPtr> - // *preserveData; - // static CacheableHashMapPtr preserveData; - static PreservedHashMap preserveData; + PreservedHashMap preserveData; - static ACE_RW_Thread_Mutex g_readerWriterLock; + ACE_RW_Thread_Mutex g_readerWriterLock; - static ACE_RW_Thread_Mutex g_preservedDataLock; + ACE_RW_Thread_Mutex g_preservedDataLock; - static bool pdxIgnoreUnreadFields; + bool pdxIgnoreUnreadFields; - static bool pdxReadSerialized; + bool pdxReadSerialized; - static CacheableHashMapPtr enumToInt; + CacheableHashMapPtr enumToInt; - static CacheableHashMapPtr intToEnum; + CacheableHashMapPtr intToEnum; public: - PdxTypeRegistry(); + PdxTypeRegistry(Cache* cache); + PdxTypeRegistry(const PdxTypeRegistry& other) = delete; virtual ~PdxTypeRegistry(); - static void init(); - - static void cleanup(); - - // test hook; - static size_t testGetNumberOfPdxIds(); - // test hook - static size_t testNumberOfPreservedData(); + size_t testNumberOfPreservedData() const; - static void addPdxType(int32_t typeId, PdxTypePtr pdxType); + void addPdxType(int32_t typeId, PdxTypePtr pdxType); - static PdxTypePtr getPdxType(int32_t typeId); + PdxTypePtr getPdxType(int32_t typeId); - static void addLocalPdxType(const char* localType, PdxTypePtr pdxType); + void addLocalPdxType(const char* localType, PdxTypePtr pdxType); // newly added - static PdxTypePtr getLocalPdxType(const char* localType); + PdxTypePtr getLocalPdxType(const char* localType); - static void setMergedType(int32_t remoteTypeId, PdxTypePtr mergedType); + void setMergedType(int32_t remoteTypeId, PdxTypePtr mergedType); - static PdxTypePtr getMergedType(int32_t remoteTypeId); + PdxTypePtr getMergedType(int32_t remoteTypeId); - static void setPreserveData(PdxSerializablePtr obj, - PdxRemotePreservedDataPtr preserveDataPtr); + void setPreserveData(PdxSerializablePtr obj, + PdxRemotePreservedDataPtr preserveDataPtr, + ExpiryTaskManager& expiryTaskManager); - static PdxRemotePreservedDataPtr getPreserveData(PdxSerializablePtr obj); + PdxRemotePreservedDataPtr getPreserveData(PdxSerializablePtr obj); - static void clear(); + void clear(); - static int32_t getPDXIdForType(const char* type, const char* poolname, - PdxTypePtr nType, bool checkIfThere); + int32_t getPDXIdForType(const char* type, const char* poolname, + PdxTypePtr nType, bool checkIfThere); - static bool getPdxIgnoreUnreadFields() { return pdxIgnoreUnreadFields; } + bool getPdxIgnoreUnreadFields() const { return pdxIgnoreUnreadFields; } - static void setPdxIgnoreUnreadFields(bool value) { - pdxIgnoreUnreadFields = value; - } + void setPdxIgnoreUnreadFields(bool value) { pdxIgnoreUnreadFields = value; } - static void setPdxReadSerialized(bool value) { pdxReadSerialized = value; } + void setPdxReadSerialized(bool value) { pdxReadSerialized = value; } - static bool getPdxReadSerialized() { return pdxReadSerialized; } + bool getPdxReadSerialized() const { return pdxReadSerialized; } - static inline PreservedHashMap& getPreserveDataMap() { return preserveData; }; + inline PreservedHashMap& getPreserveDataMap() { return preserveData; }; - static int32_t getEnumValue(EnumInfoPtr ei); + int32_t getEnumValue(EnumInfoPtr ei); - static EnumInfoPtr getEnum(int32_t enumVal); + EnumInfoPtr getEnum(int32_t enumVal); - static int32_t getPDXIdForType(PdxTypePtr nType, const char* poolname); + int32_t getPDXIdForType(PdxTypePtr nType, const char* poolname); - static ACE_RW_Thread_Mutex& getPreservedDataLock() { - return g_preservedDataLock; - } - - private: - static PdxTypeToTypeIdMap* pdxTypeToTypeIdMap; + ACE_RW_Thread_Mutex& getPreservedDataLock() { return g_preservedDataLock; } }; + +typedef std::shared_ptr<PdxTypeRegistry> PdxTypeRegistryPtr; + } // namespace client } // namespace geode } // namespace apache http://git-wip-us.apache.org/repos/asf/geode-native/blob/da389793/src/cppcache/src/PdxWrapper.cpp ---------------------------------------------------------------------- diff --git a/src/cppcache/src/PdxWrapper.cpp b/src/cppcache/src/PdxWrapper.cpp index 9306660..1c73947 100644 --- a/src/cppcache/src/PdxWrapper.cpp +++ b/src/cppcache/src/PdxWrapper.cpp @@ -30,9 +30,9 @@ namespace apache { namespace geode { namespace client { -PdxWrapper::PdxWrapper(void *userObject, const char *className) { - m_userObject = userObject; - +PdxWrapper::PdxWrapper(void *userObject, const char *className, + PdxSerializerPtr pdxSerializerPtr) + : m_userObject(userObject), m_serializer(pdxSerializerPtr) { if (className != nullptr) { m_className = Utils::copyString(className); } else { @@ -41,8 +41,6 @@ PdxWrapper::PdxWrapper(void *userObject, const char *className) { "Class name not provided to PdxWrapper constructor"); } - m_serializer = SerializationRegistry::getPdxSerializer(); - if (m_serializer == nullptr) { LOGERROR("No registered PDX serializer found for PdxWrapper"); throw IllegalArgumentException( @@ -64,7 +62,8 @@ PdxWrapper::PdxWrapper(void *userObject, const char *className) { m_sizer = m_serializer->getObjectSizer(className); } -PdxWrapper::PdxWrapper(const char *className) { +PdxWrapper::PdxWrapper(const char *className, PdxSerializerPtr pdxSerializerPtr) + : m_serializer(pdxSerializerPtr) { if (className != nullptr) { m_className = Utils::copyString(className); } else { @@ -73,8 +72,6 @@ PdxWrapper::PdxWrapper(const char *className) { "Class name not provided to PdxWrapper for deserialization"); } - m_serializer = SerializationRegistry::getPdxSerializer(); - if (m_serializer == nullptr) { LOGERROR( "No registered PDX serializer found for PdxWrapper deserialization"); @@ -173,6 +170,7 @@ PdxWrapper::~PdxWrapper() { } delete[] m_className; } + } // namespace client } // namespace geode } // namespace apache http://git-wip-us.apache.org/repos/asf/geode-native/blob/da389793/src/cppcache/src/PdxWriterWithTypeCollector.cpp ---------------------------------------------------------------------- diff --git a/src/cppcache/src/PdxWriterWithTypeCollector.cpp b/src/cppcache/src/PdxWriterWithTypeCollector.cpp index f60fc53..c143cf5 100644 --- a/src/cppcache/src/PdxWriterWithTypeCollector.cpp +++ b/src/cppcache/src/PdxWriterWithTypeCollector.cpp @@ -31,17 +31,16 @@ namespace apache { namespace geode { namespace client { -PdxWriterWithTypeCollector::PdxWriterWithTypeCollector() {} - PdxWriterWithTypeCollector::PdxWriterWithTypeCollector( - DataOutput& output, const char* domainClassName) - : PdxLocalWriter(output, nullptr) { + DataOutput& output, const char* domainClassName, + PdxTypeRegistryPtr pdxTypeRegistry) + : PdxLocalWriter(output, nullptr, pdxTypeRegistry) { m_domainClassName = domainClassName; initialize(); } void PdxWriterWithTypeCollector::initialize() { - m_pdxType = std::make_shared<PdxType>(m_domainClassName, true); + m_pdxType = std::make_shared<PdxType>(m_pdxTypeRegistry, m_domainClassName, true); } PdxWriterWithTypeCollector::~PdxWriterWithTypeCollector() {} http://git-wip-us.apache.org/repos/asf/geode-native/blob/da389793/src/cppcache/src/PdxWriterWithTypeCollector.hpp ---------------------------------------------------------------------- diff --git a/src/cppcache/src/PdxWriterWithTypeCollector.hpp b/src/cppcache/src/PdxWriterWithTypeCollector.hpp index 714f2cb..63e946d 100644 --- a/src/cppcache/src/PdxWriterWithTypeCollector.hpp +++ b/src/cppcache/src/PdxWriterWithTypeCollector.hpp @@ -33,9 +33,8 @@ class PdxWriterWithTypeCollector : public PdxLocalWriter { void initialize(); public: - PdxWriterWithTypeCollector(); - - PdxWriterWithTypeCollector(DataOutput& output, const char* pdxType); + PdxWriterWithTypeCollector(DataOutput& output, const char* pdxType, + PdxTypeRegistryPtr pdxTypeRegistry); virtual ~PdxWriterWithTypeCollector(); http://git-wip-us.apache.org/repos/asf/geode-native/blob/da389793/src/cppcache/src/Pool.cpp ---------------------------------------------------------------------- diff --git a/src/cppcache/src/Pool.cpp b/src/cppcache/src/Pool.cpp index d2effd0..12c4c68 100644 --- a/src/cppcache/src/Pool.cpp +++ b/src/cppcache/src/Pool.cpp @@ -69,16 +69,14 @@ bool Pool::getThreadLocalConnections() const { bool Pool::getMultiuserAuthentication() const { return m_attrs->getMultiuserSecureModeEnabled(); } -RegionServicePtr Pool::createSecureUserCache(PropertiesPtr credentials) { +RegionServicePtr Pool::createSecureUserCache(PropertiesPtr credentials, + CacheImpl* cacheImpl) { if (this->getMultiuserAuthentication()) { - CachePtr realCache = CacheFactory::getAnyInstance(); - - if (!(realCache != nullptr && realCache->m_cacheImpl != nullptr)) { + if (cacheImpl == nullptr) { throw IllegalStateException("cache has not been created yet."); - ; } - if (realCache->isClosed()) { + if (cacheImpl->isClosed()) { throw IllegalStateException("cache has been closed. "); } @@ -88,7 +86,8 @@ RegionServicePtr Pool::createSecureUserCache(PropertiesPtr credentials) { } // TODO: this will return cache with userattribtes - return std::make_shared<ProxyCache>(credentials, shared_from_this()); + return std::make_shared<ProxyCache>(credentials, shared_from_this(), + cacheImpl); } throw IllegalStateException( @@ -102,18 +101,17 @@ bool Pool::getPRSingleHopEnabled() const { // void Pool::releaseThreadLocalConnection(){} int Pool::getPendingEventCount() const { - TcrConnectionManager& tccm = CacheImpl::getInstance()->tcrConnectionManager(); - if (!tccm.isDurable()) { - LOGERROR("This operation should only be called by durable client."); - throw IllegalStateException( - "This operation should only be called by durable client"); - } const auto poolHADM = dynamic_cast<const ThinClientPoolHADM*>(this); if (nullptr == poolHADM || poolHADM->isReadyForEvent()) { LOGERROR("This operation should only be called before readyForEvents."); throw IllegalStateException( "This operation should only be called before readyForEvents"); } - + TcrConnectionManager& tccm = poolHADM->getConnectionManager(); + if (!tccm.isDurable()) { + LOGERROR("This operation should only be called by durable client."); + throw IllegalStateException( + "This operation should only be called by durable client"); + } return poolHADM->getPrimaryServerQueueSize(); } http://git-wip-us.apache.org/repos/asf/geode-native/blob/da389793/src/cppcache/src/PoolFactory.cpp ---------------------------------------------------------------------- diff --git a/src/cppcache/src/PoolFactory.cpp b/src/cppcache/src/PoolFactory.cpp index aa5f9b1..6a273ed 100644 --- a/src/cppcache/src/PoolFactory.cpp +++ b/src/cppcache/src/PoolFactory.cpp @@ -26,15 +26,19 @@ #include <ace/INET_Addr.h> #include <ThinClientPoolStickyDM.hpp> #include <ThinClientPoolStickyHADM.hpp> +#include "CacheRegionHelper.hpp" using namespace apache::geode::client; -const char* PoolFactory::DEFAULT_SERVER_GROUP = ""; -extern HashMapOfPools* connectionPools; -extern ACE_Recursive_Thread_Mutex connectionPoolsLock; -PoolFactory::PoolFactory() - : m_attrs(new PoolAttributes), +#define DEFAULT_SERVER_PORT 40404 +#define DEFAULT_SERVER_HOST "localhost" + +constexpr const char* PoolFactory::DEFAULT_SERVER_GROUP; + +PoolFactory::PoolFactory(const Cache& cache) + : m_attrs(std::make_shared<PoolAttributes>()), m_isSubscriptionRedundancy(false), - m_addedServerOrLocator(false) {} + m_addedServerOrLocator(false), + m_cache(cache) {} PoolFactory::~PoolFactory() {} @@ -114,29 +118,27 @@ void PoolFactory::setPRSingleHopEnabled(bool enabled) { PoolPtr PoolFactory::create(const char* name) { ThinClientPoolDMPtr poolDM; { - ACE_Guard<ACE_Recursive_Thread_Mutex> guard(connectionPoolsLock); - - if (PoolManager::find(name) != nullptr) { + if (m_cache.getPoolManager().find(name) != nullptr) { throw IllegalStateException("Pool with the same name already exists"); } + if (!m_addedServerOrLocator) { + addServer(DEFAULT_SERVER_HOST, DEFAULT_SERVER_PORT); + } // Create a clone of Attr; PoolAttributesPtr copyAttrs = m_attrs->clone(); - if (CacheImpl::getInstance() == nullptr) { - throw IllegalStateException("Cache has not been created."); - } + CacheImpl* cacheImpl = CacheRegionHelper::getCacheImpl(&m_cache); - if (CacheImpl::getInstance()->isClosed()) { + if (m_cache.isClosed()) { throw CacheClosedException("Cache is closed"); } - if (CacheImpl::getInstance()->getCacheMode() && - m_isSubscriptionRedundancy) { + if (cacheImpl->getCacheMode() && m_isSubscriptionRedundancy) { LOGWARN( "At least one pool has been created so ignoring cache level " "redundancy setting"); } - TcrConnectionManager& tccm = - CacheImpl::getInstance()->tcrConnectionManager(); + TcrConnectionManager& tccm = cacheImpl->tcrConnectionManager(); + LOGDEBUG("PoolFactory::create mulitusermode = %d ", copyAttrs->getMultiuserSecureModeEnabled()); if (copyAttrs->getMultiuserSecureModeEnabled()) { @@ -172,12 +174,15 @@ PoolPtr PoolFactory::create(const char* name) { } } - connectionPools->insert({name, std::static_pointer_cast<Pool>(poolDM)}); + cacheImpl->getPoolManager().addPool(name, + std::static_pointer_cast<Pool>(poolDM)); } // TODO: poolDM->init() should not throw exceptions! // Pool DM should only be inited once. - if (DistributedSystem::getSystemProperties()->autoReadyForEvents()) { + if (m_cache.getDistributedSystem() + .getSystemProperties() + .autoReadyForEvents()) { poolDM->init(); }