Kaydet (Commit) 8410ce03 authored tarafından Ashod Nakashian's avatar Ashod Nakashian Kaydeden (comit) Ashod Nakashian

LOK: Cache JSON payloads

Change-Id: I81f74027363d4b2959c053057851cf01fc94af8b
Reviewed-on: https://gerrit.libreoffice.org/67892
Tested-by: Jenkins
Reviewed-by: 's avatarAshod Nakashian <ashnakash@gmail.com>
üst 17a477c3
...@@ -104,10 +104,16 @@ namespace desktop { ...@@ -104,10 +104,16 @@ namespace desktop {
RectangleAndPart& setRectangleAndPart(const std::string& payload); RectangleAndPart& setRectangleAndPart(const std::string& payload);
/// Return the parsed RectangleAndPart instance. /// Return the parsed RectangleAndPart instance.
const RectangleAndPart& getRectangleAndPart() const; const RectangleAndPart& getRectangleAndPart() const;
/// Parse and set the JSON object and return it. Clobbers PayloadString.
boost::property_tree::ptree& setJson(const std::string& payload);
/// Set a Json object and update PayloadString.
void setJson(const boost::property_tree::ptree& rTree);
/// Return the parsed JSON instance.
const boost::property_tree::ptree& getJson() const;
int Type; int Type;
std::string PayloadString; std::string PayloadString;
boost::variant<boost::blank, RectangleAndPart> PayloadObject; boost::variant<boost::blank, RectangleAndPart, boost::property_tree::ptree> PayloadObject;
}; };
typedef std::vector<CallbackData> queue_type; typedef std::vector<CallbackData> queue_type;
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <memory> #include <memory>
#include <iostream> #include <iostream>
#include <boost/property_tree/json_parser.hpp> #include <boost/property_tree/json_parser.hpp>
#include <boost/algorithm/string.hpp>
#include <LibreOfficeKit/LibreOfficeKit.h> #include <LibreOfficeKit/LibreOfficeKit.h>
#include <LibreOfficeKit/LibreOfficeKitEnums.h> #include <LibreOfficeKit/LibreOfficeKitEnums.h>
...@@ -466,6 +467,33 @@ const RectangleAndPart& CallbackFlushHandler::CallbackData::getRectangleAndPart( ...@@ -466,6 +467,33 @@ const RectangleAndPart& CallbackFlushHandler::CallbackData::getRectangleAndPart(
return boost::get<RectangleAndPart>(PayloadObject); return boost::get<RectangleAndPart>(PayloadObject);
} }
boost::property_tree::ptree& CallbackFlushHandler::CallbackData::setJson(const std::string& payload)
{
boost::property_tree::ptree aTree;
std::stringstream aStream(payload);
boost::property_tree::read_json(aStream, aTree);
// Let boost normalize the payload so it always matches the cache.
setJson(aTree);
return boost::get<boost::property_tree::ptree>(PayloadObject);
}
void CallbackFlushHandler::CallbackData::setJson(const boost::property_tree::ptree& rTree)
{
std::stringstream aJSONStream;
constexpr bool bPretty = false; // Don't waste time and bloat logs.
boost::property_tree::write_json(aJSONStream, rTree, bPretty);
PayloadString = boost::trim_copy(aJSONStream.str());
PayloadObject = rTree;
}
const boost::property_tree::ptree& CallbackFlushHandler::CallbackData::getJson() const
{
return boost::get<boost::property_tree::ptree>(PayloadObject);
}
} }
namespace { namespace {
...@@ -1155,9 +1183,7 @@ void CallbackFlushHandler::queue(const int type, const char* data) ...@@ -1155,9 +1183,7 @@ void CallbackFlushHandler::queue(const int type, const char* data)
case LOK_CALLBACK_WINDOW: case LOK_CALLBACK_WINDOW:
{ {
// reading JSON by boost might be slow? // reading JSON by boost might be slow?
boost::property_tree::ptree aTree; boost::property_tree::ptree& aTree = aCallbackData.setJson(payload);
std::stringstream aStream(payload);
boost::property_tree::read_json(aStream, aTree);
const unsigned nLOKWindowId = aTree.get<unsigned>("id", 0); const unsigned nLOKWindowId = aTree.get<unsigned>("id", 0);
if (aTree.get<std::string>("action", "") == "invalidate") if (aTree.get<std::string>("action", "") == "invalidate")
{ {
...@@ -1169,9 +1195,7 @@ void CallbackFlushHandler::queue(const int type, const char* data) ...@@ -1169,9 +1195,7 @@ void CallbackFlushHandler::queue(const int type, const char* data)
removeAll([&nLOKWindowId] (const queue_type::value_type& elem) { removeAll([&nLOKWindowId] (const queue_type::value_type& elem) {
if (elem.Type == LOK_CALLBACK_WINDOW) if (elem.Type == LOK_CALLBACK_WINDOW)
{ {
boost::property_tree::ptree aOldTree; const boost::property_tree::ptree& aOldTree = elem.getJson();
std::stringstream aOldStream(elem.PayloadString);
boost::property_tree::read_json(aOldStream, aOldTree);
const unsigned nOldDialogId = aOldTree.get<unsigned>("id", 0); const unsigned nOldDialogId = aOldTree.get<unsigned>("id", 0);
if (aOldTree.get<std::string>("action", "") == "invalidate" && if (aOldTree.get<std::string>("action", "") == "invalidate" &&
nLOKWindowId == nOldDialogId) nLOKWindowId == nOldDialogId)
...@@ -1186,15 +1210,13 @@ void CallbackFlushHandler::queue(const int type, const char* data) ...@@ -1186,15 +1210,13 @@ void CallbackFlushHandler::queue(const int type, const char* data)
{ {
// if we have to invalidate all of the window, ignore // if we have to invalidate all of the window, ignore
// any part invalidation message // any part invalidation message
auto invAllExist = std::any_of(m_queue.rbegin(), m_queue.rend(), const auto invAllExist = std::any_of(m_queue.rbegin(), m_queue.rend(),
[&nLOKWindowId] (const queue_type::value_type& elem) [&nLOKWindowId] (const queue_type::value_type& elem)
{ {
if (elem.Type != LOK_CALLBACK_WINDOW) if (elem.Type != LOK_CALLBACK_WINDOW)
return false; return false;
boost::property_tree::ptree aOldTree; const boost::property_tree::ptree& aOldTree = elem.getJson();
std::stringstream aOldStream(elem.PayloadString);
boost::property_tree::read_json(aOldStream, aOldTree);
const unsigned nOldDialogId = aOldTree.get<unsigned>("id", 0); const unsigned nOldDialogId = aOldTree.get<unsigned>("id", 0);
return aOldTree.get<std::string>("action", "") == "invalidate" && return aOldTree.get<std::string>("action", "") == "invalidate" &&
nLOKWindowId == nOldDialogId && nLOKWindowId == nOldDialogId &&
...@@ -1218,9 +1240,7 @@ void CallbackFlushHandler::queue(const int type, const char* data) ...@@ -1218,9 +1240,7 @@ void CallbackFlushHandler::queue(const int type, const char* data)
if (elem.Type != LOK_CALLBACK_WINDOW) if (elem.Type != LOK_CALLBACK_WINDOW)
return false; return false;
boost::property_tree::ptree aOldTree; const boost::property_tree::ptree& aOldTree = elem.getJson();
std::stringstream aOldStream(elem.PayloadString);
boost::property_tree::read_json(aOldStream, aOldTree);
if (aOldTree.get<std::string>("action", "") == "invalidate") if (aOldTree.get<std::string>("action", "") == "invalidate")
{ {
const unsigned nOldDialogId = aOldTree.get<unsigned>("id", 0); const unsigned nOldDialogId = aOldTree.get<unsigned>("id", 0);
...@@ -1273,9 +1293,7 @@ void CallbackFlushHandler::queue(const int type, const char* data) ...@@ -1273,9 +1293,7 @@ void CallbackFlushHandler::queue(const int type, const char* data)
} }
aTree.put("rectangle", aNewRect.toString().getStr()); aTree.put("rectangle", aNewRect.toString().getStr());
std::stringstream aJSONStream; aCallbackData.setJson(aTree);
boost::property_tree::write_json(aJSONStream, aTree);
payload = aJSONStream.str();
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment