incubator-blur-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amccu...@apache.org
Subject git commit: Fixing blur-439
Date Tue, 16 Jun 2015 13:35:35 GMT
Repository: incubator-blur
Updated Branches:
  refs/heads/master 49390b52a -> 34af045ed


Fixing blur-439


Project: http://git-wip-us.apache.org/repos/asf/incubator-blur/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-blur/commit/34af045e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-blur/tree/34af045e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-blur/diff/34af045e

Branch: refs/heads/master
Commit: 34af045ede9d4ed012445676d673a2ae20c94bf5
Parents: 49390b5
Author: Aaron McCurry <amccurry@gmail.com>
Authored: Tue Jun 16 09:35:29 2015 -0400
Committer: Aaron McCurry <amccurry@gmail.com>
Committed: Tue Jun 16 09:35:29 2015 -0400

----------------------------------------------------------------------
 .../blur/lucene/index/FencedDirectory.java      | 142 +++++++++++++
 .../apache/blur/store/hdfs/BlurLockFactory.java | 192 +++++++++++------
 .../apache/lucene/index/BlurIndexWriter.java    |  19 +-
 .../blur/lucene/index/BlurIndexWriterTest.java  | 208 +++++++++++++++++++
 4 files changed, 491 insertions(+), 70 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/34af045e/blur-store/src/main/java/org/apache/blur/lucene/index/FencedDirectory.java
