summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAshod Nakashian <ashod.nakashian@collabora.co.uk>2019-02-14 10:52:33 -0500
committerAshod Nakashian <ashnakash@gmail.com>2019-02-22 15:37:33 +0100
commit8410ce037ab27a38b0449c7666f1676374803496 (patch)
treea11d31e2c2987b1f0eb182af25146e1405a9c40b
parent17a477c35cc43f37946dcea2b7c27f0f6571e118 (diff)
LOK: Cache JSON payloads
Change-Id: I81f74027363d4b2959c053057851cf01fc94af8b Reviewed-on: https://gerrit.libreoffice.org/67892 Tested-by: Jenkins Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
-rw-r--r--desktop/inc/lib/init.hxx8
-rw-r--r--desktop/source/lib/init.cxx50
2 files changed, 41 insertions, 17 deletions
diff --git a/desktop/inc/lib/init.hxx b/desktop/inc/lib/init.hxx
index 66696757292a..f2f6f5399649 100644
--- a/desktop/inc/lib/init.hxx
+++ b/desktop/inc/lib/init.hxx
@@ -104,10 +104,16 @@ namespace desktop {
RectangleAndPart& setRectangleAndPart(const std::string& payload);
/// Return the parsed RectangleAndPart instance.
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;
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;
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index d2e732e53497..6b7f8d699e8d 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -28,6 +28,7 @@
#include <memory>
#include <iostream>
#include <boost/property_tree/json_parser.hpp>
+#include <boost/algorithm/string.hpp>
#include <LibreOfficeKit/LibreOfficeKit.h>
#include <LibreOfficeKit/LibreOfficeKitEnums.h>
@@ -466,6 +467,33 @@ const RectangleAndPart& CallbackFlushHandler::CallbackData::getRectangleAndPart(
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 {
@@ -1155,9 +1183,7 @@ void CallbackFlushHandler::queue(const int type, const char* data)
case LOK_CALLBACK_WINDOW:
{
// reading JSON by boost might be slow?
- boost::property_tree::ptree aTree;
- std::stringstream aStream(payload);
- boost::property_tree::read_json(aStream, aTree);
+ boost::property_tree::ptree& aTree = aCallbackData.setJson(payload);
const unsigned nLOKWindowId = aTree.get<unsigned>("id", 0);
if (aTree.get<std::string>("action", "") == "invalidate")
{
@@ -1169,9 +1195,7 @@ void CallbackFlushHandler::queue(const int type, const char* data)
removeAll([&nLOKWindowId] (const queue_type::value_type& elem) {
if (elem.Type == LOK_CALLBACK_WINDOW)
{
- boost::property_tree::ptree aOldTree;
- std::stringstream aOldStream(elem.PayloadString);
- boost::property_tree::read_json(aOldStream, aOldTree);
+ const boost::property_tree::ptree& aOldTree = elem.getJson();
const unsigned nOldDialogId = aOldTree.get<unsigned>("id", 0);
if (aOldTree.get<std::string>("action", "") == "invalidate" &&
nLOKWindowId == nOldDialogId)
@@ -1186,15 +1210,13 @@ void CallbackFlushHandler::queue(const int type, const char* data)
{
// if we have to invalidate all of the window, ignore
// 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)
{
if (elem.Type != LOK_CALLBACK_WINDOW)
return false;
- boost::property_tree::ptree aOldTree;
- std::stringstream aOldStream(elem.PayloadString);
- boost::property_tree::read_json(aOldStream, aOldTree);
+ const boost::property_tree::ptree& aOldTree = elem.getJson();
const unsigned nOldDialogId = aOldTree.get<unsigned>("id", 0);
return aOldTree.get<std::string>("action", "") == "invalidate" &&
nLOKWindowId == nOldDialogId &&
@@ -1218,9 +1240,7 @@ void CallbackFlushHandler::queue(const int type, const char* data)
if (elem.Type != LOK_CALLBACK_WINDOW)
return false;
- boost::property_tree::ptree aOldTree;
- std::stringstream aOldStream(elem.PayloadString);
- boost::property_tree::read_json(aOldStream, aOldTree);
+ const boost::property_tree::ptree& aOldTree = elem.getJson();
if (aOldTree.get<std::string>("action", "") == "invalidate")
{
const unsigned nOldDialogId = aOldTree.get<unsigned>("id", 0);
@@ -1273,9 +1293,7 @@ void CallbackFlushHandler::queue(const int type, const char* data)
}
aTree.put("rectangle", aNewRect.toString().getStr());
- std::stringstream aJSONStream;
- boost::property_tree::write_json(aJSONStream, aTree);
- payload = aJSONStream.str();
+ aCallbackData.setJson(aTree);
}
}
}