You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by "hungphan227 (via GitHub)" <gi...@apache.org> on 2024/02/16 10:46:05 UTC

[PR] JAMES-2586 Implement PostgresEmailQueryView [james-project]

hungphan227 opened a new pull request, #2007:
URL: https://github.com/apache/james-project/pull/2007

   (no comment)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] JAMES-2586 Implement PostgresEmailQueryView [james-project]

Posted by "vttranlina (via GitHub)" <gi...@apache.org>.
vttranlina commented on code in PR #2007:
URL: https://github.com/apache/james-project/pull/2007#discussion_r1494459114


##########
server/data/data-jmap/src/test/java/org/apache/james/jmap/api/projections/EmailQueryViewContract.java:
##########
@@ -200,7 +200,7 @@ default void datesCanBeDuplicated() {
         testee().save(mailboxId1(), DATE_1, DATE_2, messageId2()).block();
 
         assertThat(testee().listMailboxContentSortedBySentAt(mailboxId1(), Limit.limit(12)).collectList().block())
-            .containsExactly(messageId1(), messageId2());
+            .containsExactlyInAnyOrder(messageId1(), messageId2());

Review Comment:
   Be careful it
   The method name is `listMailboxContentSortedBySentAt` 
   And the "***AnyOrder" will be ignored the "sorted"



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] JAMES-2586 Implement PostgresEmailQueryView [james-project]

Posted by "chibenwa (via GitHub)" <gi...@apache.org>.
chibenwa commented on code in PR #2007:
URL: https://github.com/apache/james-project/pull/2007#discussion_r1492695291


