Skip to content

Commit

Permalink
Remove the codes which create a new HttpSolrServer for each method in…
Browse files Browse the repository at this point in the history
…vocation. Create a HttpSolrServer for each EJB and reuse it for each method call.
  • Loading branch information
LuoPengcheng committed Nov 1, 2015
1 parent a690fa4 commit c6d444f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 26 deletions.
11 changes: 10 additions & 1 deletion src/main/java/edu/harvard/iq/dataverse/AutoCompleteBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,20 @@ public class AutoCompleteBean implements java.io.Serializable {

@EJB
SystemConfig systemConfig;

private static SolrServer solrServer;

public SolrServer getSolrServer(){
if(solrServer == null){
solrServer = new HttpSolrServer("http://" + systemConfig.getSolrHostColonPort() + "/solr");
}
return solrServer;
}

public List<String> complete(String query) {
List<String> results = new ArrayList<>();

SolrServer solrServer = new HttpSolrServer("http://" + systemConfig.getSolrHostColonPort() + "/solr");
solrServer = getSolrServer();
SolrQuery solrQuery = new SolrQuery();
solrQuery.setParam("qt", "/terms");
solrQuery.setTermsLower(query);
Expand Down
45 changes: 26 additions & 19 deletions src/main/java/edu/harvard/iq/dataverse/search/IndexServiceBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import java.util.Set;
import java.util.concurrent.Future;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.AsyncResult;
import javax.ejb.EJB;
import javax.ejb.EJBException;
Expand Down Expand Up @@ -99,7 +101,21 @@ public class IndexServiceBean {
private static final String DRAFT_STRING = "Draft";
private static final String IN_REVIEW_STRING = "In Review";
private static final String DEACCESSIONED_STRING = "Deaccessioned";
private Dataverse rootDataverseCached;
private Dataverse rootDataverseCached;
private SolrServer solrServer;

@PostConstruct
public void init(){
solrServer = new HttpSolrServer("http://" + systemConfig.getSolrHostColonPort() + "/solr");
}

@PreDestroy
public void close(){
if(solrServer != null){
solrServer.shutdown();
solrServer = null;
}
}

@TransactionAttribute(REQUIRES_NEW)
public Future<String> indexDataverseInNewTransaction(Dataverse dataverse) {
Expand Down Expand Up @@ -193,12 +209,10 @@ public Future<String> indexDataverse(Dataverse dataverse) {
solrInputDocument.addField(SearchFields.SUBTREE, dataversePaths);
docs.add(solrInputDocument);

SolrServer server = new HttpSolrServer("http://" + systemConfig.getSolrHostColonPort() + "/solr");

String status;
try {
if (dataverse.getId() != null) {
server.add(docs);
solrServer.add(docs);
} else {
logger.info("WARNING: indexing of a dataverse with no id attempted");
}
Expand All @@ -208,7 +222,7 @@ public Future<String> indexDataverse(Dataverse dataverse) {
return new AsyncResult<>(status);
}
try {
server.commit();
solrServer.commit();
} catch (SolrServerException | IOException ex) {
status = ex.toString();
logger.info(status);
Expand Down Expand Up @@ -998,15 +1012,13 @@ private String addOrUpdateDataset(IndexableDataset indexableDataset) {
}
}

SolrServer server = new HttpSolrServer("http://" + systemConfig.getSolrHostColonPort() + "/solr");

try {
server.add(docs);
solrServer.add(docs);
} catch (SolrServerException | IOException ex) {
return ex.toString();
}
try {
server.commit();
solrServer.commit();
} catch (SolrServerException | IOException ex) {
return ex.toString();
}
Expand Down Expand Up @@ -1099,17 +1111,15 @@ public static String getDEACCESSIONED_STRING() {
}

public String delete(Dataverse doomed) {
SolrServer server = new HttpSolrServer("http://" + systemConfig.getSolrHostColonPort() + "/solr");

logger.fine("deleting Solr document for dataverse " + doomed.getId());
UpdateResponse updateResponse;
try {
updateResponse = server.deleteById(solrDocIdentifierDataverse + doomed.getId());
updateResponse = solrServer.deleteById(solrDocIdentifierDataverse + doomed.getId());
} catch (SolrServerException | IOException ex) {
return ex.toString();
}
try {
server.commit();
solrServer.commit();
} catch (SolrServerException | IOException ex) {
return ex.toString();
}
Expand All @@ -1125,17 +1135,16 @@ public String delete(Dataverse doomed) {
* https://github.com/IQSS/dataverse/issues/142
*/
public String removeSolrDocFromIndex(String doomed) {
SolrServer server = new HttpSolrServer("http://" + systemConfig.getSolrHostColonPort() + "/solr");


logger.fine("deleting Solr document: " + doomed);
UpdateResponse updateResponse;
try {
updateResponse = server.deleteById(doomed);
updateResponse = solrServer.deleteById(doomed);
} catch (SolrServerException | IOException ex) {
return ex.toString();
}
try {
server.commit();
solrServer.commit();
} catch (SolrServerException | IOException ex) {
return ex.toString();
}
Expand Down Expand Up @@ -1341,7 +1350,6 @@ public List<Long> findFilesInSolrOnly() throws SearchException {
}

private List<Long> findDvObjectInSolrOnly(String type) throws SearchException {
SolrServer solrServer = new HttpSolrServer("http://" + systemConfig.getSolrHostColonPort() + "/solr");
SolrQuery solrQuery = new SolrQuery();
solrQuery.setQuery("*");
solrQuery.setRows(Integer.MAX_VALUE);
Expand Down Expand Up @@ -1372,7 +1380,6 @@ private List<Long> findDvObjectInSolrOnly(String type) throws SearchException {
}

private List<String> findFilesOfParentDataset(long parentDatasetId) throws SearchException {
SolrServer solrServer = new HttpSolrServer("http://" + systemConfig.getSolrHostColonPort() + "/solr");
SolrQuery solrQuery = new SolrQuery();
solrQuery.setQuery("*");
solrQuery.setRows(Integer.MAX_VALUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.ejb.EJBTransactionRolledbackException;
import javax.ejb.Stateless;
Expand Down Expand Up @@ -81,6 +83,20 @@ public class SearchServiceBean {
SystemConfig systemConfig;

public static final JsfHelper JH = new JsfHelper();
private SolrServer solrServer;

@PostConstruct
public void init(){
solrServer = new HttpSolrServer("http://" + systemConfig.getSolrHostColonPort() + "/solr");
}

@PreDestroy
public void close(){
if(solrServer != null){
solrServer.shutdown();
solrServer = null;
}
}

/**
* Import note: "onlyDatatRelatedToMe" relies on filterQueries for providing
Expand Down Expand Up @@ -110,7 +126,6 @@ public SolrQueryResponse search(User user, Dataverse dataverse, String query, Li
throw new IllegalArgumentException("numResultsPerPage must be 1 or greater");
}

SolrServer solrServer = new HttpSolrServer("http://" + systemConfig.getSolrHostColonPort() + "/solr");
SolrQuery solrQuery = new SolrQuery();
query = SearchUtil.sanitizeQuery(query);
solrQuery.setQuery(query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Named;
Expand Down Expand Up @@ -55,6 +57,20 @@ public class SolrIndexServiceBean {

public static String numRowsClearedByClearAllIndexTimes = "numRowsClearedByClearAllIndexTimes";
public static String messageString = "message";
private SolrServer solrServer;

@PostConstruct
public void init(){
solrServer = new HttpSolrServer("http://" + systemConfig.getSolrHostColonPort() + "/solr");
}

@PreDestroy
public void close(){
if(solrServer != null){
solrServer.shutdown();
solrServer = null;
}
}

/**
* @deprecated Now that MyData has shipped in 4.1 we have no plans to change
Expand Down Expand Up @@ -358,7 +374,6 @@ private void persistToSolr(Collection<SolrInputDocument> docs) throws SolrServer
return;
}
logger.fine("persisting to Solr...");
SolrServer solrServer = new HttpSolrServer("http://" + systemConfig.getSolrHostColonPort() + "/solr");
/**
* @todo Do something with these responses from Solr.
*/
Expand Down Expand Up @@ -500,7 +515,6 @@ public IndexResponse deleteMultipleSolrIds(List<String> solrIdsToDelete) {
if (solrIdsToDelete.isEmpty()) {
return new IndexResponse("nothing to delete");
}
SolrServer solrServer = new HttpSolrServer("http://" + systemConfig.getSolrHostColonPort() + "/solr");
try {
solrServer.deleteById(solrIdsToDelete);
} catch (SolrServerException | IOException ex) {
Expand All @@ -519,10 +533,9 @@ public IndexResponse deleteMultipleSolrIds(List<String> solrIdsToDelete) {

public JsonObjectBuilder deleteAllFromSolrAndResetIndexTimes() throws SolrServerException, IOException {
JsonObjectBuilder response = Json.createObjectBuilder();
SolrServer server = new HttpSolrServer("http://" + systemConfig.getSolrHostColonPort() + "/solr");
logger.info("attempting to delete all Solr documents before a complete re-index");
server.deleteByQuery("*:*");
server.commit();
solrServer.deleteByQuery("*:*");
solrServer.commit();
int numRowsAffected = dvObjectService.clearAllIndexTimes();
response.add(numRowsClearedByClearAllIndexTimes, numRowsAffected);
response.add(messageString, "Solr index and database index timestamps cleared.");
Expand Down

0 comments on commit c6d444f

Please sign in to comment.