You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2021/05/19 08:22:44 UTC

[GitHub] [ignite-3] AMashenkov commented on a change in pull request #132: GG-14290: SchemaDescriptor and ConfigurationSchema converters.

AMashenkov commented on a change in pull request #132:
URL: https://github.com/apache/ignite-3/pull/132#discussion_r635018936



##########
File path: modules/schema/src/main/java/org/apache/ignite/internal/schema/configuration/SchemaConfigurationConverter.java
##########
@@ -0,0 +1,397 @@
+/*
+ * 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.ignite.internal.schema.configuration;
+
+import org.apache.ignite.configuration.schemas.table.ColumnChange;
+import org.apache.ignite.configuration.schemas.table.ColumnTypeChange;
+import org.apache.ignite.configuration.schemas.table.ColumnTypeView;
+import org.apache.ignite.configuration.schemas.table.ColumnView;
+import org.apache.ignite.configuration.schemas.table.IndexColumnChange;
+import org.apache.ignite.configuration.schemas.table.IndexColumnView;
+import org.apache.ignite.configuration.schemas.table.TableChange;
+import org.apache.ignite.configuration.schemas.table.TableConfiguration;
+import org.apache.ignite.configuration.schemas.table.TableIndexChange;
+import org.apache.ignite.configuration.schemas.table.TableIndexView;
+import org.apache.ignite.configuration.schemas.table.TableView;
+import org.apache.ignite.configuration.schemas.table.TablesChange;
+import org.apache.ignite.configuration.tree.NamedListView;
+import org.apache.ignite.internal.schema.ColumnImpl;
+import org.apache.ignite.internal.schema.HashIndexImpl;
+import org.apache.ignite.internal.schema.PartialIndexImpl;
+import org.apache.ignite.internal.schema.PrimaryIndexImpl;
+import org.apache.ignite.internal.schema.SchemaTableImpl;
+import org.apache.ignite.internal.schema.SortedIndexColumnImpl;
+import org.apache.ignite.internal.schema.SortedIndexImpl;
+import org.apache.ignite.schema.Column;
+import org.apache.ignite.schema.ColumnType;
+import org.apache.ignite.schema.HashIndex;
+import org.apache.ignite.schema.IndexColumn;
+import org.apache.ignite.schema.PartialIndex;
+import org.apache.ignite.schema.PrimaryIndex;
+import org.apache.ignite.schema.SchemaTable;
+import org.apache.ignite.schema.SortedIndex;
+import org.apache.ignite.schema.SortedIndexColumn;
+import org.apache.ignite.schema.TableIndex;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/** Configuration to schema and vice versa converter. */
+public class SchemaConfigurationConverter {
+    /** Hash index type. */
+    private static final String HASH_TYPE = "HASH";
+
+    /** Sorted index type. */
+    private static final String SORTED_TYPE = "SORTED";
+
+    /** Partial index type. */
+    private static final String PARTIAL_TYPE = "PARTIAL";
+
+    /** Primary key index type. */
+    private static final String PK_TYPE = "PK";
+
+    /** Types map. */
+    private static Map<String, ColumnType> types = new HashMap<>();
+
+    static {
+        putType(ColumnType.INT8);
+        putType(ColumnType.INT16);
+        putType(ColumnType.INT32);
+        putType(ColumnType.INT64);
+        putType(ColumnType.UINT8);
+        putType(ColumnType.UINT16);
+        putType(ColumnType.UINT32);
+        putType(ColumnType.UINT64);
+        putType(ColumnType.FLOAT);
+        putType(ColumnType.DOUBLE);
+        putType(ColumnType.UUID);
+        putType(ColumnType.string());
+        putType(ColumnType.blobOf());
+        // TODO; handle length for some types
+        //putType(ColumnType.bitmaskOf());
+    }
+
+    private static void putType(ColumnType type) {
+        types.put(type.typeSpec().name(), type);
+    }
+
+    /**
+     * Convert SortedIndexColumn to IndexColumnChange.
+     * @param col IndexColumnChange.
+     * @param colInit IndexColumnChange to fullfill.
+     */
+    public static void convert(SortedIndexColumn col, IndexColumnChange colInit) {
+        colInit.changeName(col.name());
+        colInit.changeAsc(col.asc());
+    }
+
+    /**
+     * Convert IndexColumnView to SortedIndexColumn.
+     *
+     * @param colCfg IndexColumnView.
+     * @return SortedIndexColumn.
+     */
+    public static SortedIndexColumn convert(IndexColumnView colCfg) {
+        return new SortedIndexColumnImpl(colCfg.name(), colCfg.asc());
+    }
+
+    /**
+     * Convert TableIndex to TableIndexChange.
+     *
+     * @param idx TableIndex.
+     * @param idxChg TableIndexChange to fullfill.
+     */
+    public static void convert(TableIndex idx, TableIndexChange idxChg) {
+        idxChg.changeName(idx.name());
+        idxChg.changeType(idx.type());
+
+        if (HASH_TYPE.equals(idx.type())) {
+            HashIndex hashIdx = (HashIndex)idx;
+
+            String[] colNames = hashIdx.columns().stream().map(IndexColumn::name).toArray(String[]::new);
+            idxChg.changeColNames(colNames);
+        }
+        else if (PARTIAL_TYPE.equals(idx.type())) {
+            PartialIndex partIdx = (PartialIndex)idx;
+
+            idxChg.changeUniq(partIdx.unique());
+            idxChg.changeExpr(partIdx.expr());
+
+            idxChg.changeColumns(colsChg -> {
+                for (SortedIndexColumn col : partIdx.columns())
+                    colsChg.create(col.name(), colInit -> convert(col, colInit));
+            });
+
+        }
+        else if (SORTED_TYPE.equals(idx.type())) {
+            SortedIndex sortIdx = (SortedIndex)idx;
+            idxChg.changeUniq(sortIdx.unique());
+
+            idxChg.changeColumns(colsInit -> {
+                for (SortedIndexColumn col : sortIdx.columns())
+                    colsInit.create(col.name(), colInit -> convert(col, colInit));
+            });
+        }
+        else if (PK_TYPE.equals(idx.type())) {
+            PrimaryIndex primIdx = (PrimaryIndex)idx;
+
+            idxChg.changeColumns(colsInit -> {
+                for (SortedIndexColumn col : primIdx.columns())
+                    colsInit.create(col.name(), colInit -> convert(col, colInit));
+            });
+
+            idxChg.changeAffinityColumns(primIdx.affinityColumns().toArray(new String[primIdx.affinityColumns().size()]));
+        }
+        else throw new IllegalArgumentException("Unknown index type " + idx.type());
+    }
+
+    /**
+     * Convert TableIndexView into TableIndex.
+     *
+     * @param idxView TableIndexView.
+     * @return TableIndex.
+     */
+    public static TableIndex convert(TableIndexView idxView) {
+        String name = idxView.name();
+        String type = idxView.type();
+
+        if (type.equals("HASH")) {
+            String[] cols = idxView.colNames();
+
+            return new HashIndexImpl(name, cols);
+        }
+        else if (type.equals("SORTED") || type.equals("PARTIAL") || type.equals("PK")) {
+            if (type.equals("PARTIAL")) {

Review comment:
       Let's rewrite to 'else-if'




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org