##########
server/data/data-jmap-postgres/src/main/java/org/apache/james/jmap/postgres/projections/PostgresEmailQueryViewDAO.java:
##########
@@ -0,0 +1,145 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.postgres.projections;
+
+import static org.apache.james.jmap.postgres.projections.PostgresEmailQueryViewModule.PostgresEmailQueryViewTable.MAILBOX_ID;
+import static org.apache.james.jmap.postgres.projections.PostgresEmailQueryViewModule.PostgresEmailQueryViewTable.MESSAGE_ID;
+import static org.apache.james.jmap.postgres.projections.PostgresEmailQueryViewModule.PostgresEmailQueryViewTable.PK_CONSTRAINT_NAME;
+import static org.apache.james.jmap.postgres.projections.PostgresEmailQueryViewModule.PostgresEmailQueryViewTable.RECEIVED_AT;
+import static org.apache.james.jmap.postgres.projections.PostgresEmailQueryViewModule.PostgresEmailQueryViewTable.SENT_AT;
+import static org.apache.james.jmap.postgres.projections.PostgresEmailQueryViewModule.PostgresEmailQueryViewTable.TABLE_NAME;
+
+import java.time.ZonedDateTime;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import org.apache.james.backends.postgres.utils.PostgresExecutor;
+import org.apache.james.mailbox.model.MessageId;
+import org.apache.james.mailbox.postgres.PostgresMailboxId;
+import org.apache.james.mailbox.postgres.PostgresMessageId;
+import org.apache.james.util.streams.Limit;
+
+import com.google.common.base.Preconditions;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+public class PostgresEmailQueryViewDAO {
+    private PostgresExecutor postgresExecutor;
+
+    @Inject
+    public PostgresEmailQueryViewDAO(@Named(PostgresExecutor.NON_RLS_INJECT) PostgresExecutor postgresExecutor) {
+        this.postgresExecutor = postgresExecutor;
+    }
+
+    public Flux<MessageId> listMailboxContentSortedBySentAt(PostgresMailboxId mailboxId, Limit limit) {
+        Preconditions.checkArgument(!limit.isUnlimited(), "Limit should be defined");
+
+        return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_ID)
+                .from(TABLE_NAME)
+                .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                .orderBy(SENT_AT.desc())
+                .limit(limit.getLimit().get())))
+            .map(record -> PostgresMessageId.Factory.of(record.get(MESSAGE_ID)));
+    }
+
+    public Flux<MessageId> listMailboxContentSortedByReceivedAt(PostgresMailboxId mailboxId, Limit limit) {
+        Preconditions.checkArgument(!limit.isUnlimited(), "Limit should be defined");
+
+        return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_ID)
+                .from(TABLE_NAME)
+                .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                .orderBy(RECEIVED_AT.desc())
+                .limit(limit.getLimit().get())))
+            .map(record -> PostgresMessageId.Factory.of(record.get(MESSAGE_ID)));
+    }
+
+    public Flux<MessageId> listMailboxContentSinceAfterSortedBySentAt(PostgresMailboxId mailboxId, ZonedDateTime since, Limit limit) {
+        Preconditions.checkArgument(!limit.isUnlimited(), "Limit should be defined");
+
+        return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_ID)
+                .from(TABLE_NAME)
+                .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                .and(RECEIVED_AT.greaterOrEqual(since.toOffsetDateTime()))
+                .orderBy(SENT_AT.desc())
+                .limit(limit.getLimit().get())))
+            .map(record -> PostgresMessageId.Factory.of(record.get(MESSAGE_ID)));
+    }
+
+    public Flux<MessageId> listMailboxContentSinceAfterSortedByReceivedAt(PostgresMailboxId mailboxId, ZonedDateTime since, Limit limit) {
+        Preconditions.checkArgument(!limit.isUnlimited(), "Limit should be defined");
+
+        return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_ID)
+                .from(TABLE_NAME)
+                .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                .and(RECEIVED_AT.greaterOrEqual(since.toOffsetDateTime()))
+                .orderBy(RECEIVED_AT.desc())
+                .limit(limit.getLimit().get())))
+            .map(record -> PostgresMessageId.Factory.of(record.get(MESSAGE_ID)));
+    }
+
+    public Flux<MessageId> listMailboxContentBeforeSortedByReceivedAt(PostgresMailboxId mailboxId, ZonedDateTime since, Limit limit) {
+        Preconditions.checkArgument(!limit.isUnlimited(), "Limit should be defined");
+
+        return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_ID)
+                .from(TABLE_NAME)
+                .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                .and(RECEIVED_AT.lessOrEqual(since.toOffsetDateTime()))
+                .orderBy(RECEIVED_AT.desc())
+                .limit(limit.getLimit().get())))
+            .map(record -> PostgresMessageId.Factory.of(record.get(MESSAGE_ID)));
+    }
+
+    public Flux<MessageId> listMailboxContentSinceSentAt(PostgresMailboxId mailboxId, ZonedDateTime since, Limit limit) {
+        Preconditions.checkArgument(!limit.isUnlimited(), "Limit should be defined");
+
+        return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_ID)
+                .from(TABLE_NAME)
+                .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                .and(SENT_AT.greaterOrEqual(since.toOffsetDateTime()))
+                .orderBy(SENT_AT.desc())
+                .limit(limit.getLimit().get())))
+            .map(record -> PostgresMessageId.Factory.of(record.get(MESSAGE_ID)));
+    }
+
+    public Mono<Void> delete(PostgresMailboxId mailboxId, PostgresMessageId messageId) {
+        return postgresExecutor.executeVoid(dslContext -> Mono.from(dslContext.deleteFrom(TABLE_NAME)
+            .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+            .and(MESSAGE_ID.eq(messageId.asUuid()))));
+    }
+
+    public Mono<Void> delete(PostgresMailboxId mailboxId) {
+        return postgresExecutor.executeVoid(dslContext -> Mono.from(dslContext.deleteFrom(TABLE_NAME)
+            .where(MAILBOX_ID.eq(mailboxId.asUuid()))));
+    }
+
+    public Mono<Void> save(PostgresMailboxId mailboxId, ZonedDateTime sentAt, ZonedDateTime receivedAt, PostgresMessageId messageId) {
+        return postgresExecutor.executeVoid(dslContext -> Mono.from(dslContext.insertInto(TABLE_NAME)
+            .set(MAILBOX_ID, mailboxId.asUuid())
+            .set(MESSAGE_ID, messageId.asUuid())
+            .set(SENT_AT, sentAt.toOffsetDateTime())
+            .set(RECEIVED_AT, receivedAt.toOffsetDateTime())
+            .onConflictOnConstraint(PK_CONSTRAINT_NAME)
+            .doUpdate()
+            .set(SENT_AT, sentAt.toOffsetDateTime())
+            .set(RECEIVED_AT, receivedAt.toOffsetDateTime())));

Review Comment:
   This is supposed to be indempotent so I bet in case of conflict we can just do nothing?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] JAMES-2586 Implement PostgresEmailQueryView [james-project]

