You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2022/06/08 02:43:06 UTC

[james-project] 02/06: [PERF] Optimize further Email/get serialization

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 907cba24f06f65df89a5342a3628741b2fd584cd
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Mon Jun 6 11:20:38 2022 +0700

    [PERF] Optimize further Email/get serialization
    
     - Avoid computing body transformation when not needed
     - Avoid computing body transformation for each item
---
 .../james/jmap/json/EmailGetSerializer.scala       | 39 ++++++++++++++++------
 1 file changed, 28 insertions(+), 11 deletions(-)

diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/EmailGetSerializer.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/EmailGetSerializer.scala
index e866f834c3..a247daa1e2 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/EmailGetSerializer.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/EmailGetSerializer.scala
@@ -20,6 +20,7 @@
 package org.apache.james.jmap.json
 
 import eu.timepit.refined
+import eu.timepit.refined.auto._
 import org.apache.james.jmap.api.model.Size.Size
 import org.apache.james.jmap.api.model.{EmailAddress, EmailerName, Preview}
 import org.apache.james.jmap.core.Id.IdConstraint
@@ -180,17 +181,33 @@ object EmailGetSerializer {
 
   def serializeChanges(changesResponse: EmailChangesResponse): JsObject = Json.toJson(changesResponse).as[JsObject]
 
-  def serialize(emailGetResponse: EmailGetResponse, properties: Properties, bodyProperties: Properties): JsValue =
-    Json.toJson(emailGetResponse)
-      .transform((__ \ "list").json.update {
-        case JsArray(underlying) => JsSuccess(JsArray(underlying.map(js => js.transform {
-          case jsonObject: JsObject =>
-           bodyPropertiesFilteringTransformation(bodyProperties)
-             .reads(properties.filter(jsonObject))
-          case js => JsSuccess(js)
-        }.fold(_ => JsArray(underlying), o => o))))
-        case jsValue => JsSuccess(jsValue)
-      }).get
+  def serialize(emailGetResponse: EmailGetResponse, properties: Properties, bodyProperties: Properties): JsValue = {
+    if (includesBodyProperties(properties)) {
+      val bodyTransformation = bodyPropertiesFilteringTransformation(bodyProperties)
+      Json.toJson(emailGetResponse)
+        .transform((__ \ "list").json.update {
+          case JsArray(underlying) => JsSuccess(JsArray(underlying.map(js => js.transform {
+            case jsonObject: JsObject => bodyTransformation.reads(properties.filter(jsonObject))
+            case js => JsSuccess(js)
+          }.fold(_ => JsArray(underlying), o => o))))
+          case jsValue => JsSuccess(jsValue)
+        }).get
+    } else
+      Json.toJson(emailGetResponse)
+        .transform((__ \ "list").json.update {
+          case JsArray(underlying) => JsSuccess(JsArray(underlying.map(js => js.transform {
+            case jsonObject: JsObject => JsSuccess(properties.filter(jsonObject))
+            case js => JsSuccess(js)
+          }.fold(_ => JsArray(underlying), o => o))))
+          case jsValue => JsSuccess(jsValue)
+        }).get
+  }
+
+  private def includesBodyProperties(properties: Properties): Boolean =
+    properties.contains("attachments") ||
+      properties.contains("bodyStructure") ||
+      properties.contains("textBody") ||
+      properties.contains("htmlBody")
 
   private def bodyPropertiesFilteringTransformation(bodyProperties: Properties): Reads[JsValue] = {
     case serializedBody: JsObject =>


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org