import net.pterodactylus.sone.data.Profile.Field;
import net.pterodactylus.sone.data.Reply;
import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.util.collection.Converter;
-import net.pterodactylus.util.collection.Converters;
+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.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;
/** The logger. */
private static final Logger logger = Logging.getLogger(SearchPage.class);
+ /** Short-term cache. */
+ private final Cache<List<Phrase>, Set<Hit<Post>>> hitCache = new MemoryCache<List<Phrase>, Set<Hit<Post>>>(new ValueRetriever<List<Phrase>, Set<Hit<Post>>>() {
+
+ @SuppressWarnings("synthetic-access")
+ public CacheItem<Set<Hit<Post>>> retrieve(List<Phrase> phrases) throws CacheException {
+ Set<Post> posts = new HashSet<Post>();
+ for (Sone sone : webInterface.getCore().getSones()) {
+ posts.addAll(sone.getPosts());
+ }
+ return new DefaultCacheItem<Set<Hit<Post>>>(getHits(Filters.filteredSet(posts, Post.FUTURE_POSTS_FILTER), phrases, new PostStringGenerator()));
+ }
+
+ }, new TimedMap<List<Phrase>, CacheItem<Set<Hit<Post>>>>(300000));
+
/**
* Creates a new search page.
*
* {@inheritDoc}
*/
@Override
- protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException {
+ protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
super.processTemplate(request, templateContext);
String query = request.getHttpRequest().getParam("query").trim();
if (query.length() == 0) {
}
List<Phrase> phrases = parseSearchPhrases(query);
+ if (phrases.isEmpty()) {
+ throw new RedirectException("index.html");
+ }
Set<Sone> sones = webInterface.getCore().getSones();
Set<Hit<Sone>> soneHits = getHits(sones, phrases, SoneStringGenerator.COMPLETE_GENERATOR);
- Set<Post> posts = new HashSet<Post>();
- for (Sone sone : sones) {
- posts.addAll(sone.getPosts());
+ Set<Hit<Post>> 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<Hit<Post>> postHits = getHits(Filters.filteredSet(posts, Post.FUTURE_POSTS_FILTER), phrases, new PostStringGenerator());
/* now filter. */
soneHits = Filters.filteredSet(soneHits, Hit.POSITIVE_FILTER);
Collections.sort(sortedPostHits, Hit.DESCENDING_COMPARATOR);
/* extract Sones and posts. */
- List<Sone> resultSones = Converters.convertList(sortedSoneHits, new HitConverter<Sone>());
- List<Post> resultPosts = Converters.convertList(sortedPostHits, new HitConverter<Post>());
+ List<Sone> resultSones = Mappers.mappedList(sortedSoneHits, new HitMapper<Sone>());
+ List<Post> resultPosts = Mappers.mappedList(sortedPostHits, new HitMapper<Post>());
/* pagination. */
Pagination<Sone> sonePagination = new Pagination<Sone>(resultSones, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("sonePage"), 0));
List<Phrase> phrases = new ArrayList<Phrase>();
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;
}
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);
+ }
+
}
/**
* The type of the object to extract
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
- public static class HitConverter<T> implements Converter<Hit<T>, T> {
+ public static class HitMapper<T> implements Mapper<Hit<T>, T> {
/**
* {@inheritDoc}
*/
@Override
- public T convert(Hit<T> input) {
+ public T map(Hit<T> input) {
return input.getObject();
}