Posted by "hungphan227 (via GitHub)" <gi...@apache.org>.
hungphan227 commented on code in PR #2007:
URL: https://github.com/apache/james-project/pull/2007#discussion_r1493976392


##########
server/data/data-jmap-postgres/src/main/java/org/apache/james/jmap/postgres/projections/PostgresEmailQueryViewDAO.java:
##########
@@ -0,0 +1,145 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.postgres.projections;
+
+import static org.apache.james.jmap.postgres.projections.PostgresEmailQueryViewModule.PostgresEmailQueryViewTable.MAILBOX_ID;
+import static org.apache.james.jmap.postgres.projections.PostgresEmailQueryViewModule.PostgresEmailQueryViewTable.MESSAGE_ID;
+import static org.apache.james.jmap.postgres.projections.PostgresEmailQueryViewModule.PostgresEmailQueryViewTable.PK_CONSTRAINT_NAME;
+import static org.apache.james.jmap.postgres.projections.PostgresEmailQueryViewModule.PostgresEmailQueryViewTable.RECEIVED_AT;
+import static org.apache.james.jmap.postgres.projections.PostgresEmailQueryViewModule.PostgresEmailQueryViewTable.SENT_AT;
+import static org.apache.james.jmap.postgres.projections.PostgresEmailQueryViewModule.PostgresEmailQueryViewTable.TABLE_NAME;
+
+import java.time.ZonedDateTime;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import org.apache.james.backends.postgres.utils.PostgresExecutor;
+import org.apache.james.mailbox.model.MessageId;
+import org.apache.james.mailbox.postgres.PostgresMailboxId;
+import org.apache.james.mailbox.postgres.PostgresMessageId;
+import org.apache.james.util.streams.Limit;
+
+import com.google.common.base.Preconditions;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+public class PostgresEmailQueryViewDAO {
+    private PostgresExecutor postgresExecutor;
+
+    @Inject
+    public PostgresEmailQueryViewDAO(@Named(PostgresExecutor.NON_RLS_INJECT) PostgresExecutor postgresExecutor) {
+        this.postgresExecutor = postgresExecutor;
+    }
+
+    public Flux<MessageId> listMailboxContentSortedBySentAt(PostgresMailboxId mailboxId, Limit limit) {
+        Preconditions.checkArgument(!limit.isUnlimited(), "Limit should be defined");
+
+        return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_ID)
+                .from(TABLE_NAME)
+                .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                .orderBy(SENT_AT.desc())
+                .limit(limit.getLimit().get())))
+            .map(record -> PostgresMessageId.Factory.of(record.get(MESSAGE_ID)));
+    }
+
+    public Flux<MessageId> listMailboxContentSortedByReceivedAt(PostgresMailboxId mailboxId, Limit limit) {
+        Preconditions.checkArgument(!limit.isUnlimited(), "Limit should be defined");
+
+        return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_ID)
+                .from(TABLE_NAME)
+                .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                .orderBy(RECEIVED_AT.desc())
+                .limit(limit.getLimit().get())))
+            .map(record -> PostgresMessageId.Factory.of(record.get(MESSAGE_ID)));
+    }
+
+    public Flux<MessageId> listMailboxContentSinceAfterSortedBySentAt(PostgresMailboxId mailboxId, ZonedDateTime since, Limit limit) {
+        Preconditions.checkArgument(!limit.isUnlimited(), "Limit should be defined");
+
+        return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_ID)
+                .from(TABLE_NAME)
+                .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                .and(RECEIVED_AT.greaterOrEqual(since.toOffsetDateTime()))
+                .orderBy(SENT_AT.desc())
+                .limit(limit.getLimit().get())))
+            .map(record -> PostgresMessageId.Factory.of(record.get(MESSAGE_ID)));
+    }
+
+    public Flux<MessageId> listMailboxContentSinceAfterSortedByReceivedAt(PostgresMailboxId mailboxId, ZonedDateTime since, Limit limit) {
+        Preconditions.checkArgument(!limit.isUnlimited(), "Limit should be defined");
+
+        return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_ID)
+                .from(TABLE_NAME)
+                .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                .and(RECEIVED_AT.greaterOrEqual(since.toOffsetDateTime()))
+                .orderBy(RECEIVED_AT.desc())
+                .limit(limit.getLimit().get())))
+            .map(record -> PostgresMessageId.Factory.of(record.get(MESSAGE_ID)));
+    }
+
+    public Flux<MessageId> listMailboxContentBeforeSortedByReceivedAt(PostgresMailboxId mailboxId, ZonedDateTime since, Limit limit) {
+        Preconditions.checkArgument(!limit.isUnlimited(), "Limit should be defined");
+
+        return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_ID)
+                .from(TABLE_NAME)
+                .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                .and(RECEIVED_AT.lessOrEqual(since.toOffsetDateTime()))
+                .orderBy(RECEIVED_AT.desc())
+                .limit(limit.getLimit().get())))
+            .map(record -> PostgresMessageId.Factory.of(record.get(MESSAGE_ID)));
+    }
+
+    public Flux<MessageId> listMailboxContentSinceSentAt(PostgresMailboxId mailboxId, ZonedDateTime since, Limit limit) {
+        Preconditions.checkArgument(!limit.isUnlimited(), "Limit should be defined");
+
+        return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_ID)
+                .from(TABLE_NAME)
+                .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                .and(SENT_AT.greaterOrEqual(since.toOffsetDateTime()))
+                .orderBy(SENT_AT.desc())
+                .limit(limit.getLimit().get())))
+            .map(record -> PostgresMessageId.Factory.of(record.get(MESSAGE_ID)));
+    }
+
+    public Mono<Void> delete(PostgresMailboxId mailboxId, PostgresMessageId messageId) {
+        return postgresExecutor.executeVoid(dslContext -> Mono.from(dslContext.deleteFrom(TABLE_NAME)
+            .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+            .and(MESSAGE_ID.eq(messageId.asUuid()))));
+    }
+
+    public Mono<Void> delete(PostgresMailboxId mailboxId) {
+        return postgresExecutor.executeVoid(dslContext -> Mono.from(dslContext.deleteFrom(TABLE_NAME)
+            .where(MAILBOX_ID.eq(mailboxId.asUuid()))));
+    }
+
+    public Mono<Void> save(PostgresMailboxId mailboxId, ZonedDateTime sentAt, ZonedDateTime receivedAt, PostgresMessageId messageId) {
+        return postgresExecutor.executeVoid(dslContext -> Mono.from(dslContext.insertInto(TABLE_NAME)
+            .set(MAILBOX_ID, mailboxId.asUuid())
+            .set(MESSAGE_ID, messageId.asUuid())
+            .set(SENT_AT, sentAt.toOffsetDateTime())
+            .set(RECEIVED_AT, receivedAt.toOffsetDateTime())
+            .onConflictOnConstraint(PK_CONSTRAINT_NAME)
+            .doUpdate()
+            .set(SENT_AT, sentAt.toOffsetDateTime())
+            .set(RECEIVED_AT, receivedAt.toOffsetDateTime())));

Review Comment:
   actually in the contract, there is a test:
   
   ```
   @Test
       default void saveShouldBeIdempotent() {
           testee().save(mailboxId1(), DATE_1, DATE_2, messageId1()).block();
           testee().save(mailboxId1(), DATE_1, DATE_2, messageId1()).block();
   
           assertThat(testee().listMailboxContentSortedBySentAt(mailboxId1(), Limit.limit(12)).collectList().block())
               .containsExactly(messageId1());
       }
   ```
   
   Does this mean in case of conflict we just do nothing?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] JAMES-2586 Implement PostgresEmailQueryView [james-project]

