From cfd9b4107cae7fda35546a6efc3937618288a7dc Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 20 Jun 2022 13:16:13 +0200 Subject: [PATCH 1/5] 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); + } + } +} -- 2.39.2 From 6bd95ad84c44440f01515e56cacbf44b8ff0aa4a Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 20 Jun 2022 13:26:33 +0200 Subject: [PATCH 2/5] Fix one thing --- src/de/steamwar/command/TabCompletionCache.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/command/TabCompletionCache.java b/src/de/steamwar/command/TabCompletionCache.java index e746155..aa60cc6 100644 --- a/src/de/steamwar/command/TabCompletionCache.java +++ b/src/de/steamwar/command/TabCompletionCache.java @@ -35,7 +35,7 @@ public class TabCompletionCache { TabCompletions tabCompletions = tabCompletionCache.computeIfAbsent(key, ignore -> { return new TabCompletions(command, System.currentTimeMillis(), tabCompleteSupplier.get()); }); - if (tabCompletions.command != command) { + if (tabCompletions.command != command || System.currentTimeMillis() - tabCompletions.timestamp > cacheDuration.get(typeMapper)) { tabCompletions = new TabCompletions(command, System.currentTimeMillis(), tabCompleteSupplier.get()); tabCompletionCache.put(key, tabCompletions); } -- 2.39.2 From 75f88d65ac1878a22b68eee8cd1ac7a3d9f4160d Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 20 Jun 2022 14:06:28 +0200 Subject: [PATCH 3/5] Fix stuff Add tests --- src/de/steamwar/command/CommandPart.java | 2 +- src/de/steamwar/command/SWCommandUtils.java | 2 +- testsrc/de/steamwar/command/CacheCommand.java | 38 +++++++++++++++++++ .../de/steamwar/command/CacheCommandTest.java | 28 ++++++++++++++ 4 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 testsrc/de/steamwar/command/CacheCommand.java create mode 100644 testsrc/de/steamwar/command/CacheCommandTest.java diff --git a/src/de/steamwar/command/CommandPart.java b/src/de/steamwar/command/CommandPart.java index 283dd51..d5a5220 100644 --- a/src/de/steamwar/command/CommandPart.java +++ b/src/de/steamwar/command/CommandPart.java @@ -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); } 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/testsrc/de/steamwar/command/CacheCommand.java b/testsrc/de/steamwar/command/CacheCommand.java new file mode 100644 index 0000000..870d56d --- /dev/null +++ b/testsrc/de/steamwar/command/CacheCommand.java @@ -0,0 +1,38 @@ +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..5152d0b --- /dev/null +++ b/testsrc/de/steamwar/command/CacheCommandTest.java @@ -0,0 +1,28 @@ +package de.steamwar.command; + +import org.hamcrest.Matchers; +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)))); + } +} -- 2.39.2 From 96953bdec40be6e8cc478e2eba0cd45d840fbd48 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 20 Jun 2022 19:35:02 +0200 Subject: [PATCH 4/5] Update stuff --- src/de/steamwar/command/AbstractSWCommand.java | 12 ++---------- src/de/steamwar/command/TabCompletionCache.java | 8 ++++++++ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/de/steamwar/command/AbstractSWCommand.java b/src/de/steamwar/command/AbstractSWCommand.java index ad249cb..1de5785 100644 --- a/src/de/steamwar/command/AbstractSWCommand.java +++ b/src/de/steamwar/command/AbstractSWCommand.java @@ -111,7 +111,7 @@ public abstract class AbstractSWCommand { 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); + TabCompletionCache.add(typeMapper, cached); if (anno.local()) { localTypeMapper.putIfAbsent(anno.value(), (AbstractTypeMapper) typeMapper); } else { @@ -119,7 +119,7 @@ public abstract class AbstractSWCommand { } }); addMapper(ClassMapper.class, method, i -> i == 0, false, AbstractTypeMapper.class, (anno, typeMapper) -> { - addToCache(cached, typeMapper); + TabCompletionCache.add(typeMapper, cached); if (anno.local()) { localTypeMapper.putIfAbsent(anno.value().getTypeName(), (AbstractTypeMapper) typeMapper); } else { @@ -213,14 +213,6 @@ 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; diff --git a/src/de/steamwar/command/TabCompletionCache.java b/src/de/steamwar/command/TabCompletionCache.java index aa60cc6..258b253 100644 --- a/src/de/steamwar/command/TabCompletionCache.java +++ b/src/de/steamwar/command/TabCompletionCache.java @@ -15,6 +15,14 @@ public class TabCompletionCache { 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 { -- 2.39.2 From c06fad6593e64c64e6f74f26185cd78353abffc0 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 20 Jun 2022 21:20:42 +0200 Subject: [PATCH 5/5] Fixed copyright headers --- .../steamwar/command/TabCompletionCache.java | 19 ++++++++++++++++++ testsrc/de/steamwar/command/CacheCommand.java | 19 ++++++++++++++++++ .../de/steamwar/command/CacheCommandTest.java | 20 ++++++++++++++++++- 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/de/steamwar/command/TabCompletionCache.java b/src/de/steamwar/command/TabCompletionCache.java index 258b253..5cf555c 100644 --- a/src/de/steamwar/command/TabCompletionCache.java +++ b/src/de/steamwar/command/TabCompletionCache.java @@ -1,3 +1,22 @@ +/* + * 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; diff --git a/testsrc/de/steamwar/command/CacheCommand.java b/testsrc/de/steamwar/command/CacheCommand.java index 870d56d..1771bca 100644 --- a/testsrc/de/steamwar/command/CacheCommand.java +++ b/testsrc/de/steamwar/command/CacheCommand.java @@ -1,3 +1,22 @@ +/* + * 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; diff --git a/testsrc/de/steamwar/command/CacheCommandTest.java b/testsrc/de/steamwar/command/CacheCommandTest.java index 5152d0b..122528f 100644 --- a/testsrc/de/steamwar/command/CacheCommandTest.java +++ b/testsrc/de/steamwar/command/CacheCommandTest.java @@ -1,6 +1,24 @@ +/* + * 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.hamcrest.Matchers; import org.junit.Test; import java.util.List; -- 2.39.2