org.apache.lucene.store.Lock.obtain()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(230)

本文整理了Java中org.apache.lucene.store.Lock.obtain()方法的一些代码示例,展示了Lock.obtain()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Lock.obtain()方法的具体详情如下:
包路径:org.apache.lucene.store.Lock
类名称:Lock
方法名:obtain

Lock.obtain介绍

[英]Attempts to obtain an exclusive lock within amount of time given. Polls once per #LOCK_POLL_INTERVAL(currently 1000) milliseconds until lockWaitTimeout is passed.
[中]试图在给定的时间内获得独占锁。在通过lockWaitTimeout之前,每#LOCK_POLL_间隔(当前为1000)毫秒轮询一次。

代码示例

代码示例来源:origin: org.apache.lucene/com.springsource.org.apache.lucene

public synchronized boolean obtain()
 throws LockObtainFailedException, IOException {
 return lock.obtain();
}

代码示例来源:origin: org.apache.lucene/lucene-core-jfrog

public synchronized boolean obtain()
 throws LockObtainFailedException, IOException {
 return lock.obtain();
}

代码示例来源:origin: gncloud/fastcatsearch

@Override
public synchronized boolean obtain() throws IOException {
 return lock.obtain();
}

代码示例来源:origin: org.apache.lucene/lucene-core-jfrog

public synchronized boolean obtain(long lockWaitTimeout)
 throws LockObtainFailedException, IOException {
 boolean obtained = lock.obtain(lockWaitTimeout);
 if (obtained)
  verify((byte) 1);
 return obtained;
}

代码示例来源:origin: org.compass-project/compass

@Override
public boolean obtain(long lockWaitTimeout) throws LockObtainFailedException, IOException {
  boolean unlocked;
  try {
    rwl.readLock().unlock();
    unlocked = true;
  } catch (IllegalMonitorStateException e) {
    unlocked = false;
  }
  try {
    return lock.obtain(lockWaitTimeout);
  } finally {
    if (unlocked) {
      rwl.readLock().lock();
    }
  }
}

代码示例来源:origin: org.compass-project/compass

@Override
public boolean obtain() throws IOException {
  boolean unlocked;
  try {
    rwl.readLock().unlock();
    unlocked = true;
  } catch (IllegalMonitorStateException e) {
    unlocked = false;
  }
  try {
    return lock.obtain();
  } finally {
    if (unlocked) {
      rwl.readLock().lock();
    }
  }
}

代码示例来源:origin: org.apache.lucene/com.springsource.org.apache.lucene

public synchronized boolean obtain(long lockWaitTimeout)
 throws LockObtainFailedException, IOException {
 boolean obtained = lock.obtain(lockWaitTimeout);
 if (obtained)
  verify((byte) 1);
 return obtained;
}

代码示例来源:origin: gncloud/fastcatsearch

@Override
public synchronized boolean obtain(long lockWaitTimeout) throws IOException {
 boolean obtained = lock.obtain(lockWaitTimeout);
 if (obtained)
  verify((byte) 1);
 return obtained;
}

代码示例来源:origin: lucene/lucene

/** Attempts to obtain an exclusive lock within amount
 *  of time given. Currently polls once per second until
 *  lockWaitTimeout is passed.
 * @param lockWaitTimeout length of time to wait in ms
 * @return true if lock was obtained
 * @throws IOException if lock wait times out or obtain() throws an IOException
 */
public boolean obtain(long lockWaitTimeout) throws IOException {
 boolean locked = obtain();
 int maxSleepCount = (int)(lockWaitTimeout / LOCK_POLL_INTERVAL);
 int sleepCount = 0;
 while (!locked) {
  if (++sleepCount == maxSleepCount) {
   throw new IOException("Lock obtain timed out: " + this.toString());
  }
  try {
   Thread.sleep(LOCK_POLL_INTERVAL);
  } catch (InterruptedException e) {
   throw new IOException(e.toString());
  }
  locked = obtain();
 }
 return locked;
}

代码示例来源:origin: org.apache.lucene/lucene-core-jfrog

private void acquireTestLock() throws IOException {
 String randomLockName = "lucene-" + Long.toString(new Random().nextInt(), Character.MAX_RADIX) + "-test.lock";
 
 Lock l = makeLock(randomLockName);
 try {
  l.obtain();
 } catch (IOException e) {
  IOException e2 = new IOException("Failed to acquire random test lock; please verify filesystem for lock directory '" + lockDir + "' supports locking");
  e2.initCause(e);
  throw e2;
 }
 l.release();
}

代码示例来源:origin: org.apache.lucene/com.springsource.org.apache.lucene

private void acquireTestLock() throws IOException {
 String randomLockName = "lucene-" + Long.toString(new Random().nextInt(), Character.MAX_RADIX) + "-test.lock";
 
 Lock l = makeLock(randomLockName);
 try {
  l.obtain();
 } catch (IOException e) {
  IOException e2 = new IOException("Failed to acquire random test lock; please verify filesystem for lock directory '" + lockDir + "' supports locking");
  e2.initCause(e);
  throw e2;
 }
 l.release();
}

代码示例来源:origin: org.apache.lucene/lucene-core-jfrog

