@Test public void shouldNotCreateNewSearchResultEntityWhenOneExists() throws Exception { SearchResultItem item = new SearchResultItem(); item.setIndexerGuid("guid"); item.setIndexer(indexerMock); when(searchResultEntityMock.getIndexerGuid()).thenReturn("guid"); when(searchResultRepositoryMock.findAllIdsByIdIn(anyList())).thenReturn(Sets.newHashSet(299225959498991027L)); testee.persistSearchResults(Collections.singletonList(item), new IndexerSearchResult()); verify(searchResultRepositoryMock).saveAll(searchResultEntitiesCaptor.capture()); List<SearchResultEntity> persistedEntities = searchResultEntitiesCaptor.getValue(); assertThat(persistedEntities.size(), is(0)); }
@Transactional protected List<SearchResultItem> persistSearchResults(List<SearchResultItem> searchResultItems, IndexerSearchResult indexerSearchResult) { Stopwatch stopwatch = Stopwatch.createStarted(); synchronized (dbLock) { ArrayList<SearchResultEntity> searchResultEntities = new ArrayList<>(); Set<Long> alreadySavedIds = searchResultRepository.findAllIdsByIdIn(searchResultItems.stream().map(SearchResultIdCalculator::calculateSearchResultId).collect(Collectors.toList())); for (SearchResultItem item : searchResultItems) { long guid = SearchResultIdCalculator.calculateSearchResultId(item); if (!alreadySavedIds.contains(guid)) { SearchResultEntity searchResultEntity = new SearchResultEntity(); searchResultEntity.setIndexer(indexer); searchResultEntity.setTitle(item.getTitle()); searchResultEntity.setLink(item.getLink()); searchResultEntity.setDetails(item.getDetails()); searchResultEntity.setIndexerGuid(item.getIndexerGuid()); searchResultEntity.setFirstFound(Instant.now()); searchResultEntity.setDownloadType(item.getDownloadType()); searchResultEntity.setPubDate(item.getPubDate()); searchResultEntities.add(searchResultEntity); } item.setGuid(guid); item.setSearchResultId(guid); } debug("Found {} results which were already in the database and {} new ones", alreadySavedIds.size(), searchResultEntities.size()); try { searchResultRepository.saveAll(searchResultEntities); indexerSearchResult.setSearchResultEntities(new HashSet<>(searchResultEntities)); } catch (EntityExistsException e) { error("Unable to save the search results to the database", e); } } getLogger().debug(LoggingMarkers.PERFORMANCE, "Persisting {} search results took {}ms", searchResultItems.size(), stopwatch.elapsed(TimeUnit.MILLISECONDS)); return searchResultItems; }
Indexer { @Transactional protected List<SearchResultItem> persistSearchResults(List<SearchResultItem> searchResultItems, IndexerSearchResult indexerSearchResult) { Stopwatch stopwatch = Stopwatch.createStarted(); synchronized (dbLock) { ArrayList<SearchResultEntity> searchResultEntities = new ArrayList<>(); Set<Long> alreadySavedIds = searchResultRepository.findAllIdsByIdIn(searchResultItems.stream().map(SearchResultIdCalculator::calculateSearchResultId).collect(Collectors.toList())); for (SearchResultItem item : searchResultItems) { long guid = SearchResultIdCalculator.calculateSearchResultId(item); if (!alreadySavedIds.contains(guid)) { SearchResultEntity searchResultEntity = new SearchResultEntity(); searchResultEntity.setIndexer(indexer); searchResultEntity.setTitle(item.getTitle()); searchResultEntity.setLink(item.getLink()); searchResultEntity.setDetails(item.getDetails()); searchResultEntity.setIndexerGuid(item.getIndexerGuid()); searchResultEntity.setFirstFound(Instant.now()); searchResultEntity.setDownloadType(item.getDownloadType()); searchResultEntity.setPubDate(item.getPubDate()); searchResultEntities.add(searchResultEntity); } item.setGuid(guid); item.setSearchResultId(guid); } debug("Found {} results which were already in the database and {} new ones", alreadySavedIds.size(), searchResultEntities.size()); try { searchResultRepository.saveAll(searchResultEntities); indexerSearchResult.setSearchResultEntities(new HashSet<>(searchResultEntities)); } catch (EntityExistsException e) { error("Unable to save the search results to the database", e); } } getLogger().debug(LoggingMarkers.PERFORMANCE, "Persisting {} search results took {}ms", searchResultItems.size(), stopwatch.elapsed(TimeUnit.MILLISECONDS)); return searchResultItems; } }
Indexer { @Transactional protected List<SearchResultItem> persistSearchResults(List<SearchResultItem> searchResultItems, IndexerSearchResult indexerSearchResult) { Stopwatch stopwatch = Stopwatch.createStarted(); synchronized (dbLock) { ArrayList<SearchResultEntity> searchResultEntities = new ArrayList<>(); Set<Long> alreadySavedIds = searchResultRepository.findAllIdsByIdIn(searchResultItems.stream().map(SearchResultIdCalculator::calculateSearchResultId).collect(Collectors.toList())); for (SearchResultItem item : searchResultItems) { long guid = SearchResultIdCalculator.calculateSearchResultId(item); if (!alreadySavedIds.contains(guid)) { SearchResultEntity searchResultEntity = new SearchResultEntity(); searchResultEntity.setIndexer(indexer); searchResultEntity.setTitle(item.getTitle()); searchResultEntity.setLink(item.getLink()); searchResultEntity.setDetails(item.getDetails()); searchResultEntity.setIndexerGuid(item.getIndexerGuid()); searchResultEntity.setFirstFound(Instant.now()); searchResultEntity.setDownloadType(item.getDownloadType()); searchResultEntity.setPubDate(item.getPubDate()); searchResultEntities.add(searchResultEntity); } item.setGuid(guid); item.setSearchResultId(guid); } debug("Found {} results which were already in the database and {} new ones", alreadySavedIds.size(), searchResultEntities.size()); try { searchResultRepository.saveAll(searchResultEntities); indexerSearchResult.setSearchResultEntities(new HashSet<>(searchResultEntities)); } catch (EntityExistsException e) { error("Unable to save the search results to the database", e); } } getLogger().debug(LoggingMarkers.PERFORMANCE, "Persisting {} search results took {}ms", searchResultItems.size(), stopwatch.elapsed(TimeUnit.MILLISECONDS)); return searchResultItems; }  }
Indexer { @Transactional protected List<SearchResultItem> persistSearchResults(List<SearchResultItem> searchResultItems, IndexerSearchResult indexerSearchResult) { Stopwatch stopwatch = Stopwatch.createStarted(); synchronized (dbLock) { ArrayList<SearchResultEntity> searchResultEntities = new ArrayList<>(); Set<Long> alreadySavedIds = searchResultRepository.findAllIdsByIdIn(searchResultItems.stream().map(SearchResultIdCalculator::calculateSearchResultId).collect(Collectors.toList())); for (SearchResultItem item : searchResultItems) { long guid = SearchResultIdCalculator.calculateSearchResultId(item); if (!alreadySavedIds.contains(guid)) { SearchResultEntity searchResultEntity = new SearchResultEntity(); searchResultEntity.setIndexer(indexer); searchResultEntity.setTitle(item.getTitle()); searchResultEntity.setLink(item.getLink()); searchResultEntity.setDetails(item.getDetails()); searchResultEntity.setIndexerGuid(item.getIndexerGuid()); searchResultEntity.setFirstFound(Instant.now()); searchResultEntity.setDownloadType(item.getDownloadType()); searchResultEntity.setPubDate(item.getPubDate()); searchResultEntities.add(searchResultEntity); } item.setGuid(guid); item.setSearchResultId(guid); } debug("Found {} results which were already in the database and {} new ones", alreadySavedIds.size(), searchResultEntities.size()); try { searchResultRepository.saveAll(searchResultEntities); indexerSearchResult.setSearchResultEntities(new HashSet<>(searchResultEntities)); } catch (EntityExistsException e) { error("Unable to save the search results to the database", e); } } getLogger().debug(LoggingMarkers.PERFORMANCE, "Persisting {} search results took {}ms", searchResultItems.size(), stopwatch.elapsed(TimeUnit.MILLISECONDS)); return searchResultItems; }  void initialize(IndexerConfig config, IndexerEntity indexer); @EventListener void handleNewConfig(ConfigChangedEvent configChangedEvent); IndexerSearchResult search(SearchRequest searchRequest, int offset, Integer limit); abstract NfoResult getNfo(String guid); String getName(); IndexerConfig getConfig(); IndexerEntity getIndexerEntity(); String cleanUpTitle(String title); Optional<Instant> tryParseDate(String dateString); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); }
Indexer { @Transactional protected List<SearchResultItem> persistSearchResults(List<SearchResultItem> searchResultItems, IndexerSearchResult indexerSearchResult) { Stopwatch stopwatch = Stopwatch.createStarted(); synchronized (dbLock) { ArrayList<SearchResultEntity> searchResultEntities = new ArrayList<>(); Set<Long> alreadySavedIds = searchResultRepository.findAllIdsByIdIn(searchResultItems.stream().map(SearchResultIdCalculator::calculateSearchResultId).collect(Collectors.toList())); for (SearchResultItem item : searchResultItems) { long guid = SearchResultIdCalculator.calculateSearchResultId(item); if (!alreadySavedIds.contains(guid)) { SearchResultEntity searchResultEntity = new SearchResultEntity(); searchResultEntity.setIndexer(indexer); searchResultEntity.setTitle(item.getTitle()); searchResultEntity.setLink(item.getLink()); searchResultEntity.setDetails(item.getDetails()); searchResultEntity.setIndexerGuid(item.getIndexerGuid()); searchResultEntity.setFirstFound(Instant.now()); searchResultEntity.setDownloadType(item.getDownloadType()); searchResultEntity.setPubDate(item.getPubDate()); searchResultEntities.add(searchResultEntity); } item.setGuid(guid); item.setSearchResultId(guid); } debug("Found {} results which were already in the database and {} new ones", alreadySavedIds.size(), searchResultEntities.size()); try { searchResultRepository.saveAll(searchResultEntities); indexerSearchResult.setSearchResultEntities(new HashSet<>(searchResultEntities)); } catch (EntityExistsException e) { error("Unable to save the search results to the database", e); } } getLogger().debug(LoggingMarkers.PERFORMANCE, "Persisting {} search results took {}ms", searchResultItems.size(), stopwatch.elapsed(TimeUnit.MILLISECONDS)); return searchResultItems; }  void initialize(IndexerConfig config, IndexerEntity indexer); @EventListener void handleNewConfig(ConfigChangedEvent configChangedEvent); IndexerSearchResult search(SearchRequest searchRequest, int offset, Integer limit); abstract NfoResult getNfo(String guid); String getName(); IndexerConfig getConfig(); IndexerEntity getIndexerEntity(); String cleanUpTitle(String title); Optional<Instant> tryParseDate(String dateString); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString();  }