This is an automated email from the ASF dual-hosted git repository.

rouazana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 6aa503310b2be9d945e17723d30df3e9b8ae3f91
Author: Matthieu Baechler <[email protected]>
AuthorDate: Mon Jul 22 15:27:31 2019 +0200

    JAMES-2813 replace combination complexity with some old-school loops
---
 .../migration/CassandraMigrationService.java       | 21 ++---
 .../backends/cassandra/migration/Migration.java    | 10 ---
 .../cassandra/migration/MigrationTask.java         | 14 +++-
 .../cassandra/migration/MigrationTest.java         | 89 ----------------------
 .../routes/CassandraMigrationRoutesTest.java       | 12 +--
 5 files changed, 29 insertions(+), 117 deletions(-)

diff --git 
a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/migration/CassandraMigrationService.java
 
b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/migration/CassandraMigrationService.java
index b52126d..5dcf3b9 100644
--- 
a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/migration/CassandraMigrationService.java
+++ 
b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/migration/CassandraMigrationService.java
@@ -21,6 +21,8 @@ package org.apache.james.backends.cassandra.migration;
 
 import static 
org.apache.james.backends.cassandra.versions.CassandraSchemaVersionManager.DEFAULT_VERSION;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.stream.IntStream;
@@ -60,17 +62,18 @@ public class CassandraMigrationService {
     public Migration upgradeToVersion(SchemaVersion newVersion) {
         SchemaVersion currentVersion = 
getCurrentVersion().orElse(DEFAULT_VERSION);
 
-        Migration migrationCombination = 
IntStream.range(currentVersion.getValue(), newVersion.getValue())
-            .mapToObj(SchemaVersion::new)
-            .map(SchemaVersion::next)
-            .map(SchemaTransition::to)
-            .map(this::validateVersionNumber)
-            .map(this::toMigration)
-            .reduce(Migration.IDENTITY, Migration::combine);
-        return new MigrationTask(migrationCombination, newVersion);
+        List<Migration> migrations = new ArrayList<>();
+        SchemaVersion migrateTo = currentVersion.next();
+        while (newVersion.isAfterOrEquals(migrateTo)) {
+            SchemaTransition transition = SchemaTransition.to(migrateTo);
+            validateTransitionExists(transition);
+            migrations.add(toMigration(transition));
+            migrateTo = migrateTo.next();
+        }
+        return new MigrationTask(migrations, newVersion);
     }
 
-    private SchemaTransition validateVersionNumber(SchemaTransition 
transition) {
+    private SchemaTransition validateTransitionExists(SchemaTransition 
transition) {
         if (!allMigrationClazz.containsKey(transition)) {
             String message = String.format("Can not migrate from %s to %s. No 
migration class registered.", transition.fromAsString(), 
transition.toAsString());
             logger.error(message);
diff --git 
a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/migration/Migration.java
 
b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/migration/Migration.java
index 18be8dc..b4f15f4 100644
--- 
a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/migration/Migration.java
+++ 
b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/migration/Migration.java
@@ -22,15 +22,5 @@ package org.apache.james.backends.cassandra.migration;
 import org.apache.james.task.Task;
 
 public interface Migration extends Task {
-    Migration IDENTITY = () -> Result.COMPLETED;
 
-    static Migration combine(Migration migration1, Migration migration2) {
-        return () -> {
-            Result migration1Result = migration1.run();
-            if (migration1Result == Result.COMPLETED) {
-                return migration2.run();
-            }
-            return Result.PARTIAL;
-        };
-    }
 }
diff --git 
a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/migration/MigrationTask.java
 
b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/migration/MigrationTask.java
index 40c7c62..c88b104 100644
--- 
a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/migration/MigrationTask.java
+++ 
b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/migration/MigrationTask.java
@@ -19,11 +19,14 @@
 
 package org.apache.james.backends.cassandra.migration;
 
+import java.util.List;
 import java.util.Optional;
 
 import org.apache.james.backends.cassandra.versions.SchemaVersion;
 import org.apache.james.task.TaskExecutionDetails;
 
+import com.google.common.collect.ImmutableList;
+
 public class MigrationTask implements Migration {
     public static final String CASSANDRA_MIGRATION = "CassandraMigration";
 
@@ -39,17 +42,20 @@ public class MigrationTask implements Migration {
         }
     }
 
-    private final Migration migration;
+    private final ImmutableList<Migration> migrations;
     private final SchemaVersion toVersion;
 
-    public MigrationTask(Migration migration, SchemaVersion toVersion) {
-        this.migration = migration;
+    public MigrationTask(List<Migration> migration, SchemaVersion toVersion) {
+        this.migrations = ImmutableList.copyOf(migration);
         this.toVersion = toVersion;
     }
 
     @Override
     public Result run() throws InterruptedException {
-        return migration.run();
+        for (Migration migration: migrations) {
+            migration.run();
+        }
+        return Result.COMPLETED;
     }
 
     @Override
diff --git 
a/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/migration/MigrationTest.java
 
b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/migration/MigrationTest.java
deleted file mode 100644
index 23a3a3d..0000000
--- 
a/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/migration/MigrationTest.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/****************************************************************
- * 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.backends.cassandra.migration;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-import java.util.concurrent.atomic.AtomicBoolean;
-
-import org.junit.Test;
-
-public class MigrationTest {
-    @Test
-    public void 
combineShouldNotExecuteSecondMigrationExecutionWhenTheFirstOneIsFailing() 
throws InterruptedException {
-        AtomicBoolean migration2Done = new AtomicBoolean(false);
-
-        Migration migration1 = () -> Migration.Result.PARTIAL;
-        Migration migration2 = () -> {
-            migration2Done.set(true);
-            return Migration.Result.COMPLETED;
-        };
-
-        Migration.combine(migration1, migration2).run();
-
-        assertThat(migration2Done).isFalse();
-    }
-
-    @Test
-    public void combineShouldTriggerSecondMigrationWhenTheFirstOneSucceed() 
throws InterruptedException {
-        AtomicBoolean migration2Done = new AtomicBoolean(false);
-
-        Migration migration1 = () -> Migration.Result.COMPLETED;
-        Migration migration2 = () -> {
-            migration2Done.set(true);
-            return Migration.Result.COMPLETED;
-        };
-
-        Migration.combine(migration1, migration2).run();
-
-        assertThat(migration2Done).isTrue();
-    }
-
-    @Test
-    public void combineShouldExecuteTheFirstMigrationWhenSecondWillFail() 
throws InterruptedException {
-        AtomicBoolean migration1Done = new AtomicBoolean(false);
-
-        Migration migration1 = () -> {
-            migration1Done.set(true);
-            return Migration.Result.COMPLETED;
-        };
-        Migration migration2 = () -> Migration.Result.PARTIAL;
-
-
-        Migration.combine(migration1, migration2).run();
-
-        assertThat(migration1Done).isTrue();
-    }
-
-    @Test
-    public void combineShouldExecuteTheFirstMigration() throws 
InterruptedException {
-        AtomicBoolean migration1Done = new AtomicBoolean(false);
-
-        Migration migration1 = () -> {
-            migration1Done.set(true);
-            return Migration.Result.COMPLETED;
-        };
-        Migration migration2 = () -> Migration.Result.COMPLETED;
-
-        Migration.combine(migration1, migration2).run();
-
-        assertThat(migration1Done).isTrue();
-    }
-}
\ No newline at end of file
diff --git 
a/server/protocols/webadmin/webadmin-cassandra/src/test/java/org/apache/james/webadmin/routes/CassandraMigrationRoutesTest.java
 
b/server/protocols/webadmin/webadmin-cassandra/src/test/java/org/apache/james/webadmin/routes/CassandraMigrationRoutesTest.java
index d7490e6..22beef0 100644
--- 
a/server/protocols/webadmin/webadmin-cassandra/src/test/java/org/apache/james/webadmin/routes/CassandraMigrationRoutesTest.java
+++ 
b/server/protocols/webadmin/webadmin-cassandra/src/test/java/org/apache/james/webadmin/routes/CassandraMigrationRoutesTest.java
@@ -41,6 +41,7 @@ import 
org.apache.james.backends.cassandra.migration.CassandraMigrationService;
 import org.apache.james.backends.cassandra.migration.Migration;
 import org.apache.james.backends.cassandra.migration.MigrationTask;
 import org.apache.james.backends.cassandra.versions.CassandraSchemaVersionDAO;
+import org.apache.james.backends.cassandra.versions.SchemaTransition;
 import org.apache.james.backends.cassandra.versions.SchemaVersion;
 import org.apache.james.task.MemoryTaskManager;
 import org.apache.james.webadmin.WebAdminServer;
@@ -62,6 +63,8 @@ public class CassandraMigrationRoutesTest {
     private static final SchemaVersion LATEST_VERSION = new SchemaVersion(3);
     private static final SchemaVersion CURRENT_VERSION = new SchemaVersion(2);
     private static final SchemaVersion OLDER_VERSION = new SchemaVersion(1);
+    private static final SchemaTransition FROM_OLDER_TO_CURRENT = 
SchemaTransition.to(CURRENT_VERSION);
+    private static final SchemaTransition FROM_CURRENT_TO_LATEST = 
SchemaTransition.to(LATEST_VERSION);
     private WebAdminServer webAdminServer;
     private CassandraSchemaVersionDAO schemaVersionDAO;
     private MemoryTaskManager taskManager;
@@ -70,11 +73,10 @@ public class CassandraMigrationRoutesTest {
         Migration successfulMigration = mock(Migration.class);
         when(successfulMigration.run()).thenReturn(Migration.Result.COMPLETED);
 
-        Map<SchemaVersion, Migration> allMigrationClazz = 
ImmutableMap.<SchemaVersion, Migration>builder()
-            .put(OLDER_VERSION, successfulMigration)
-            .put(CURRENT_VERSION, successfulMigration)
-            .put(LATEST_VERSION, successfulMigration)
-            .build();
+        Map<SchemaTransition, Migration> allMigrationClazz = ImmutableMap.of(
+            FROM_OLDER_TO_CURRENT, successfulMigration,
+            FROM_CURRENT_TO_LATEST, successfulMigration);
+
         schemaVersionDAO = mock(CassandraSchemaVersionDAO.class);
         when(schemaVersionDAO.updateVersion(any())).thenReturn(Mono.empty());
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to