diff --git a/src/de/steamwar/command/AbstractSWCommand.java b/src/de/steamwar/command/AbstractSWCommand.java index 75feed9..1de5785 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) -> { + TabCompletionCache.add(typeMapper, cached); 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) -> { + TabCompletionCache.add(typeMapper, cached); if (anno.local()) { localTypeMapper.putIfAbsent(anno.value().getTypeName(), (AbstractTypeMapper) typeMapper); } else { @@ -315,6 +319,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..d5a5220 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); } @@ -188,7 +188,7 @@ class CommandPart { return; } - Collection strings = typeMapper.tabCompletes(sender, Arrays.copyOf(args, startIndex), args[startIndex]); + Collection strings = tabCompletes(sender, args, startIndex); 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/SWCommandUtils.java b/src/de/steamwar/command/SWCommandUtils.java index 86716d2..9620631 100644 --- a/src/de/steamwar/command/SWCommandUtils.java +++ b/src/de/steamwar/command/SWCommandUtils.java @@ -313,7 +313,7 @@ public class SWCommandUtils { } static T[] getAnnotation(Method method, Class annotation) { - if (method.getAnnotations().length != 1) return null; + if (method.getAnnotations().length == 0) return null; return method.getDeclaredAnnotationsByType(annotation); } } diff --git a/src/de/steamwar/command/TabCompletionCache.java b/src/de/steamwar/command/TabCompletionCache.java new file mode 100644 index 0000000..5cf555c --- /dev/null +++ b/src/de/steamwar/command/TabCompletionCache.java @@ -0,0 +1,84 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +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<>(); + + void add(AbstractTypeMapper typeMapper, AbstractSWCommand.Cached cached) { + if (cached != null) { + TabCompletionCache.cached.add(typeMapper); + if (cached.global()) TabCompletionCache.global.add(typeMapper); + TabCompletionCache.cacheDuration.put(typeMapper, cached.timeUnit().toMillis(cached.cacheDuration())); + } + } + + @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 || System.currentTimeMillis() - tabCompletions.timestamp > cacheDuration.get(typeMapper)) { + 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); + } + } +} diff --git a/testsrc/de/steamwar/command/CacheCommand.java b/testsrc/de/steamwar/command/CacheCommand.java new file mode 100644 index 0000000..1771bca --- /dev/null +++ b/testsrc/de/steamwar/command/CacheCommand.java @@ -0,0 +1,57 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.command; + +import de.steamwar.command.dto.TestSWCommand; +import de.steamwar.command.dto.TestTypeMapper; + +import java.util.Arrays; +import java.util.Collection; +import java.util.concurrent.atomic.AtomicInteger; + +public class CacheCommand extends TestSWCommand { + + public CacheCommand() { + super("typemapper"); + } + + @Register + public void test(String sender, int tabCompleteTest) { + } + + private AtomicInteger count = new AtomicInteger(); + + @Cached + @Mapper(value = "int", local = true) + public AbstractTypeMapper typeMapper() { + System.out.println("TypeMapper register"); + return new TestTypeMapper() { + @Override + public Integer map(String sender, String[] previousArguments, String s) { + return Integer.parseInt(s); + } + + @Override + public Collection tabCompletes(String sender, String[] previousArguments, String s) { + return Arrays.asList(count.getAndIncrement() + ""); + } + }; + } +} diff --git a/testsrc/de/steamwar/command/CacheCommandTest.java b/testsrc/de/steamwar/command/CacheCommandTest.java new file mode 100644 index 0000000..122528f --- /dev/null +++ b/testsrc/de/steamwar/command/CacheCommandTest.java @@ -0,0 +1,46 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.command; + +import org.junit.Test; + +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +public class CacheCommandTest { + + @Test + public void testCaching() { + CacheCommand cmd = new CacheCommand(); + List tabCompletions1 = cmd.tabComplete("test", "", new String[]{""}); + List tabCompletions2 = cmd.tabComplete("test", "", new String[]{""}); + assertThat(tabCompletions1, is(equalTo(tabCompletions2))); + } + + @Test + public void testCachingWithDifferentSenders() { + CacheCommand cmd = new CacheCommand(); + List tabCompletions1 = cmd.tabComplete("test", "", new String[]{""}); + List tabCompletions2 = cmd.tabComplete("test2", "", new String[]{""}); + assertThat(tabCompletions1, is(not(equalTo(tabCompletions2)))); + } +}