smiklosovic commented on code in PR #3737:
URL: https://github.com/apache/cassandra/pull/3737#discussion_r1880534399


##########
src/java/org/apache/cassandra/db/SystemKeyspaceMigrator50.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.cassandra.db;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.function.Function;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.ImmutableMap;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.cql3.QueryProcessor;
+import org.apache.cassandra.cql3.UntypedResultSet;
+import org.apache.cassandra.db.marshal.Int32Type;
+import org.apache.cassandra.db.marshal.LongType;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.schema.SchemaConstants;
+
+/**
+ * Migrate 4.x versions of some tables to 5.0. In this case it's just extra 
columns that are changed.
+ */
+public class SystemKeyspaceMigrator50
+{
+    private static final Logger logger = 
LoggerFactory.getLogger(SystemKeyspaceMigrator50.class);
+
+    private SystemKeyspaceMigrator50()
+    {
+    }
+
+    public static void migrate()
+    {
+        migrateCompactionHistory();
+    }
+
+    @VisibleForTesting
+    static void migrateCompactionHistory()
+    {
+        migrateTable(false,
+                     SystemKeyspace.COMPACTION_HISTORY,
+                     SystemKeyspace.COMPACTION_HISTORY,
+                     new String[]{ "id",
+                                   "bytes_in",
+                                   "bytes_out",
+                                   "columnfamily_name",
+                                   "compacted_at",
+                                   "keyspace_name",
+                                   "rows_merged",
+                                   "compaction_properties" },
+                     row -> Collections.singletonList(new Object[]{ 
row.getTimeUUID("id") ,
+                                                                    
row.has("bytes_in") ? row.getLong("bytes_in") : null,
+                                                                    
row.has("bytes_out") ? row.getLong("bytes_out") : null,
+                                                                    
row.has("columnfamily_name") ? row.getString("columnfamily_name") : null,
+                                                                    
row.has("compacted_at") ? row.getTimestamp("compacted_at") : null,
+                                                                    
row.has("keyspace_name") ? row.getString("keyspace_name") : null,
+                                                                    
row.has("rows_merged") ? row.getMap("rows_merged", Int32Type.instance, 
LongType.instance) : null,
+                                                                    
row.has("compaction_properties") ? row.getMap("compaction_properties", 
UTF8Type.instance, UTF8Type.instance) : ImmutableMap.of() })
+        );
+    }
+
+    /**
+     * Perform table migration by reading data from the old table, converting 
it, and adding to the new table.
+     * If oldName and newName are same, it means data in the table will be 
refreshed.
+     * 
+     * @param truncateIfExists truncate the existing table if it exists before 
migration; if it is disabled
+     *                         and the new table is not empty and oldName is 
not equal to newName, no migration is performed
+     * @param oldName          old table name
+     * @param newName          new table name
+     * @param columns          columns to fill in the new table in the same 
order as returned by the transformation
+     * @param transformation   transformation function which gets the row from 
the old table and returns a row for the new table
+     */
+    @VisibleForTesting
+    static void migrateTable(boolean truncateIfExists, String oldName, String 
newName, String[] columns, Function<UntypedResultSet.Row, Collection<Object[]>> 
transformation)

Review Comment:
   @driftx does this method differ from the one in 40 migrator? What about 
having abstract migrator these two extend and putting that method there so we 
can reuse?



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