Search in sources :

Example 1 with SolrIndexWriter

use of org.apache.solr.update.SolrIndexWriter in project lucene-solr by apache.

the class SolrSnapshotManager method deleteSnapshotIndexFiles.

/**
   * This method deletes index files of the {@linkplain IndexCommit} for the specified generation number.
   *
   * @param core The Solr core
   * @param dir The index directory storing the snapshot.
   * @throws IOException in case of I/O errors.
   */
private static void deleteSnapshotIndexFiles(SolrCore core, Directory dir, IndexDeletionPolicy delPolicy) throws IOException {
    IndexWriterConfig conf = core.getSolrConfig().indexConfig.toIndexWriterConfig(core);
    conf.setOpenMode(OpenMode.APPEND);
    //Don't want to merge any commits here!
    conf.setMergePolicy(NoMergePolicy.INSTANCE);
    conf.setIndexDeletionPolicy(delPolicy);
    conf.setCodec(core.getCodec());
    try (SolrIndexWriter iw = new SolrIndexWriter("SolrSnapshotCleaner", dir, conf)) {
    // Do nothing. The only purpose of opening index writer is to invoke the Lucene IndexDeletionPolicy#onInit
    // method so that we can cleanup the files associated with specified index commit.
    // Note the index writer creates a new commit during the close() operation (which is harmless).
    }
}
Also used : SolrIndexWriter(org.apache.solr.update.SolrIndexWriter) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 2 with SolrIndexWriter

use of org.apache.solr.update.SolrIndexWriter in project lucene-solr by apache.

the class SolrCore method initIndex.

void initIndex(boolean passOnPreviousState, boolean reload) throws IOException {
    String indexDir = getNewIndexDir();
    boolean indexExists = getDirectoryFactory().exists(indexDir);
    boolean firstTime;
    synchronized (SolrCore.class) {
        firstTime = dirs.add(getDirectoryFactory().normalize(indexDir));
    }
    initIndexReaderFactory();
    if (indexExists && firstTime && !passOnPreviousState) {
        final String lockType = getSolrConfig().indexConfig.lockType;
        Directory dir = directoryFactory.get(indexDir, DirContext.DEFAULT, lockType);
        try {
            if (isWriterLocked(dir)) {
                log.error(logid + "Solr index directory '{}' is locked (lockType={}).  Throwing exception.", indexDir, lockType);
                throw new LockObtainFailedException("Index dir '" + indexDir + "' of core '" + name + "' is already locked. " + "The most likely cause is another Solr server (or another solr core in this server) " + "also configured to use this directory; other possible causes may be specific to lockType: " + lockType);
            }
        } finally {
            directoryFactory.release(dir);
        }
    }
    // Create the index if it doesn't exist.
    if (!indexExists) {
        log.debug(logid + "Solr index directory '" + new File(indexDir) + "' doesn't exist." + " Creating new index...");
        SolrIndexWriter writer = SolrIndexWriter.create(this, "SolrCore.initIndex", indexDir, getDirectoryFactory(), true, getLatestSchema(), solrConfig.indexConfig, solrDelPolicy, codec);
        writer.close();
    }
    cleanupOldIndexDirectories(reload);
}
Also used : SolrIndexWriter(org.apache.solr.update.SolrIndexWriter) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) File(java.io.File) Directory(org.apache.lucene.store.Directory)

Aggregations

SolrIndexWriter (org.apache.solr.update.SolrIndexWriter)2 File (java.io.File)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 Directory (org.apache.lucene.store.Directory)1 LockObtainFailedException (org.apache.lucene.store.LockObtainFailedException)1