tledkov commented on code in PR #10200:
URL: https://github.com/apache/ignite/pull/10200#discussion_r958252972


##########
modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2SchemaManager.java:
##########
@@ -0,0 +1,418 @@
+/*
+ * 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.processors.query.h2;
+
+import java.lang.reflect.Method;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.processors.cache.GridCacheContextInfo;
+import org.apache.ignite.internal.processors.cache.query.IgniteQueryErrorCode;
+import org.apache.ignite.internal.processors.query.GridQueryRowDescriptorImpl;
+import org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor;
+import org.apache.ignite.internal.processors.query.IgniteSQLException;
+import org.apache.ignite.internal.processors.query.QueryField;
+import org.apache.ignite.internal.processors.query.QueryUtils;
+import org.apache.ignite.internal.processors.query.h2.opt.GridH2ProxyIndex;
+import org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor;
+import org.apache.ignite.internal.processors.query.h2.opt.GridH2Table;
+import org.apache.ignite.internal.processors.query.h2.sys.SqlSystemTableEngine;
+import 
org.apache.ignite.internal.processors.query.h2.sys.view.FiltrableSystemViewLocal;
+import org.apache.ignite.internal.processors.query.h2.sys.view.SystemViewLocal;
+import org.apache.ignite.internal.processors.query.schema.SchemaChangeListener;
+import 
org.apache.ignite.internal.processors.query.schema.management.IndexDescriptor;
+import 
org.apache.ignite.internal.processors.query.schema.management.SchemaManager;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.spi.systemview.view.FiltrableSystemView;
+import org.apache.ignite.spi.systemview.view.SystemView;
+import org.h2.index.Index;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * H2 schema manager. Responsible for reflecting to H2 database all schema 
changes.
+ */
+public class H2SchemaManager implements SchemaChangeListener {
+    /** Connection manager. */
+    private final ConnectionManager connMgr;
+
+    /** Data tables. */
+    private final ConcurrentMap<QueryTable, GridH2Table> dataTables = new 
ConcurrentHashMap<>();
+
+    /** Kernal context. */
+    private final GridKernalContext ctx;
+
+    /** Indexing. */
+    private final IgniteH2Indexing idx;
+
+    /** H2 index factory. */
+    private final H2IndexFactory idxFactory;
+
+    /** Logger. */
+    private final IgniteLogger log;
+
+    /** Underlying core schema manager. */
+    private volatile SchemaManager schemaMgr;
+
+    /**
+     * Constructor.
+     *
+     * @param ctx Kernal context.
+     * @param idx Indexing.
+     * @param connMgr Connection manager.
+     */
+    public H2SchemaManager(GridKernalContext ctx, IgniteH2Indexing idx, 
ConnectionManager connMgr) {
+        this.ctx = ctx;
+        this.idx = idx;
+        this.connMgr = connMgr;
+
+        idxFactory = new H2IndexFactory(ctx);
+        log = ctx.log(H2SchemaManager.class);
+    }
+
+    /**
+     * Handle node start.
+     */
+    public void start() throws IgniteCheckedException {
+        schemaMgr = ctx.query().schemaManager();
+
+        ctx.internalSubscriptionProcessor().registerSchemaChangeListener(this);
+
+        // Register predefined system functions.
+        createSqlFunction(QueryUtils.DFLT_SCHEMA, "QUERY_ENGINE", true,
+            H2Utils.class.getName() + ".queryEngine");
+    }
+
+    /** {@inheritDoc} */
+    @Override public void onSchemaCreated(String schema) {
+        try {
+            connMgr.executeSystemStatement("CREATE SCHEMA IF NOT EXISTS " + 
H2Utils.withQuotes(schema));
+
+            if (log.isDebugEnabled())
+                log.debug("Created H2 schema for index database: " + schema);
+        }
+        catch (IgniteCheckedException e) {
+            throw new IgniteException("Failed to create database schema: " + 
schema, e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void onSchemaDropped(String schema) {
+        try {
+            connMgr.executeSystemStatement("DROP SCHEMA IF EXISTS " + 
H2Utils.withQuotes(schema));
+
+            if (log.isDebugEnabled())
+                log.debug("Dropped H2 schema for index database: " + schema);
+        }
+        catch (IgniteCheckedException e) {
+            throw new IgniteException("Failed to drop database schema: " + 
schema, e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void onSqlTypeCreated(
+        String schemaName,
+        GridQueryTypeDescriptor typeDesc,
+        GridCacheContextInfo<?, ?> cacheInfo
+    ) {
+        H2TableDescriptor tblDesc = new H2TableDescriptor(idx, schemaName, 
typeDesc, cacheInfo);

Review Comment:
   Please add the synchonization between `dataTable` method and table creation.
   Without synchonization there is a race: query syccessfully parsed because H2 
table is creaed by SQL and dataTables doesn't contains the instance of the 
`GridH2Table`.



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

Reply via email to