Example usage for org.apache.lucene.store FSDirectory getDirectory

List of usage examples for org.apache.lucene.store FSDirectory getDirectory

Introduction

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

Prototype

public Path getDirectory() 

Source Link

Usage

From source file:edu.stanford.muse.index.Indexer.java

License:Apache License

private synchronized Directory removeFieldsFromDirectory(Directory dir, String... fields_to_be_removed)
        throws IOException {
    if (!indexHasFields(dir, fields_to_be_removed))
        return dir;

    boolean is_file_based = dir instanceof FSDirectory;

    String tmp_name = ".tmp";
    FSDirectory fsdir = null;
    if (is_file_based) {
        fsdir = (FSDirectory) dir;/*  www  . j  a v a 2  s.  co m*/
        tmp_name = fsdir.getDirectory().getName() + tmp_name;
    }

    Directory newDir = copyDirectoryExcludeFields(dir, baseDir, tmp_name, fields_to_be_removed);

    if (is_file_based) {
        // delete the original dir and rename tmp
        File org_file = fsdir.getDirectory();
        File tmp_file = ((FSDirectory) newDir).getDirectory();
        FileUtils.deleteDirectory(org_file);
        boolean res = tmp_file.renameTo(org_file);
        if (!res)
            log.warn("Rename of " + tmp_file.getName() + " failed, things may not work as expected!!");
    }
    return fsdir;
}

From source file:info.boytsov.lucene.CreateIndex.java

License:Open Source License

public static void main(String[] args) throws Exception {
    if (args.length != 3 && args.length != 4) {
        printUsage();/*from   w  w  w . ja  va2s.  c o m*/
        System.exit(1);
    }
    String indexType = args[0];
    String indexSource = args[1];
    int commitInterval = 1000000;

    if (args.length >= 4) {
        commitInterval = Integer.parseInt(args[3]);
    }

    System.out.println("Commiting after indexing " + commitInterval + " docs");

    File outputDir = new File(args[2]);
    if (!outputDir.exists()) {
        if (!outputDir.mkdirs()) {
            System.out.println("couldn't create " + outputDir.getAbsolutePath());
            return;
        }
    }
    if (!outputDir.isDirectory()) {
        System.out.println(outputDir.getAbsolutePath() + " is not a directory!");
        return;
    }
    if (!outputDir.canWrite()) {
        System.out.println("Can't write to " + outputDir.getAbsolutePath());
        return;
    }

    FSDirectory dir = FSDirectory.open(outputDir);

    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_46);// default
                                                                        // stop
                                                                        // words
    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, analyzer);
    config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);// overwrites
                                                          // if
                                                          // needed
    IndexWriter indexWriter = new IndexWriter(dir, config);

    DocMaker docMaker = new DocMaker();
    Properties properties = new Properties();
    properties.setProperty("content.source.forever", "false"); // will
                                                               // parse
                                                               // each
                                                               // document
                                                               // only
                                                               // once
    properties.setProperty("doc.index.props", "true");
    // We want to store small-size fields like URL or even title  ...
    properties.setProperty("doc.stored", "true");
    // but not the large one (great savings, 3x reduction in space)!
    properties.setProperty("doc.body.stored", "false");

    ContentSource source = CreateSource(indexType, indexSource, properties);

    if (source == null) {
        System.err.println("Failed to create a source: " + indexType + "(" + indexSource + ")");
        printUsage();
        System.exit(1);
    }

    Config c = new Config(properties);
    source.setConfig(c);
    source.resetInputs();// though this does not seem needed, it is
                         // (gets the file opened?)
    docMaker.setConfig(c, source);
    int count = 0;
    System.out.println("Starting Indexing of " + indexType + " source " + indexSource);

    long start = System.currentTimeMillis();
    Document doc;
    try {
        while ((doc = docMaker.makeDocument()) != null) {
            indexWriter.addDocument(doc);
            ++count;
            if (count % 5000 == 0) {
                System.out.println(
                        "Indexed " + count + " documents in " + (System.currentTimeMillis() - start) + " ms");
            }
            if (count % commitInterval == 0) {
                indexWriter.commit();
                System.out.println("Committed");
            }
        }
    } catch (org.apache.lucene.benchmark.byTask.feeds.NoMoreDataException nmd) {
        System.out.println("Caught NoMoreDataException! -- Finishing"); // All done
    }
    long finish = System.currentTimeMillis();
    System.out.println("Indexing " + count + " documents took " + (finish - start) + " ms");
    System.out.println("Total data processed: " + source.getTotalBytesCount() + " bytes");
    System.out.println("Index should be located at " + dir.getDirectory().getAbsolutePath());
    docMaker.close();
    indexWriter.commit();
    indexWriter.close();

}

