本文整理汇总了Java中org.apache.lucene.store.Lock类的典型用法代码示例。如果您正苦于以下问题:Java Lock类的具体用法?Java Lock怎么用?Java Lock使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Lock类属于org.apache.lucene.store包,在下文中一共展示了Lock类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testHasLocks
import org.apache.lucene.store.Lock; //导入依赖的package包/类
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());
}
示例2: acquireFSLockForPaths
import org.apache.lucene.store.Lock; //导入依赖的package包/类
/**
* 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);
}
}
示例3: snapshotStoreMetadata
import org.apache.lucene.store.Lock; //导入依赖的package包/类
/**
* gets a {@link Store.MetadataSnapshot} for the current directory. This method is safe to call in all lifecycle of the index shard,
* without having to worry about the current state of the engine and concurrent flushes.
*
* @throws org.apache.lucene.index.IndexNotFoundException if no index is found in the current directory
* @throws CorruptIndexException if the lucene index is corrupted. This can be caused by a checksum mismatch or an
* unexpected exception when opening the index reading the segments file.
* @throws IndexFormatTooOldException if the lucene index is too old to be opened.
* @throws IndexFormatTooNewException if the lucene index is too new to be opened.
* @throws FileNotFoundException if one or more files referenced by a commit are not present.
* @throws NoSuchFileException if one or more files referenced by a commit are not present.
*/
public Store.MetadataSnapshot snapshotStoreMetadata() throws IOException {
IndexCommit indexCommit = null;
store.incRef();
try {
synchronized (mutex) {
// if the engine is not running, we can access the store directly, but we need to make sure no one starts
// the engine on us. If the engine is running, we can get a snapshot via the deletion policy which is initialized.
// That can be done out of mutex, since the engine can be closed half way.
Engine engine = getEngineOrNull();
if (engine == null) {
try (Lock ignored = store.directory().obtainLock(IndexWriter.WRITE_LOCK_NAME)) {
return store.getMetadata(null);
}
}
}
indexCommit = deletionPolicy.snapshot();
return store.getMetadata(indexCommit);
} finally {
store.decRef();
if (indexCommit != null) {
deletionPolicy.release(indexCommit);
}
}
}
示例4: cleanLuceneIndex
import org.apache.lucene.store.Lock; //导入依赖的package包/类
/**
* 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)
.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
}
}
示例5: acquireWriteLocks
import org.apache.lucene.store.Lock; //导入依赖的package包/类
/** Acquires write locks on all the directories; be sure
* to match with a call to {@link IOUtils#close} in a
* finally clause. */
private List<Lock> acquireWriteLocks(Directory... dirs) throws IOException {
List<Lock> locks = new ArrayList<>();
for(int i=0;i<dirs.length;i++) {
boolean success = false;
try {
Lock lock = dirs[i].makeLock(WRITE_LOCK_NAME);
locks.add(lock);
lock.obtain(config.getWriteLockTimeout());
success = true;
} finally {
if (success == false) {
// Release all previously acquired locks:
IOUtils.closeWhileHandlingException(locks);
}
}
}
return locks;
}
示例6: acquireFSLockForPaths
import org.apache.lucene.store.Lock; //导入依赖的package包/类
/**
* 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);
}
}
示例7: makeLock
import org.apache.lucene.store.Lock; //导入依赖的package包/类
@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 "[email protected]" + new Path(directory, name);
}
};
}
示例8: makeLock
import org.apache.lucene.store.Lock; //导入依赖的package包/类
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 "[email protected]" + new Path(directory, name);
}
};
}
示例9: validateCreateSnapshot
import org.apache.lucene.store.Lock; //导入依赖的package包/类
void validateCreateSnapshot() throws IOException {
Lock lock = lockFactory.makeLock(directoryName + ".lock");
snapShotDir = new File(snapDir, directoryName);
if (lock.isLocked()) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
"Unable to acquire lock for snapshot directory: " + snapShotDir.getAbsolutePath());
}
if (snapShotDir.exists()) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
"Snapshot directory already exists: " + snapShotDir.getAbsolutePath());
}
if (!snapShotDir.mkdirs()) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
"Unable to create snapshot directory: " + snapShotDir.getAbsolutePath());
}
}
示例10: testBasic
import org.apache.lucene.store.Lock; //导入依赖的package包/类
@Test
public void testBasic() throws IOException {
URI uri = dfsCluster.getURI();
Path lockPath = new Path(uri.toString(), "/basedir/lock");
HdfsLockFactory lockFactory = new HdfsLockFactory(lockPath, new Configuration());
Lock lock = lockFactory.makeLock("testlock");
boolean success = lock.obtain();
assertTrue("We could not get the lock when it should be available", success);
success = lock.obtain();
assertFalse("We got the lock but it should be unavailble", success);
lock.close();
success = lock.obtain();
assertTrue("We could not get the lock when it should be available", success);
success = lock.obtain();
assertFalse("We got the lock but it should be unavailble", success);
}
示例11: IndexWriter
import org.apache.lucene.store.Lock; //导入依赖的package包/类
/** 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();
}
}
示例12: IndexWriter
import org.apache.lucene.store.Lock; //导入依赖的package包/类
/** 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();
}
}
示例13: IndexWriter
import org.apache.lucene.store.Lock; //导入依赖的package包/类
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();
}
}
示例14: acquireWriteLocks
import org.apache.lucene.store.Lock; //导入依赖的package包/类
/** Acquires write locks on all the directories; be sure
* to match with a call to {@link IOUtils#close} in a
* finally clause. */
private List<Lock> acquireWriteLocks(Directory... dirs) throws IOException {
List<Lock> locks = new ArrayList<Lock>();
for(int i=0;i<dirs.length;i++) {
boolean success = false;
try {
Lock lock = dirs[i].makeLock(WRITE_LOCK_NAME);
locks.add(lock);
lock.obtain(config.getWriteLockTimeout());
success = true;
} finally {
if (success == false) {
// Release all previously acquired locks:
IOUtils.closeWhileHandlingException(locks);
}
}
}
return locks;
}
示例15: makeLock
import org.apache.lucene.store.Lock; //导入依赖的package包/类
@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;
}
}