diff --git a/lib/message/mu-document.cc b/lib/message/mu-document.cc index 4e4d9d24..87ddd683 100644 --- a/lib/message/mu-document.cc +++ b/lib/message/mu-document.cc @@ -38,7 +38,7 @@ const Xapian::Document& Document::xapian_document() const { if (dirty_sexp_) { - xdoc_.set_data(sexp_.to_string()); + xdoc_.set_data(sexp().to_string()); dirty_sexp_ = false; } return xdoc_; @@ -47,7 +47,7 @@ Document::xapian_document() const template void Document::put_prop(const std::string& pname, SexpType&& val) { - sexp_.put_props(pname, std::forward(val)); + cached_sexp().put_props(pname, std::forward(val)); dirty_sexp_ = true; } diff --git a/lib/message/mu-document.hh b/lib/message/mu-document.hh index 7e7d9cc9..9aef65db 100644 --- a/lib/message/mu-document.hh +++ b/lib/message/mu-document.hh @@ -1,4 +1,4 @@ -/** Copyright (C) 2022 Dirk-Jan C. Binnema +/** Copyright (C) 2022-2023 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the @@ -43,19 +43,15 @@ class Document { public: /** * Construct a message for a new Xapian Document - * */ Document() {} /** - * Construct a message document based on on existing Xapian document. + * Construct a message document based on an existing Xapian document. * * @param doc */ - Document(const Xapian::Document& doc): xdoc_{doc} { - if (auto&& s{Sexp::parse(xdoc_.get_data())}; s) - sexp_ = std::move(*s); - } + Document(const Xapian::Document& doc): xdoc_{doc} {} /** * DTOR @@ -153,7 +149,7 @@ public: * * @return the cached s-expression */ - const Sexp& sexp() const { return sexp_; } + const Sexp& sexp() const { return cached_sexp(); } /** * Generically adds an optional value, if set, to the document @@ -231,9 +227,16 @@ private: template void put_prop(const Field& field, SexpType&& val); template void put_prop(const std::string& pname, SexpType&& val); + Sexp& cached_sexp() const { + if (cached_sexp_.empty()) + if (auto&& s{Sexp::parse(xdoc_.get_data())}; s) + cached_sexp_ = std::move(*s); + return cached_sexp_; + } + mutable Xapian::Document xdoc_; - Sexp sexp_; + mutable Sexp cached_sexp_; mutable bool dirty_sexp_{}; /* xdoc's sexp is outdated */ };