----------------------------------------------------------------------
diff --git a/blur-store/src/main/java/org/apache/blur/lucene/index/FencedDirectory.java b/blur-store/src/main/java/org/apache/blur/lucene/index/FencedDirectory.java
new file mode 100644
index 0000000..f7a05c0
--- /dev/null
+++ b/blur-store/src/main/java/org/apache/blur/lucene/index/FencedDirectory.java
@@ -0,0 +1,142 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.blur.lucene.index;
+
+import java.io.IOException;
+import java.util.Collection;
+
+import org.apache.blur.store.blockcache.LastModified;
+import org.apache.blur.store.hdfs.DirectoryDecorator;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.IOContext;
+import org.apache.lucene.store.IndexInput;
+import org.apache.lucene.store.IndexOutput;
+import org.apache.lucene.store.Lock;
+import org.apache.lucene.store.LockFactory;
+
+public class FencedDirectory extends Directory implements DirectoryDecorator, LastModified
{
+
+  private static final String WRITE_LOCK = "write.lock";
+  private final Directory _directory;
+  private final LastModified _lastModified;
+
+  private Lock _writeLock;
+
+  public FencedDirectory(Directory directory) {
+    _directory = directory;
+    if (_directory instanceof LastModified) {
+      _lastModified = (LastModified) _directory;
+    } else {
+      _lastModified = null;
+    }
+  }
+
+  public String[] listAll() throws IOException {
+    return _directory.listAll();
+  }
+
+  public boolean fileExists(String name) throws IOException {
+    return _directory.fileExists(name);
+  }
+
+  public void deleteFile(String name) throws IOException {
+    checkLock();
+    _directory.deleteFile(name);
+  }
+
+  private void checkLock() throws IOException {
+    if (_writeLock != null && !_writeLock.isLocked()) {
+      throw new IOException("Lock [" + WRITE_LOCK + "] has been lost.");
+    }
+  }
+
+  public long fileLength(String name) throws IOException {
+    return _directory.fileLength(name);
+  }
+
+  public IndexOutput createOutput(String name, IOContext context) throws IOException {
+    checkLock();
+    return _directory.createOutput(name, context);
+  }
+
+  public void sync(Collection<String> names) throws IOException {
+    checkLock();
+    _directory.sync(names);
+  }
+
+  public IndexInput openInput(String name, IOContext context) throws IOException {
+    return _directory.openInput(name, context);
+  }
+
+  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.");
+    }
+  }
+
+  public void clearLock(String name) throws IOException {
+    _directory.clearLock(name);
+  }
+
+  public void close() throws IOException {
+    _directory.close();
+  }
+
+  public void setLockFactory(LockFactory lockFactory) throws IOException {
+    _directory.setLockFactory(lockFactory);
+  }
+
+  public LockFactory getLockFactory() {
+    return _directory.getLockFactory();
+  }
+
+  public String getLockID() {
+    return _directory.getLockID();
+  }
+
+  public String toString() {
+    return "FencedDirectory:{" + _directory.toString() + "}";
+  }
+
+  public void copy(Directory to, String src, String dest, IOContext context) throws IOException
{
+    checkLock();
+    _directory.copy(to, src, dest, context);
+  }
+
+  public IndexInputSlicer createSlicer(String name, IOContext context) throws IOException
{
+    return _directory.createSlicer(name, context);
+  }
+
+  @Override
+  public long getFileModified(String name) throws IOException {
+    if (_directory instanceof LastModified) {
+      return _lastModified.getFileModified(name);
+    }
+    throw new RuntimeException("Directory [] does not support last modified call.");
+  }
+
+  @Override
+  public Directory getOriginalDirectory() {
+    return _directory;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/34af045e/blur-store/src/main/java/org/apache/blur/store/hdfs/BlurLockFactory.java
----------------------------------------------------------------------
diff --git a/blur-store/src/main/java/org/apache/blur/store/hdfs/BlurLockFactory.java b/blur-store/src/main/java/org/apache/blur/store/hdfs/BlurLockFactory.java
index 4d9359a..aab1806 100644
--- a/blur-store/src/main/java/org/apache/blur/store/hdfs/BlurLockFactory.java
+++ b/blur-store/src/main/java/org/apache/blur/store/hdfs/BlurLockFactory.java
@@ -16,21 +16,19 @@ package org.apache.blur.store.hdfs;
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+import java.io.FileNotFoundException;
 import java.io.IOException;
-import java.util.Arrays;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.blur.log.Log;
 import org.apache.blur.log.LogFactory;
 import org.apache.blur.trace.Trace;
 import org.apache.blur.trace.Tracer;
 import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.fs.FSDataInputStream;
 import org.apache.hadoop.fs.FSDataOutputStream;
 import org.apache.hadoop.fs.FileStatus;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.fs.PathFilter;
 import org.apache.lucene.store.Lock;
 import org.apache.lucene.store.LockFactory;
 
@@ -39,98 +37,158 @@ public class BlurLockFactory extends LockFactory {
   private static final Log LOG = LogFactory.getLog(BlurLockFactory.class);
 
   private final Configuration _configuration;
-  private final FileSystem _fileSystem;
   private final String _baseLockKey;
-  private byte[] _lockKey;
   private final Path _dir;
-  private long _checkTime = TimeUnit.SECONDS.toMillis(1);
+  private final FileSystem _fileSystem;
 
   public BlurLockFactory(Configuration configuration, Path dir, String host, String pid)
throws IOException {
     _configuration = configuration;
     _dir = dir;
-    _fileSystem = _dir.getFileSystem(_configuration);
     _baseLockKey = host + "/" + pid;
+    _fileSystem = _dir.getFileSystem(_configuration);
   }
 
-  @Override
-  public Lock makeLock(String lockName) {
-    final Path lockPath = new Path(_dir, lockName);
-    final AtomicLong _lastCheck = new AtomicLong();
+  static class BlurLock extends Lock {
 
-    return new Lock() {
-      private boolean _set;
+    private static final String UTF_8 = "UTF-8";
+    private final String _lockName;
+    private final Path _dir;
+    private final String _baseLockKey;
+    private final FileSystem _fileSystem;
+    private boolean _set;
+    private long _id;
 
-      @Override
-      public boolean obtain() throws IOException {
-        Tracer trace = Trace.trace("filesystem - obtain", Trace.param("lockPath", lockPath));
+    BlurLock(FileSystem fileSystem, Path dir, String lockName, String baseLockKey) {
+      _fileSystem = fileSystem;
+      _dir = dir;
+      _lockName = lockName;
+      _baseLockKey = baseLockKey;
+    }
+
+    @Override
+    public boolean obtain() throws IOException {
+      Tracer trace = Trace.trace("filesystem - obtain", Trace.param("dir", _dir), Trace.param("lockName",
_lockName));
+      try {
+        if (_set) {
+          throw new IOException("Lock for [" + _baseLockKey + "] can only be set once.");
+        }
         try {
-          if (_set) {
-            throw new IOException("Lock for [" + _baseLockKey + "] can only be set once.");
-          }
+          long id = findNextLockId();
           try {
-            _lockKey = (_baseLockKey + "/" + System.currentTimeMillis()).getBytes();
-            FSDataOutputStream outputStream = _fileSystem.create(lockPath, true);
-            outputStream.write(_lockKey);
+            LOG.info("Writing lock [{0}] with id [{1}] for [{2}]", _lockName, id, _dir);
+            FSDataOutputStream outputStream = _fileSystem.create(getLockPath(id), false);
+            outputStream.write(_baseLockKey.getBytes(UTF_8));
             outputStream.close();
-          } finally {
-            _set = true;
+            _id = id;
+            cleanupOldLocks();
+            return true;
+          } catch (IOException e) {
+            return false;
           }
-          return true;
         } finally {
-          trace.done();
+          _set = true;
         }
+      } finally {
+        trace.done();
       }
+    }
 
-      @Override
-      public void release() throws IOException {
-        Tracer trace = Trace.trace("filesystem - release",Trace.param("lockPath", lockPath));
-        try {
-          _fileSystem.delete(lockPath, false);
-        } finally {
-          trace.done();
+    private void cleanupOldLocks() throws IOException {
+      FileStatus[] fileStatusForLock = getFileStatusForLock(_fileSystem, _dir, _lockName);
+      for (FileStatus fileStatus : fileStatusForLock) {
+        long id = getId(fileStatus);
+        if (id < _id) {
+          LOG.info("Deleting old lock [{0}] with id [{1}] for [{2}]", _lockName, id, _dir);
+          _fileSystem.delete(getLockPath(id), false);
         }
       }
+    }
 
-      @Override
-      public boolean isLocked() throws IOException {
-        if (_lastCheck.get() + _checkTime >= System.currentTimeMillis()) {
-          return true;
+    private Path getLockPath(long id) {
+      return new Path(_dir, _lockName + "." + id);
+    }
+
+    private long findNextLockId() throws IOException {
+      FileStatus[] listStatus = getFileStatusForLock(_fileSystem, _dir, _lockName);
+      if (listStatus == null || listStatus.length == 0) {
+        return 0L;
+      }
+      long largestId = -1L;
+      for (FileStatus fileStatus : listStatus) {
+        long id = getId(fileStatus);
+        if (id > largestId) {
+          largestId = id;
         }
-        Tracer trace = Trace.trace("filesystem - isLocked", Trace.param("lockPath", lockPath));
-        try {
-          if (!_set) {
-            LOG.info("The lock has NOT been set.");
-            return false;
-          }
-          if (!_fileSystem.exists(lockPath)) {
-            LOG.info("The lock file has been removed.");
-            return false;
-          }
-          FileStatus fileStatus = _fileSystem.getFileStatus(lockPath);
-          long len = fileStatus.getLen();
-          if (len != _lockKey.length) {
-            LOG.info("The lock file length has changed.");
-            return false;
-          }
-          byte[] buf = new byte[_lockKey.length];
-          FSDataInputStream inputStream = _fileSystem.open(lockPath);
-          inputStream.readFully(buf);
-          inputStream.close();
-          if (Arrays.equals(_lockKey, buf)) {
-            _lastCheck.set(System.currentTimeMillis());
-            return true;
-          }
-          LOG.info("The lock information has been changed.");
+      }
+      return largestId + 1L;
+    }
+
+    private long getId(FileStatus fileStatus) {
+      String name = fileStatus.getPath().getName();
+      int lastIndexOf = name.lastIndexOf('.');
+      long id = Long.parseLong(name.substring(lastIndexOf + 1));
+      return id;
+    }
+
+    @Override
+    public boolean isLocked() throws IOException {
+      Tracer trace = Trace.trace("filesystem - isLocked", Trace.param("dir", _dir), Trace.param("lockName",
_lockName));
+      try {
+        // test my file
+        Path myLockPath = getLockPath(_id);
+        Path nextLockPath = getLockPath(_id + 1);
+        boolean e1 = _fileSystem.exists(myLockPath);
+        if (!e1) {
+          LOG.info("Lock lost [{0}] with id [{1}] for [{2}]", _lockName, _id, _dir);
+          return false;
+        }
+        // test next file
+        boolean e2 = _fileSystem.exists(nextLockPath);
+        if (e2) {
+          LOG.info("Lock lost [{0}] with id [{1}] for [{2}]", _lockName, _id, _dir);
           return false;
-        } finally {
-          trace.done();
         }
+        return true;
+      } finally {
+        trace.done();
       }
-    };
+    }
+
+    @Override
+    public void release() throws IOException {
+      Tracer trace = Trace.trace("filesystem - release", Trace.param("dir", _dir), Trace.param("lockName",
_lockName));
+      try {
+        LOG.info("Releasing Lock [{0}] with id [{1}] for [{2}]", _lockName, _id, _dir);
+        _fileSystem.delete(getLockPath(_id), false);
+      } finally {
+        trace.done();
+      }
+    }
+
+  }
+
+  @Override
+  public Lock makeLock(String lockName) {
+    return new BlurLock(_fileSystem, _dir, lockName, _baseLockKey);
   }
 
   @Override
   public void clearLock(String lockName) throws IOException {
-    _fileSystem.delete(new Path(_dir, lockName), false);
+    LOG.info("Clearing lock [{0}] with for [{2}]", lockName, _dir);
+    FileStatus[] fileStatusForLock = getFileStatusForLock(_fileSystem, _dir, lockName);
+    for (FileStatus fileStatus : fileStatusForLock) {
+      _fileSystem.delete(fileStatus.getPath(), false);
+    }
+  }
+
+  private static FileStatus[] getFileStatusForLock(FileSystem fileSystem, Path dir, final
String lockName)
+      throws FileNotFoundException, IOException {
+    FileStatus[] listStatus = fileSystem.listStatus(dir, new PathFilter() {
+      @Override
+      public boolean accept(Path path) {
+        return path.getName().startsWith(lockName + ".");
+      }
+    });
+    return listStatus;
   }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/34af045e/blur-store/src/main/java/org/apache/lucene/index/BlurIndexWriter.java
----------------------------------------------------------------------
diff --git a/blur-store/src/main/java/org/apache/lucene/index/BlurIndexWriter.java b/blur-store/src/main/java/org/apache/lucene/index/BlurIndexWriter.java
index 4de10c0..309489e 100644
--- a/blur-store/src/main/java/org/apache/lucene/index/BlurIndexWriter.java
+++ b/blur-store/src/main/java/org/apache/lucene/index/BlurIndexWriter.java
@@ -20,6 +20,7 @@ import java.io.IOException;
 import java.lang.reflect.Field;
 
 import org.apache.blur.index.ExitableReader;
+import org.apache.blur.lucene.index.FencedDirectory;
 import org.apache.lucene.index.CorruptIndexException;
 import org.apache.lucene.index.IndexWriterConfig;
 import org.apache.lucene.store.Directory;
@@ -46,15 +47,27 @@ public class BlurIndexWriter extends org.apache.lucene.index.IndexWriter
{
 
   public BlurIndexWriter(Directory d, IndexWriterConfig conf, boolean makeReaderExitable)
throws CorruptIndexException,
       LockObtainFailedException, IOException {
-    super(d, conf);
+    super(fence(d), conf);
     try {
-      internalLock = getInternalLock();
+      internalLock = findInternalLock();
     } catch (Exception e) {
       throw new RuntimeException("Could not get the write lock instance.", e);
     }
     _makeReaderExitable = makeReaderExitable;
   }
 
+  private static Directory fence(Directory directory) {
+    if (directory instanceof FencedDirectory) {
+      return directory;
+    } else {
+      return new FencedDirectory(directory);
+    }
+  }
+
+  public Lock getInternalLock() {
+    return internalLock;
+  }
+
   @Override
   DirectoryReader getReader() throws IOException {
     return wrap(super.getReader());
@@ -72,7 +85,7 @@ public class BlurIndexWriter extends org.apache.lucene.index.IndexWriter
{
     return reader;
   }
 
-  private Lock getInternalLock() throws SecurityException, NoSuchFieldException, IllegalArgumentException,
+  private Lock findInternalLock() throws SecurityException, NoSuchFieldException, IllegalArgumentException,
       IllegalAccessException {
     Field field = org.apache.lucene.index.IndexWriter.class.getDeclaredField("writeLock");
     field.setAccessible(true);

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/34af045e/blur-store/src/test/java/org/apache/blur/lucene/index/BlurIndexWriterTest.java
----------------------------------------------------------------------
diff --git a/blur-store/src/test/java/org/apache/blur/lucene/index/BlurIndexWriterTest.java
b/blur-store/src/test/java/org/apache/blur/lucene/index/BlurIndexWriterTest.java
new file mode 100644
index 0000000..c197a7e
--- /dev/null
+++ b/blur-store/src/test/java/org/apache/blur/lucene/index/BlurIndexWriterTest.java
@@ -0,0 +1,208 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.blur.lucene.index;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.apache.blur.store.hdfs.BlurLockFactory;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.Path;
+import org.apache.lucene.analysis.core.KeywordAnalyzer;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field.Store;
+import org.apache.lucene.document.StringField;
+import org.apache.lucene.index.AtomicReaderContext;
+import org.apache.lucene.index.BlurIndexWriter;
+import org.apache.lucene.index.CorruptIndexException;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.IndexWriterConfig;
+import org.apache.lucene.index.IndexableField;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.LockObtainFailedException;
+import org.apache.lucene.store.RAMDirectory;
+import org.apache.lucene.util.InfoStream;
+import org.apache.lucene.util.Version;
+import org.junit.Test;
+
+public class BlurIndexWriterTest {
+
+  private Configuration _configuration = new Configuration();
+  private final Object lock = new Object();
+
+  @Test
+  public void testIndexRelocationFencing() throws CorruptIndexException, LockObtainFailedException,
IOException,
+      InterruptedException {
+    final AtomicBoolean fail1 = new AtomicBoolean();
+    final AtomicBoolean fail2 = new AtomicBoolean();
+    final Path hdfsDirPath = new Path(toUri("./target/tmp/BlurIndexWriterTest"));
+    final Directory directory = new RAMDirectory();
+    Thread thread1 = new Thread(new Runnable() {
+      @Override
+      public void run() {
+        BlurIndexWriter writer1 = null;
+        try {
+          BlurLockFactory blurLockFactory = new BlurLockFactory(_configuration, hdfsDirPath,
"node1", "1");
+          directory.setLockFactory(blurLockFactory);
+          IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_43, new KeywordAnalyzer());
+          conf.setInfoStream(getInfoStream());
+          writer1 = new BlurIndexWriter(directory, conf);
+          writer1.addIndexes(addDir("1"));
+          waitToLooseLock();
+          writer1.prepareCommit();
+          fail1.set(true);
+        } catch (IOException e) {
+          if (writer1 != null) {
+            try {
+              writer1.rollback();
+            } catch (IOException e1) {
+              e1.printStackTrace();
+            }
+          }
+          if (writer1 != null) {
+            try {
+              writer1.close();
+            } catch (IOException e1) {
+              e1.printStackTrace();
+            }
+          }
+        }
+      }
+    });
+
+    Thread thread2 = new Thread(new Runnable() {
+      @Override
+      public void run() {
+        try {
+          waitForDirInThread1ToBeAdded(directory);
+          BlurLockFactory blurLockFactory = new BlurLockFactory(_configuration, hdfsDirPath,
"node2", "2");
+          directory.setLockFactory(blurLockFactory);
+          IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_43, new KeywordAnalyzer());
+          conf.setInfoStream(getInfoStream());
+          BlurIndexWriter writer1 = new BlurIndexWriter(directory, conf);
+          obtainLock();
+          writer1.addIndexes(addDir("2"));
+          writer1.commit();
+          writer1.close();
+        } catch (IOException e) {
+          e.printStackTrace();
+          fail2.set(true);
+        }
+      }
+
+    });
+    thread1.start();
+    thread2.start();
+    thread1.join();
+    thread2.join();
+
+    if (fail1.get()) {
+      fail();
+    }
+
+    if (fail2.get()) {
+      fail();
+    }
+
+    DirectoryReader reader = DirectoryReader.open(directory);
+    List<AtomicReaderContext> leaves = reader.leaves();
+    assertEquals(leaves.size(), 1);
+    assertEquals(reader.numDocs(), 1);
+    Document document = reader.document(0);
+    assertEquals("2", document.get("f"));
+    reader.close();
+  }
+
+  protected InfoStream getInfoStream() {
+    return new InfoStream() {
+
+      @Override
+      public void close() throws IOException {
+      }
+
+      @Override
+      public void message(String component, String message) {
+        System.out.println("Thread [" + Thread.currentThread().getName() + "] Comp [" + component
+ "] Message ["
+            + message + "]");
+      }
+
+      @Override
+      public boolean isEnabled(String component) {
+        return false;
+      }
+    };
+  }
+
+  private void waitForDirInThread1ToBeAdded(Directory directory) throws IOException {
+    while (true) {
+      String[] listAll = directory.listAll();
+      if (Arrays.asList(listAll).contains("_0.fnm")) {
+        return;
+      }
+      try {
+        Thread.sleep(100);
+      } catch (InterruptedException e) {
+        throw new IOException(e);
+      }
+    }
+  }
+
+  protected void waitToLooseLock() throws IOException {
+    synchronized (lock) {
+      try {
+        lock.wait();
+      } catch (InterruptedException e) {
+        throw new IOException(e);
+      }
+    }
+  }
+
+  private void obtainLock() {
+    synchronized (lock) {
+      lock.notify();
+    }
+  }
+
+  private Directory addDir(String v) throws IOException {
+    RAMDirectory directory = new RAMDirectory();
+    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_43, new KeywordAnalyzer());
+    IndexWriter writer = new IndexWriter(directory, config);
+    writer.addDocument(getDoc(v));
+    writer.close();
+
+    return directory;
+  }
+
+  private Iterable<? extends IndexableField> getDoc(String v) {
+    Document document = new Document();
+    document.add(new StringField("f", v, Store.YES));
+    return document;
+  }
+
+  private URI toUri(String f) {
+    return new File(f).getAbsoluteFile().toURI();
+  }
+
+}


Mime
View raw message