diff options
Diffstat (limited to 'libs/xmpp-addr/src/main/java/rocks/xmpp/util/cache')
3 files changed, 448 insertions, 0 deletions
diff --git a/libs/xmpp-addr/src/main/java/rocks/xmpp/util/cache/DirectoryCache.java b/libs/xmpp-addr/src/main/java/rocks/xmpp/util/cache/DirectoryCache.java new file mode 100644 index 000000000..9b7d66d04 --- /dev/null +++ b/libs/xmpp-addr/src/main/java/rocks/xmpp/util/cache/DirectoryCache.java @@ -0,0 +1,192 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2014-2016 Christian Schudt + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package rocks.xmpp.util.cache; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.function.BiConsumer; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * A simple directory based cache for caching of persistent items like avatars or entity capabilities. + * + * @author Christian Schudt + */ +public final class DirectoryCache implements Map<String, byte[]> { + + private final Path cacheDirectory; + + public DirectoryCache(Path cacheDirectory) { + this.cacheDirectory = cacheDirectory; + } + + @Override + public final int size() { + try (final Stream<Path> files = cacheContent()) { + return (int) Math.min(files.count(), Integer.MAX_VALUE); + } + } + + @Override + public final boolean isEmpty() { + try (final Stream<Path> files = cacheContent()) { + return files.findAny().map(file -> Boolean.FALSE).orElse(Boolean.TRUE); + } + } + + @Override + public final boolean containsKey(Object key) { + return Files.exists(cacheDirectory.resolve(key.toString())); + } + + @Override + public final boolean containsValue(Object value) { + throw new UnsupportedOperationException(); + } + + @Override + public final byte[] get(final Object key) { + return Optional.ofNullable(key).map(Object::toString).filter(((Predicate<String>) String::isEmpty).negate()).map(cacheDirectory::resolve).filter(Files::isReadable).map(file -> { + try { + return Files.readAllBytes(file); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }).orElse(null); + } + + @Override + public final byte[] put(String key, byte[] value) { + // Make sure the directory exists. + byte[] data = get(key); + if (!Arrays.equals(data, value)) + try { + if (Files.notExists(cacheDirectory)) { + Files.createDirectories(cacheDirectory); + } + Path file = cacheDirectory.resolve(key); + Files.write(file, value); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + return data; + } + + @Override + public final byte[] remove(Object key) { + byte[] data = get(key); + try { + Files.deleteIfExists(cacheDirectory.resolve(key.toString())); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + return data; + } + + @Override + public final void putAll(Map<? extends String, ? extends byte[]> m) { + m.forEach(this::put); + } + + @Override + public final void clear() { + try { + Files.walkFileTree(cacheDirectory, new SimpleFileVisitor<Path>() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Files.deleteIfExists(file); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + // Don't delete the cache directory itself. + if (!Files.isSameFile(dir, cacheDirectory)) { + Files.deleteIfExists(dir); + } + return FileVisitResult.CONTINUE; + } + }); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + @Override + public final Set<String> keySet() { + try (final Stream<Path> files = Files.list(cacheDirectory)) { + return Collections.unmodifiableSet(files.map(Path::getFileName).map(Path::toString).collect(Collectors.toSet())); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + @Override + public final Collection<byte[]> values() { + throw new UnsupportedOperationException(); + } + + @Override + public final Set<Entry<String, byte[]>> entrySet() { + throw new UnsupportedOperationException(); + } + + @Override + public final void forEach(final BiConsumer<? super String, ? super byte[]> action) { + if (Files.exists(cacheDirectory)) + try (final Stream<Path> files = cacheContent().filter(Files::isReadable)) { + files.forEach(file -> { + try { + action.accept(file.getFileName().toString(), Files.readAllBytes(file)); + } catch (final IOException e) { + throw new UncheckedIOException(e); + } + }); + } + } + + @SuppressWarnings("StreamResourceLeak") + private final Stream<Path> cacheContent() { + try { + return Files.walk(cacheDirectory).filter(Files::isRegularFile); + } catch (final IOException e) { + throw new UncheckedIOException(e); + } + } + +} diff --git a/libs/xmpp-addr/src/main/java/rocks/xmpp/util/cache/LruCache.java b/libs/xmpp-addr/src/main/java/rocks/xmpp/util/cache/LruCache.java new file mode 100644 index 000000000..c2fbb0c3f --- /dev/null +++ b/libs/xmpp-addr/src/main/java/rocks/xmpp/util/cache/LruCache.java @@ -0,0 +1,228 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2014-2016 Christian Schudt + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package rocks.xmpp.util.cache; + +import java.util.Collection; +import java.util.Map; +import java.util.Queue; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedDeque; +import java.util.function.BiFunction; +import java.util.function.Function; + +/** + * A simple concurrent implementation of a least-recently-used cache. + * <p> + * This cache is keeps a maximal number of items in memory and removes the least-recently-used item, when new items are added. + * + * @param <K> The key. + * @param <V> The value. + * @author Christian Schudt + * @see <a href="http://javadecodedquestions.blogspot.de/2013/02/java-cache-static-data-loading.html">http://javadecodedquestions.blogspot.de/2013/02/java-cache-static-data-loading.html</a> + * @see <a href="http://stackoverflow.com/a/22891780">http://stackoverflow.com/a/22891780</a> + */ +public final class LruCache<K, V> implements Map<K, V> { + private final int maxEntries; + + private final Map<K, V> map; + + final Queue<K> queue; + + public LruCache(final int maxEntries) { + this.maxEntries = maxEntries; + this.map = new ConcurrentHashMap<>(maxEntries); + // Don't use a ConcurrentLinkedQueue here. + // There's a JDK bug, leading to OutOfMemoryError and high CPU usage: + // https://bugs.openjdk.java.net/browse/JDK-8054446 + this.queue = new ConcurrentLinkedDeque<>(); + } + + @Override + public final int size() { + return map.size(); + } + + @Override + public final boolean isEmpty() { + return map.isEmpty(); + } + + @Override + public final boolean containsKey(final Object key) { + return map.containsKey(key); + } + + @Override + public final boolean containsValue(final Object value) { + return map.containsValue(value); + } + + @SuppressWarnings("unchecked") + @Override + public final V get(final Object key) { + final V v = map.get(key); + if (v != null) { + // Remove the key from the queue and re-add it to the tail. It is now the most recently used key. + keyUsed((K) key); + } + return v; + } + + + @Override + public final V put(final K key, final V value) { + V v = map.put(key, value); + keyUsed(key); + limit(); + return v; + } + + @Override + public final V remove(final Object key) { + queue.remove(key); + return map.remove(key); + } + + + @Override + public final void putAll(final Map<? extends K, ? extends V> m) { + for (Map.Entry<? extends K, ? extends V> entry : m.entrySet()) { + put(entry.getKey(), entry.getValue()); + } + } + + @Override + public final void clear() { + queue.clear(); + map.clear(); + } + + @Override + public final Set<K> keySet() { + return map.keySet(); + } + + @Override + public final Collection<V> values() { + return map.values(); + } + + @Override + public final Set<Entry<K, V>> entrySet() { + return map.entrySet(); + } + + + // Default methods + + @Override + public final V putIfAbsent(final K key, final V value) { + final V v = map.putIfAbsent(key, value); + if (v == null) { + keyUsed(key); + } + limit(); + return v; + } + + @Override + public final boolean remove(final Object key, final Object value) { + final boolean removed = map.remove(key, value); + if (removed) { + queue.remove(key); + } + return removed; + } + + @Override + public final boolean replace(final K key, final V oldValue, final V newValue) { + final boolean replaced = map.replace(key, oldValue, newValue); + if (replaced) { + keyUsed(key); + } + return replaced; + } + + @Override + public final V replace(final K key, final V value) { + final V v = map.replace(key, value); + if (v != null) { + keyUsed(key); + } + return v; + } + + @Override + public final V computeIfAbsent(final K key, final Function<? super K, ? extends V> mappingFunction) { + return map.computeIfAbsent(key, mappingFunction.<V>andThen(v -> { + keyUsed(key); + limit(); + return v; + })); + } + + @Override + public final V computeIfPresent(final K key, final BiFunction<? super K, ? super V, ? extends V> remappingFunction) { + return map.computeIfPresent(key, remappingFunction.<V>andThen(v -> { + keyUsed(key); + limit(); + return v; + })); + } + + @Override + public final V compute(final K key, final BiFunction<? super K, ? super V, ? extends V> remappingFunction) { + return map.compute(key, remappingFunction.<V>andThen(v -> { + keyUsed(key); + limit(); + return v; + })); + } + + @Override + public final V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) { + return map.merge(key, value, remappingFunction.<V>andThen(v -> { + keyUsed(key); + limit(); + return v; + })); + } + + private void limit() { + while (queue.size() > maxEntries) { + final K oldestKey = queue.poll(); + if (oldestKey != null) { + map.remove(oldestKey); + } + } + } + + private void keyUsed(final K key) { + // remove it from the queue and re-add it, to make it the most recently used key. + queue.remove(key); + queue.offer(key); + } +}
\ No newline at end of file diff --git a/libs/xmpp-addr/src/main/java/rocks/xmpp/util/cache/package-info.java b/libs/xmpp-addr/src/main/java/rocks/xmpp/util/cache/package-info.java new file mode 100644 index 000000000..c5e449d4c --- /dev/null +++ b/libs/xmpp-addr/src/main/java/rocks/xmpp/util/cache/package-info.java @@ -0,0 +1,28 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2014-2016 Christian Schudt + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * Provides simple cache implementations. + */ +package rocks.xmpp.util.cache;
\ No newline at end of file |