boolean locked = obtain();
if (lockWaitTimeout < 0 && lockWaitTimeout != LOCK_OBTAIN_WAIT_FOREVER)
 throw new IllegalArgumentException("lockWaitTimeout should be LOCK_OBTAIN_WAIT_FOREVER or a non-negative number (got " + lockWaitTimeout + ")");
  throw new IOException(e.toString());
 locked = obtain();

代码示例来源:origin: lucene/lucene

/** Calls {@link #doBody} while <i>lock</i> is obtained.  Blocks if lock
  * cannot be obtained immediately.  Retries to obtain lock once per second
  * until it is obtained, or until it has tried ten times. Lock is released when
  * {@link #doBody} exits. */
 public Object run() throws IOException {
  boolean locked = false;
  try {
    locked = lock.obtain(lockWaitTimeout);
    return doBody();
  } finally {
   if (locked)
    lock.release();
  }
 }
}

代码示例来源:origin: org.apache.lucene/lucene-core-jfrog

/** Calls {@link #doBody} while <i>lock</i> is obtained.  Blocks if lock
  * cannot be obtained immediately.  Retries to obtain lock once per second
  * until it is obtained, or until it has tried ten times. Lock is released when
  * {@link #doBody} exits.
  * @throws LockObtainFailedException if lock could not
  * be obtained
  * @throws IOException if {@link Lock#obtain} throws IOException
  */
 public Object run() throws LockObtainFailedException, IOException {
  boolean locked = false;
  try {
    locked = lock.obtain(lockWaitTimeout);
    return doBody();
  } finally {
   if (locked)
    lock.release();
  }
 }
}

代码示例来源:origin: org.apache.lucene/com.springsource.org.apache.lucene

/** Calls {@link #doBody} while <i>lock</i> is obtained.  Blocks if lock
  * cannot be obtained immediately.  Retries to obtain lock once per second
  * until it is obtained, or until it has tried ten times. Lock is released when
  * {@link #doBody} exits.
  * @throws LockObtainFailedException if lock could not
  * be obtained
  * @throws IOException if {@link Lock#obtain} throws IOException
  */
 public Object run() throws LockObtainFailedException, IOException {
  boolean locked = false;
  try {
    locked = lock.obtain(lockWaitTimeout);
    return doBody();
  } finally {
   if (locked)
    lock.release();
  }
 }
}

代码示例来源:origin: gncloud/fastcatsearch

/** Calls {@link #doBody} while <i>lock</i> is obtained.  Blocks if lock
  * cannot be obtained immediately.  Retries to obtain lock once per second
  * until it is obtained, or until it has tried ten times. Lock is released when
  * {@link #doBody} exits.
  * @throws LockObtainFailedException if lock could not
  * be obtained
  * @throws IOException if {@link Lock#obtain} throws IOException
  */
 public Object run() throws IOException {
  boolean locked = false;
  try {
    locked = lock.obtain(lockWaitTimeout);
    return doBody();
  } finally {
   if (locked)
    lock.release();
  }
 }
}

代码示例来源:origin: lucene/lucene

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();
  }
}

代码示例来源:origin: lucene/lucene

/**
 * Trys to acquire the WriteLock on this directory.
 * this method is only valid if this IndexReader is directory owner.
 * 
 * @throws IOException If WriteLock cannot be acquired.
 */
private void aquireWriteLock() throws IOException {
 if (stale)
  throw new IOException("IndexReader out of date and no longer valid for delete, undelete, or setNorm operations");
 if (writeLock == null) {
  Lock writeLock = directory.makeLock(IndexWriter.WRITE_LOCK_NAME);
  if (!writeLock.obtain(IndexWriter.WRITE_LOCK_TIMEOUT)) // obtain write lock
   throw new IOException("Index locked for write: " + writeLock);
  this.writeLock = writeLock;
  // we have to check whether index has changed since this reader was opened.
  // if so, this reader is no longer valid for deletion
  if (SegmentInfos.readCurrentVersion(directory) > segmentInfos.getVersion()) {
   stale = true;
   this.writeLock.release();
   this.writeLock = null;
   throw new IOException("IndexReader out of date and no longer valid for delete, undelete, or setNorm operations");
  }
 }
}

代码示例来源:origin: org.compass-project/compass

public Object doInTransaction() throws CompassException {
    for (int i = 0; i < finalSubIndexes.length; i++) {
      Directory dir = getDirectory(finalSubIndexes[i]);
      writerLocks[i] = dir.makeLock(IndexWriter.WRITE_LOCK_NAME);
      try {
        writerLocks[i].obtain(luceneSettings.getTransactionLockTimout());
      } catch (IOException e) {
        throw new SearchEngineException("Failed to retrieve transaction locks", e);
      }
    }
    return null;
  }
});

代码示例来源:origin: org.compass-project/compass

private void obtainOrderLockIfNeeded(String subIndex) throws SearchEngineException {
  if (!maintainOrder) {
    return;
  }
  Lock lock = orderLocks.get(subIndex);
  if (lock == null) {
    lock = searchEngine.getSearchEngineFactory().getLuceneIndexManager().getStore().openDirectory(subIndex).makeLock("order.lock");
    try {
      lock.obtain(searchEngine.getSearchEngineFactory().getLuceneSettings().getTransactionLockTimout());
    } catch (IOException e) {
      clearLocksIfNeeded();
      throw new SearchEngineException("Failed to obtain order lock on sub index [" + subIndex + "]", e);
    }
    orderLocks.put(subIndex, lock);
  }
}

相关文章