Arsnael commented on code in PR #1786: URL: https://github.com/apache/james-project/pull/1786#discussion_r1387441438
########## mailbox/postgres/src/main/java/org/apache/james/mailbox/jpa/mail/dao/PostgresMailboxDAOImpl.java: ########## @@ -0,0 +1,176 @@ +/**************************************************************** + * 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.jpa.mail.dao; + +import static org.apache.james.mailbox.jpa.mail.table.PostgresMailboxTable.MAILBOX_ID; +import static org.apache.james.mailbox.jpa.mail.table.PostgresMailboxTable.MAILBOX_NAME; +import static org.apache.james.mailbox.jpa.mail.table.PostgresMailboxTable.MAILBOX_NAMESPACE; +import static org.apache.james.mailbox.jpa.mail.table.PostgresMailboxTable.MAILBOX_UID_VALIDITY; +import static org.apache.james.mailbox.jpa.mail.table.PostgresMailboxTable.TABLE_NAME; +import static org.apache.james.mailbox.jpa.mail.table.PostgresMailboxTable.USER_NAME; +import static org.jooq.impl.DSL.count; + +import java.util.NoSuchElementException; + +import javax.inject.Inject; + +import org.apache.james.backends.postgres.utils.PostgresExecutor; +import org.apache.james.core.Username; +import org.apache.james.mailbox.exception.MailboxExistsException; +import org.apache.james.mailbox.jpa.mail.table.PostgresMailboxTable; +import org.apache.james.mailbox.jpa.user.PostgresMailboxId; +import org.apache.james.mailbox.model.Mailbox; +import org.apache.james.mailbox.model.MailboxId; +import org.apache.james.mailbox.model.MailboxPath; +import org.apache.james.mailbox.model.UidValidity; +import org.apache.james.mailbox.model.search.MailboxQuery; +import org.jooq.Record1; + +import com.google.common.base.Preconditions; + +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +public class PostgresMailboxDAOImpl implements PostgresMailboxDAO { + private static final char SQL_WILDCARD_CHAR = '%'; + + private PostgresExecutor postgresExecutor; + + @Inject + public PostgresMailboxDAOImpl(PostgresExecutor postgresExecutor) { + this.postgresExecutor = postgresExecutor; + } + + @Override + public Mono<Mailbox> create(MailboxPath mailboxPath, UidValidity uidValidity) { + return postgresExecutor.dslContext() + .flatMapMany(dslContext -> Mono.from(dslContext.insertInto(TABLE_NAME, MAILBOX_NAME, USER_NAME, MAILBOX_NAMESPACE, MAILBOX_UID_VALIDITY) + .values(mailboxPath.getName(), mailboxPath.getUser().asString(), mailboxPath.getNamespace(), uidValidity.asLong()) + .returningResult(MAILBOX_ID)) + .map(record -> new Mailbox(mailboxPath, uidValidity, PostgresMailboxId.of(record.get(MAILBOX_ID))))) + .last() + .onErrorResume(throwable -> { + if (throwable.getMessage().contains("duplicate key value violates unique constraint")) { + return Mono.error(new MailboxExistsException(mailboxPath.getName())); + } else { + return Mono.error(throwable); + } + }); + } + + @Override + public Mono<MailboxId> rename(Mailbox mailbox) { + Preconditions.checkNotNull(mailbox.getMailboxId(), "A mailbox we want to rename should have a defined mailboxId"); + + return postgresExecutor.dslContext() + .flatMap(dslContext -> Mono.from(dslContext.update(TABLE_NAME) + .set(MAILBOX_NAME, mailbox.getName()) + .set(USER_NAME, mailbox.getUser().asString()) + .set(MAILBOX_NAMESPACE, mailbox.getNamespace()))) + .thenReturn(mailbox.getMailboxId()) + .onErrorResume(throwable -> { + if (throwable.getMessage().contains("duplicate key value violates unique constraint")) { + return Mono.error(new MailboxExistsException(mailbox.getName())); + } else { + return Mono.error(throwable); + } + }); + } + + @Override + public Mono<Void> delete(MailboxId mailboxId) { + return postgresExecutor.dslContext() + .flatMap(dslContext -> Mono.from(dslContext.deleteFrom(TABLE_NAME) + .where(MAILBOX_ID.eq(((PostgresMailboxId) mailboxId).getRawId())))) + .then(); + } + + @Override + public Mono<Mailbox> findMailboxByPath(MailboxPath mailboxPath) { + return postgresExecutor.dslContext() + .flatMapMany(dsl -> Flux.from(dsl.selectFrom(TABLE_NAME) + .where(MAILBOX_NAME.eq(mailboxPath.getName()) + .and(USER_NAME.eq(mailboxPath.getUser().asString())) + .and(MAILBOX_NAMESPACE.eq(mailboxPath.getNamespace())))) + .map(record -> generateMailbox(record.get(PostgresMailboxTable.MAILBOX_ID), + record.get(MAILBOX_NAMESPACE), + record.get(USER_NAME), + record.get(MAILBOX_NAME), + record.get(MAILBOX_UID_VALIDITY)))) + .last() + .onErrorResume(NoSuchElementException.class, e -> Mono.empty()); + } + + @Override + public Mono<Mailbox> findMailboxById(MailboxId id) { + return postgresExecutor.dslContext() + .flatMapMany(dsl -> Flux.from(dsl.selectFrom(TABLE_NAME) + .where(MAILBOX_ID.eq(((PostgresMailboxId) id).getRawId()))) + .map(record -> generateMailbox(record.get(PostgresMailboxTable.MAILBOX_ID), + record.get(MAILBOX_NAMESPACE), + record.get(USER_NAME), + record.get(MAILBOX_NAME), + record.get(MAILBOX_UID_VALIDITY)))) + .last() + .onErrorResume(NoSuchElementException.class, e -> Mono.empty()); + } + + @Override + public Flux<Mailbox> findMailboxWithPathLike(MailboxQuery.UserBound query) { + return null; Review Comment: todo ########## mailbox/postgres/src/main/java/org/apache/james/mailbox/jpa/mail/table/PostgresMailboxTable.java: ########## @@ -0,0 +1,57 @@ +/**************************************************************** + * 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.jpa.mail.table; + +import org.apache.james.backends.postgres.PostgresTable; +import org.jooq.Field; +import org.jooq.Record; +import org.jooq.Table; +import org.jooq.impl.DSL; +import org.jooq.impl.SQLDataType; + +public interface PostgresMailboxTable { + Table<Record> TABLE_NAME = DSL.table("mailbox"); + + Field<Long> MAILBOX_ID = DSL.field("mailbox_id", SQLDataType.BIGINT.notNull()); + Field<String> MAILBOX_NAME = DSL.field("mailbox_name", SQLDataType.VARCHAR(255).notNull()); + Field<Long> MAILBOX_UID_VALIDITY = DSL.field("mailbox_uid_validity", SQLDataType.BIGINT.notNull()); + Field<String> USER_NAME = DSL.field("user_name", SQLDataType.VARCHAR(255)); + Field<String> MAILBOX_NAMESPACE = DSL.field("mailbox_namespace", SQLDataType.VARCHAR(255).notNull()); + Field<Long> MAILBOX_LAST_UID = DSL.field("mailbox_last_uid", SQLDataType.BIGINT); + Field<Long> MAILBOX_HIGHEST_MODSEQ = DSL.field("mailbox_highest_modseq", SQLDataType.BIGINT); + + PostgresTable TABLE = PostgresTable.name(TABLE_NAME.getName()) + .createTableStep(((dsl, tableName) -> dsl.createTable(tableName) + .column(MAILBOX_ID, SQLDataType.BIGINT.identity(true)) + .column(MAILBOX_NAME) + .column(MAILBOX_UID_VALIDITY) + .column(USER_NAME) + .column(MAILBOX_NAMESPACE) + .column(MAILBOX_LAST_UID) + .column(MAILBOX_HIGHEST_MODSEQ) + .constraint(DSL.primaryKey(MAILBOX_ID)) + .constraint(DSL.unique(MAILBOX_NAME, USER_NAME, MAILBOX_NAMESPACE)))) + .enableRowLevelSecurity(); + +// PostgresIndex INDEX = PostgresIndex.name("subscription_user_index") +// .createIndexStep((dsl, indexName) -> dsl.createIndex(indexName) +// .on(TABLE_NAME, USER)); Review Comment: todo? ########## mailbox/postgres/src/main/java/org/apache/james/mailbox/jpa/mail/dao/PostgresMailboxDAOImpl.java: ########## @@ -0,0 +1,176 @@ +/**************************************************************** + * 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.jpa.mail.dao; + +import static org.apache.james.mailbox.jpa.mail.table.PostgresMailboxTable.MAILBOX_ID; +import static org.apache.james.mailbox.jpa.mail.table.PostgresMailboxTable.MAILBOX_NAME; +import static org.apache.james.mailbox.jpa.mail.table.PostgresMailboxTable.MAILBOX_NAMESPACE; +import static org.apache.james.mailbox.jpa.mail.table.PostgresMailboxTable.MAILBOX_UID_VALIDITY; +import static org.apache.james.mailbox.jpa.mail.table.PostgresMailboxTable.TABLE_NAME; +import static org.apache.james.mailbox.jpa.mail.table.PostgresMailboxTable.USER_NAME; +import static org.jooq.impl.DSL.count; + +import java.util.NoSuchElementException; + +import javax.inject.Inject; + +import org.apache.james.backends.postgres.utils.PostgresExecutor; +import org.apache.james.core.Username; +import org.apache.james.mailbox.exception.MailboxExistsException; +import org.apache.james.mailbox.jpa.mail.table.PostgresMailboxTable; +import org.apache.james.mailbox.jpa.user.PostgresMailboxId; +import org.apache.james.mailbox.model.Mailbox; +import org.apache.james.mailbox.model.MailboxId; +import org.apache.james.mailbox.model.MailboxPath; +import org.apache.james.mailbox.model.UidValidity; +import org.apache.james.mailbox.model.search.MailboxQuery; +import org.jooq.Record1; + +import com.google.common.base.Preconditions; + +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +public class PostgresMailboxDAOImpl implements PostgresMailboxDAO { + private static final char SQL_WILDCARD_CHAR = '%'; + + private PostgresExecutor postgresExecutor; + + @Inject + public PostgresMailboxDAOImpl(PostgresExecutor postgresExecutor) { + this.postgresExecutor = postgresExecutor; + } + + @Override + public Mono<Mailbox> create(MailboxPath mailboxPath, UidValidity uidValidity) { + return postgresExecutor.dslContext() + .flatMapMany(dslContext -> Mono.from(dslContext.insertInto(TABLE_NAME, MAILBOX_NAME, USER_NAME, MAILBOX_NAMESPACE, MAILBOX_UID_VALIDITY) + .values(mailboxPath.getName(), mailboxPath.getUser().asString(), mailboxPath.getNamespace(), uidValidity.asLong()) + .returningResult(MAILBOX_ID)) + .map(record -> new Mailbox(mailboxPath, uidValidity, PostgresMailboxId.of(record.get(MAILBOX_ID))))) + .last() + .onErrorResume(throwable -> { + if (throwable.getMessage().contains("duplicate key value violates unique constraint")) { + return Mono.error(new MailboxExistsException(mailboxPath.getName())); + } else { + return Mono.error(throwable); + } + }); + } + + @Override + public Mono<MailboxId> rename(Mailbox mailbox) { + Preconditions.checkNotNull(mailbox.getMailboxId(), "A mailbox we want to rename should have a defined mailboxId"); + + return postgresExecutor.dslContext() + .flatMap(dslContext -> Mono.from(dslContext.update(TABLE_NAME) + .set(MAILBOX_NAME, mailbox.getName()) + .set(USER_NAME, mailbox.getUser().asString()) + .set(MAILBOX_NAMESPACE, mailbox.getNamespace()))) + .thenReturn(mailbox.getMailboxId()) + .onErrorResume(throwable -> { + if (throwable.getMessage().contains("duplicate key value violates unique constraint")) { Review Comment: Could extract it into a method, to avoid duplicate error management (I see same block in create method) -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
