Example usage for org.apache.lucene.index IndexFileNames SEGMENTS

List of usage examples for org.apache.lucene.index IndexFileNames SEGMENTS

Introduction

In this page you can find the example usage for org.apache.lucene.index IndexFileNames SEGMENTS.

Prototype

String SEGMENTS

To view the source code for org.apache.lucene.index IndexFileNames SEGMENTS.

Click Source Link

Document

Name of the index segment file

Usage

From source file:com.github.lucene.store.jdbc.support.LuceneFileNames.java

License:Apache License

/**
 * Returns if the name is a segment file or not.
 *///from   w w w  .j a v a2 s . c om
public static boolean isSegmentsFile(final String name) {
    logger.debug("LuceneFileNames.isSegmentsFile({})", name);
    return name.equals(IndexFileNames.SEGMENTS) || name.equals(IndexFileNames.OLD_SEGMENTS_GEN)
            || name.equals(IndexFileNames.PENDING_SEGMENTS);
}

From source file:org.apache.mahout.text.LuceneIndexFileNameFilter.java

License:Apache License

public boolean accept(Path path) {
    String name = path.getName();
    if (CODEC_FILE_PATTERN.matcher(name).matches() || name.startsWith(IndexFileNames.SEGMENTS)) {
        return true;
    }//from   w  w w  . j av a2 s . co  m
    for (String extension : IndexFileNames.INDEX_EXTENSIONS) {
        if (name.endsWith(extension)) {
            return true;
        }
    }
    return false;
}

From source file:org.apache.solr.handler.TestRestoreCore.java

License:Apache License

@Test
public void testFailedRestore() throws Exception {
    int nDocs = BackupRestoreUtils.indexDocs(masterClient, "collection1", docsSeed);

    String location = createTempDir().toFile().getAbsolutePath();
    String snapshotName = TestUtil.randomSimpleString(random(), 1, 5);
    String params = "&name=" + snapshotName + "&location=" + URLEncoder.encode(location, "UTF-8");
    String baseUrl = masterJetty.getBaseUrl().toString();

    TestReplicationHandlerBackup.runBackupCommand(masterJetty, ReplicationHandler.CMD_BACKUP, params);

    CheckBackupStatus checkBackupStatus = new CheckBackupStatus((HttpSolrClient) masterClient,
            DEFAULT_TEST_CORENAME, null);
    while (!checkBackupStatus.success) {
        checkBackupStatus.fetchStatus();
        Thread.sleep(1000);/* w  ww.  ja va 2s. com*/
    }

    //Remove the segments_n file so that the backup index is corrupted.
    //Restore should fail and it should automatically rollback to the original index.
    Path restoreIndexPath = Paths.get(location).resolve("snapshot." + snapshotName);
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(restoreIndexPath,
            IndexFileNames.SEGMENTS + "*")) {
        Path segmentFileName = stream.iterator().next();
        Files.delete(segmentFileName);
    }

    TestReplicationHandlerBackup.runBackupCommand(masterJetty, ReplicationHandler.CMD_RESTORE, params);

    try {
        while (!fetchRestoreStatus(baseUrl, DEFAULT_TEST_CORENAME)) {
            Thread.sleep(1000);
        }
        fail("Should have thrown an error because restore could not have been successful");
    } catch (AssertionError e) {
        //supposed to happen
    }

    BackupRestoreUtils.verifyDocs(nDocs, masterClient, DEFAULT_TEST_CORENAME);

    //make sure we can write to the index again
    nDocs = BackupRestoreUtils.indexDocs(masterClient, "collection1", docsSeed);
    BackupRestoreUtils.verifyDocs(nDocs, masterClient, DEFAULT_TEST_CORENAME);

}

From source file:org.codelibs.elasticsearch.common.lucene.Lucene.java

License:Apache License

/**
 * Reads the segments infos from the given commit, failing if it fails to load
 *//*from   ww  w .j a v  a2 s  .com*/
public static SegmentInfos readSegmentInfos(IndexCommit commit) throws IOException {
    // Using commit.getSegmentsFileName() does NOT work here, have to
    // manually create the segment filename
    String filename = IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS, "",
            commit.getGeneration());
    return SegmentInfos.readCommit(commit.getDirectory(), filename);
}

From source file:org.codelibs.elasticsearch.common.lucene.Lucene.java