From source file:it.agilelab.bigdata.spark.search.impl.BigChunksRAMDirectory.java

License:Apache License

private BigChunksRAMDirectory(FSDirectory dir, boolean closeDir, IOContext context) throws IOException {
    this();//from   w  w w.java  2 s .co m
    for (String file : dir.listAll()) {
        if (!Files.isDirectory(dir.getDirectory().resolve(file))) {
            copyFrom(dir, file, file, context);
        }
    }
    if (closeDir) {
        dir.close();
    }
}

From source file:org.apache.maven.index.context.DefaultIndexingContext.java

License:Apache License

private static void unlockForcibly(final TrackingLockFactory lockFactory, final Directory dir)
        throws IOException {
    //Warning: Not doable in lucene >= 5.3 consider to remove it as IndexWriter.unlock
    //was always strongly non recommended by Lucene.
    //For now try to do the best to simulate the IndexWriter.unlock at least on FSDirectory
    //using FSLockFactory, the RAMDirectory uses SingleInstanceLockFactory.
    //custom lock factory?
    if (lockFactory != null) {
        final Set<? extends Lock> emittedLocks = lockFactory.getEmittedLocks(IndexWriter.WRITE_LOCK_NAME);
        for (Lock emittedLock : emittedLocks) {
            emittedLock.close();/*from  www.j a  v  a 2s .c o  m*/
        }
    }
    if (dir instanceof FSDirectory) {
        final FSDirectory fsdir = (FSDirectory) dir;
        final Path dirPath = fsdir.getDirectory();
        if (Files.isDirectory(dirPath)) {
            Path lockPath = dirPath.resolve(IndexWriter.WRITE_LOCK_NAME);
            try {
                lockPath = lockPath.toRealPath();
            } catch (IOException ioe) {
                // Not locked
                return;
            }
            try (final FileChannel fc = FileChannel.open(lockPath, StandardOpenOption.CREATE,
                    StandardOpenOption.WRITE)) {
                final FileLock lck = fc.tryLock();
                if (lck == null) {
                    // Still active
                    throw new LockObtainFailedException("Lock held by another process: " + lockPath);
                } else {
                    // Not held fine to release
                    lck.close();
                }
            }
            Files.delete(lockPath);
        }
    }
}

From source file:org.apache.solr.core.SolrDeletionPolicy.java

License:Apache License

private String getId(IndexCommit commit) {
    StringBuilder sb = new StringBuilder();
    Directory dir = commit.getDirectory();

    // For anything persistent, make something that will
    // be the same, regardless of the Directory instance.
    if (dir instanceof FSDirectory) {
        FSDirectory fsd = (FSDirectory) dir;
        File fdir = fsd.getDirectory();
        sb.append(fdir.getPath());/*w w w  . j av  a 2s . c o  m*/
    } else {
        sb.append(dir);
    }

    sb.append('/');
    sb.append(commit.getGeneration());
    return sb.toString();
}

From source file:org.carrot2.source.lucene.FSDirectoryWrapperTest.java

License:Open Source License

