diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/binsaver/bin_saver.cpp | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/binsaver/bin_saver.cpp')
-rw-r--r-- | library/cpp/binsaver/bin_saver.cpp | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/library/cpp/binsaver/bin_saver.cpp b/library/cpp/binsaver/bin_saver.cpp new file mode 100644 index 00000000000..fe0775af9f2 --- /dev/null +++ b/library/cpp/binsaver/bin_saver.cpp @@ -0,0 +1,81 @@ +#include "bin_saver.h" + +TClassFactory<IObjectBase>* pSaverClasses; +void StartRegisterSaveload() { + if (!pSaverClasses) + pSaverClasses = new TClassFactory<IObjectBase>; +} +struct SBasicChunkInit { + ~SBasicChunkInit() { + if (pSaverClasses) + delete pSaverClasses; + } +} initSaver; + +////////////////////////////////////////////////////////////////////////// +void IBinSaver::StoreObject(IObjectBase* pObject) { + if (pObject) { + Y_ASSERT(pSaverClasses->GetObjectTypeID(pObject) != -1 && "trying to save unregistered object"); + } + + ui64 ptrId = ((char*)pObject) - ((char*)nullptr); + if (StableOutput) { + ui32 id = 0; + if (pObject) { + if (!PtrIds.Get()) + PtrIds.Reset(new PtrIdHash); + PtrIdHash::iterator pFound = PtrIds->find(pObject); + if (pFound != PtrIds->end()) + id = pFound->second; + else { + id = PtrIds->ysize() + 1; + PtrIds->insert(std::make_pair(pObject, id)); + } + } + ptrId = id; + } + + DataChunk(&ptrId, sizeof(ptrId)); + if (!Objects.Get()) + Objects.Reset(new CObjectsHash); + if (ptrId != 0 && Objects->find(ptrId) == Objects->end()) { + ObjectQueue.push_back(pObject); + (*Objects)[ptrId]; + int typeId = pSaverClasses->GetObjectTypeID(pObject); + if (typeId == -1) { + fprintf(stderr, "IBinSaver: trying to save unregistered object\n"); + abort(); + } + DataChunk(&typeId, sizeof(typeId)); + } +} + +IObjectBase* IBinSaver::LoadObject() { + ui64 ptrId = 0; + DataChunk(&ptrId, sizeof(ptrId)); + if (ptrId != 0) { + if (!Objects.Get()) + Objects.Reset(new CObjectsHash); + CObjectsHash::iterator pFound = Objects->find(ptrId); + if (pFound != Objects->end()) + return pFound->second; + int typeId; + DataChunk(&typeId, sizeof(typeId)); + IObjectBase* pObj = pSaverClasses->CreateObject(typeId); + Y_ASSERT(pObj != nullptr); + if (pObj == nullptr) { + fprintf(stderr, "IBinSaver: trying to load unregistered object\n"); + abort(); + } + (*Objects)[ptrId] = pObj; + ObjectQueue.push_back(pObj); + return pObj; + } + return nullptr; +} + +IBinSaver::~IBinSaver() { + for (size_t i = 0; i < ObjectQueue.size(); ++i) { + AddPolymorphicBase(1, ObjectQueue[i]); + } +} |