License:Apache License

/**
 * This method removes all files from the given directory that are not referenced by the given segments file.
 * This method will open an IndexWriter and relies on index file deleter to remove all unreferenced files. Segment files
 * that are newer than the given segments file are removed forcefully to prevent problems with IndexWriter opening a potentially
 * broken commit point / leftover./* www .  j  av a  2  s .c  om*/
 * <b>Note:</b> this method will fail if there is another IndexWriter open on the given directory. This method will also acquire
 * a write lock from the directory while pruning unused files. This method expects an existing index in the given directory that has
 * the given segments file.
 */
public static SegmentInfos pruneUnreferencedFiles(String segmentsFileName, Directory directory)
        throws IOException {
    final SegmentInfos si = readSegmentInfos(segmentsFileName, directory);
    try (Lock writeLock = directory.obtainLock(IndexWriter.WRITE_LOCK_NAME)) {
        int foundSegmentFiles = 0;
        for (final String file : directory.listAll()) {
            /**
             * we could also use a deletion policy here but in the case of snapshot and restore
             * sometimes we restore an index and override files that were referenced by a "future"
             * commit. If such a commit is opened by the IW it would likely throw a corrupted index exception
             * since checksums don's match anymore. that's why we prune the name here directly.
             * We also want the caller to know if we were not able to remove a segments_N file.
             */
            if (file.startsWith(IndexFileNames.SEGMENTS) || file.equals(IndexFileNames.OLD_SEGMENTS_GEN)) {
                foundSegmentFiles++;
                if (file.equals(si.getSegmentsFileName()) == false) {
                    directory.deleteFile(file); // remove all segment_N files except of the one we wanna keep
                }
            }
        }
        assert SegmentInfos.getLastCommitSegmentsFileName(directory).equals(segmentsFileName);
        if (foundSegmentFiles == 0) {
            throw new IllegalStateException("no commit found in the directory");
        }
    }
    final CommitPoint cp = new CommitPoint(si, directory);
    try (IndexWriter writer = new IndexWriter(directory,
            new IndexWriterConfig(Lucene.STANDARD_ANALYZER).setIndexCommit(cp).setCommitOnClose(false)
                    .setMergePolicy(NoMergePolicy.INSTANCE).setOpenMode(IndexWriterConfig.OpenMode.APPEND))) {
        // do nothing and close this will kick of IndexFileDeleter which will remove all pending files
    }
    return si;
}

From source file:org.codelibs.elasticsearch.common.lucene.Lucene.java

License:Apache License

/**
 * This method removes all lucene files from the given directory. It will first try to delete all commit points / segments
 * files to ensure broken commits or corrupted indices will not be opened in the future. If any of the segment files can't be deleted
 * this operation fails.//  ww w. j a v a2s.c om
 */
public static void cleanLuceneIndex(Directory directory) throws IOException {
    try (Lock writeLock = directory.obtainLock(IndexWriter.WRITE_LOCK_NAME)) {
        for (final String file : directory.listAll()) {
            if (file.startsWith(IndexFileNames.SEGMENTS) || file.equals(IndexFileNames.OLD_SEGMENTS_GEN)) {
                directory.deleteFile(file); // remove all segment_N files
            }
        }
    }
    try (IndexWriter writer = new IndexWriter(directory,
            new IndexWriterConfig(Lucene.STANDARD_ANALYZER).setMergePolicy(NoMergePolicy.INSTANCE) // no merges
                    .setCommitOnClose(false) // no commits
                    .setOpenMode(IndexWriterConfig.OpenMode.CREATE))) // force creation - don't append...
    {
        // do nothing and close this will kick of IndexFileDeleter which will remove all pending files
    }
}

From source file:org.neo4j.kernel.api.impl.labelscan.LuceneLabelScanStoreTest.java

License:Open Source License

@Test
public void snapshotReadOnlyLabelScanStore() throws IOException {
    prepareIndex();/*from   w  w  w.ja va2 s. c  o m*/
    startReadOnlyLabelScanStore();
    try (ResourceIterator<File> indexFiles = store.snapshotStoreFiles()) {
        List<String> filesNames = indexFiles.stream().map(File::getName).collect(toList());
        assertThat("Should have at least index segment file.", filesNames,
                hasItem(startsWith(IndexFileNames.SEGMENTS)));
    }
}