diff --git a/sql/src/main/java/org/apache/druid/sql/hook/DruidHook.java b/sql/src/main/java/org/apache/druid/sql/hook/DruidHook.java index fd2b32ebb98..5969d608f2f 100644 --- a/sql/src/main/java/org/apache/druid/sql/hook/DruidHook.java +++ b/sql/src/main/java/org/apache/druid/sql/hook/DruidHook.java @@ -21,15 +21,13 @@ package org.apache.druid.sql.hook; import com.google.errorprone.annotations.Immutable; import org.apache.calcite.rel.RelNode; -import org.apache.druid.annotations.SuppressFBWarnings; - -import java.io.Closeable; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; import java.util.Objects; +/** + * Interface for hooks that can be invoked by {@link DruidHookDispatcher}. + * + * HookKey should be added at every place a new hook is needed. + */ @FunctionalInterface public interface DruidHook { @@ -74,41 +72,4 @@ public interface DruidHook } void invoke(HookKey key, T object); - - @SuppressFBWarnings({"MS_OOI_PKGPROTECT"}) - Map, List>> GLOBAL = new HashMap<>(); - - static void register1(HookKey label, DruidHook hook) - { - GLOBAL.computeIfAbsent(label, k -> new ArrayList<>()).add(hook); - } - - static void unregister1(HookKey key, DruidHook hook) - { - GLOBAL.get(key).remove(hook); - } - - static Closeable withHook1(HookKey key, DruidHook hook) - { - register1(key, hook); - return new Closeable() - { - @Override - public void close() - { - unregister1(key, hook); - } - }; - } - - @SuppressWarnings({"rawtypes", "unchecked"}) - static void dispatch12(HookKey key, T object) - { - List> hooks = GLOBAL.get(key); - if (hooks != null) { - for (DruidHook hook : hooks) { - hook.invoke(key, object); - } - } - } } diff --git a/sql/src/main/java/org/apache/druid/sql/hook/DruidHook2.java b/sql/src/main/java/org/apache/druid/sql/hook/DruidHook2.java deleted file mode 100644 index e1bf52b74a8..00000000000 --- a/sql/src/main/java/org/apache/druid/sql/hook/DruidHook2.java +++ /dev/null @@ -1,24 +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.druid.sql.hook; - -public interface DruidHook2 -{ -} diff --git a/sql/src/main/java/org/apache/druid/sql/hook/DruidHookDispatcher.java b/sql/src/main/java/org/apache/druid/sql/hook/DruidHookDispatcher.java index 47b80ddca36..ea0dcfc0dfc 100644 --- a/sql/src/main/java/org/apache/druid/sql/hook/DruidHookDispatcher.java +++ b/sql/src/main/java/org/apache/druid/sql/hook/DruidHookDispatcher.java @@ -21,6 +21,8 @@ package org.apache.druid.sql.hook; import com.google.inject.Inject; import org.apache.druid.guice.LazySingleton; +import org.apache.druid.quidem.DruidConnectionExtras; +import org.apache.druid.sql.calcite.planner.PlannerContext; import org.apache.druid.sql.hook.DruidHook.HookKey; import java.io.Closeable; @@ -29,24 +31,32 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +/** + * Dispatcher for Druid hooks. + * + * A single instance should live in the system and be used to dispatch hooks. + * Usual way to dispatch should be via + * {@link PlannerContext#dispatchHook(HookKey, Object)}. Access to this class is + * also possible thru {@link DruidConnectionExtras}. + */ @LazySingleton public class DruidHookDispatcher { + Map, List>> hooks = new HashMap<>(); + @Inject public DruidHookDispatcher() { } - Map, List>> GLOBAL = new HashMap<>(); - public void register(HookKey label, DruidHook hook) { - GLOBAL.computeIfAbsent(label, k -> new ArrayList<>()).add(hook); + hooks.computeIfAbsent(label, k -> new ArrayList<>()).add(hook); } public void unregister(HookKey key, DruidHook hook) { - GLOBAL.get(key).remove(hook); + hooks.get(key).remove(hook); } public Closeable withHook(HookKey key, DruidHook hook) @@ -65,9 +75,9 @@ public class DruidHookDispatcher @SuppressWarnings({"rawtypes", "unchecked"}) public void dispatch(HookKey key, T object) { - List> hooks = GLOBAL.get(key); - if (hooks != null) { - for (DruidHook hook : hooks) { + List> currentHooks = hooks.get(key); + if (currentHooks != null) { + for (DruidHook hook : currentHooks) { hook.invoke(key, object); } }