Example usage for org.apache.lucene.store FilterDirectory FilterDirectory

List of usage examples for org.apache.lucene.store FilterDirectory FilterDirectory

Introduction

In this page you can find the example usage for org.apache.lucene.store FilterDirectory FilterDirectory.

Prototype

protected FilterDirectory(Directory in) 

Source Link

Document

Sole constructor, typically called from sub-classes.

Usage

From source file:org.apache.jackrabbit.oak.plugins.index.lucene.IndexCopierTest.java

License:Apache License

/**
 * Test the interaction between COR and COW using same underlying directory
 *//*  w  w w  . j  av  a2 s .  c om*/
@Test
public void cowConcurrentAccess() throws Exception {
    CollectingExecutor executor = new CollectingExecutor();
    ExecutorService executorService = Executors.newFixedThreadPool(2);
    executor.setForwardingExecutor(executorService);

    Directory baseDir = new CloseSafeDir();
    String indexPath = "/foo";
    builder.setProperty(IndexConstants.INDEX_PATH, indexPath);
    IndexDefinition defn = new IndexDefinition(root, builder.getNodeState());
    IndexCopier copier = new RAMIndexCopier(baseDir, executor, getWorkDir(), true);

    Directory remote = new CloseSafeDir();
    byte[] f1 = writeFile(remote, "f1");

    Directory cor1 = copier.wrapForRead(indexPath, defn, remote);
    readAndAssert(cor1, "f1", f1);
    cor1.close();

    final CountDownLatch pauseCopyLatch = new CountDownLatch(1);
    Directory remote2 = new FilterDirectory(remote) {
        @Override
        public IndexOutput createOutput(String name, IOContext context) throws IOException {
            try {
                pauseCopyLatch.await();
            } catch (InterruptedException ignore) {

            }
            return super.createOutput(name, context);
        }
    };

    //Start copying a file to remote via COW
    Directory cow1 = copier.wrapForWrite(defn, remote2, false);
    byte[] f2 = writeFile(cow1, "f2");

    //Before copy is done to remote lets delete f1 from remote and
    //open a COR and close it such that it triggers delete of f1
    remote.deleteFile("f1");
    Directory cor2 = copier.wrapForRead(indexPath, defn, remote);

    //Ensure that deletion task submitted to executor get processed immediately
    executor.enableImmediateExecution();
    cor2.close();
    executor.enableDelayedExecution();

    assertFalse(baseDir.fileExists("f1"));
    assertFalse("f2 should not have been copied to remote so far", remote.fileExists("f2"));
    assertTrue("f2 should exist", baseDir.fileExists("f2"));

    pauseCopyLatch.countDown();
    cow1.close();
    assertTrue("f2 should exist", remote.fileExists("f2"));

    executorService.shutdown();
}

From source file:org.elasticsearch.index.shard.LocalShardSnapshot.java

License:Apache License

Directory getSnapshotDirectory() {
    /* this directory will not be used for anything else but reading / copying files to another directory
     * we prevent all write operations on this directory with UOE - nobody should close it either. */
    return new FilterDirectory(store.directory()) {
        @Override//from  ww  w . j  a  va 2s  . c o m
        public String[] listAll() throws IOException {
            Collection<String> fileNames = indexCommit.getFileNames();
            final String[] fileNameArray = fileNames.toArray(new String[fileNames.size()]);
            return fileNameArray;
        }

        @Override
        public void deleteFile(String name) throws IOException {
            throw new UnsupportedOperationException("this directory is read-only");
        }

        @Override
        public void sync(Collection<String> names) throws IOException {
            throw new UnsupportedOperationException("this directory is read-only");
        }

        @Override
        public void rename(String source, String dest) throws IOException {
            throw new UnsupportedOperationException("this directory is read-only");
        }

        @Override
        public IndexOutput createOutput(String name, IOContext context) throws IOException {
            throw new UnsupportedOperationException("this directory is read-only");
        }

        @Override
        public IndexOutput createTempOutput(String prefix, String suffix, IOContext context)
                throws IOException {
            throw new UnsupportedOperationException("this directory is read-only");
        }

        @Override
        public Lock obtainLock(String name) throws IOException {
            /* we do explicitly a no-lock instance since we hold an index commit from a SnapshotDeletionPolicy so we
             * can we certain that nobody messes with the files on disk. We also hold a ref on the store which means
             * no external source will delete files either.*/
            return NoLockFactory.INSTANCE.obtainLock(in, name);
        }

        @Override
        public void close() throws IOException {
            throw new UnsupportedOperationException("nobody should close this directory wrapper");
        }
    };
}

From source file:org.elasticsearch.index.store.DirectoryUtilsTest.java

License:Apache License

@Test
public void testGetLeave() throws IOException {
    File file = ElasticsearchTestCase.newTempDir(LifecycleScope.TEST);
    final int iters = scaledRandomIntBetween(10, 100);
    for (int i = 0; i < iters; i++) {
        {//from  ww  w  .  j  a va2s.  c o  m
            BaseDirectoryWrapper dir = newFSDirectory(file);
            FSDirectory directory = DirectoryUtils.getLeaf(new FilterDirectory(dir) {
            }, FSDirectory.class, null);
            assertThat(directory, notNullValue());
            assertThat(directory, sameInstance(DirectoryUtils.getLeafDirectory(dir, null)));
            dir.close();
        }

        {
            BaseDirectoryWrapper dir = newFSDirectory(file);
            FSDirectory directory = DirectoryUtils.getLeaf(dir, FSDirectory.class, null);
            assertThat(directory, notNullValue());
            assertThat(directory, sameInstance(DirectoryUtils.getLeafDirectory(dir, null)));
            dir.close();
        }

        {
            Set<String> stringSet = Collections.emptySet();
            BaseDirectoryWrapper dir = newFSDirectory(file);
            FSDirectory directory = DirectoryUtils.getLeaf(
                    new FileSwitchDirectory(stringSet, dir, dir, random().nextBoolean()), FSDirectory.class,
                    null);
            assertThat(directory, notNullValue());
            assertThat(directory, sameInstance(DirectoryUtils.getLeafDirectory(dir, null)));
            dir.close();
        }

        {
            Set<String> stringSet = Collections.emptySet();
            BaseDirectoryWrapper dir = newFSDirectory(file);
            FSDirectory directory = DirectoryUtils.getLeaf(
                    new FilterDirectory(new FileSwitchDirectory(stringSet, dir, dir, random().nextBoolean())) {
                    }, FSDirectory.class, null);
            assertThat(directory, notNullValue());
            assertThat(directory, sameInstance(DirectoryUtils.getLeafDirectory(dir, null)));
            dir.close();
        }

        {
            Set<String> stringSet = Collections.emptySet();
            BaseDirectoryWrapper dir = newFSDirectory(file);
            RAMDirectory directory = DirectoryUtils.getLeaf(
                    new FilterDirectory(new FileSwitchDirectory(stringSet, dir, dir, random().nextBoolean())) {
                    }, RAMDirectory.class, null);
            assertThat(directory, nullValue());
            dir.close();
        }

    }
}

From source file:org.elasticsearch.index.store.DirectoryUtilsTests.java

License:Apache License

@Test
public void testGetLeave() throws IOException {
    Path file = createTempDir();/*www  . ja v a2 s  . co m*/
    final int iters = scaledRandomIntBetween(10, 100);
    for (int i = 0; i < iters; i++) {
        {
            BaseDirectoryWrapper dir = newFSDirectory(file);
            FSDirectory directory = DirectoryUtils.getLeaf(new FilterDirectory(dir) {
            }, FSDirectory.class, null);
            assertThat(directory, notNullValue());
            assertThat(directory, sameInstance(DirectoryUtils.getLeafDirectory(dir, null)));
            dir.close();
        }

        {
            BaseDirectoryWrapper dir = newFSDirectory(file);
            FSDirectory directory = DirectoryUtils.getLeaf(dir, FSDirectory.class, null);
            assertThat(directory, notNullValue());
            assertThat(directory, sameInstance(DirectoryUtils.getLeafDirectory(dir, null)));
            dir.close();
        }

        {
            Set<String> stringSet = Collections.emptySet();
            BaseDirectoryWrapper dir = newFSDirectory(file);
            FSDirectory directory = DirectoryUtils.getLeaf(
                    new FileSwitchDirectory(stringSet, dir, dir, random().nextBoolean()), FSDirectory.class,
                    null);
            assertThat(directory, notNullValue());
            assertThat(directory, sameInstance(DirectoryUtils.getLeafDirectory(dir, null)));
            dir.close();
        }

        {
            Set<String> stringSet = Collections.emptySet();
            BaseDirectoryWrapper dir = newFSDirectory(file);
            FSDirectory directory = DirectoryUtils.getLeaf(
                    new FilterDirectory(new FileSwitchDirectory(stringSet, dir, dir, random().nextBoolean())) {
                    }, FSDirectory.class, null);
            assertThat(directory, notNullValue());
            assertThat(directory, sameInstance(DirectoryUtils.getLeafDirectory(dir, null)));
            dir.close();
        }

        {
            Set<String> stringSet = Collections.emptySet();
            BaseDirectoryWrapper dir = newFSDirectory(file);
            RAMDirectory directory = DirectoryUtils.getLeaf(
                    new FilterDirectory(new FileSwitchDirectory(stringSet, dir, dir, random().nextBoolean())) {
                    }, RAMDirectory.class, null);
            assertThat(directory, nullValue());
            dir.close();
        }

    }
}

From source file:org.elasticsearch.index.store.mock.MockDirectoryHelper.java

License:Apache License

public Directory wrap(Directory dir) {
    final MockDirectoryWrapper w = new MockDirectoryWrapper(random, dir);
    w.setRandomIOExceptionRate(randomIOExceptionRate);
    w.setRandomIOExceptionRateOnOpen(randomIOExceptionRateOnOpen);
    w.setThrottling(throttle);//from   w ww.j  av  a  2s.co m
    w.setCheckIndexOnClose(checkIndexOnClose);
    wrappers.add(w);
    return new FilterDirectory(w) {
        @Override
        public Directory getDelegate() {
            // TODO we should port this FilterDirectory to Lucene
            return w.getDelegate();
        }
    };
}

From source file:org.elasticsearch.test.store.mock.MockDirectoryHelper.java

License:Apache License

public Directory wrap(Directory dir) {
    final MockDirectoryWrapper w = new MockDirectoryWrapper(random, dir);
    w.setRandomIOExceptionRate(randomIOExceptionRate);
    w.setRandomIOExceptionRateOnOpen(randomIOExceptionRateOnOpen);
    w.setThrottling(throttle);//from  w  w  w  .  ja v  a2 s.c  o m
    w.setCheckIndexOnClose(checkIndexOnClose);
    w.setPreventDoubleWrite(preventDoubleWrite);
    w.setNoDeleteOpenFile(noDeleteOpenFile);
    wrappers.add(w);
    return new FilterDirectory(w) {
        @Override
        public Directory getDelegate() {
            // TODO we should port this FilterDirectory to Lucene
            return w.getDelegate();
        }
    };
}