Example usage for org.apache.lucene.store Directory obtainLock

List of usage examples for org.apache.lucene.store Directory obtainLock

Introduction

In this page you can find the example usage for org.apache.lucene.store Directory obtainLock.

Prototype

public abstract Lock obtainLock(String name) throws IOException;

Source Link

Document

Acquires and returns a Lock for a file with the given name.

Usage

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./*from  w  ww .  j av a  2s.  c  o  m*/
 * <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.//from  ww w  . j ava 2  s  .c o  m
 */
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.hibernate.search.test.indexmanager.DirectoryBasedIndexManagerTest.java

License:LGPL

private boolean isIndexWriterLocked(IndexManager indexManager) {
    Directory directory = ((DirectoryBasedIndexManager) indexManager).getDirectoryProvider().getDirectory();
    Lock lock = null;/*from  www.jav  a 2s . com*/
    try {
        lock = directory.obtainLock(IndexWriter.WRITE_LOCK_NAME);
    } catch (IOException e) {
        return true;
    } finally {
        if (lock != null) {
            try {
                lock.close();
            } catch (Exception ignored) {
            }
        }

    }
    return false;
}

From source file:org.hibernate.search.testsupport.backend.LuceneBackendTestHelpers.java

License:LGPL

public static boolean isLocked(Directory directory) throws IOException {
    try {//from  w  w w. j av a 2 s .co m
        directory.obtainLock(org.apache.lucene.index.IndexWriter.WRITE_LOCK_NAME).close();
        return false;
    } catch (LockObtainFailedException failed) {
        return true;
    }
}