Java tutorial
package de.fhg.iais.cortex.search; /****************************************************************************** * Copyright 2011 (c) Fraunhofer IAIS Netmedia http://www.iais.fraunhofer.de * * ************************************************************************** * * Licensed under the Apache License, Version 2.0 (the "License"); you may * * not use this file except in compliance with the License. * * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * * software distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the License for the specific language governing permissions and * * limitations under the License. * ******************************************************************************/ import java.io.IOException; import java.util.LinkedList; import java.util.List; import javax.inject.Inject; import javax.inject.Named; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.common.SolrDocument; import com.google.common.collect.Lists; import de.fhg.iais.commons.annotation.UsedBy; import de.fhg.iais.cortex.search.exception.IndexerException; @UsedBy("guice") public class DeleterImpl implements IDeleter { public static final long DELETE_BATCH_SIZE = 1000; private final SolrServer solrMasterServer; private final SolrServer solrSlaveServer; @Inject public DeleterImpl(@Named("search.indexer") SolrServer solrMasterServer, @Named("search") SolrServer solrSlaveServer) { this.solrMasterServer = solrMasterServer; this.solrSlaveServer = solrSlaveServer; } @Override public void deleteById(String id, boolean commit) { try { this.solrMasterServer.deleteByQuery(SolrFields.ID + ":" + id); if (commit) { this.solrMasterServer.commit(); } } catch (SolrServerException e) { throw new IndexerException("Could not delete document " + id + ".", e); } catch (IOException e) { throw new IndexerException("Could not delete document " + id + ".", e); } } @Override public List<String> deleteByIds(List<String> ids) { try { List<String> subList = Lists.newArrayList(); for (int i = 0; i < ids.size(); i++) { if (((i % DELETE_BATCH_SIZE) == 0) && !subList.isEmpty()) { deleteListOfItems(subList); subList.clear(); } subList.add(ids.get(i)); } deleteListOfItems(subList); } finally { try { this.solrMasterServer.commit(); } catch (SolrServerException e) { throw new IndexerException("Could not delete documents.", e); } catch (IOException e) { throw new IndexerException("Could not delete documents.", e); } } return ids; } private void deleteListOfItems(List<String> itemIds) { StringBuilder sb = new StringBuilder(SolrFields.ID).append(":").append("("); for (int i = 0; i < itemIds.size(); i++) { if (i == 0) { sb.append(itemIds.get(i)); } else { sb.append(" OR ").append(itemIds.get(i)); } } sb.append(")"); try { this.solrMasterServer.deleteByQuery(sb.toString()); } catch (SolrServerException e) { throw new IndexerException("Could not delete documents.", e); } catch (IOException e) { throw new IndexerException("Could not delete documents.", e); } } private List<String> getDocumentIds(SolrQuery solrQuery) throws SolrServerException { List<String> ids = new LinkedList<String>(); solrQuery.setRows(Integer.MAX_VALUE); QueryResponse queryResponse = this.solrSlaveServer.query(solrQuery); long numFound = queryResponse.getResults().getNumFound(); for (SolrDocument solrDocument : queryResponse.getResults()) { ids.add((String) solrDocument.getFieldValue(SolrFields.ID)); } if (numFound > Integer.MAX_VALUE) { throw new IndexerException( "Can only delete " + Integer.MAX_VALUE + " many items, but found " + numFound + " items."); } return ids; } @Override public List<String> getDocumentIdsByProvider(String providerId) { try { return getDocumentIds(new SolrQuery(SolrFields.PROVIDER_ID + ":" + providerId)); } catch (SolrServerException e) { throw new IndexerException("Could not get ids for provider " + providerId + ".", e); } } @Override public List<String> deleteByIngestId(String providerId, String ingestId) { List<String> ids = null; try { String deleteQuery = SolrFields.PROVIDER_ID + ":" + providerId + " AND " + SolrFields.INGEST_ID + ":" + ingestId; ids = getDocumentIds(new SolrQuery(deleteQuery)); this.solrMasterServer.deleteByQuery(deleteQuery); this.solrMasterServer.commit(); } catch (SolrServerException e) { throw new IndexerException("Could not delete for ingest " + ingestId + ".", e); } catch (IOException e) { throw new IndexerException("Could not delete for ingest " + ingestId + ".", e); } return ids; } @Override public List<String> deleteByIngestId(String ingestId, boolean commit) { List<String> ids = null; try { String deleteQuery = SolrFields.INGEST_ID + ":" + ingestId; ids = getDocumentIds(new SolrQuery(deleteQuery)); this.solrMasterServer.deleteByQuery(deleteQuery); if (commit) { this.solrMasterServer.commit(); } } catch (SolrServerException e) { throw new IndexerException("Could not delete for ingest " + ingestId + ".", e); } catch (IOException e) { throw new IndexerException("Could not delete for ingest " + ingestId + ".", e); } return ids; } @Override public List<String> deleteByProviderId(String id) { List<String> ids = null; try { ids = getDocumentIds(new SolrQuery(SolrFields.PROVIDER_ID + ":" + id)); this.solrMasterServer.deleteByQuery(SolrFields.PROVIDER_ID + ":" + id); this.solrMasterServer.commit(); } catch (SolrServerException e) { throw new IndexerException("Could not delete for provider " + id + ".", e); } catch (IOException e) { throw new IndexerException("Could not delete for provider " + id + ".", e); } return ids; } }