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

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

本文整理了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

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

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

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

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

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

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

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

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

  1. @Override
  2. public boolean obtain(long lockWaitTimeout) throws LockObtainFailedException, IOException {
  3. boolean unlocked;
  4. try {
  5. rwl.readLock().unlock();
  6. unlocked = true;
  7. } catch (IllegalMonitorStateException e) {
  8. unlocked = false;
  9. }
  10. try {
  11. return lock.obtain(lockWaitTimeout);
  12. } finally {
  13. if (unlocked) {
  14. rwl.readLock().lock();
  15. }
  16. }
  17. }

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

  1. @Override
  2. public boolean obtain() throws IOException {
  3. boolean unlocked;
  4. try {
  5. rwl.readLock().unlock();
  6. unlocked = true;
  7. } catch (IllegalMonitorStateException e) {
  8. unlocked = false;
  9. }
  10. try {
  11. return lock.obtain();
  12. } finally {
  13. if (unlocked) {
  14. rwl.readLock().lock();
  15. }
  16. }
  17. }

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

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

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

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

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

  1. /** Attempts to obtain an exclusive lock within amount
  2. * of time given. Currently polls once per second until
  3. * lockWaitTimeout is passed.
  4. * @param lockWaitTimeout length of time to wait in ms
  5. * @return true if lock was obtained
  6. * @throws IOException if lock wait times out or obtain() throws an IOException
  7. */
  8. public boolean obtain(long lockWaitTimeout) throws IOException {
  9. boolean locked = obtain();
  10. int maxSleepCount = (int)(lockWaitTimeout / LOCK_POLL_INTERVAL);
  11. int sleepCount = 0;
  12. while (!locked) {
  13. if (++sleepCount == maxSleepCount) {
  14. throw new IOException("Lock obtain timed out: " + this.toString());
  15. }
  16. try {
  17. Thread.sleep(LOCK_POLL_INTERVAL);
  18. } catch (InterruptedException e) {
  19. throw new IOException(e.toString());
  20. }
  21. locked = obtain();
  22. }
  23. return locked;
  24. }

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

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

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

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

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

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

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

  1. /** Calls {@link #doBody} while <i>lock</i> is obtained. Blocks if lock
  2. * cannot be obtained immediately. Retries to obtain lock once per second
  3. * until it is obtained, or until it has tried ten times. Lock is released when
  4. * {@link #doBody} exits. */
  5. public Object run() throws IOException {
  6. boolean locked = false;
  7. try {
  8. locked = lock.obtain(lockWaitTimeout);
  9. return doBody();
  10. } finally {
  11. if (locked)
  12. lock.release();
  13. }
  14. }
  15. }

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

  1. /** Calls {@link #doBody} while <i>lock</i> is obtained. Blocks if lock
  2. * cannot be obtained immediately. Retries to obtain lock once per second
  3. * until it is obtained, or until it has tried ten times. Lock is released when
  4. * {@link #doBody} exits.
  5. * @throws LockObtainFailedException if lock could not
  6. * be obtained
  7. * @throws IOException if {@link Lock#obtain} throws IOException
  8. */
  9. public Object run() throws LockObtainFailedException, IOException {
  10. boolean locked = false;
  11. try {
  12. locked = lock.obtain(lockWaitTimeout);
  13. return doBody();
  14. } finally {
  15. if (locked)
  16. lock.release();
  17. }
  18. }
  19. }

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

  1. /** Calls {@link #doBody} while <i>lock</i> is obtained. Blocks if lock
  2. * cannot be obtained immediately. Retries to obtain lock once per second
  3. * until it is obtained, or until it has tried ten times. Lock is released when
  4. * {@link #doBody} exits.
  5. * @throws LockObtainFailedException if lock could not
  6. * be obtained
  7. * @throws IOException if {@link Lock#obtain} throws IOException
  8. */
  9. public Object run() throws LockObtainFailedException, IOException {
  10. boolean locked = false;
  11. try {
  12. locked = lock.obtain(lockWaitTimeout);
  13. return doBody();
  14. } finally {
  15. if (locked)
  16. lock.release();
  17. }
  18. }
  19. }

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

  1. /** Calls {@link #doBody} while <i>lock</i> is obtained. Blocks if lock
  2. * cannot be obtained immediately. Retries to obtain lock once per second
  3. * until it is obtained, or until it has tried ten times. Lock is released when
  4. * {@link #doBody} exits.
  5. * @throws LockObtainFailedException if lock could not
  6. * be obtained
  7. * @throws IOException if {@link Lock#obtain} throws IOException
  8. */
  9. public Object run() throws IOException {
  10. boolean locked = false;
  11. try {
  12. locked = lock.obtain(lockWaitTimeout);
  13. return doBody();
  14. } finally {
  15. if (locked)
  16. lock.release();
  17. }
  18. }
  19. }

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

  1. private IndexWriter(Directory d, Analyzer a, final boolean create, boolean closeDir)
  2. throws IOException {
  3. this.closeDir = closeDir;
  4. directory = d;
  5. analyzer = a;
  6. Lock writeLock = directory.makeLock(IndexWriter.WRITE_LOCK_NAME);
  7. if (!writeLock.obtain(WRITE_LOCK_TIMEOUT)) // obtain write lock
  8. throw new IOException("Index locked for write: " + writeLock);
  9. this.writeLock = writeLock; // save it
  10. synchronized (directory) { // in- & inter-process sync
  11. new Lock.With(directory.makeLock(IndexWriter.COMMIT_LOCK_NAME), COMMIT_LOCK_TIMEOUT) {
  12. public Object doBody() throws IOException {
  13. if (create)
  14. segmentInfos.write(directory);
  15. else
  16. segmentInfos.read(directory);
  17. return null;
  18. }
  19. }.run();
  20. }
  21. }

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

  1. /**
  2. * Trys to acquire the WriteLock on this directory.
  3. * this method is only valid if this IndexReader is directory owner.
  4. *
  5. * @throws IOException If WriteLock cannot be acquired.
  6. */
  7. private void aquireWriteLock() throws IOException {
  8. if (stale)
  9. throw new IOException("IndexReader out of date and no longer valid for delete, undelete, or setNorm operations");
  10. if (writeLock == null) {
  11. Lock writeLock = directory.makeLock(IndexWriter.WRITE_LOCK_NAME);
  12. if (!writeLock.obtain(IndexWriter.WRITE_LOCK_TIMEOUT)) // obtain write lock
  13. throw new IOException("Index locked for write: " + writeLock);
  14. this.writeLock = writeLock;
  15. // we have to check whether index has changed since this reader was opened.
  16. // if so, this reader is no longer valid for deletion
  17. if (SegmentInfos.readCurrentVersion(directory) > segmentInfos.getVersion()) {
  18. stale = true;
  19. this.writeLock.release();
  20. this.writeLock = null;
  21. throw new IOException("IndexReader out of date and no longer valid for delete, undelete, or setNorm operations");
  22. }
  23. }
  24. }

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

  1. public Object doInTransaction() throws CompassException {
  2. for (int i = 0; i < finalSubIndexes.length; i++) {
  3. Directory dir = getDirectory(finalSubIndexes[i]);
  4. writerLocks[i] = dir.makeLock(IndexWriter.WRITE_LOCK_NAME);
  5. try {
  6. writerLocks[i].obtain(luceneSettings.getTransactionLockTimout());
  7. } catch (IOException e) {
  8. throw new SearchEngineException("Failed to retrieve transaction locks", e);
  9. }
  10. }
  11. return null;
  12. }
  13. });

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

  1. private void obtainOrderLockIfNeeded(String subIndex) throws SearchEngineException {
  2. if (!maintainOrder) {
  3. return;
  4. }
  5. Lock lock = orderLocks.get(subIndex);
  6. if (lock == null) {
  7. lock = searchEngine.getSearchEngineFactory().getLuceneIndexManager().getStore().openDirectory(subIndex).makeLock("order.lock");
  8. try {
  9. lock.obtain(searchEngine.getSearchEngineFactory().getLuceneSettings().getTransactionLockTimout());
  10. } catch (IOException e) {
  11. clearLocksIfNeeded();
  12. throw new SearchEngineException("Failed to obtain order lock on sub index [" + subIndex + "]", e);
  13. }
  14. orderLocks.put(subIndex, lock);
  15. }
  16. }

相关文章