use of org.nzbhydra.searching.SearchResult in project nzbhydra2 by theotherp.
the class ExternalApi method handleCachingSearch.
protected ResponseEntity<?> handleCachingSearch(NewznabParameters params) {
// Remove old entries
cache.entrySet().removeIf(x -> x.getValue().getLastUpdate().isBefore(clock.instant().minus(MAX_CACHE_AGE_HOURS, ChronoUnit.HOURS)));
CacheEntryValue cacheEntryValue;
if (cache.containsKey(params.cacheKey())) {
cacheEntryValue = cache.get(params.cacheKey());
if (cacheEntryValue.getLastUpdate().isAfter(clock.instant().minus(params.getCachetime(), ChronoUnit.MINUTES))) {
Instant nextUpdate = cacheEntryValue.getLastUpdate().plus(params.getCachetime(), ChronoUnit.MINUTES);
logger.info("Returning cached search result. Next update of search will be done at {}", nextUpdate);
return new ResponseEntity<>(cacheEntryValue.getSearchResult(), HttpStatus.OK);
} else {
logger.info("Updating search because cache time is exceeded");
}
}
// Remove oldest entry when max size is reached
if (cache.size() == MAX_CACHE_SIZE) {
Optional<Entry<Integer, CacheEntryValue>> keyToEvict = cache.entrySet().stream().min(Comparator.comparing(o -> o.getValue().getLastUpdate()));
// Should always be the case anyway
logger.info("Removing oldest entry from cache because its limit of {} is reached", MAX_CACHE_SIZE);
keyToEvict.ifPresent(newznabParametersCacheEntryValueEntry -> cache.remove(newznabParametersCacheEntryValueEntry.getKey()));
}
NewznabResponse searchResult = search(params);
logger.info("Putting search result into cache");
cache.put(params.cacheKey(), new CacheEntryValue(params, clock.instant(), searchResult));
return new ResponseEntity<>(searchResult, HttpStatus.OK);
}
use of org.nzbhydra.searching.SearchResult in project nzbhydra2 by theotherp.
the class SearchingIntegrationTest method shouldSearch.
@Test
public void shouldSearch() throws Exception {
// One indexer has two results, the other one. A request is done with a limit of 2. Both indexers return one result. Another request is done with offset 2, the first indexer returns its second result
String expectedContent1a = Resources.toString(Resources.getResource(SearchingIntegrationTest.class, "simplesearchresult1a.xml"), Charsets.UTF_8);
String expectedContent1b = Resources.toString(Resources.getResource(SearchingIntegrationTest.class, "simplesearchresult1b.xml"), Charsets.UTF_8);
String expectedContent2 = Resources.toString(Resources.getResource(SearchingIntegrationTest.class, "simplesearchresult2.xml"), Charsets.UTF_8);
mockWebServer.enqueue(new MockResponse().setBody(expectedContent1a).setHeader("Content-Type", "application/xml; charset=utf-8"));
mockWebServer.enqueue(new MockResponse().setBody(expectedContent2).setHeader("Content-Type", "application/xml; charset=utf-8"));
mockWebServer.enqueue(new MockResponse().setBody(expectedContent1b).setHeader("Content-Type", "application/xml; charset=utf-8"));
SearchRequest searchRequest = new SearchRequest(SearchSource.INTERNAL, SearchType.SEARCH, 0, 2);
SearchResult searchResult = searcher.search(searchRequest);
assertThat(searchResult.getSearchResultItems().size(), is(2));
searchRequest.setLimit(100);
searchRequest.setOffset(2);
searchResult = searcher.search(searchRequest);
assertThat(searchResult.getSearchResultItems().size(), is(1));
assertThat(searchResult.getSearchResultItems().get(0).getTitle(), is("itemTitle1b"));
}
use of org.nzbhydra.searching.SearchResult in project nzbhydra2 by theotherp.
the class ExternalApi method search.
protected NewznabResponse search(NewznabParameters params) {
Stopwatch stopwatch = Stopwatch.createStarted();
SearchRequest searchRequest = buildBaseSearchRequest(params);
if (isTorznabCall()) {
searchRequest.setDownloadType(org.nzbhydra.searching.DownloadType.TORRENT);
} else {
searchRequest.setDownloadType(org.nzbhydra.searching.DownloadType.NZB);
}
SearchResult searchResult = searcher.search(searchRequest);
NewznabResponse transformedResults = transformResults(searchResult, params, searchRequest);
logger.info("Search took {}ms. Returning {} results", stopwatch.elapsed(TimeUnit.MILLISECONDS), searchResult.getSearchResultItems().size());
return transformedResults;
}
Aggregations