Skip to content

Commit

Permalink
FOGL-6772 Add state to know if we have loaded data from the database (#…
Browse files Browse the repository at this point in the history
…770) (#773)

Signed-off-by: Mark Riddoch <[email protected]>

Co-authored-by: Mark Riddoch <[email protected]>
  • Loading branch information
ashish-jabble and MarkRiddoch authored Aug 5, 2022
1 parent ae02062 commit 9c11e12
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions C/common/include/plugin_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class PluginData

private:
StorageClient* m_storage;
bool m_dataLoaded;
};

#endif
43 changes: 35 additions & 8 deletions C/common/plugin_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using namespace rapidjson;
* PluginData constructor
* @param client StorageClient pointer
*/
PluginData::PluginData(StorageClient* client) : m_storage(client)
PluginData::PluginData(StorageClient* client) : m_storage(client), m_dataLoaded(false)
{
}

Expand All @@ -43,6 +43,7 @@ string PluginData::loadStoredData(const string& key)
ResultSet* pluginData = m_storage->queryTable("plugin_data", wKey);
if (pluginData != NULL && pluginData->rowCount())
{
m_dataLoaded = true;
// Get the first row only
ResultSet::RowIterator it = pluginData->firstRow();
// Access the element
Expand Down Expand Up @@ -109,23 +110,49 @@ bool PluginData::persistPluginData(const string& key,
InsertValues updateData;
updateData.push_back(InsertValue("data", JSONData));

// Try update first
if (m_storage->updateTable("plugin_data",
updateData,
wKey) == -1)
if (m_dataLoaded)
{
// Update filure: try insert
// Try update first
if (m_storage->updateTable("plugin_data",
updateData,
wKey) == -1)
{
// Update filure: try insert
InsertValues insertData;
insertData.push_back(InsertValue("key", key));
insertData.push_back(InsertValue("data", JSONData));

if (m_storage->insertTable("plugin_data",
insertData) == -1)
{
ret = false;
}
}
}
else
{ // We didn't load the data so do an insert first
InsertValues insertData;
insertData.push_back(InsertValue("key", key));
insertData.push_back(InsertValue("data", JSONData));

if (m_storage->insertTable("plugin_data",
insertData) == -1)
{
ret = false;
// The insert failed, so try an update before giving up
if (m_storage->updateTable("plugin_data", updateData, wKey) == -1)
{
ret = false;
}
}
else
{
m_dataLoaded = true; // Data is now in the database
}
}

Logger::getLogger()->warn("Failed to persist data for %s, unable to insert into storage", key.c_str());
if (!ret)
{
Logger::getLogger()->warn("Failed to persist data for %s, unable to insert into storage", key.c_str());
}
return ret;
}

0 comments on commit 9c11e12

Please sign in to comment.