add apidoc/etc

This commit is contained in:
Zoltan Haindrich 2024-07-30 13:14:51 +00:00
parent 9ac26e3a89
commit df42245685
3 changed files with 22 additions and 75 deletions

View File

@ -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<T>
{
@ -74,41 +72,4 @@ public interface DruidHook<T>
}
void invoke(HookKey<T> key, T object);
@SuppressFBWarnings({"MS_OOI_PKGPROTECT"})
Map<HookKey<?>, List<DruidHook<?>>> 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 <T> Closeable withHook1(HookKey<T> key, DruidHook<T> hook)
{
register1(key, hook);
return new Closeable()
{
@Override
public void close()
{
unregister1(key, hook);
}
};
}
@SuppressWarnings({"rawtypes", "unchecked"})
static <T> void dispatch12(HookKey<T> key, T object)
{
List<DruidHook<?>> hooks = GLOBAL.get(key);
if (hooks != null) {
for (DruidHook hook : hooks) {
hook.invoke(key, object);
}
}
}
}

View File

@ -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<T>
{
}

View File

@ -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<HookKey<?>, List<DruidHook<?>>> hooks = new HashMap<>();
@Inject
public DruidHookDispatcher()
{
}
Map<HookKey<?>, List<DruidHook<?>>> 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 <T> Closeable withHook(HookKey<T> key, DruidHook<T> hook)
@ -65,9 +75,9 @@ public class DruidHookDispatcher
@SuppressWarnings({"rawtypes", "unchecked"})
public <T> void dispatch(HookKey<T> key, T object)
{
List<DruidHook<?>> hooks = GLOBAL.get(key);
if (hooks != null) {
for (DruidHook hook : hooks) {
List<DruidHook<?>> currentHooks = hooks.get(key);
if (currentHooks != null) {
for (DruidHook hook : currentHooks) {
hook.invoke(key, object);
}
}