vttranlina commented on code in PR #2408:
URL: https://github.com/apache/james-project/pull/2408#discussion_r1758897862


##########
mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/task/SolveMessageDeletedInconsistenciesService.java:
##########
@@ -0,0 +1,103 @@
+/****************************************************************
+ * 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.cassandra.mail.task;
+
+import java.util.concurrent.ConcurrentLinkedDeque;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.stream.Collectors;
+
+import jakarta.inject.Inject;
+import jakarta.mail.Flags;
+
+import org.apache.james.mailbox.cassandra.ids.CassandraId;
+import org.apache.james.mailbox.cassandra.mail.CassandraDeletedMessageDAO;
+import org.apache.james.mailbox.cassandra.mail.CassandraMailboxDAO;
+import org.apache.james.mailbox.cassandra.mail.CassandraMessageIdDAO;
+import org.apache.james.mailbox.model.MessageRange;
+import org.apache.james.task.Task;
+import org.apache.james.util.streams.Limit;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.ImmutableList;
+
+import reactor.core.publisher.Mono;
+
+public class SolveMessageDeletedInconsistenciesService {
+
+    public record Context(AtomicLong processedMailboxEntries,
+                          ConcurrentLinkedDeque<CassandraId> errors) {
+
+        record Snapshot(long processedMailboxEntries, 
ImmutableList<CassandraId> errors) {
+        }
+
+        public Context() {
+            this(new AtomicLong(0), new ConcurrentLinkedDeque<>());
+        }
+
+        void incrementProcessedMailboxEntries() {
+            processedMailboxEntries.incrementAndGet();
+        }
+
+        void addError(CassandraId cassandraId) {
+            errors.add(cassandraId);
+        }
+
+        Snapshot snapshot() {
+            return new Snapshot(processedMailboxEntries.get(), 
ImmutableList.copyOf(errors));
+        }
+    }
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(SolveMessageDeletedInconsistenciesService.class);
+    private final CassandraMessageIdDAO messageIdDAO;
+    private final CassandraDeletedMessageDAO deletedMessageDAO;
+    private final CassandraMailboxDAO mailboxDAO;
+
+    @Inject
+    public SolveMessageDeletedInconsistenciesService(CassandraMessageIdDAO 
messageIdDAO,
+                                                     
CassandraDeletedMessageDAO deletedMessageDAO,
+                                                     CassandraMailboxDAO 
mailboxDAO) {
+        this.messageIdDAO = messageIdDAO;
+        this.deletedMessageDAO = deletedMessageDAO;
+        this.mailboxDAO = mailboxDAO;
+    }
+
+    public Mono<Task.Result> fixInconsistencies(Context context) {
+        return mailboxDAO.retrieveAllMailboxes()
+            .concatMap(mb -> 
fixDeletedMessagesInconsistencyPerMailbox((CassandraId) mb.getMailboxId(), 
context)
+                .doOnNext(any -> context.incrementProcessedMailboxEntries()))
+            .reduce(Task.Result.COMPLETED, Task::combine);
+    }
+
+    private Mono<Task.Result> 
fixDeletedMessagesInconsistencyPerMailbox(CassandraId cassandraId, Context 
context) {
+        return deletedMessageDAO.removeAll(cassandraId)
+            .then(messageIdDAO.retrieveMessages(cassandraId, 
MessageRange.all(), Limit.unlimited())
+                .filter(metadata -> 
metadata.getComposedMessageId().getFlags().contains(Flags.Flag.DELETED))
+                .collect(Collectors.mapping(metadata -> 
metadata.getComposedMessageId().getComposedMessageId().getUid(), 
Collectors.toList()))

Review Comment:
   i think it is similar, just another syntax? 



-- 
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]

Reply via email to