quantranhong1999 commented on code in PR #1858:
URL: https://github.com/apache/james-project/pull/1858#discussion_r1429764902
##########
mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/PostgresMessageMapper.java:
##########
@@ -321,27 +321,40 @@ private Flux<UpdatedFlags>
updatedFlags(List<ComposedMessageIdWithMetaData> list
}
private Mono<UpdatedFlags> updateFlags(ComposedMessageIdWithMetaData
currentMetaData,
- FlagsUpdateCalculator
flagsUpdateCalculator,
- ModSeq newModSeq) {
+ FlagsUpdateCalculator
flagsUpdateCalculator,
+ ModSeq newModSeq) {
Review Comment:
The old indent is better?
##########
mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/PostgresMessageModule.java:
##########
@@ -108,6 +108,24 @@ interface MessageToMailboxTable {
Field<String[]> USER_FLAGS = DSL.field("user_flags",
DataTypes.STRING_ARRAY);
Field<LocalDateTime> SAVE_DATE = DSL.field("save_date",
DataTypes.TIMESTAMP);
+ String ARRAY_REMOVE_JAMES_FUNCTION_NAME = "array_remove_james";
+ String CREATE_ARRAY_REMOVE_JAMES_FUNCTION =
+ "CREATE OR REPLACE FUNCTION " + ARRAY_REMOVE_JAMES_FUNCTION_NAME +
"(\n" +
+ " p_remove_flags_1 text[],\n" +
+ " p_remove_flags_2 text[])\n" +
Review Comment:
If I do not understand wrong, we are removing `p_remove_flags_2` array out
of `p_remove_flags_1` array.
Maybe rename the variables for better readability?
##########
mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxMessageDAO.java:
##########
@@ -313,46 +317,93 @@ public Flux<ComposedMessageIdWithMetaData>
findAllRecentMessageMetadata(Postgres
.map(RECORD_TO_COMPOSED_MESSAGE_ID_WITH_META_DATA_FUNCTION);
}
- public Mono<Void> updateFlag(PostgresMailboxId mailboxId, MessageUid uid,
UpdatedFlags updatedFlags) {
- return postgresExecutor.executeVoid(dslContext ->
- Mono.from(buildUpdateFlagStatement(dslContext, updatedFlags,
mailboxId, uid)));
+ public Mono<Flags> replaceFlags(PostgresMailboxId mailboxId, MessageUid
uid, Flags newFlags, ModSeq newModSeq) {
+ return postgresExecutor.executeRow(dslContext ->
Mono.from(buildReplaceFlagsStatement(dslContext, newFlags, mailboxId, uid,
newModSeq)
+ .returning(MESSAGE_METADATA_FIELDS_REQUIRE)))
+ .map(RECORD_TO_FLAGS_FUNCTION);
}
- public Mono<Flags> listDistinctUserFlags(PostgresMailboxId mailboxId) {
- return postgresExecutor.executeRows(dslContext ->
Flux.from(dslContext.selectDistinct(UNNEST_FIELD.apply(USER_FLAGS))
- .from(TABLE_NAME)
- .where(MAILBOX_ID.eq(mailboxId.asUuid()))))
- .map(record -> record.get(0, String.class))
- .collectList()
- .map(flagList -> {
- Flags flags = new Flags();
- flagList.forEach(flags::add);
- return flags;
- });
+ public Mono<Flags> addFlags(PostgresMailboxId mailboxId, MessageUid uid,
Flags appendFlags, ModSeq newModSeq) {
+ return postgresExecutor.executeRow(dslContext ->
Mono.from(buildAddFlagsStatement(dslContext, appendFlags, mailboxId, uid,
newModSeq)
+ .returning(MESSAGE_METADATA_FIELDS_REQUIRE)))
+ .map(RECORD_TO_FLAGS_FUNCTION);
}
- private UpdateConditionStep<Record> buildUpdateFlagStatement(DSLContext
dslContext, UpdatedFlags updatedFlags,
-
PostgresMailboxId mailboxId, MessageUid uid) {
+ public Mono<Flags> removeFlags(PostgresMailboxId mailboxId, MessageUid
uid, Flags removeFlags, ModSeq newModSeq) {
+ return postgresExecutor.executeRow(dslContext ->
Mono.from(buildRemoveFlagsStatement(dslContext, removeFlags, mailboxId, uid,
newModSeq)
+ .returning(MESSAGE_METADATA_FIELDS_REQUIRE)))
+ .map(RECORD_TO_FLAGS_FUNCTION);
+ }
+
+ private UpdateConditionStep<Record> buildAddFlagsStatement(DSLContext
dslContext, Flags addFlags,
+
PostgresMailboxId mailboxId, MessageUid uid, ModSeq newModSeq) {
AtomicReference<UpdateSetStep<Record>> updateStatement = new
AtomicReference<>(dslContext.update(TABLE_NAME));
BOOLEAN_FLAGS_MAPPING.forEach((flagColumn, flagMapped) -> {
- if (updatedFlags.isChanged(flagMapped)) {
- updateStatement.getAndUpdate(currentStatement -> {
- if (flagMapped.equals(Flag.RECENT)) {
- return currentStatement.set(flagColumn,
updatedFlags.getNewFlags().contains(Flag.RECENT));
- }
- return currentStatement.set(flagColumn,
updatedFlags.isModifiedToSet(flagMapped));
- });
+ if (addFlags.contains(flagMapped)) {
+ updateStatement.getAndUpdate(currentStatement ->
currentStatement.set(flagColumn, true));
}
});
return updateStatement.get()
- .set(USER_FLAGS, updatedFlags.getNewFlags().getUserFlags())
- .set(MOD_SEQ, updatedFlags.getModSeq().asLong())
+ .set(USER_FLAGS, PostgresDSL.arrayCat(USER_FLAGS,
addFlags.getUserFlags()))
+ .set(MOD_SEQ, newModSeq.asLong())
+ .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+ .and(MESSAGE_UID.eq(uid.asLong()));
+ }
+
+ private UpdateConditionStep<Record> buildReplaceFlagsStatement(DSLContext
dslContext, Flags newFlags,
+
PostgresMailboxId mailboxId, MessageUid uid, ModSeq newModSeq) {
+ AtomicReference<UpdateSetStep<Record>> updateStatement = new
AtomicReference<>(dslContext.update(TABLE_NAME));
+
+ BOOLEAN_FLAGS_MAPPING.forEach((flagColumn, flagMapped) -> {
+ updateStatement.getAndUpdate(currentStatement ->
currentStatement.set(flagColumn, newFlags.contains(flagMapped)));
+ });
+
+ return updateStatement.get()
+ .set(USER_FLAGS, newFlags.getUserFlags())
+ .set(MOD_SEQ, newModSeq.asLong())
.where(MAILBOX_ID.eq(mailboxId.asUuid()))
.and(MESSAGE_UID.eq(uid.asLong()));
}
+ private UpdateConditionStep<Record> buildRemoveFlagsStatement(DSLContext
dslContext, Flags removeFlags,
+
PostgresMailboxId mailboxId, MessageUid uid, ModSeq newModSeq) {
Review Comment:
weird indent?
##########
mailbox/postgres/src/main/java/org/apache/james/mailbox/postgres/mail/dao/PostgresMailboxMessageDAO.java:
##########
@@ -313,46 +317,93 @@ public Flux<ComposedMessageIdWithMetaData>
findAllRecentMessageMetadata(Postgres
.map(RECORD_TO_COMPOSED_MESSAGE_ID_WITH_META_DATA_FUNCTION);
}
- public Mono<Void> updateFlag(PostgresMailboxId mailboxId, MessageUid uid,
UpdatedFlags updatedFlags) {
- return postgresExecutor.executeVoid(dslContext ->
- Mono.from(buildUpdateFlagStatement(dslContext, updatedFlags,
mailboxId, uid)));
+ public Mono<Flags> replaceFlags(PostgresMailboxId mailboxId, MessageUid
uid, Flags newFlags, ModSeq newModSeq) {
+ return postgresExecutor.executeRow(dslContext ->
Mono.from(buildReplaceFlagsStatement(dslContext, newFlags, mailboxId, uid,
newModSeq)
+ .returning(MESSAGE_METADATA_FIELDS_REQUIRE)))
+ .map(RECORD_TO_FLAGS_FUNCTION);
}
- public Mono<Flags> listDistinctUserFlags(PostgresMailboxId mailboxId) {
- return postgresExecutor.executeRows(dslContext ->
Flux.from(dslContext.selectDistinct(UNNEST_FIELD.apply(USER_FLAGS))
- .from(TABLE_NAME)
- .where(MAILBOX_ID.eq(mailboxId.asUuid()))))
- .map(record -> record.get(0, String.class))
- .collectList()
- .map(flagList -> {
- Flags flags = new Flags();
- flagList.forEach(flags::add);
- return flags;
- });
+ public Mono<Flags> addFlags(PostgresMailboxId mailboxId, MessageUid uid,
Flags appendFlags, ModSeq newModSeq) {
+ return postgresExecutor.executeRow(dslContext ->
Mono.from(buildAddFlagsStatement(dslContext, appendFlags, mailboxId, uid,
newModSeq)
+ .returning(MESSAGE_METADATA_FIELDS_REQUIRE)))
+ .map(RECORD_TO_FLAGS_FUNCTION);
}
- private UpdateConditionStep<Record> buildUpdateFlagStatement(DSLContext
dslContext, UpdatedFlags updatedFlags,
-
PostgresMailboxId mailboxId, MessageUid uid) {
+ public Mono<Flags> removeFlags(PostgresMailboxId mailboxId, MessageUid
uid, Flags removeFlags, ModSeq newModSeq) {
+ return postgresExecutor.executeRow(dslContext ->
Mono.from(buildRemoveFlagsStatement(dslContext, removeFlags, mailboxId, uid,
newModSeq)
+ .returning(MESSAGE_METADATA_FIELDS_REQUIRE)))
+ .map(RECORD_TO_FLAGS_FUNCTION);
+ }
+
+ private UpdateConditionStep<Record> buildAddFlagsStatement(DSLContext
dslContext, Flags addFlags,
+
PostgresMailboxId mailboxId, MessageUid uid, ModSeq newModSeq) {
AtomicReference<UpdateSetStep<Record>> updateStatement = new
AtomicReference<>(dslContext.update(TABLE_NAME));
BOOLEAN_FLAGS_MAPPING.forEach((flagColumn, flagMapped) -> {
- if (updatedFlags.isChanged(flagMapped)) {
- updateStatement.getAndUpdate(currentStatement -> {
- if (flagMapped.equals(Flag.RECENT)) {
- return currentStatement.set(flagColumn,
updatedFlags.getNewFlags().contains(Flag.RECENT));
- }
- return currentStatement.set(flagColumn,
updatedFlags.isModifiedToSet(flagMapped));
- });
+ if (addFlags.contains(flagMapped)) {
+ updateStatement.getAndUpdate(currentStatement ->
currentStatement.set(flagColumn, true));
}
});
return updateStatement.get()
- .set(USER_FLAGS, updatedFlags.getNewFlags().getUserFlags())
- .set(MOD_SEQ, updatedFlags.getModSeq().asLong())
+ .set(USER_FLAGS, PostgresDSL.arrayCat(USER_FLAGS,
addFlags.getUserFlags()))
+ .set(MOD_SEQ, newModSeq.asLong())
+ .where(MAILBOX_ID.eq(mailboxId.asUuid()))
+ .and(MESSAGE_UID.eq(uid.asLong()));
+ }
+
+ private UpdateConditionStep<Record> buildReplaceFlagsStatement(DSLContext
dslContext, Flags newFlags,
+
PostgresMailboxId mailboxId, MessageUid uid, ModSeq newModSeq) {
Review Comment:
weird indent?
--
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]