Posted by "hungphan227 (via GitHub)" <gi...@apache.org>.
hungphan227 commented on code in PR #2007:
URL: https://github.com/apache/james-project/pull/2007#discussion_r1495221758


##########
server/data/data-jmap/src/test/java/org/apache/james/jmap/api/projections/EmailQueryViewContract.java:
##########
@@ -200,7 +200,7 @@ default void datesCanBeDuplicated() {
         testee().save(mailboxId1(), DATE_1, DATE_2, messageId2()).block();
 
         assertThat(testee().listMailboxContentSortedBySentAt(mailboxId1(), Limit.limit(12)).collectList().block())
-            .containsExactly(messageId1(), messageId2());
+            .containsExactlyInAnyOrder(messageId1(), messageId2());

Review Comment:
   Date of msg1 and msg2 are equal so the order does not matter. The reason I change to containsExactlyInAnyOrder is that the order in Postgres implementation is different that Cassandra implementation



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] JAMES-2586 Implement PostgresEmailQueryView [james-project]

Posted by "chibenwa (via GitHub)" <gi...@apache.org>.
chibenwa commented on code in PR #2007:
URL: https://github.com/apache/james-project/pull/2007#discussion_r1492696348


##########
server/data/data-jmap-postgres/src/main/java/org/apache/james/jmap/postgres/projections/PostgresEmailQueryViewModule.java:
##########
@@ -0,0 +1,66 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.postgres.projections;
+
+import java.time.OffsetDateTime;
+import java.util.UUID;
+
+import org.apache.james.backends.postgres.PostgresIndex;
+import org.apache.james.backends.postgres.PostgresModule;
+import org.apache.james.backends.postgres.PostgresTable;
+import org.apache.james.mailbox.postgres.mail.PostgresMessageModule;
+import org.jooq.Field;
+import org.jooq.Name;
+import org.jooq.Record;
+import org.jooq.Table;
+import org.jooq.impl.DSL;
+import org.jooq.impl.SQLDataType;
+
+public interface PostgresEmailQueryViewModule {
+    interface PostgresEmailQueryViewTable {
+        Table<Record> TABLE_NAME = DSL.table("email_query_view");
+
+        Field<UUID> MAILBOX_ID = DSL.field("mailbox_id", SQLDataType.UUID.notNull());
+        Field<UUID> MESSAGE_ID = PostgresMessageModule.MESSAGE_ID;
+        Field<OffsetDateTime> RECEIVED_AT = DSL.field("received_at", SQLDataType.TIMESTAMPWITHTIMEZONE.notNull());
+        Field<OffsetDateTime> SENT_AT = DSL.field("sent_at", SQLDataType.TIMESTAMPWITHTIMEZONE.notNull());
+
+        Name PK_CONSTRAINT_NAME = DSL.name("email_query_view_pkey");
+
+        PostgresTable TABLE = PostgresTable.name(TABLE_NAME.getName())
+            .createTableStep(((dsl, tableName) -> dsl.createTableIfNotExists(tableName)
+                .column(MAILBOX_ID)
+                .column(MESSAGE_ID)
+                .column(RECEIVED_AT)
+                .column(SENT_AT)
+                .constraint(DSL.constraint(PK_CONSTRAINT_NAME).primaryKey(MAILBOX_ID, MESSAGE_ID))))
+            .supportsRowLevelSecurity()
+            .build();
+
+        PostgresIndex INDEX = PostgresIndex.name("email_query_view_mailbox_id_index")

Review Comment:
   We want to have an index on sentAt? receivedAt?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] JAMES-2586 Implement PostgresEmailQueryView [james-project]

Posted by "hungphan227 (via GitHub)" <gi...@apache.org>.
hungphan227 commented on code in PR #2007:
URL: https://github.com/apache/james-project/pull/2007#discussion_r1493967775


##########
server/data/data-jmap-postgres/src/main/java/org/apache/james/jmap/postgres/projections/PostgresEmailQueryViewModule.java:
##########
@@ -0,0 +1,66 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.postgres.projections;
+
+import java.time.OffsetDateTime;
+import java.util.UUID;
+
+import org.apache.james.backends.postgres.PostgresIndex;
+import org.apache.james.backends.postgres.PostgresModule;
+import org.apache.james.backends.postgres.PostgresTable;
+import org.apache.james.mailbox.postgres.mail.PostgresMessageModule;
+import org.jooq.Field;
+import org.jooq.Name;
+import org.jooq.Record;
+import org.jooq.Table;
+import org.jooq.impl.DSL;
+import org.jooq.impl.SQLDataType;
+
+public interface PostgresEmailQueryViewModule {
+    interface PostgresEmailQueryViewTable {
+        Table<Record> TABLE_NAME = DSL.table("email_query_view");
+
+        Field<UUID> MAILBOX_ID = DSL.field("mailbox_id", SQLDataType.UUID.notNull());
+        Field<UUID> MESSAGE_ID = PostgresMessageModule.MESSAGE_ID;
+        Field<OffsetDateTime> RECEIVED_AT = DSL.field("received_at", SQLDataType.TIMESTAMPWITHTIMEZONE.notNull());
+        Field<OffsetDateTime> SENT_AT = DSL.field("sent_at", SQLDataType.TIMESTAMPWITHTIMEZONE.notNull());
+
+        Name PK_CONSTRAINT_NAME = DSL.name("email_query_view_pkey");
+
+        PostgresTable TABLE = PostgresTable.name(TABLE_NAME.getName())
+            .createTableStep(((dsl, tableName) -> dsl.createTableIfNotExists(tableName)
+                .column(MAILBOX_ID)
+                .column(MESSAGE_ID)
+                .column(RECEIVED_AT)
+                .column(SENT_AT)
+                .constraint(DSL.constraint(PK_CONSTRAINT_NAME).primaryKey(MAILBOX_ID, MESSAGE_ID))))
+            .supportsRowLevelSecurity()
+            .build();
+
+        PostgresIndex INDEX = PostgresIndex.name("email_query_view_mailbox_id_index")

Review Comment:
   oh I forgot to add :))



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] JAMES-2586 Implement PostgresEmailQueryView [james-project]

