X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FSearchPage.java;h=fab7a5751d6a6f5aeb5af55e3214cad100196e71;hb=4a079c5610c1e8eac406fdfb7077525ef5b5fe2d;hp=6f5e001ad290d6451f382c99fb83ee2c1ac76ddc;hpb=72242bb0a4fc126c1a82cdc99cf5bbf8646b434e;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/web/SearchPage.java b/src/main/java/net/pterodactylus/sone/web/SearchPage.java index 6f5e001..fab7a57 100644 --- a/src/main/java/net/pterodactylus/sone/web/SearchPage.java +++ b/src/main/java/net/pterodactylus/sone/web/SearchPage.java @@ -32,9 +32,16 @@ import net.pterodactylus.sone.data.Profile; import net.pterodactylus.sone.data.Profile.Field; import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; +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.Mapper; import net.pterodactylus.util.collection.Mappers; import net.pterodactylus.util.collection.Pagination; +import net.pterodactylus.util.collection.TimedMap; import net.pterodactylus.util.filter.Filter; import net.pterodactylus.util.filter.Filters; import net.pterodactylus.util.logging.Logging; @@ -55,6 +62,20 @@ public class SearchPage extends SoneTemplatePage { /** The logger. */ private static final Logger logger = Logging.getLogger(SearchPage.class); + /** Short-term cache. */ + private final Cache, Set>> hitCache = new MemoryCache, Set>>(new ValueRetriever, Set>>() { + + @SuppressWarnings("synthetic-access") + public CacheItem>> retrieve(List phrases) throws CacheException { + 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())); + } + + }, new TimedMap, CacheItem>>>(300000)); + /** * Creates a new search page. * @@ -83,16 +104,21 @@ public class SearchPage extends SoneTemplatePage { } List phrases = parseSearchPhrases(query); + if (phrases.isEmpty()) { + throw new RedirectException("index.html"); + } Set sones = webInterface.getCore().getSones(); Set> soneHits = getHits(sones, phrases, SoneStringGenerator.COMPLETE_GENERATOR); - Set posts = new HashSet(); - for (Sone sone : sones) { - posts.addAll(sone.getPosts()); + 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(); } - @SuppressWarnings("synthetic-access") - Set> postHits = getHits(Filters.filteredSet(posts, Post.FUTURE_POSTS_FILTER), phrases, new PostStringGenerator()); /* now filter. */ soneHits = Filters.filteredSet(soneHits, Hit.POSITIVE_FILTER); @@ -172,11 +198,20 @@ public class SearchPage extends SoneTemplatePage { List phrases = new ArrayList(); for (String phrase : parsedPhrases) { if (phrase.startsWith("+")) { - phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.REQUIRED)); + if (phrase.length() > 1) { + phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.REQUIRED)); + } else { + phrases.add(new Phrase("+", Phrase.Optionality.OPTIONAL)); + } } else if (phrase.startsWith("-")) { - phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.FORBIDDEN)); + if (phrase.length() > 1) { + phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.FORBIDDEN)); + } else { + phrases.add(new Phrase("-", Phrase.Optionality.OPTIONAL)); + } + } else { + phrases.add(new Phrase(phrase, Phrase.Optionality.OPTIONAL)); } - phrases.add(new Phrase(phrase, Phrase.Optionality.OPTIONAL)); } return phrases; } @@ -401,6 +436,30 @@ public class SearchPage extends SoneTemplatePage { return optionality; } + // + // OBJECT METHODS + // + + /** + * {@inheritDoc} + */ + @Override + public int hashCode() { + return phrase.hashCode() ^ ((optionality == Optionality.FORBIDDEN) ? (0xaaaaaaaa) : ((optionality == Optionality.REQUIRED) ? 0x55555555 : 0)); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean equals(Object object) { + if (!(object instanceof Phrase)) { + return false; + } + Phrase phrase = (Phrase) object; + return (this.optionality == phrase.optionality) && this.phrase.equals(phrase.phrase); + } + } /**