This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch postgresql in repository https://gitbox.apache.org/repos/asf/james-project.git
commit d18939b3be5e8bc9e3939ebace721f0599f3898a Author: Tung Tran <[email protected]> AuthorDate: Wed Nov 22 10:13:45 2023 +0700 JAMES-2586 Implement PostgresModSeqProvider --- .../postgres/mail/PostgresModSeqProvider.java | 92 ++++++++++++++++++ .../postgres/mail/dao/PostgresMailboxDAO.java | 20 ++++ .../postgres/mail/PostgresModSeqProviderTest.java | 104 +++++++++++++++++++++ 3 files changed, 216 insertions(+) diff --git a/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/PostgresModSeqProvider.java b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/PostgresModSeqProvider.java new file mode 100644 index 0000000000..23734e8138 --- /dev/null +++ b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/PostgresModSeqProvider.java @@ -0,0 +1,92 @@ +/**************************************************************** + * 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.mailbox.postgres.mail; + +import org.apache.james.backends.postgres.utils.PostgresExecutor; +import org.apache.james.mailbox.MailboxSession; +import org.apache.james.mailbox.ModSeq; +import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.model.Mailbox; +import org.apache.james.mailbox.model.MailboxId; +import org.apache.james.mailbox.postgres.mail.dao.PostgresMailboxDAO; +import org.apache.james.mailbox.store.mail.ModSeqProvider; + +import reactor.core.publisher.Mono; + +public class PostgresModSeqProvider implements ModSeqProvider { + + public static class Factory { + + private final PostgresExecutor.Factory executorFactory; + + public Factory(PostgresExecutor.Factory executorFactory) { + this.executorFactory = executorFactory; + } + + public PostgresModSeqProvider create(MailboxSession session) { + PostgresExecutor postgresExecutor = executorFactory.create(session.getUser().getDomainPart()); + return new PostgresModSeqProvider(new PostgresMailboxDAO(postgresExecutor)); + } + } + + private final PostgresMailboxDAO mailboxDAO; + + public PostgresModSeqProvider(PostgresMailboxDAO mailboxDAO) { + this.mailboxDAO = mailboxDAO; + } + + @Override + public ModSeq nextModSeq(Mailbox mailbox) throws MailboxException { + return nextModSeq(mailbox.getMailboxId()); + } + + @Override + public ModSeq nextModSeq(MailboxId mailboxId) throws MailboxException { + return nextModSeqReactive(mailboxId) + .blockOptional() + .orElseThrow(() -> new MailboxException("Can not retrieve modseq for " + mailboxId)); + } + + @Override + public ModSeq highestModSeq(Mailbox mailbox) { + return highestModSeqReactive(mailbox).block(); + } + + @Override + public Mono<ModSeq> highestModSeqReactive(Mailbox mailbox) { + return getHighestModSeq(mailbox.getMailboxId()); + } + + private Mono<ModSeq> getHighestModSeq(MailboxId mailboxId) { + return mailboxDAO.findHighestModSeqByMailboxId(mailboxId) + .defaultIfEmpty(ModSeq.first()); + } + + @Override + public ModSeq highestModSeq(MailboxId mailboxId) { + return getHighestModSeq(mailboxId).block(); + } + + @Override + public Mono<ModSeq> nextModSeqReactive(MailboxId mailboxId) { + return mailboxDAO.incrementAndGetModSeq(mailboxId) + .defaultIfEmpty(ModSeq.first()); + } +} diff --git a/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxDAO.java b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxDAO.java index de6a42e826..c63909a43a 100644 --- a/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxDAO.java +++ b/mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxDAO.java @@ -20,6 +20,7 @@ package org.apache.james.mailbox.postgres.mail.dao; import static org.apache.james.mailbox.postgres.PostgresMailboxIdFaker.getMailboxId; +import static org.apache.james.mailbox.postgres.mail.PostgresMailboxModule.PostgresMailboxTable.MAILBOX_HIGHEST_MODSEQ; import static org.apache.james.mailbox.postgres.mail.PostgresMailboxModule.PostgresMailboxTable.MAILBOX_ID; import static org.apache.james.mailbox.postgres.mail.PostgresMailboxModule.PostgresMailboxTable.MAILBOX_LAST_UID; import static org.apache.james.mailbox.postgres.mail.PostgresMailboxModule.PostgresMailboxTable.MAILBOX_NAME; @@ -33,6 +34,7 @@ import static org.jooq.impl.DSL.count; import org.apache.james.backends.postgres.utils.PostgresExecutor; import org.apache.james.core.Username; import org.apache.james.mailbox.MessageUid; +import org.apache.james.mailbox.ModSeq; import org.apache.james.mailbox.exception.MailboxExistsException; import org.apache.james.mailbox.exception.MailboxNotFoundException; import org.apache.james.mailbox.model.Mailbox; @@ -158,4 +160,22 @@ public class PostgresMailboxDAO { .map(record -> record.get(MAILBOX_LAST_UID)) .map(MessageUid::of); } + + + public Mono<ModSeq> findHighestModSeqByMailboxId(MailboxId mailboxId) { + return postgresExecutor.executeRow(dsl -> Mono.from(dsl.select(MAILBOX_HIGHEST_MODSEQ) + .from(TABLE_NAME) + .where(MAILBOX_ID.eq(getMailboxId(mailboxId).asUuid())))) + .flatMap(record -> Mono.justOrEmpty(record.get(MAILBOX_HIGHEST_MODSEQ))) + .map(ModSeq::of); + } + + public Mono<ModSeq> incrementAndGetModSeq(MailboxId mailboxId) { + return postgresExecutor.executeRow(dsl -> Mono.from(dsl.update(TABLE_NAME) + .set(MAILBOX_HIGHEST_MODSEQ, coalesce(MAILBOX_HIGHEST_MODSEQ, 0L).add(1)) + .where(MAILBOX_ID.eq(getMailboxId(mailboxId).asUuid())) + .returning(MAILBOX_HIGHEST_MODSEQ))) + .map(record -> record.get(MAILBOX_HIGHEST_MODSEQ)) + .map(ModSeq::of); + } } diff --git a/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/mail/PostgresModSeqProviderTest.java b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/mail/PostgresModSeqProviderTest.java new file mode 100644 index 0000000000..eff361562c --- /dev/null +++ b/mailbox/postgres/src/test/java/org/apache/james/mailbox/postgres/mail/PostgresModSeqProviderTest.java @@ -0,0 +1,104 @@ +/**************************************************************** + * 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.mailbox.postgres.mail; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.Duration; +import java.util.UUID; +import java.util.concurrent.ConcurrentSkipListSet; +import java.util.concurrent.ExecutionException; +import java.util.stream.LongStream; + +import org.apache.james.backends.postgres.PostgresExtension; +import org.apache.james.core.Username; +import org.apache.james.mailbox.ModSeq; +import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.model.Mailbox; +import org.apache.james.mailbox.model.MailboxPath; +import org.apache.james.mailbox.model.UidValidity; +import org.apache.james.mailbox.postgres.mail.dao.PostgresMailboxDAO; +import org.apache.james.mailbox.store.mail.ModSeqProvider; +import org.apache.james.util.concurrency.ConcurrentTestRunner; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import com.github.fge.lambdas.Throwing; + +public class PostgresModSeqProviderTest { + + @RegisterExtension + static PostgresExtension postgresExtension = PostgresExtension.withoutRowLevelSecurity(PostgresMailboxModule.MODULE); + + private ModSeqProvider modSeqProvider; + + private Mailbox mailbox; + + @BeforeEach + void setup() { + PostgresMailboxDAO mailboxDAO = new PostgresMailboxDAO(postgresExtension.getPostgresExecutor()); + modSeqProvider = new PostgresModSeqProvider(mailboxDAO); + MailboxPath mailboxPath = new MailboxPath("gsoc", Username.of("ieugen" + UUID.randomUUID()), "INBOX"); + UidValidity uidValidity = UidValidity.of(1234); + mailbox = mailboxDAO.create(mailboxPath, uidValidity).block(); + } + + @Test + void highestModSeqShouldRetrieveValueStoredNextModSeq() throws Exception { + int nbEntries = 100; + ModSeq result = modSeqProvider.highestModSeq(mailbox); + assertThat(result).isEqualTo(ModSeq.first()); + LongStream.range(0, nbEntries) + .forEach(Throwing.longConsumer(value -> { + ModSeq modSeq = modSeqProvider.nextModSeq(mailbox); + assertThat(modSeq).isEqualTo(modSeqProvider.highestModSeq(mailbox)); + }) + ); + } + + @Test + void nextModSeqShouldIncrementValueByOne() throws Exception { + int nbEntries = 100; + ModSeq lastModSeq = modSeqProvider.highestModSeq(mailbox); + LongStream.range(lastModSeq.asLong() + 1, lastModSeq.asLong() + nbEntries) + .forEach(Throwing.longConsumer(value -> { + ModSeq result = modSeqProvider.nextModSeq(mailbox); + assertThat(result.asLong()).isEqualTo(value); + })); + } + + @Test + void nextModSeqShouldGenerateUniqueValuesWhenParallelCalls() throws ExecutionException, InterruptedException, MailboxException { + modSeqProvider.nextModSeq(mailbox); + + ConcurrentSkipListSet<ModSeq> modSeqs = new ConcurrentSkipListSet<>(); + int nbEntries = 10; + + ConcurrentTestRunner.builder() + .operation( + (threadNumber, step) -> modSeqs.add(modSeqProvider.nextModSeq(mailbox))) + .threadCount(10) + .operationCount(nbEntries) + .runSuccessfullyWithin(Duration.ofMinutes(1)); + + assertThat(modSeqs).hasSize(100); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