Posted by "Arsnael (via GitHub)" <gi...@apache.org>.
Arsnael merged PR #2007:
URL: https://github.com/apache/james-project/pull/2007


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] JAMES-2586 Implement PostgresEmailQueryView [james-project]

Posted by "hungphan227 (via GitHub)" <gi...@apache.org>.
hungphan227 commented on code in PR #2007:
URL: https://github.com/apache/james-project/pull/2007#discussion_r1495221758


##########
server/data/data-jmap/src/test/java/org/apache/james/jmap/api/projections/EmailQueryViewContract.java:
##########
@@ -200,7 +200,7 @@ default void datesCanBeDuplicated() {
         testee().save(mailboxId1(), DATE_1, DATE_2, messageId2()).block();
 
         assertThat(testee().listMailboxContentSortedBySentAt(mailboxId1(), Limit.limit(12)).collectList().block())
-            .containsExactly(messageId1(), messageId2());
+            .containsExactlyInAnyOrder(messageId1(), messageId2());

Review Comment:
   Date of msg1 and msg2 are equal so the order does not matter. The reason I change to containsExactlyInAnyOrder is that the order in Postgres implementation is different than Cassandra implementation



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] JAMES-2586 Implement PostgresEmailQueryView [james-project]

Posted by "Arsnael (via GitHub)" <gi...@apache.org>.
Arsnael commented on code in PR #2007:
URL: https://github.com/apache/james-project/pull/2007#discussion_r1494135713


