X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FSearchPage.java;h=b937b2f97d45c0c13a0b2d4ef3ac32d244831b8c;hp=0f12abff77de543cfa29aa462a9481d28426096a;hb=93fdf9e218d808f65f618abbddce183191d8c15b;hpb=61fea0173ef87542cae35247e6356ef36f2664d3 diff --git a/src/main/java/net/pterodactylus/sone/web/SearchPage.java b/src/main/java/net/pterodactylus/sone/web/SearchPage.java index 0f12abf..b937b2f 100644 --- a/src/main/java/net/pterodactylus/sone/web/SearchPage.java +++ b/src/main/java/net/pterodactylus/sone/web/SearchPage.java @@ -1,5 +1,5 @@ /* - * Sone - SearchPage.java - Copyright © 2010–2012 David Roden + * Sone - SearchPage.java - Copyright © 2010–2013 David Roden * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,6 +24,7 @@ import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; @@ -34,18 +35,7 @@ import net.pterodactylus.sone.data.Profile.Field; import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.page.FreenetRequest; -import net.pterodactylus.util.cache.Cache; -import net.pterodactylus.util.cache.CacheException; -import net.pterodactylus.util.cache.CacheItem; -import net.pterodactylus.util.cache.DefaultCacheItem; -import net.pterodactylus.util.cache.MemoryCache; -import net.pterodactylus.util.cache.ValueRetriever; import net.pterodactylus.util.collection.Pagination; -import net.pterodactylus.util.collection.TimedMap; -import net.pterodactylus.util.collection.filter.Filter; -import net.pterodactylus.util.collection.filter.Filters; -import net.pterodactylus.util.collection.mapper.Mapper; -import net.pterodactylus.util.collection.mapper.Mappers; import net.pterodactylus.util.logging.Logging; import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.template.Template; @@ -53,6 +43,16 @@ import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.text.StringEscaper; import net.pterodactylus.util.text.TextException; +import com.google.common.base.Function; +import com.google.common.base.Optional; +import com.google.common.base.Predicate; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import com.google.common.collect.Collections2; +import com.google.common.collect.FluentIterable; +import com.google.common.collect.Ordering; + /** * This page lets the user search for posts and replies that contain certain * words. @@ -65,19 +65,18 @@ public class SearchPage extends SoneTemplatePage { private static final Logger logger = Logging.getLogger(SearchPage.class); /** Short-term cache. */ - private final Cache, Set>> hitCache = new MemoryCache, Set>>(new ValueRetriever, Set>>() { + private final LoadingCache, Set>> hitCache = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES).build(new CacheLoader, Set>>() { @Override @SuppressWarnings("synthetic-access") - public CacheItem>> retrieve(List phrases) throws CacheException { + public Set> load(List phrases) { Set posts = new HashSet(); for (Sone sone : webInterface.getCore().getSones()) { posts.addAll(sone.getPosts()); } - return new DefaultCacheItem>>(getHits(Filters.filteredSet(posts, Post.FUTURE_POSTS_FILTER), phrases, new PostStringGenerator())); + return getHits(Collections2.filter(posts, Post.FUTURE_POSTS_FILTER), phrases, new PostStringGenerator()); } - - }, new TimedMap, CacheItem>>>(300000)); + }); /** * Creates a new search page. @@ -99,6 +98,7 @@ public class SearchPage extends SoneTemplatePage { * {@inheritDoc} */ @Override + @SuppressWarnings("synthetic-access") protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException { super.processTemplate(request, templateContext); String query = request.getHttpRequest().getParam("query").trim(); @@ -132,30 +132,21 @@ public class SearchPage extends SoneTemplatePage { } Set sones = webInterface.getCore().getSones(); - Set> soneHits = getHits(sones, phrases, SoneStringGenerator.COMPLETE_GENERATOR); + Collection> soneHits = getHits(sones, phrases, SoneStringGenerator.COMPLETE_GENERATOR); - Set> postHits; - try { - postHits = hitCache.get(phrases); - } catch (CacheException ce1) { - /* should never happen. */ - logger.log(Level.SEVERE, "Could not get search results from cache!", ce1); - postHits = Collections.emptySet(); - } + Collection> postHits = hitCache.getUnchecked(phrases); /* now filter. */ - soneHits = Filters.filteredSet(soneHits, Hit.POSITIVE_FILTER); - postHits = Filters.filteredSet(postHits, Hit.POSITIVE_FILTER); + soneHits = Collections2.filter(soneHits, Hit.POSITIVE_FILTER); + postHits = Collections2.filter(postHits, Hit.POSITIVE_FILTER); /* now sort. */ - List> sortedSoneHits = new ArrayList>(soneHits); - Collections.sort(sortedSoneHits, Hit.DESCENDING_COMPARATOR); - List> sortedPostHits = new ArrayList>(postHits); - Collections.sort(sortedPostHits, Hit.DESCENDING_COMPARATOR); + List> sortedSoneHits = Ordering.from(Hit.DESCENDING_COMPARATOR).sortedCopy(soneHits); + List> sortedPostHits = Ordering.from(Hit.DESCENDING_COMPARATOR).sortedCopy(postHits); /* extract Sones and posts. */ - List resultSones = Mappers.mappedList(sortedSoneHits, new HitMapper()); - List resultPosts = Mappers.mappedList(sortedPostHits, new HitMapper()); + List resultSones = FluentIterable.from(sortedSoneHits).transform(new HitMapper()).toList(); + List resultPosts = FluentIterable.from(sortedPostHits).transform(new HitMapper()).toList(); /* pagination. */ Pagination sonePagination = new Pagination(resultSones, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("sonePage"), 0)); @@ -332,7 +323,7 @@ public class SearchPage extends SoneTemplatePage { */ private String getPostId(String phrase) { String postId = phrase.startsWith("post://") ? phrase.substring(7) : phrase; - return (webInterface.getCore().getPost(postId, false) != null) ? postId : null; + return (webInterface.getCore().getPost(postId) != null) ? postId : null; } /** @@ -346,7 +337,11 @@ public class SearchPage extends SoneTemplatePage { */ private String getReplyPostId(String phrase) { String replyId = phrase.startsWith("reply://") ? phrase.substring(8) : phrase; - return (webInterface.getCore().getReply(replyId, false) != null) ? webInterface.getCore().getReply(replyId, false).getPost().getId() : null; + Optional postReply = webInterface.getCore().getPostReply(replyId); + if (!postReply.isPresent()) { + return null; + } + return postReply.get().getPost().getId(); } /** @@ -472,7 +467,7 @@ public class SearchPage extends SoneTemplatePage { if (post.getRecipient() != null) { postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(post.getRecipient())); } - for (PostReply reply : Filters.filteredList(webInterface.getCore().getReplies(post), Reply.FUTURE_REPLY_FILTER)) { + for (PostReply reply : Collections2.filter(webInterface.getCore().getReplies(post), Reply.FUTURE_REPLY_FILTER)) { postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(reply.getSone())); postString.append(' ').append(reply.getText()); } @@ -582,10 +577,10 @@ public class SearchPage extends SoneTemplatePage { private static class Hit { /** Filter for {@link Hit}s with a score of more than 0. */ - public static final Filter> POSITIVE_FILTER = new Filter>() { + public static final Predicate> POSITIVE_FILTER = new Predicate>() { @Override - public boolean filterObject(Hit hit) { + public boolean apply(Hit hit) { return hit.getScore() > 0; } @@ -647,13 +642,13 @@ public class SearchPage extends SoneTemplatePage { * The type of the object to extract * @author David ‘Bombe’ Roden */ - public static class HitMapper implements Mapper, T> { + private static class HitMapper implements Function, T> { /** * {@inheritDoc} */ @Override - public T map(Hit input) { + public T apply(Hit input) { return input.getObject(); }