@Test
public void testFSDirectorySerialization() throws Exception {
    FSDirectory unserializedDir = null;
    try {/*  w  w w . j  a v  a  2s  .  c  om*/
        final File file = indexDir;
        final Persister persister = new Persister();

        final StringWriter writer = new StringWriter();
        persister.write(SimpleXmlWrappers.wrap(directory), writer);

        final SimpleXmlWrapperValue wrapper = persister.read(SimpleXmlWrapperValue.class,
                new StringReader(writer.toString()));
        assertThat(wrapper).describedAs("Wrapper for: " + writer.toString()).isNotNull();
        unserializedDir = SimpleXmlWrappers.unwrap(wrapper);

        assertThat(unserializedDir).isNotNull();
        assertThat(Files.isSameFile(unserializedDir.getDirectory(), file.toPath())).isTrue();
    } finally {
        if (unserializedDir != null) {
            unserializedDir.close();
        }
    }
}

From source file:org.elasticsearch.index.store.distributor.AbstractDistributor.java

License:Apache License

@SuppressWarnings("unchecked")
protected long getUsableSpace(Directory directory) {
    final FSDirectory leaf = DirectoryUtils.getLeaf(directory, FSDirectory.class);
    if (leaf != null) {
        return leaf.getDirectory().getUsableSpace();
    } else {/*from   w ww  .j a v a  2  s .  com*/
        return 0;
    }
}

From source file:org.elasticsearch.index.store.fs.FsDirectoryService.java

License:Apache License

@Override
public final void renameFile(Directory dir, String from, String to) throws IOException {
    final FSDirectory fsDirectory = DirectoryUtils.getLeaf(dir, FSDirectory.class);
    if (fsDirectory == null) {
        throw new ElasticsearchIllegalArgumentException(
                "Can not rename file on non-filesystem based directory ");
    }/*from   w  w w  .  j  av a2 s.  co  m*/
    File directory = fsDirectory.getDirectory();
    File old = new File(directory, from);
    File nu = new File(directory, to);
    if (nu.exists())
        if (!nu.delete())
            throw new IOException("Cannot delete " + nu);

    if (!old.exists()) {
        throw new FileNotFoundException(
                "Can't rename from [" + from + "] to [" + to + "], from does not exists");
    }

    boolean renamed = false;
    for (int i = 0; i < 3; i++) {
        if (old.renameTo(nu)) {
            renamed = true;
            break;
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new InterruptedIOException(e.getMessage());
        }
    }
    if (!renamed) {
        throw new IOException("Failed to rename, from [" + from + "], to [" + to + "]");
    }
}

From source file:org.elasticsearch.index.store.fs.FsDirectoryService.java

License:Apache License

@Override
public final void fullDelete(Directory dir) throws IOException {
    final FSDirectory fsDirectory = DirectoryUtils.getLeaf(dir, FSDirectory.class);
    if (fsDirectory == null) {
        throw new ElasticsearchIllegalArgumentException(
                "Can not fully delete on non-filesystem based directory");
    }//from w  w  w . j  a  v  a2 s.  c  om
    FileSystemUtils.deleteRecursively(fsDirectory.getDirectory());
    // if we are the last ones, delete also the actual index
    String[] list = fsDirectory.getDirectory().getParentFile().list();
    if (list == null || list.length == 0) {
        FileSystemUtils.deleteRecursively(fsDirectory.getDirectory().getParentFile());
    }
}

From source file:org.modeshape.search.lucene.LuceneConfigurationsTest.java

License:Open Source License

@Test
public void shouldCreateConfigurationFromFileSystemStorage() {
    config = LuceneConfigurations.using(tempArea);
    assertThat(config, is(notNullValue()));
    directory = getDirectory(config, workspace, index);
    assertThat(directory, is(instanceOf(FSDirectory.class)));
    FSDirectory fsDirectory = (FSDirectory) directory;
    assertThat(fsDirectory.getDirectory().getName(), is(index));
    assertThat(fsDirectory.getDirectory().getParentFile().getName(), is(workspace));
}