package eu.dnetlib.functionality.index.solr; import java.io.File; import java.io.IOException; import java.util.Collections; import java.util.List; import java.util.Set; import javax.xml.parsers.ParserConfigurationException; import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.lucene.store.SimpleFSDirectory; import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer; import org.apache.solr.core.CoreContainer; import org.apache.solr.schema.IndexSchema; import org.apache.solr.update.MergeIndexesCommand; import org.xml.sax.SAXException; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import eu.dnetlib.miscutils.collections.MappedCollection; import eu.dnetlib.miscutils.functional.UnaryFunction; public class LocalIndexServer { private static final Log log = LogFactory.getLog(LocalIndexServer.class); // NOPMD by marko on 11/24/08 5:02 PM private CoreContainer container; private IndexSchema schema; private SolrServer server; private String indexName; private Set dsIds; public LocalIndexServer(final String indexName, final List dsIds, final String solrDataDir, final IndexSchema schema) throws ParserConfigurationException, IOException, SAXException { this.indexName = indexName; this.setSchema(schema); this.dsIds = Collections.synchronizedSet(Sets.newHashSet(dsIds)); server = newEmbeddedSolrServer(indexName, solrDataDir); } private SolrServer newEmbeddedSolrServer(final String indexName, final String solrDataDir) throws ParserConfigurationException, IOException, SAXException { File home = new File(solrDataDir + IOUtils.DIR_SEPARATOR_UNIX + indexName); this.container = new CoreContainer(home.getAbsolutePath()); getContainer().load(home.getAbsolutePath(), new File(home, "solr.xml")); return new EmbeddedSolrServer(getContainer(), indexName); } public void mergeIndexes(final List paths) throws IOException { log.info("merging PATHS: " + paths); UnaryFunction mapper = new UnaryFunction() { @Override public SimpleFSDirectory evaluate(String path) { try { return new SimpleFSDirectory(new File(path)); } catch (IOException e) { throw new RuntimeException(e); } } }; SimpleFSDirectory[] dirs = Iterables.toArray(new MappedCollection(paths, mapper ), SimpleFSDirectory.class); getContainer().getCore(indexName).getUpdateHandler().mergeIndexes(new MergeIndexesCommand(dirs)); log.info("DONE merging DIRS: " + Lists.newArrayList(dirs)); } public void restartCore() throws ParserConfigurationException, IOException, SAXException { log.info("restarting core: " + indexName); getContainer().reload(indexName); } public SolrServer getServer() { return server; } public IndexSchema getSchema() { return schema; } public void setSchema(IndexSchema schema) { this.schema = schema; } public Set getDsIds() { return dsIds; } public CoreContainer getContainer() { return container; } }