org.apache.lucene.store.Lock Java Examples
The following examples show how to use
org.apache.lucene.store.Lock.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: left_IndexWriter_1.41.java From gumtree-spoon-ast-diff with Apache License 2.0 | 6 votes |
private IndexWriter(Directory d, Analyzer a, final boolean create, boolean closeDir) throws IOException { this.closeDir = closeDir; directory = d; analyzer = a; Lock writeLock = directory.makeLock(IndexWriter.WRITE_LOCK_NAME); if (!writeLock.obtain(WRITE_LOCK_TIMEOUT)) // obtain write lock throw new IOException("Index locked for write: " + writeLock); this.writeLock = writeLock; // save it synchronized (directory) { // in- & inter-process sync new Lock.With(directory.makeLock(IndexWriter.COMMIT_LOCK_NAME), COMMIT_LOCK_TIMEOUT) { public Object doBody() throws IOException { if (create) segmentInfos.write(directory); else segmentInfos.read(directory); return null; } }.run(); } }
Example #2
Source File: right_IndexWriter_1.3.java From gumtree-spoon-ast-diff with Apache License 2.0 | 6 votes |
/** Constructs an IndexWriter for the index in <code>d</code>. Text will be analyzed with <code>a</code>. If <code>create</code> is true, then a new, empty index will be created in <code>d</code>, replacing the index already there, if any. */ public IndexWriter(Directory d, Analyzer a, final boolean create) throws IOException { directory = d; analyzer = a; Lock writeLock = directory.makeLock("write.lock"); if (!writeLock.obtain()) // obtain write lock throw new IOException("Index locked for write: " + writeLock); synchronized (directory) { // in- & inter-process sync new Lock.With(directory.makeLock("commit.lock")) { public Object doBody() throws IOException { if (create) segmentInfos.write(directory); else segmentInfos.read(directory); return null; } }.run(); } }
Example #3
Source File: left_IndexWriter_1.2.java From gumtree-spoon-ast-diff with Apache License 2.0 | 6 votes |
/** Constructs an IndexWriter for the index in <code>d</code>. Text will be analyzed with <code>a</code>. If <code>create</code> is true, then a new, empty index will be created in <code>d</code>, replacing the index already there, if any. */ public IndexWriter(Directory d, Analyzer a, final boolean create) throws IOException { directory = d; analyzer = a; Lock writeLock = directory.makeLock("write.lock"); if (!writeLock.obtain()) // obtain write lock throw new IOException("Index locked for write: " + writeLock); synchronized (directory) { // in- & inter-process sync new Lock.With(directory.makeLock("commit.lock")) { public Object doBody() throws IOException { if (create) segmentInfos.write(directory); else segmentInfos.read(directory); return null; } }.run(); } }
Example #4
Source File: right_IndexWriter_1.22.java From gumtree-spoon-ast-diff with Apache License 2.0 | 6 votes |
/** Constructs an IndexWriter for the index in <code>d</code>. Text will be analyzed with <code>a</code>. If <code>create</code> is true, then a new, empty index will be created in <code>d</code>, replacing the index already there, if any. */ public IndexWriter(Directory d, Analyzer a, final boolean create) throws IOException { directory = d; analyzer = a; Lock writeLock = directory.makeLock(IndexWriter.WRITE_LOCK_NAME); if (!writeLock.obtain(WRITE_LOCK_TIMEOUT)) // obtain write lock throw new IOException("Index locked for write: " + writeLock); this.writeLock = writeLock; // save it synchronized (directory) { // in- & inter-process sync new Lock.With(directory.makeLock(IndexWriter.COMMIT_LOCK_NAME), COMMIT_LOCK_TIMEOUT) { public Object doBody() throws IOException { if (create) segmentInfos.write(directory); else segmentInfos.read(directory); return null; } }.run(); } }
Example #5
Source File: left_IndexWriter_1.21.java From gumtree-spoon-ast-diff with Apache License 2.0 | 6 votes |
/** Constructs an IndexWriter for the index in <code>d</code>. Text will be analyzed with <code>a</code>. If <code>create</code> is true, then a new, empty index will be created in <code>d</code>, replacing the index already there, if any. */ public IndexWriter(Directory d, Analyzer a, final boolean create) throws IOException { directory = d; analyzer = a; Lock writeLock = directory.makeLock(IndexWriter.WRITE_LOCK_NAME); if (!writeLock.obtain(WRITE_LOCK_TIMEOUT)) // obtain write lock throw new IOException("Index locked for write: " + writeLock); this.writeLock = writeLock; // save it synchronized (directory) { // in- & inter-process sync new Lock.With(directory.makeLock(IndexWriter.COMMIT_LOCK_NAME), COMMIT_LOCK_TIMEOUT) { public Object doBody() throws IOException { if (create) segmentInfos.write(directory); else segmentInfos.read(directory); return null; } }.run(); } }
Example #6
Source File: FileSystemDirectory.java From linden with Apache License 2.0 | 6 votes |
@Override public Lock makeLock(final String name) { return new Lock() { @Override public boolean obtain() { return true; } @Override public void close() throws IOException { } @Override public boolean isLocked() { throw new UnsupportedOperationException(); } @Override public String toString() { return "Lock@" + new Path(directory, name); } }; }
Example #7
Source File: right_IndexWriter_1.42.java From gumtree-spoon-ast-diff with Apache License 2.0 | 6 votes |
private IndexWriter(Directory d, Analyzer a, final boolean create, boolean closeDir) throws IOException { this.closeDir = closeDir; directory = d; analyzer = a; Lock writeLock = directory.makeLock(IndexWriter.WRITE_LOCK_NAME); if (!writeLock.obtain(WRITE_LOCK_TIMEOUT)) // obtain write lock throw new IOException("Index locked for write: " + writeLock); this.writeLock = writeLock; // save it synchronized (directory) { // in- & inter-process sync new Lock.With(directory.makeLock(IndexWriter.COMMIT_LOCK_NAME), COMMIT_LOCK_TIMEOUT) { public Object doBody() throws IOException { if (create) segmentInfos.write(directory); else segmentInfos.read(directory); return null; } }.run(); } }
Example #8
Source File: NodeEnvironment.java From Elasticsearch with Apache License 2.0 | 6 votes |
/** * Acquires, then releases, all {@code write.lock} files in the given * shard paths. The "write.lock" file is assumed to be under the shard * path's "index" directory as used by Elasticsearch. * * @throws LockObtainFailedException if any of the locks could not be acquired */ public static void acquireFSLockForPaths(Settings indexSettings, Path... shardPaths) throws IOException { Lock[] locks = new Lock[shardPaths.length]; Directory[] dirs = new Directory[shardPaths.length]; try { for (int i = 0; i < shardPaths.length; i++) { // resolve the directory the shard actually lives in Path p = shardPaths[i].resolve("index"); // open a directory (will be immediately closed) on the shard's location dirs[i] = new SimpleFSDirectory(p, FsDirectoryService.buildLockFactory(indexSettings)); // create a lock for the "write.lock" file try { locks[i] = dirs[i].obtainLock(IndexWriter.WRITE_LOCK_NAME); } catch (IOException ex) { throw new LockObtainFailedException("unable to acquire " + IndexWriter.WRITE_LOCK_NAME + " for " + p); } } } finally { IOUtils.closeWhileHandlingException(locks); IOUtils.closeWhileHandlingException(dirs); } }
Example #9
Source File: FileSystemDirectory.java From RDFS with Apache License 2.0 | 6 votes |
public Lock makeLock(final String name) { return new Lock() { public boolean obtain() { return true; } public void release() { } public boolean isLocked() { throw new UnsupportedOperationException(); } public String toString() { return "Lock@" + new Path(directory, name); } }; }
Example #10
Source File: FileSystemDirectory.java From hadoop-gpu with Apache License 2.0 | 6 votes |
public Lock makeLock(final String name) { return new Lock() { public boolean obtain() { return true; } public void release() { } public boolean isLocked() { throw new UnsupportedOperationException(); } public String toString() { return "Lock@" + new Path(directory, name); } }; }
Example #11
Source File: NodeEnvironment.java From crate with Apache License 2.0 | 6 votes |
/** * Acquires, then releases, all {@code write.lock} files in the given * shard paths. The "write.lock" file is assumed to be under the shard * path's "index" directory as used by Elasticsearch. * * @throws LockObtainFailedException if any of the locks could not be acquired */ public static void acquireFSLockForPaths(IndexSettings indexSettings, Path... shardPaths) throws IOException { Lock[] locks = new Lock[shardPaths.length]; Directory[] dirs = new Directory[shardPaths.length]; try { for (int i = 0; i < shardPaths.length; i++) { // resolve the directory the shard actually lives in Path p = shardPaths[i].resolve("index"); // open a directory (will be immediately closed) on the shard's location dirs[i] = new SimpleFSDirectory(p, indexSettings.getValue(FsDirectoryService.INDEX_LOCK_FACTOR_SETTING)); // create a lock for the "write.lock" file try { locks[i] = dirs[i].obtainLock(IndexWriter.WRITE_LOCK_NAME); } catch (IOException ex) { throw new LockObtainFailedException("unable to acquire " + IndexWriter.WRITE_LOCK_NAME + " for " + p, ex); } } } finally { IOUtils.closeWhileHandlingException(locks); IOUtils.closeWhileHandlingException(dirs); } }
Example #12
Source File: RecordOwnerLockFactoryTest.java From netbeans with Apache License 2.0 | 6 votes |
public void testHasLocks() throws IOException { assertFalse(lockFactory.hasLocks()); final Lock lock1 = lockFactory.makeLock("test1"); //NOI18N final Lock lock2 = lockFactory.makeLock("test2"); //NOI18N final Lock lock3 = lockFactory.makeLock("test3"); //NOI18N final Lock lock4 = lockFactory.makeLock("test4"); //NOI18N assertFalse(lockFactory.hasLocks()); assertTrue(lock2.obtain()); assertTrue(lockFactory.hasLocks()); lock2.release(); assertFalse(lockFactory.hasLocks()); assertTrue(lock3.obtain()); assertTrue(lockFactory.hasLocks()); assertTrue(lock4.obtain()); assertTrue(lockFactory.hasLocks()); lockFactory.clearLock("test3"); //NOI18N assertTrue(lockFactory.hasLocks()); assertTrue(lock2.obtain()); lockFactory.clearLock("test4"); //NOI18N assertTrue(lockFactory.hasLocks()); lock2.release();; assertFalse(lockFactory.hasLocks()); }
Example #13
Source File: Lucene.java From crate with Apache License 2.0 | 6 votes |
/** * 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. */ 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) .setSoftDeletesField(Lucene.SOFT_DELETES_FIELD) .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 } }
Example #14
Source File: RecordOwnerLockFactory.java From netbeans with Apache License 2.0 | 5 votes |
Collection<? extends Lock> forceClearLocks() { synchronized (locks) { final Queue<RecordOwnerLock> locked = new ArrayDeque<>(); for (Iterator<RecordOwnerLock> it = locks.values().iterator(); it.hasNext();) { RecordOwnerLock lock = it.next(); if (lock.isLocked()) { it.remove(); locked.offer(lock); } } return locked; } }
Example #15
Source File: left_IndexWriter_1.21.java From gumtree-spoon-ast-diff with Apache License 2.0 | 5 votes |
/** Merges the provided indexes into this index. * <p>After this completes, the index is optimized. */ public synchronized void addIndexes(IndexReader[] readers) throws IOException { optimize(); // start with zero or 1 seg String mergedName = newSegmentName(); SegmentMerger merger = new SegmentMerger(directory, mergedName, false); if (segmentInfos.size() == 1) // add existing index, if any merger.add(new SegmentReader(segmentInfos.info(0))); for (int i = 0; i < readers.length; i++) // add new indexes merger.add(readers[i]); int docCount = merger.merge(); // merge 'em segmentInfos.setSize(0); // pop old infos & add new segmentInfos.addElement(new SegmentInfo(mergedName, docCount, directory)); synchronized (directory) { // in- & inter-process sync new Lock.With(directory.makeLock("commit.lock")) { public Object doBody() throws IOException { segmentInfos.write(directory); // commit changes return null; } }.run(); } }
Example #16
Source File: NodeEnvironment.java From crate with Apache License 2.0 | 5 votes |
@Override public void close() { if (closed.compareAndSet(false, true) && locks != null) { for (Lock lock : locks) { try { logger.trace("releasing lock [{}]", lock); lock.close(); } catch (IOException e) { logger.trace(() -> new ParameterizedMessage("failed to release lock [{}]", lock), e); } } } }
Example #17
Source File: right_IndexWriter_1.22.java From gumtree-spoon-ast-diff with Apache License 2.0 | 5 votes |
/** Merges the provided indexes into this index. * <p>After this completes, the index is optimized. */ public synchronized void addIndexes(IndexReader[] readers) throws IOException { optimize(); // start with zero or 1 seg String mergedName = newSegmentName(); SegmentMerger merger = new SegmentMerger(directory, mergedName, false); if (segmentInfos.size() == 1) // add existing index, if any merger.add(new SegmentReader(segmentInfos.info(0))); for (int i = 0; i < readers.length; i++) // add new indexes merger.add(readers[i]); int docCount = merger.merge(); // merge 'em segmentInfos.setSize(0); // pop old infos & add new segmentInfos.addElement(new SegmentInfo(mergedName, docCount, directory)); synchronized (directory) { // in- & inter-process sync new Lock.With(directory.makeLock("commit.lock"), COMMIT_LOCK_TIMEOUT) { public Object doBody() throws IOException { segmentInfos.write(directory); // commit changes return null; } }.run(); } }
Example #18
Source File: NodeEnvironment.java From crate with Apache License 2.0 | 5 votes |
private void assertEnvIsLocked() { if (!closed.get() && locks != null) { for (Lock lock : locks) { try { lock.ensureValid(); } catch (IOException e) { logger.warn("lock assertion failed", e); throw new IllegalStateException("environment is not locked", e); } } } }
Example #19
Source File: left_IndexWriter_1.2.java From gumtree-spoon-ast-diff with Apache License 2.0 | 5 votes |
/** Pops segments off of segmentInfos stack down to minSegment, merges them, and pushes the merged index onto the top of the segmentInfos stack. */ private final void mergeSegments(int minSegment, boolean delete) throws IOException { String mergedName = newSegmentName(); int mergedDocCount = 0; if (infoStream != null) infoStream.print("merging segments"); SegmentMerger merger = new SegmentMerger(directory, mergedName); final Vector segmentsToDelete = new Vector(); for (int i = minSegment; i < segmentInfos.size(); i++) { SegmentInfo si = segmentInfos.info(i); if (infoStream != null) infoStream.print(" " + si.name + " (" + si.docCount + " docs)"); SegmentReader reader = new SegmentReader(si); merger.add(reader); if (delete) segmentsToDelete.addElement(reader); // queue for deletion mergedDocCount += si.docCount; } if (infoStream != null) { infoStream.println(); infoStream.println(" into "+mergedName+" ("+mergedDocCount+" docs)"); } merger.merge(); segmentInfos.setSize(minSegment); // pop old infos & add new segmentInfos.addElement(new SegmentInfo(mergedName, mergedDocCount, directory)); synchronized (directory) { // in- & inter-process sync new Lock.With(directory.makeLock("commit.lock")) { public Object doBody() throws IOException { segmentInfos.write(directory); // commit before deleting deleteSegments(segmentsToDelete); // delete now-unused segments return null; } }.run(); } }
Example #20
Source File: Store.java From crate with Apache License 2.0 | 5 votes |
/** * This method deletes every file in this store that is not contained in the given source meta data or is a * legacy checksum file. After the delete it pulls the latest metadata snapshot from the store and compares it * to the given snapshot. If the snapshots are inconsistent an illegal state exception is thrown. * * @param reason the reason for this cleanup operation logged for each deleted file * @param sourceMetaData the metadata used for cleanup. all files in this metadata should be kept around. * @throws IOException if an IOException occurs * @throws IllegalStateException if the latest snapshot in this store differs from the given one after the cleanup. */ public void cleanupAndVerify(String reason, MetadataSnapshot sourceMetaData) throws IOException { metadataLock.writeLock().lock(); try (Lock writeLock = directory.obtainLock(IndexWriter.WRITE_LOCK_NAME)) { for (String existingFile : directory.listAll()) { if (Store.isAutogenerated(existingFile) || sourceMetaData.contains(existingFile)) { continue; // don't delete snapshot file, or the checksums file (note, this is extra protection since the Store won't delete checksum) } try { directory.deleteFile(reason, existingFile); // FNF should not happen since we hold a write lock? } catch (IOException ex) { if (existingFile.startsWith(IndexFileNames.SEGMENTS) || existingFile.equals(IndexFileNames.OLD_SEGMENTS_GEN) || existingFile.startsWith(CORRUPTED)) { // TODO do we need to also fail this if we can't delete the pending commit file? // if one of those files can't be deleted we better fail the cleanup otherwise we might leave an old commit point around? throw new IllegalStateException("Can't delete " + existingFile + " - cleanup failed", ex); } logger.debug(() -> new ParameterizedMessage("failed to delete file [{}]", existingFile), ex); // ignore, we don't really care, will get deleted later on } } directory.syncMetaData(); final Store.MetadataSnapshot metadataOrEmpty = getMetadata(null); verifyAfterCleanup(sourceMetaData, metadataOrEmpty); } finally { metadataLock.writeLock().unlock(); } }
Example #21
Source File: right_IndexWriter_1.3.java From gumtree-spoon-ast-diff with Apache License 2.0 | 5 votes |
/** Pops segments off of segmentInfos stack down to minSegment, merges them, and pushes the merged index onto the top of the segmentInfos stack. */ private final void mergeSegments(int minSegment, boolean delete) throws IOException { String mergedName = newSegmentName(); int mergedDocCount = 0; if (infoStream != null) infoStream.print("merging segments"); SegmentMerger merger = new SegmentMerger(directory, mergedName); final Vector segmentsToDelete = new Vector(); for (int i = minSegment; i < segmentInfos.size(); i++) { SegmentInfo si = segmentInfos.info(i); if (infoStream != null) infoStream.print(" " + si.name + " (" + si.docCount + " docs)"); SegmentReader reader = new SegmentReader(si); merger.add(reader); if (delete) segmentsToDelete.addElement(reader); // queue for deletion mergedDocCount += si.docCount; } if (infoStream != null) { infoStream.println(); infoStream.println(" into "+mergedName+" ("+mergedDocCount+" docs)"); } merger.merge(); segmentInfos.setSize(minSegment); // pop old infos & add new segmentInfos.addElement(new SegmentInfo(mergedName, mergedDocCount, directory)); synchronized (directory) { // in- & inter-process sync new Lock.With(directory.makeLock("commit.lock")) { public Object doBody() throws IOException { segmentInfos.write(directory); // commit before deleting deleteSegments(segmentsToDelete); // delete now-unused segments return null; } }.run(); } }
Example #22
Source File: NodeEnvironment.java From Elasticsearch with Apache License 2.0 | 5 votes |
private boolean assertEnvIsLocked() { if (!closed.get() && locks != null) { for (Lock lock : locks) { try { lock.ensureValid(); } catch (IOException e) { logger.warn("lock assertion failed", e); return false; } } } return true; }
Example #23
Source File: RecordOwnerLockFactory.java From netbeans with Apache License 2.0 | 5 votes |
@Override public Lock makeLock(String lockName) { synchronized (locks) { RecordOwnerLock res = locks.get(lockName); if (res == null) { res = new RecordOwnerLock(); locks.put(lockName, res); } return res; } }
Example #24
Source File: RecordOwnerLockFactoryTest.java From netbeans with Apache License 2.0 | 5 votes |
public void testLock() throws IOException { final Lock lock = lockFactory.makeLock("test"); //NOI18N assertFalse(lock.isLocked()); lock.obtain(); assertTrue(lock.isLocked()); lock.release(); assertFalse(lock.isLocked()); }
Example #25
Source File: RecordOwnerLockFactoryTest.java From netbeans with Apache License 2.0 | 5 votes |
public void testLockInstances() throws IOException { final Lock lock1 = lockFactory.makeLock("test"); //NOI18N final Lock lock2 = lockFactory.makeLock("test"); //NOI18N assertFalse(lock1.isLocked()); assertFalse(lock2.isLocked()); lock1.obtain(); assertTrue(lock1.isLocked()); assertTrue(lock2.isLocked()); lock2.release(); assertFalse(lock1.isLocked()); assertFalse(lock2.isLocked()); }
Example #26
Source File: RecordOwnerLockFactoryTest.java From netbeans with Apache License 2.0 | 5 votes |
public void testClearLock() throws IOException { Lock lock = lockFactory.makeLock("test"); //NOI18N assertFalse(lock.isLocked()); lock.obtain(); assertTrue(lock.isLocked()); lockFactory.clearLock("test"); //NOI18N assertFalse(lock.isLocked()); }
Example #27
Source File: RecordOwnerLockFactoryTest.java From netbeans with Apache License 2.0 | 5 votes |
public void testForceClearLocks() throws IOException { final Lock lock1 = lockFactory.makeLock("test1"); //NOI18N assertTrue(lock1.obtain()); assertTrue(lockFactory.hasLocks()); lockFactory.makeLock("test2"); //NOI18N assertTrue(lockFactory.makeLock("test3").obtain()); //NOI18N lockFactory.makeLock("test3").release(); //NOI18N assertTrue(lockFactory.hasLocks()); Collection<? extends Lock> locks = lockFactory.forceClearLocks(); assertEquals(1, locks.size()); assertEquals(lock1, locks.iterator().next()); assertFalse(lockFactory.hasLocks()); }
Example #28
Source File: NodeEnvironment.java From Elasticsearch with Apache License 2.0 | 5 votes |
private static void releaseAndNullLocks(Lock[] locks) { for (int i = 0; i < locks.length; i++) { if (locks[i] != null) { IOUtils.closeWhileHandlingException(locks[i]); } locks[i] = null; } }
Example #29
Source File: NodeEnvironment.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public void close() { if (closed.compareAndSet(false, true) && locks != null) { for (Lock lock : locks) { try { logger.trace("releasing lock [{}]", lock); lock.close(); } catch (IOException e) { logger.trace("failed to release lock [{}]", e, lock); } } } }
Example #30
Source File: FencedDirectory.java From incubator-retired-blur with Apache License 2.0 | 5 votes |
public Lock makeLock(String name) { if (name.equals(WRITE_LOCK)) { if (_writeLock == null) { return _writeLock = _directory.makeLock(name); } return _writeLock; } else { throw new RuntimeException("Locks with name [" + name + "] not supported."); } }