Added save session feature in the Software Integration module

This commit is contained in:
Victor Lindquist
2022-06-14 02:00:00 -06:00
parent d341961c40
commit f7e5dcf2e2
17 changed files with 812 additions and 354 deletions

View File

@@ -30,7 +30,9 @@
#include <ghoul/logging/logmanager.h>
namespace {
constexpr const char* _loggerCat = "SyncableFloatDataStorage";
constexpr const char* _loggerCat = "SyncableFloatDataStorage";
} // namespace
namespace openspace {
@@ -39,206 +41,209 @@ using namespace softwareintegration;
/* ============== SyncEngine functions ============== */
void SyncableFloatDataStorage::encode(SyncBuffer* syncBuffer) {
ZoneScopedN("SyncableFloatDataStorage::encode")
ZoneScopedN("SyncableFloatDataStorage::encode");
std::lock_guard guard(_mutex);
size_t nSGNs = _storage.size();
syncBuffer->encode(nSGNs);
for (auto& [identifier, sgnStorage] : _storage) {
syncBuffer->encode(identifier.size());
syncBuffer->encode(identifier);
size_t nStorageEntries = sgnStorage.size();
syncBuffer->encode(nStorageEntries);
for (auto& [key, storageEntry] : sgnStorage) {
bool isSyncDirty = storageEntry.syncDirty;
syncBuffer->encode(isSyncDirty);
if (!isSyncDirty) continue;
syncBuffer->encode(static_cast<uint32_t>(key));
size_t nValues = storageEntry.data.size();
syncBuffer->encode(nValues);
// Go trough all data in data entry.
// Sequentially structured as: x1, y1, z1, x2, y2, z2...
for (auto val : storageEntry.data) {
syncBuffer->encode(val);
}
// TODO: Maybe combine solution with syncDirty?
storageEntry.hasEncoded = true;
}
}
encodeStorage(syncBuffer);
}
void SyncableFloatDataStorage::decode(SyncBuffer* syncBuffer) {
ZoneScopedN("SyncableFloatDataStorage::decode")
ZoneScopedN("SyncableFloatDataStorage::decode");
std::lock_guard guard(_mutex);
size_t nSGNs;
syncBuffer->decode(nSGNs);
for (size_t i = 0; i < nSGNs; ++i) {
size_t identifierLength;
syncBuffer->decode(identifierLength);
std::string identifier;
identifier.resize(identifierLength);
syncBuffer->decode(identifier);
size_t nStorageEntries;
syncBuffer->decode(nStorageEntries);
for (size_t j = 0; j < nStorageEntries; ++j) {
bool isSyncDirty;
syncBuffer->decode(isSyncDirty);
if (!isSyncDirty) continue;
uint32_t keyRaw;
syncBuffer->decode(keyRaw);
auto key = static_cast<storage::Key>(keyRaw);
size_t nValues;
syncBuffer->decode(nValues);
std::vector<float> dataEntry{};
dataEntry.reserve(nValues);
for (size_t k = 0; k < nValues; ++k) {
float value;
syncBuffer->decode(value);
dataEntry.push_back(std::move(value));
}
insertAssign(identifier, key, Value{ dataEntry });
}
}
decodeStorage(syncBuffer);
}
void SyncableFloatDataStorage::postSync(bool isMaster) {
if (isMaster) {
std::lock_guard guard(_mutex);
for (auto& sgnStorage : _storage) {
for (auto& storageEntry : sgnStorage.second) {
if (storageEntry.second.syncDirty && storageEntry.second.hasEncoded) {
storageEntry.second.syncDirty = false;
storageEntry.second.hasEncoded = false;
}
}
}
}
if (isMaster) {
std::lock_guard guard(_mutex);
for (auto& sgnStorage : _storage) {
for (auto& storageEntry : sgnStorage.second) {
if (storageEntry.second.syncDirty && storageEntry.second.hasEncoded) {
storageEntry.second.syncDirty = false;
storageEntry.second.hasEncoded = false;
}
}
}
}
}
/* ================================================== */
const SyncableFloatDataStorage::ValueData& SyncableFloatDataStorage::fetch(
const Identifier& identifier, const storage::Key key
) {
LDEBUG(fmt::format("Loading data from float data storage: {}-{}", identifier, storage::getStorageKeyString(key)));
std::lock_guard guard(_mutex);
if (!count(identifier)) {
LERROR(fmt::format(
"Could not find any data for SceneGraphNode '{}' in the centralized data storage", identifier
));
return ValueData{};
}
const SyncableFloatDataStorage::ValueData& SyncableFloatDataStorage::fetch(const Identifier& identifier,
const storage::Key key) {
LDEBUG(fmt::format("Loading data from float data storage: {}-{}", identifier, storage::getStorageKeyString(key)));
std::lock_guard guard(_mutex);
if (!count(identifier)) {
LERROR(fmt::format("Could not find any data for SceneGraphNode '{}' in the centralized data storage",
identifier));
return ValueData{};
}
if (!count(identifier, key)) {
LERROR(fmt::format(
"SceneGraphNode {} has no data with key '{}' in the centralized data storage",
identifier,
storage::getStorageKeyString(key)
));
return ValueData{};
}
if (!count(identifier, key)) {
LERROR(fmt::format("SceneGraphNode {} has no data with key '{}' in the centralized data storage", identifier,
storage::getStorageKeyString(key)));
return ValueData{};
}
auto& value = _storage.find(identifier)->second.find(key)->second;
auto& value = _storage.find(identifier)->second.find(key)->second;
value.dirty = false;
value.dirty = false;
return value.data;
return value.data;
}
bool SyncableFloatDataStorage::isDirty(
const Identifier& identifier, const storage::Key key
) {
if (!count(identifier, key)) {
return false;
}
bool SyncableFloatDataStorage::isDirty(const Identifier& identifier, const storage::Key key) {
if (!count(identifier, key)) {
return false;
}
return _storage.find(identifier)->second.find(key)->second.dirty;
return _storage.find(identifier)->second.find(key)->second.dirty;
}
bool SyncableFloatDataStorage::isSyncDirty(
const Identifier& identifier, const storage::Key key
) {
if (!count(identifier, key)) {
return true;
}
bool SyncableFloatDataStorage::isSyncDirty(const Identifier& identifier, const storage::Key key) {
if (!count(identifier, key)) {
return true;
}
return _storage.find(identifier)->second.find(key)->second.syncDirty;
return _storage.find(identifier)->second.find(key)->second.syncDirty;
}
void SyncableFloatDataStorage::store(
const Identifier& identifier, const storage::Key key, const ValueData& data
) {
LDEBUG(fmt::format("Storing data in float data storage: {}-{}", identifier, storage::getStorageKeyString(key)));
std::lock_guard guard(_mutex);
insertAssign(identifier, key, { data });
void SyncableFloatDataStorage::store(const Identifier& identifier, const storage::Key key, const ValueData& data) {
LDEBUG(fmt::format("Storing data in float data storage: {}-{}", identifier, storage::getStorageKeyString(key)));
std::lock_guard guard(_mutex);
insertAssign(identifier, key, { data });
}
void SyncableFloatDataStorage::encodeStorage(SyncBuffer* syncBuffer, bool skipNonSynced) {
std::lock_guard guard(_mutex);
syncBuffer->encode(static_cast<uint16_t>(_storage.size()));
for (auto& [identifier, sgnStorage] : _storage) {
syncBuffer->encode(identifier);
syncBuffer->encode(static_cast<uint16_t>(sgnStorage.size()));
for (auto& [key, storageEntry] : sgnStorage) {
if (skipNonSynced) {
bool isSyncDirty = storageEntry.syncDirty;
syncBuffer->encode(isSyncDirty);
if (!isSyncDirty) continue;
}
syncBuffer->encode(static_cast<uint8_t>(key));
syncBuffer->encode(storageEntry.data);
// TODO: Maybe combine solution with syncDirty?
storageEntry.hasEncoded = true;
}
}
}
void SyncableFloatDataStorage::decodeStorage(SyncBuffer* syncBuffer, bool skipNonSynced) {
std::lock_guard guard(_mutex);
uint16_t nSGNs;
syncBuffer->decode(nSGNs);
for (uint16_t i = 0; i < nSGNs; ++i) {
std::string identifier;
syncBuffer->decode(identifier);
uint16_t nStorageEntries;
syncBuffer->decode(nStorageEntries);
for (uint16_t j = 0; j < nStorageEntries; ++j) {
if (skipNonSynced) {
bool isSyncDirty;
syncBuffer->decode(isSyncDirty);
if (!isSyncDirty) continue;
}
uint8_t keyRaw;
syncBuffer->decode(keyRaw);
auto key = static_cast<storage::Key>(keyRaw);
std::vector<float> dataEntry{};
syncBuffer->decode(dataEntry);
insertAssign(identifier, key, Value{ dataEntry });
}
}
}
void SyncableFloatDataStorage::store(const std::vector<std::byte>& storageDump) {
ZoneScopedN("SyncableFloatDataStorage::store");
auto syncBuffer = new SyncBuffer{ 0 };
syncBuffer->setData(storageDump);
decodeStorage(syncBuffer, false);
}
void SyncableFloatDataStorage::dump(std::vector<std::byte>& storageDump) {
ZoneScopedN("SyncableFloatDataStorage::dump");
auto syncBuffer = new SyncBuffer{ 0 };
encodeStorage(syncBuffer, false);
storageDump = syncBuffer->data();
}
/* =============== Utility functions ================ */
bool SyncableFloatDataStorage::erase(const Identifier& identifier, const storage::Key key) {
if (count(identifier, key) == 0) return false;
if (count(identifier, key) == 0) return false;
auto nErased = _storage.find(identifier)->second.erase(key);
return nErased > 0;
auto nErased = _storage.find(identifier)->second.erase(key);
return nErased > 0;
}
void SyncableFloatDataStorage::insertAssign(const Identifier& identifier, const storage::Key key, const Value& value) {
if (count(identifier)) {
if (count(identifier, key)) {
_storage.find(identifier)->second.find(key)->second = value;
}
else {
_storage.find(identifier)->second.emplace(key, value);
}
}
else {
SceneStorage newSceneStorage{ { key, value } };
_storage.emplace(identifier, std::move(newSceneStorage));
}
if (count(identifier)) {
if (count(identifier, key)) {
_storage.find(identifier)->second.find(key)->second = value;
}
else {
_storage.find(identifier)->second.emplace(key, value);
}
}
else {
SceneStorage newSceneStorage{ { key, value } };
_storage.emplace(identifier, std::move(newSceneStorage));
}
}
size_t SyncableFloatDataStorage::count(const Identifier& identifier) {
return _storage.count(identifier);
return _storage.count(identifier);
}
size_t SyncableFloatDataStorage::count(const Identifier& identifier, const storage::Key key) {
auto sceneIt = _storage.find(identifier);
if (sceneIt == _storage.end()) return 0;
auto sceneIt = _storage.find(identifier);
if (sceneIt == _storage.end()) return 0;
return sceneIt->second.count(key);
return sceneIt->second.count(key);
}
// Helper function for debugging
std::vector<SyncableFloatDataStorage::Identifier> SyncableFloatDataStorage::getAllIdentifiers() {
std::vector<Identifier> identifiers;
identifiers.reserve(_storage.size());
for (auto [identifier, sceneStorage] : _storage) {
identifiers.push_back(identifier);
}
return std::move(identifiers);
}
// Helper function for debugging
std::string SyncableFloatDataStorage::getStringOfAllKeysInStorage() {
std::string keysString;
for (auto [id, sceneStorage]: _storage) {
keysString += '(' + id + ')' + ": ";
for(auto [key, val]: sceneStorage) {
keysString += storage::getStorageKeyString(key) + " ";
}
keysString += '\n';
}
return keysString;
std::string keysString;
for (auto [id, sceneStorage] : _storage) {
keysString += '(' + id + ')' + ": ";
for (auto [key, val] : sceneStorage) {
keysString += storage::getStorageKeyString(key) + " ";
}
keysString += '\n';
}
return keysString;
}
/* ================================================== */
} // namespace openspace
} // namespace openspace