From cfd9b4107cae7fda35546a6efc3937618288a7dc Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 20 Jun 2022 13:16:13 +0200 Subject: [PATCH] Add TabCompletionCache --- .../steamwar/command/AbstractSWCommand.java | 20 +++++++ src/de/steamwar/command/CommandPart.java | 8 ++- .../steamwar/command/TabCompletionCache.java | 57 +++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 src/de/steamwar/command/TabCompletionCache.java diff --git a/src/de/steamwar/command/AbstractSWCommand.java b/src/de/steamwar/command/AbstractSWCommand.java index 75feed9..ad249cb 100644 --- a/src/de/steamwar/command/AbstractSWCommand.java +++ b/src/de/steamwar/command/AbstractSWCommand.java @@ -23,6 +23,7 @@ import java.lang.annotation.*; import java.lang.reflect.Method; import java.lang.reflect.Parameter; import java.util.*; +import java.util.concurrent.TimeUnit; import java.util.function.BiConsumer; import java.util.function.IntPredicate; import java.util.function.Supplier; @@ -108,7 +109,9 @@ public abstract class AbstractSWCommand { private synchronized void createMapping() { List methods = methods(); for (Method method : methods) { + Cached cached = method.getAnnotation(Cached.class); addMapper(Mapper.class, method, i -> i == 0, false, AbstractTypeMapper.class, (anno, typeMapper) -> { + addToCache(cached, typeMapper); if (anno.local()) { localTypeMapper.putIfAbsent(anno.value(), (AbstractTypeMapper) typeMapper); } else { @@ -116,6 +119,7 @@ public abstract class AbstractSWCommand { } }); addMapper(ClassMapper.class, method, i -> i == 0, false, AbstractTypeMapper.class, (anno, typeMapper) -> { + addToCache(cached, typeMapper); if (anno.local()) { localTypeMapper.putIfAbsent(anno.value().getTypeName(), (AbstractTypeMapper) typeMapper); } else { @@ -209,6 +213,14 @@ public abstract class AbstractSWCommand { initialized = true; } + private void addToCache(Cached cached, AbstractTypeMapper typeMapper) { + if (cached != null) { + TabCompletionCache.cached.add(typeMapper); + if (cached.global()) TabCompletionCache.global.add(typeMapper); + TabCompletionCache.cacheDuration.put(typeMapper, cached.timeUnit().toMillis(cached.cacheDuration())); + } + } + private void add(Class annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class returnType, BiConsumer consumer) { T[] anno = SWCommandUtils.getAnnotation(method, annotation); if (anno == null || anno.length == 0) return; @@ -315,6 +327,14 @@ public abstract class AbstractSWCommand { boolean local() default false; } + @Retention(RetentionPolicy.RUNTIME) + @Target({ElementType.METHOD}) + protected @interface Cached { + long cacheDuration() default 5; + TimeUnit timeUnit() default TimeUnit.SECONDS; + boolean global() default false; + } + @Deprecated @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.PARAMETER, ElementType.METHOD}) diff --git a/src/de/steamwar/command/CommandPart.java b/src/de/steamwar/command/CommandPart.java index ef888ec..283dd51 100644 --- a/src/de/steamwar/command/CommandPart.java +++ b/src/de/steamwar/command/CommandPart.java @@ -169,7 +169,7 @@ class CommandPart { return; } } - Collection strings = typeMapper.tabCompletes(sender, Arrays.copyOf(args, args.length - 1), args[args.length - 1]); + Collection strings = tabCompletes(sender, args, args.length - 1); if (strings != null) { current.addAll(strings); } @@ -197,6 +197,12 @@ class CommandPart { } } + private Collection tabCompletes(T sender, String[] args, int startIndex) { + return TabCompletionCache.tabComplete(sender, typeMapper, command, () -> { + return typeMapper.tabCompletes(sender, Arrays.copyOf(args, startIndex), args[startIndex]); + }); + } + private CheckArgumentResult checkArgument(Consumer errors, GuardCheckType guardCheckType, T sender, String[] args, int index) { try { Object value = typeMapper.map(sender, Arrays.copyOf(args, index), args[index]); diff --git a/src/de/steamwar/command/TabCompletionCache.java b/src/de/steamwar/command/TabCompletionCache.java new file mode 100644 index 0000000..e746155 --- /dev/null +++ b/src/de/steamwar/command/TabCompletionCache.java @@ -0,0 +1,57 @@ +package de.steamwar.command; + +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.experimental.UtilityClass; + +import java.util.*; +import java.util.function.Supplier; + +@UtilityClass +public class TabCompletionCache { + + private Map tabCompletionCache = new HashMap<>(); + Set> cached = new HashSet<>(); + Set> global = new HashSet<>(); + Map, Long> cacheDuration = new HashMap<>(); + + @EqualsAndHashCode + @AllArgsConstructor + private static class Key { + private Object sender; + private AbstractTypeMapper typeMapper; + } + + @AllArgsConstructor + private static class TabCompletions { + private AbstractSWCommand command; + private long timestamp; + private Collection tabCompletions; + } + + Collection tabComplete(Object sender, AbstractTypeMapper typeMapper, AbstractSWCommand command, Supplier> tabCompleteSupplier) { + if (!cached.contains(typeMapper)) return tabCompleteSupplier.get(); + Key key = global.contains(typeMapper) ? new Key(null, typeMapper) : new Key(sender, typeMapper); + TabCompletions tabCompletions = tabCompletionCache.computeIfAbsent(key, ignore -> { + return new TabCompletions(command, System.currentTimeMillis(), tabCompleteSupplier.get()); + }); + if (tabCompletions.command != command) { + tabCompletions = new TabCompletions(command, System.currentTimeMillis(), tabCompleteSupplier.get()); + tabCompletionCache.put(key, tabCompletions); + } + tabCompletions.timestamp = System.currentTimeMillis(); + return tabCompletions.tabCompletions; + } + + public void invalidateOldEntries() { + Set toRemove = new HashSet<>(); + for (Map.Entry tabCompletionsEntry : tabCompletionCache.entrySet()) { + if (System.currentTimeMillis() - tabCompletionsEntry.getValue().timestamp > cacheDuration.get(tabCompletionsEntry.getKey().typeMapper)) { + toRemove.add(tabCompletionsEntry.getKey()); + } + } + for (Key key : toRemove) { + tabCompletionCache.remove(key); + } + } +}