SearchServiceImpl extends AbstractJobProducer implements SearchService, ManagedService { protected void populateIndex(String systemUserName) { long instancesInSolr = 0L; try { instancesInSolr = indexManager.count(); } catch (Exception e) { throw new IllegalStateException(e); } if (instancesInSolr > 0) { logger.debug("Search index found"); return; } if (instancesInSolr == 0L) { logger.info("No search index found"); Iterator<Tuple<MediaPackage, String>> mediaPackages; int total = 0; try { total = persistence.countMediaPackages(); logger.info("Starting population of search index from {} items in database", total); mediaPackages = persistence.getAllMediaPackages(); } catch (SearchServiceDatabaseException e) { logger.error("Unable to load the search entries: {}", e.getMessage()); throw new ServiceException(e.getMessage()); } int errors = 0; int current = 0; while (mediaPackages.hasNext()) { current++; try { Tuple<MediaPackage, String> mediaPackage = mediaPackages.next(); String mediaPackageId = mediaPackage.getA().getIdentifier().toString(); Organization organization = organizationDirectory.getOrganization(mediaPackage.getB()); securityService.setOrganization(organization); securityService.setUser(SecurityUtil.createSystemUser(systemUserName, organization)); AccessControlList acl = persistence.getAccessControlList(mediaPackageId); Date modificationDate = persistence.getModificationDate(mediaPackageId); Date deletionDate = persistence.getDeletionDate(mediaPackageId); indexManager.add(mediaPackage.getA(), acl, deletionDate, modificationDate); } catch (Exception e) { logger.error("Unable to index search instances:", e); if (retryToPopulateIndex(systemUserName)) { logger.warn("Trying to re-index search index later. Aborting for now."); return; } errors++; } finally { securityService.setOrganization(null); securityService.setUser(null); } if (current % 100 == 0) { logger.info("Indexing search {}/{} ({} percent done)", current, total, current * 100 / total); } } if (errors > 0) logger.error("Skipped {} erroneous search entries while populating the search index", errors); logger.info("Finished populating search index"); } } SearchServiceImpl(); SolrIndexManager getSolrIndexManager(); @Override void activate(final ComponentContext cc); void deactivate(); SearchResult getByQuery(String query, int limit, int offset); Job add(MediaPackage mediaPackage); void addSynchronously(MediaPackage mediaPackage); Job delete(String mediaPackageId); boolean deleteSynchronously(String mediaPackageId); void clear(); SearchResult getByQuery(SearchQuery q); @Override SearchResult getForAdministrativeRead(SearchQuery q); void setStaticMetadataService(StaticMetadataService mdService); void unsetStaticMetadataService(StaticMetadataService mdService); void setMpeg7CatalogService(Mpeg7CatalogService mpeg7CatalogService); void setPersistence(SearchServiceDatabase persistence); void setSeriesService(SeriesService seriesService); void setWorkspace(Workspace workspace); void setAuthorizationService(AuthorizationService authorizationService); void setServiceRegistry(ServiceRegistry serviceRegistry); void setSecurityService(SecurityService securityService); void setUserDirectoryService(UserDirectoryService userDirectoryService); void setOrganizationDirectoryService(OrganizationDirectoryService organizationDirectory); @Override void updated(@SuppressWarnings("rawtypes") Dictionary properties); static final String CONFIG_SOLR_URL; static final String CONFIG_SOLR_ROOT; static final String JOB_TYPE; static final float DEFAULT_ADD_JOB_LOAD; static final float DEFAULT_DELETE_JOB_LOAD; static final String ADD_JOB_LOAD_KEY; static final String DELETE_JOB_LOAD_KEY; }
@SuppressWarnings("unchecked") @Test public void testPopulateIndex() throws Exception { List<String> args = new ArrayList<String>(); args.add(new DefaultOrganization().getId()); List<Job> jobs = new ArrayList<Job>(); for (long i = 0; i < 10; i++) { MediaPackage mediaPackage = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew(); mediaPackage.setIdentifier(IdImpl.fromUUID()); searchDatabase.storeMediaPackage(mediaPackage, acl, new Date()); String payload = MediaPackageParser.getAsXml(mediaPackage); Job job = new JobImpl(i); job.setArguments(args); job.setPayload(payload); job.setStatus(Status.FINISHED); jobs.add(job); } ServiceRegistry serviceRegistry = EasyMock.createNiceMock(ServiceRegistry.class); EasyMock.expect( serviceRegistry.createJob((String) EasyMock.anyObject(), (String) EasyMock.anyObject(), (List<String>) EasyMock.anyObject(), (String) EasyMock.anyObject(), EasyMock.anyBoolean())) .andReturn(new JobImpl()).anyTimes(); EasyMock.expect(serviceRegistry.updateJob((Job) EasyMock.anyObject())).andReturn(new JobImpl()).anyTimes(); EasyMock.expect(serviceRegistry.getJobs((String) EasyMock.anyObject(), (Status) EasyMock.anyObject())) .andReturn(jobs).anyTimes(); EasyMock.replay(serviceRegistry); service.setServiceRegistry(serviceRegistry); OrganizationDirectoryService orgDirectory = EasyMock.createNiceMock(OrganizationDirectoryService.class); EasyMock.expect(orgDirectory.getOrganization((String) EasyMock.anyObject())).andReturn(new DefaultOrganization()) .anyTimes(); EasyMock.replay(orgDirectory); service.setOrganizationDirectoryService(orgDirectory); assertEquals(0, service.getByQuery(new SearchQuery()).size()); service.populateIndex("System Admin"); assertEquals(10, service.getByQuery(new SearchQuery()).size()); }