##########
server/data/data-jmap-postgres/src/main/java/org/apache/james/jmap/postgres/projections/PostgresEmailQueryViewDAO.java:
##########
@@ -0,0 +1,145 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.postgres.projections;
+
+import static org.apache.james.jmap.postgres.projections.PostgresEmailQueryViewModule.PostgresEmailQueryViewTable.MAILBOX_ID;
+import static org.apache.james.jmap.postgres.projections.PostgresEmailQueryViewModule.PostgresEmailQueryViewTable.MESSAGE_ID;
+import static org.apache.james.jmap.postgres.projections.PostgresEmailQueryViewModule.PostgresEmailQueryViewTable.PK_CONSTRAINT_NAME;
+import static org.apache.james.jmap.postgres.projections.PostgresEmailQueryViewModule.PostgresEmailQueryViewTable.RECEIVED_AT;
+import static org.apache.james.jmap.postgres.projections.PostgresEmailQueryViewModule.PostgresEmailQueryViewTable.SENT_AT;
+import static org.apache.james.jmap.postgres.projections.PostgresEmailQueryViewModule.PostgresEmailQueryViewTable.TABLE_NAME;
+
+import java.time.ZonedDateTime;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import org.apache.james.backends.postgres.utils.PostgresExecutor;
+import org.apache.james.mailbox.model.MessageId;
+import org.apache.james.mailbox.postgres.PostgresMailboxId;
+import org.apache.james.mailbox.postgres.PostgresMessageId;
+import org.apache.james.util.streams.Limit;
+
+import com.google.common.base.Preconditions;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+public class PostgresEmailQueryViewDAO {
+    private PostgresExecutor postgresExecutor;
+
+    @Inject
+    public PostgresEmailQueryViewDAO(@Named(PostgresExecutor.NON_RLS_INJECT) PostgresExecutor postgresExecutor) {
+        this.postgresExecutor = postgresExecutor;
+    }
+
+    public Flux<MessageId> listMailboxContentSortedBySentAt(PostgresMailboxId mailboxId, Limit limit) {
+        Preconditions.checkArgument(!limit.isUnlimited(), "Limit should be defined");
+
+        return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_ID)
+                .from(TABLE_NAME)
+                .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                .orderBy(SENT_AT.desc())
+                .limit(limit.getLimit().get())))
+            .map(record -> PostgresMessageId.Factory.of(record.get(MESSAGE_ID)));
+    }
+
+    public Flux<MessageId> listMailboxContentSortedByReceivedAt(PostgresMailboxId mailboxId, Limit limit) {
+        Preconditions.checkArgument(!limit.isUnlimited(), "Limit should be defined");
+
+        return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_ID)
+                .from(TABLE_NAME)
+                .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                .orderBy(RECEIVED_AT.desc())
+                .limit(limit.getLimit().get())))
+            .map(record -> PostgresMessageId.Factory.of(record.get(MESSAGE_ID)));
+    }
+
+    public Flux<MessageId> listMailboxContentSinceAfterSortedBySentAt(PostgresMailboxId mailboxId, ZonedDateTime since, Limit limit) {
+        Preconditions.checkArgument(!limit.isUnlimited(), "Limit should be defined");
+
+        return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_ID)
+                .from(TABLE_NAME)
+                .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                .and(RECEIVED_AT.greaterOrEqual(since.toOffsetDateTime()))
+                .orderBy(SENT_AT.desc())
+                .limit(limit.getLimit().get())))
+            .map(record -> PostgresMessageId.Factory.of(record.get(MESSAGE_ID)));
+    }
+
+    public Flux<MessageId> listMailboxContentSinceAfterSortedByReceivedAt(PostgresMailboxId mailboxId, ZonedDateTime since, Limit limit) {
+        Preconditions.checkArgument(!limit.isUnlimited(), "Limit should be defined");
+
+        return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_ID)
+                .from(TABLE_NAME)
+                .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                .and(RECEIVED_AT.greaterOrEqual(since.toOffsetDateTime()))
+                .orderBy(RECEIVED_AT.desc())
+                .limit(limit.getLimit().get())))
+            .map(record -> PostgresMessageId.Factory.of(record.get(MESSAGE_ID)));
+    }
+
+    public Flux<MessageId> listMailboxContentBeforeSortedByReceivedAt(PostgresMailboxId mailboxId, ZonedDateTime since, Limit limit) {
+        Preconditions.checkArgument(!limit.isUnlimited(), "Limit should be defined");
+
+        return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_ID)
+                .from(TABLE_NAME)
+                .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                .and(RECEIVED_AT.lessOrEqual(since.toOffsetDateTime()))
+                .orderBy(RECEIVED_AT.desc())
+                .limit(limit.getLimit().get())))
+            .map(record -> PostgresMessageId.Factory.of(record.get(MESSAGE_ID)));
+    }
+
+    public Flux<MessageId> listMailboxContentSinceSentAt(PostgresMailboxId mailboxId, ZonedDateTime since, Limit limit) {
+        Preconditions.checkArgument(!limit.isUnlimited(), "Limit should be defined");
+
+        return postgresExecutor.executeRows(dslContext -> Flux.from(dslContext.select(MESSAGE_ID)
+                .from(TABLE_NAME)
+                .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+                .and(SENT_AT.greaterOrEqual(since.toOffsetDateTime()))
+                .orderBy(SENT_AT.desc())
+                .limit(limit.getLimit().get())))
+            .map(record -> PostgresMessageId.Factory.of(record.get(MESSAGE_ID)));
+    }
+
+    public Mono<Void> delete(PostgresMailboxId mailboxId, PostgresMessageId messageId) {
+        return postgresExecutor.executeVoid(dslContext -> Mono.from(dslContext.deleteFrom(TABLE_NAME)
+            .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+            .and(MESSAGE_ID.eq(messageId.asUuid()))));
+    }
+
+    public Mono<Void> delete(PostgresMailboxId mailboxId) {
+        return postgresExecutor.executeVoid(dslContext -> Mono.from(dslContext.deleteFrom(TABLE_NAME)
+            .where(MAILBOX_ID.eq(mailboxId.asUuid()))));
+    }
+
+    public Mono<Void> save(PostgresMailboxId mailboxId, ZonedDateTime sentAt, ZonedDateTime receivedAt, PostgresMessageId messageId) {
+        return postgresExecutor.executeVoid(dslContext -> Mono.from(dslContext.insertInto(TABLE_NAME)
+            .set(MAILBOX_ID, mailboxId.asUuid())
+            .set(MESSAGE_ID, messageId.asUuid())
+            .set(SENT_AT, sentAt.toOffsetDateTime())
+            .set(RECEIVED_AT, receivedAt.toOffsetDateTime())
+            .onConflictOnConstraint(PK_CONSTRAINT_NAME)
+            .doUpdate()
+            .set(SENT_AT, sentAt.toOffsetDateTime())
+            .set(RECEIVED_AT, receivedAt.toOffsetDateTime())));

Review Comment:
   Idempotent means that if you insert the same record twice you still have the same result. The sent_at and received_at here are not supposed to change, thus why you can simply just do nothing, result will be the same, as for the test.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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