Example usage for org.apache.lucene.index DirectoryReader indexExists

List of usage examples for org.apache.lucene.index DirectoryReader indexExists

Introduction

In this page you can find the example usage for org.apache.lucene.index DirectoryReader indexExists.

Prototype

public static boolean indexExists(Directory directory) throws IOException 

Source Link

Document

Returns true if an index likely exists at the specified directory.

Usage

From source file:org.opensolaris.opengrok.index.IndexDatabase.java

License:Open Source License

/**
 * Get an indexReader for the Index database where a given file
 *
 * @param path the file to get the database for
 * @return The index database where the file should be located or null if it
 * cannot be located./*www  . j  a v  a  2s  .  c o m*/
 */
public static IndexReader getIndexReader(String path) {
    IndexReader ret = null;

    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    File indexDir = new File(env.getDataRootFile(), INDEX_DIR);

    if (env.hasProjects()) {
        Project p = Project.getProject(path);
        if (p == null) {
            return null;
        }
        indexDir = new File(indexDir, p.getPath());
    }
    try {
        FSDirectory fdir = FSDirectory.open(indexDir, NoLockFactory.getNoLockFactory());
        if (indexDir.exists() && DirectoryReader.indexExists(fdir)) {
            ret = DirectoryReader.open(fdir);
        }
    } catch (Exception ex) {
        log.log(Level.SEVERE, "Failed to open index: {0}", indexDir.getAbsolutePath());
        log.log(Level.FINE, "Stack Trace: ", ex);
    }
    return ret;
}

From source file:org.pageseeder.flint.lucene.LuceneIndexIO.java

License:Apache License

private void open() throws IOException {
    // create it?
    boolean createIt = !DirectoryReader.indexExists(this._directory);
    // read only?
    boolean readonly = isReadOnly(this._directory);
    if (readonly) {
        this._writer = null;
        this._reader = new ReaderManager(this._directory);
        this._searcher = new SearcherManager(this._directory, FACTORY);
    } else {/*from   w ww.  j  a v  a  2s.c  o  m*/
        // create writer
        IndexWriterConfig config = new IndexWriterConfig(this._analyzer);
        ConcurrentMergeScheduler merger = new ConcurrentMergeScheduler();
        //      merger.setMaxMergesAndThreads(maxMergeCount, maxThreadCount);
        config.setMergeScheduler(merger);
        if (createIt)
            config.setOpenMode(OpenMode.CREATE);
        this._writer = new IndexWriter(this._directory, config);
        if (createIt)
            this._writer.commit();
        boolean applyAllDeletes = true;
        // create searcher
        this._searcher = new SearcherManager(this._writer, applyAllDeletes, FACTORY);
        // create reader
        this._reader = new ReaderManager(this._writer, applyAllDeletes);
    }
    // add it to list of opened indexes
    OpenIndexManager.add(this);
    // set state to clean
    state(State.CLEAN);
}

From source file:org.pageseeder.flint.lucene.LuceneLocalIndex.java

License:Apache License

/**
 * Indicates whether the folder exists and is a valid Lucene index.
 *
 * <p>This method uses the Lucene {@link IndexReader} to check whether the folder corresponds to a valid Lucene Index.
 *
 * @param location the folder where the index is located.
 * @return <code>true</code> if the folder exists and is a Lucene index; <code>false</code> otherwise
 *///  w w w . j a v a 2s  . c  o m
public static boolean exists(File location) {
    if (!location.exists() || !location.isDirectory())
        return false;
    boolean exists = false;
    // Get the last modified from the index
    try {
        Directory directory = FSDirectory.open(location.toPath());
        exists = DirectoryReader.indexExists(directory);
        directory.close();
    } catch (IOException ex) {
        LOGGER.error("Unable to check if local index exists", ex);
    }
    return exists;
}

From source file:org.tightblog.service.LuceneIndexer.java

License:Apache License

/**
 * Initialize the Lucene indexer./*from  ww  w .  ja va2 s  .  co  m*/
 */
public void initialize() {

    // only initialize the index if search is enabled
    if (searchEnabled) {
        boolean indexNeedsCreating = false;

        try {
            // existing indexConsistencyMarker means this.shutdown() wasn't called at last app shutdown
            if (indexConsistencyMarker.exists()) {
                log.info("Index was not closed properly with last shutdown; will be rebuilt");
                indexNeedsCreating = true;
            } else {
                // see if index directory exists, if not create
                File testIndexDir = new File(indexDir);
                if (!testIndexDir.exists()) {
                    if (testIndexDir.mkdirs()) {
                        indexNeedsCreating = true;
                        log.info("Index folder path {} created", testIndexDir.getAbsolutePath());
                    } else {
                        throw new IOException("Folder path " + testIndexDir.getAbsolutePath() + " could not be "
                                + "created (file permission rights?)");
                    }
                } else {
                    // OK, index directory exists, see if Lucene index exists within it
                    if (!DirectoryReader.indexExists(getIndexDirectory())) {
                        log.info("Lucene index not detected, will create");
                        indexNeedsCreating = true;
                    } else {
                        log.info("Lucene search index already available and ready for use.");
                    }
                }
            }

            if (indexNeedsCreating) {
                log.info("Generating Lucene index in the background...");
                // deletes index consistency marker if it exists
                createIndex(getFSDirectory(true));

                // create index consistency marker for next app shutdown
                if (!indexConsistencyMarker.createNewFile()) {
                    throw new IOException("Could not create index consistency marker "
                            + indexConsistencyMarker.getAbsolutePath() + " (file permission rights?)");
                }
                rebuildWeblogIndex();
            }

            reader = DirectoryReader.open(getIndexDirectory());

        } catch (IOException e) {
            log.error("Could not create index, searching will be deactivated.", e);
            searchEnabled = false;
        }
    }

}

From source file:org.ujmp.lucene.LuceneMap.java

License:Open Source License

private synchronized IndexWriter getIndexWriter() {
    try {/*  w ww .ja  v a  2  s .co  m*/
        if (!readOnly && indexSearcher != null && indexSearcher.getIndexReader().getRefCount() > 0) {
            indexSearcher.getIndexReader().close();
            indexSearcher = null;
        }
        if (indexWriter == null) {
            if (DirectoryReader.indexExists(getDirectory())) {
                if (!readOnly) {
                    if (IndexWriter.isLocked(getDirectory())) {
                        IndexWriter.unlock(getDirectory());
                    }
                    indexWriter = new IndexWriter(getDirectory(),
                            new IndexWriterConfig(Version.LUCENE_47, getAnalyzer()));
                }
            } else {
                if (!readOnly) {
                    indexWriter = new IndexWriter(getDirectory(),
                            new IndexWriterConfig(Version.LUCENE_47, getAnalyzer()));
                }
            }
        }
        return indexWriter;
    } catch (Exception e) {
        throw new RuntimeException("could not prepare writher", e);
    }
}

From source file:org.ujmp.lucene.LuceneMap.java

License:Open Source License

private synchronized IndexSearcher getIndexSearcher() {
    try {/*from  www  .j  a  v a  2 s  .c om*/
        if (!DirectoryReader.indexExists(getDirectory())) {
            getIndexWriter();
        }

        if (indexSearcher == null) {
            indexSearcher = new IndexSearcher(DirectoryReader.open(getIndexWriter(), true));
        }
        return indexSearcher;
    } catch (Exception e) {
        throw new RuntimeException("could not prepare reader", e);
    }
}

From source file:perf.SearchPerfTest.java

License:Apache License

private static void _main(String[] clArgs) throws Exception {

    // args: dirImpl indexPath numThread numIterPerThread
    // eg java SearchPerfTest /path/to/index 4 100
    final Args args = new Args(clArgs);

    Directory dir0;/*from   w  ww .  j  a  va2 s  .  com*/
    final String dirPath = args.getString("-indexPath") + "/index";
    final String dirImpl = args.getString("-dirImpl");

    OpenDirectory od = OpenDirectory.get(dirImpl);

    /*
    } else if (dirImpl.equals("NativePosixMMapDirectory")) {
      dir0 = new NativePosixMMapDirectory(new File(dirPath));
      ramDir = null;
      if (doFacets) {
        facetsDir = new NativePosixMMapDirectory(new File(facetsDirPath));
      }
    } else if (dirImpl.equals("CachingDirWrapper")) {
      dir0 = new CachingRAMDirectory(new MMapDirectory(new File(dirPath)));
      ramDir = null;
    } else if (dirImpl.equals("RAMExceptDirectPostingsDirectory")) {
      // Load only non-postings files into RAMDir (assumes
      // Lucene40PF is the wrapped PF):
      Set<String> postingsExtensions = new HashSet<String>();
      postingsExtensions.add("frq");
      postingsExtensions.add("prx");
      postingsExtensions.add("tip");
      postingsExtensions.add("tim");
              
      ramDir =  new RAMDirectory();
      Directory fsDir = new MMapDirectory(new File(dirPath));
      for (String file : fsDir.listAll()) {
        int idx = file.indexOf('.');
        if (idx != -1 && postingsExtensions.contains(file.substring(idx+1, file.length()))) {
          continue;
        }
            
        fsDir.copy(ramDir, file, file, IOContext.READ);
      }
      dir0 = new FileSwitchDirectory(postingsExtensions,
                             fsDir,
                             ramDir,
                             true);
      if (doFacets) {
        facetsDir = new RAMDirectory(new SimpleFSDirectory(new File(facetsDirPath)), IOContext.READ);
      }
      */

    final RAMDirectory ramDir;
    dir0 = od.open(Paths.get(dirPath));
    if (dir0 instanceof RAMDirectory) {
        ramDir = (RAMDirectory) dir0;
    } else {
        ramDir = null;
    }

    // TODO: NativeUnixDir?

    final String analyzer = args.getString("-analyzer");
    final String tasksFile = args.getString("-taskSource");
    final int searchThreadCount = args.getInt("-searchThreadCount");
    final String fieldName = args.getString("-field");
    final boolean printHeap = args.getFlag("-printHeap");
    final boolean doPKLookup = args.getFlag("-pk");
    final int topN = args.getInt("-topN");
    final boolean doStoredLoads = args.getFlag("-loadStoredFields");

    // Used to choose which random subset of tasks we will
    // run, to generate the PKLookup tasks, and to generate
    // any random pct filters:
    final long staticRandomSeed = args.getLong("-staticSeed");

    // Used to shuffle the random subset of tasks:
    final long randomSeed = args.getLong("-seed");

    // TODO: this could be way better.
    final String similarity = args.getString("-similarity");
    // now reflect
    final Class<? extends Similarity> simClazz = Class
            .forName("org.apache.lucene.search.similarities." + similarity).asSubclass(Similarity.class);
    final Similarity sim = simClazz.newInstance();

    System.out.println("Using dir impl " + dir0.getClass().getName());
    System.out.println("Analyzer " + analyzer);
    System.out.println("Similarity " + similarity);
    System.out.println("Search thread count " + searchThreadCount);
    System.out.println("topN " + topN);
    System.out.println("JVM " + (Constants.JRE_IS_64BIT ? "is" : "is not") + " 64bit");
    System.out.println("Pointer is " + RamUsageEstimator.NUM_BYTES_OBJECT_REF + " bytes");

    final Analyzer a;
    if (analyzer.equals("EnglishAnalyzer")) {
        a = new EnglishAnalyzer();
    } else if (analyzer.equals("ClassicAnalyzer")) {
        a = new ClassicAnalyzer();
    } else if (analyzer.equals("StandardAnalyzer")) {
        a = new StandardAnalyzer();
    } else if (analyzer.equals("StandardAnalyzerNoStopWords")) {
        a = new StandardAnalyzer(CharArraySet.EMPTY_SET);
    } else if (analyzer.equals("ShingleStandardAnalyzer")) {
        a = new ShingleAnalyzerWrapper(new StandardAnalyzer(CharArraySet.EMPTY_SET), 2, 2,
                ShingleFilter.DEFAULT_TOKEN_SEPARATOR, true, true, ShingleFilter.DEFAULT_FILLER_TOKEN);
    } else {
        throw new RuntimeException("unknown analyzer " + analyzer);
    }

    final ReferenceManager<IndexSearcher> mgr;
    final IndexWriter writer;
    final Directory dir;

    final String commit = args.getString("-commit");
    final String hiliteImpl = args.getString("-hiliteImpl");

    final String logFile = args.getString("-log");

    final long tSearcherStart = System.currentTimeMillis();

    final boolean verifyCheckSum = !args.getFlag("-skipVerifyChecksum");
    final boolean recacheFilterDeletes = args.getFlag("-recacheFilterDeletes");

    if (recacheFilterDeletes) {
        throw new UnsupportedOperationException("recacheFilterDeletes was deprecated");
    }

    if (args.getFlag("-nrt")) {
        // TODO: get taxoReader working here too
        // TODO: factor out & share this CL processing w/ Indexer
        final int indexThreadCount = args.getInt("-indexThreadCount");
        final String lineDocsFile = args.getString("-lineDocsFile");
        final float docsPerSecPerThread = args.getFloat("-docsPerSecPerThread");
        final float reopenEverySec = args.getFloat("-reopenEverySec");
        final boolean storeBody = args.getFlag("-store");
        final boolean tvsBody = args.getFlag("-tvs");
        final boolean useCFS = args.getFlag("-cfs");
        final String defaultPostingsFormat = args.getString("-postingsFormat");
        final String idFieldPostingsFormat = args.getString("-idFieldPostingsFormat");
        final boolean verbose = args.getFlag("-verbose");
        final boolean cloneDocs = args.getFlag("-cloneDocs");
        final Mode mode = Mode.valueOf(args.getString("-mode", "update").toUpperCase(Locale.ROOT));

        final long reopenEveryMS = (long) (1000 * reopenEverySec);

        if (verbose) {
            InfoStream.setDefault(new PrintStreamInfoStream(System.out));
        }

        if (!dirImpl.equals("RAMDirectory") && !dirImpl.equals("RAMExceptDirectPostingsDirectory")) {
            System.out.println("Wrap NRTCachingDirectory");
            dir0 = new NRTCachingDirectory(dir0, 20, 400.0);
        }

        dir = dir0;

        final IndexWriterConfig iwc = new IndexWriterConfig(a);
        iwc.setOpenMode(IndexWriterConfig.OpenMode.APPEND);
        iwc.setRAMBufferSizeMB(256.0);
        iwc.setIndexDeletionPolicy(NoDeletionPolicy.INSTANCE);

        // TODO: also RAMDirExceptDirect...?  need to
        // ... block deletes against wrapped FSDir?
        if (dirImpl.equals("RAMDirectory")) {
            // Let IW remove files only referenced by starting commit:
            iwc.setIndexDeletionPolicy(new KeepNoCommitsDeletionPolicy());
        }

        if (commit != null && commit.length() > 0) {
            System.out.println("Opening writer on commit=" + commit);
            iwc.setIndexCommit(PerfUtils.findCommitPoint(commit, dir));
        }

        ((TieredMergePolicy) iwc.getMergePolicy()).setNoCFSRatio(useCFS ? 1.0 : 0.0);
        //((TieredMergePolicy) iwc.getMergePolicy()).setMaxMergedSegmentMB(1024);
        //((TieredMergePolicy) iwc.getMergePolicy()).setReclaimDeletesWeight(3.0);
        //((TieredMergePolicy) iwc.getMergePolicy()).setMaxMergeAtOnce(4);

        final Codec codec = new Lucene62Codec() {
            @Override
            public PostingsFormat getPostingsFormatForField(String field) {
                return PostingsFormat
                        .forName(field.equals("id") ? idFieldPostingsFormat : defaultPostingsFormat);
            }
        };
        iwc.setCodec(codec);

        final ConcurrentMergeScheduler cms = (ConcurrentMergeScheduler) iwc.getMergeScheduler();
        // Only let one merge run at a time...
        // ... but queue up up to 4, before index thread is stalled:
        cms.setMaxMergesAndThreads(4, 1);

        iwc.setMergedSegmentWarmer(new IndexWriter.IndexReaderWarmer() {
            @Override
            public void warm(LeafReader reader) throws IOException {
                final long t0 = System.currentTimeMillis();
                //System.out.println("DO WARM: " + reader);
                IndexSearcher s = new IndexSearcher(reader);
                s.setQueryCache(null); // don't bench the cache
                s.search(new TermQuery(new Term(fieldName, "united")), 10);
                final long t1 = System.currentTimeMillis();
                System.out.println("warm segment=" + reader + " numDocs=" + reader.numDocs() + ": took "
                        + (t1 - t0) + " msec");
            }
        });

        writer = new IndexWriter(dir, iwc);
        System.out.println("Initial writer.maxDoc()=" + writer.maxDoc());

        // TODO: add -nrtBodyPostingsOffsets instead of
        // hardwired false:
        boolean addDVFields = mode == Mode.BDV_UPDATE || mode == Mode.NDV_UPDATE;
        LineFileDocs lineFileDocs = new LineFileDocs(lineDocsFile, false, storeBody, tvsBody, false, cloneDocs,
                null, null, null, addDVFields);
        IndexThreads threads = new IndexThreads(new Random(17), writer, new AtomicBoolean(false), lineFileDocs,
                indexThreadCount, -1, false, false, mode, docsPerSecPerThread, null, -1.0, -1);
        threads.start();

        mgr = new SearcherManager(writer, new SearcherFactory() {
            @Override
            public IndexSearcher newSearcher(IndexReader reader, IndexReader previous) {
                IndexSearcher s = new IndexSearcher(reader);
                s.setQueryCache(null); // don't bench the cache
                s.setSimilarity(sim);
                return s;
            }
        });

        System.out.println("reopen every " + reopenEverySec);

        Thread reopenThread = new Thread() {
            @Override
            public void run() {
                try {
                    final long startMS = System.currentTimeMillis();

                    int reopenCount = 1;
                    while (true) {
                        final long sleepMS = startMS + (reopenCount * reopenEveryMS)
                                - System.currentTimeMillis();
                        if (sleepMS < 0) {
                            System.out.println("WARNING: reopen fell behind by " + Math.abs(sleepMS) + " ms");
                        } else {
                            Thread.sleep(sleepMS);
                        }

                        Thread.sleep(sleepMS);
                        mgr.maybeRefresh();
                        reopenCount++;
                        IndexSearcher s = mgr.acquire();
                        try {
                            if (ramDir != null) {
                                System.out.println(String.format(Locale.ENGLISH,
                                        "%.1fs: index: %d bytes in RAMDir; writer.maxDoc()=%d; searcher.maxDoc()=%d; searcher.numDocs()=%d",
                                        (System.currentTimeMillis() - startMS) / 1000.0, ramDir.ramBytesUsed(),
                                        writer.maxDoc(), s.getIndexReader().maxDoc(),
                                        s.getIndexReader().numDocs()));
                                //String[] l = ramDir.listAll();
                                //Arrays.sort(l);
                                //for(String f : l) {
                                //System.out.println("  " + f + ": " + ramDir.fileLength(f));
                                //}
                            } else {
                                System.out.println(String.format(Locale.ENGLISH,
                                        "%.1fs: done reopen; writer.maxDoc()=%d; searcher.maxDoc()=%d; searcher.numDocs()=%d",
                                        (System.currentTimeMillis() - startMS) / 1000.0, writer.maxDoc(),
                                        s.getIndexReader().maxDoc(), s.getIndexReader().numDocs()));
                            }
                        } finally {
                            mgr.release(s);
                        }
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        };
        reopenThread.setName("ReopenThread");
        reopenThread.setPriority(4 + Thread.currentThread().getPriority());
        reopenThread.start();

    } else {
        dir = dir0;
        writer = null;
        final DirectoryReader reader;
        if (commit != null && commit.length() > 0) {
            System.out.println("Opening searcher on commit=" + commit);
            reader = DirectoryReader.open(PerfUtils.findCommitPoint(commit, dir));
        } else {
            // open last commit
            reader = DirectoryReader.open(dir);
        }
        IndexSearcher s = new IndexSearcher(reader);
        s.setQueryCache(null); // don't bench the cache
        s.setSimilarity(sim);
        System.out.println("maxDoc=" + reader.maxDoc() + " numDocs=" + reader.numDocs() + " %tg deletes="
                + (100. * reader.maxDoc() / reader.numDocs()));

        mgr = new SingleIndexSearcher(s);
    }

    System.out.println((System.currentTimeMillis() - tSearcherStart) + " msec to init searcher/NRT");

    {
        IndexSearcher s = mgr.acquire();
        try {
            System.out.println("Searcher: numDocs=" + s.getIndexReader().numDocs() + " maxDoc="
                    + s.getIndexReader().maxDoc() + ": " + s);
        } finally {
            mgr.release(s);
        }
    }

    //System.out.println("searcher=" + searcher);

    FacetsConfig facetsConfig = new FacetsConfig();
    facetsConfig.setHierarchical("Date", true);

    TaxonomyReader taxoReader;
    Path taxoPath = Paths.get(args.getString("-indexPath"), "facets");
    Directory taxoDir = od.open(taxoPath);
    if (DirectoryReader.indexExists(taxoDir)) {
        taxoReader = new DirectoryTaxonomyReader(taxoDir);
        System.out.println("Taxonomy has " + taxoReader.getSize() + " ords");
    } else {
        taxoReader = null;
    }

    final Random staticRandom = new Random(staticRandomSeed);
    final Random random = new Random(randomSeed);

    final DirectSpellChecker spellChecker = new DirectSpellChecker();
    final IndexState indexState = new IndexState(mgr, taxoReader, fieldName, spellChecker, hiliteImpl,
            facetsConfig);

    final QueryParser queryParser = new QueryParser("body", a);
    TaskParser taskParser = new TaskParser(indexState, queryParser, fieldName, topN, staticRandom,
            doStoredLoads);

    final TaskSource tasks;

    if (tasksFile.startsWith("server:")) {
        int idx = tasksFile.indexOf(':', 8);
        if (idx == -1) {
            throw new RuntimeException(
                    "server is missing the port; should be server:interface:port (got: " + tasksFile + ")");
        }
        String iface = tasksFile.substring(7, idx);
        int port = Integer.valueOf(tasksFile.substring(1 + idx));
        RemoteTaskSource remoteTasks = new RemoteTaskSource(iface, port, searchThreadCount, taskParser);

        // nocommit must stop thread?
        tasks = remoteTasks;
    } else {
        // Load the tasks from a file:
        final int taskRepeatCount = args.getInt("-taskRepeatCount");
        final int numTaskPerCat = args.getInt("-tasksPerCat");
        tasks = new LocalTaskSource(indexState, taskParser, tasksFile, staticRandom, random, numTaskPerCat,
                taskRepeatCount, doPKLookup);
        System.out.println("Task repeat count " + taskRepeatCount);
        System.out.println("Tasks file " + tasksFile);
        System.out.println("Num task per cat " + numTaskPerCat);
    }

    args.check();

    // Evil respeller:
    //spellChecker.setMinPrefix(0);
    //spellChecker.setMaxInspections(1024);
    final TaskThreads taskThreads = new TaskThreads(tasks, indexState, searchThreadCount);
    Thread.sleep(10);

    final long startNanos = System.nanoTime();
    taskThreads.start();
    taskThreads.finish();
    final long endNanos = System.nanoTime();

    System.out.println("\n" + ((endNanos - startNanos) / 1000000.0) + " msec total");

    final List<Task> allTasks = tasks.getAllTasks();

    PrintStream out = new PrintStream(logFile);

    if (allTasks != null) {
        // Tasks were local: verify checksums:

        // indexState.setDocIDToID();

        final Map<Task, Task> tasksSeen = new HashMap<Task, Task>();

        out.println("\nResults for " + allTasks.size() + " tasks:");

        boolean fail = false;
        for (final Task task : allTasks) {
            if (verifyCheckSum) {
                final Task other = tasksSeen.get(task);
                if (other != null) {
                    if (task.checksum() != other.checksum()) {
                        System.out.println("\nTASK:");
                        task.printResults(System.out, indexState);
                        System.out.println("\nOTHER TASK:");
                        other.printResults(System.out, indexState);
                        fail = true;
                        //throw new RuntimeException("task " + task + " hit different checksums: " + task.checksum() + " vs " + other.checksum() + " other=" + other);
                    }
                } else {
                    tasksSeen.put(task, task);
                }
            }
            out.println("\nTASK: " + task);
            out.println("  " + (task.runTimeNanos / 1000000.0) + " msec");
            out.println("  thread " + task.threadID);
            task.printResults(out, indexState);
        }
        if (fail) {
            throw new RuntimeException("some tasks got different results across different threads");
        }

        allTasks.clear();
    }

    mgr.close();

    if (taxoReader != null) {
        taxoReader.close();
    }

    if (writer != null) {
        // Don't actually commit any index changes:
        writer.rollback();
    }

    dir.close();

    if (printHeap) {

        // Try to get RAM usage -- some ideas poached from http://www.javaworld.com/javaworld/javatips/jw-javatip130.html
        final Runtime runtime = Runtime.getRuntime();
        long usedMem1 = PerfUtils.usedMemory(runtime);
        long usedMem2 = Long.MAX_VALUE;
        for (int iter = 0; iter < 10; iter++) {
            runtime.runFinalization();
            runtime.gc();
            Thread.yield();
            Thread.sleep(100);
            usedMem2 = usedMem1;
            usedMem1 = PerfUtils.usedMemory(runtime);
        }
        out.println("\nHEAP: " + PerfUtils.usedMemory(runtime));
    }
    out.close();
}

From source file:pt.ua.ieeta.biomedhub.dictionaries.wrappers.CTakesLucene.java

License:Open Source License

public Multimap readBlocks(int blockSize) throws IOException {

    try {// w  w  w  . j  a  va  2  s .  c o  m
        if (!DirectoryReader.indexExists(index)) {
            return null;
        }
    } catch (IOException ex) {
        Logger.getLogger("lucene").log(Level.SEVERE, null, ex);
    }

    String querystr = "*:*";
    Query q = null;
    try {
        q = new QueryParser(Version.LUCENE_4_9, "title", analyzer).parse(querystr);
    } catch (ParseException ex) {
        Logger.getLogger(CTakesLucene.class.getName()).log(Level.SEVERE, null, ex);
    }
    int hitsPerPage = 100;
    IndexReader reader = DirectoryReader.open(index);
    IndexSearcher searcher = new IndexSearcher(reader);
    TopScoreDocCollector collector = TopScoreDocCollector.create(200000, true);
    searcher.search(q, collector);

    ScoreDoc[] hits = collector.topDocs().scoreDocs;
    System.out.println("Found " + hits.length + " hits.");
    for (int i = 0; i < hits.length; ++i) {
        int docId = hits[i].doc;
        Document d = searcher.doc(docId);
        //System.out.println((i + 1) + ". " + d.get("first_word") + "\t" + d.get("code"));
        System.out.print(".");
        synons.put(d.get("code"), d.get("first_word"));
    }

    return synons;

}

From source file:start.lucene.IndexAndSearch.java

/**
 *
 * @param headers a list of CPeptides entries
 * @param ptmFactory/*  w w w. j  a v  a  2 s . c  o  m*/
 * @param linkerName - just name of linker to create both heavy and light
 * labeled versions
 * @param fragMode fragmentation mode
 * @param folder index folder
 * @throws IOException
 * @throws Exception
 */
public IndexAndSearch(HashSet<StringBuilder> headers, File folder, PTMFactory ptmFactory,
        FragmentationMode fragMode, String linkerName) throws IOException, Exception {
    // canCrosslinkerAttach if index files exist on given folder
    boolean reader = DirectoryReader.indexExists(FSDirectory.open(folder));
    // if an index folder is absent, first index 
    if (!reader) {
        CPeptidesIndexer indexer = new CPeptidesIndexer(headers, folder);
        indexer.index();
    }
    cPeptideSearcher = new CPeptideSearcher(folder);
    this.ptmFactory = ptmFactory;
    this.fragMode = fragMode;
    heavyLinker = GetCrossLinker.getCrossLinker(linkerName, true);
    lightLinker = GetCrossLinker.getCrossLinker(linkerName, false);
}

From source file:Tweet_search.Indexer.java

public Indexer() throws IOException, ClassCastException, ClassNotFoundException {

    GetProjetBaseDirAndSetProjectPropFile setPropFile = new GetProjetBaseDirAndSetProjectPropFile();
    prop = setPropFile.prop;/*from   w  ww. java  2s . com*/
    tweet_starts_from_date = Integer.parseInt(prop.getProperty("tweet.starts.from.date", "20"));
    tweet_ends_from_date = Integer.parseInt(prop.getProperty("tweet.ends.from.date", "29"));
    //        
    //        if(prop.getProperty("pos.tag","false").toLowerCase().contentEquals("true"))
    //            postag=new POStag();
    //        if(prop.getProperty("ner.classify","false").toLowerCase().contentEquals("true"))
    //            nertag=new NERtag();

    setAnalyzer();

    /* property files are loaded */

    /* collection path setting */
    if (prop.containsKey("collSpec")) {
        boolIndexFromSpec = true;
    } else if (prop.containsKey("collPath")) {
        boolIndexFromSpec = false;
        collPath = prop.getProperty("collPath");
        collDir = new File(collPath);
        if (!collDir.exists() || !collDir.canRead()) {
            System.err.println("Collection directory '" + collDir.getAbsolutePath()
                    + "' does not exist or is not readable");
            System.exit(1);
        }
    } else {
        System.err.println("Neither collPath not collSpec is present");
        System.exit(1);
    }
    /* collection path set */

    /* index path setting */
    indexFile = new File[tweet_ends_from_date - tweet_starts_from_date + 1];
    Directory[] indexDir = new Directory[tweet_ends_from_date - tweet_starts_from_date + 1];
    indexWriter = new IndexWriter[tweet_ends_from_date - tweet_starts_from_date + 1];

    String indexBasePath = prop.getProperty("indexPath");
    System.err.println(indexBasePath);
    if (indexBasePath.endsWith("/"))
        ;
    else
        indexBasePath = indexBasePath + "/";
    System.err.println(indexBasePath);
    for (int i = tweet_starts_from_date; i <= tweet_ends_from_date; i++) {
        int j = i - tweet_starts_from_date;

        indexFile[j] = new File(indexBasePath.concat(Integer.toString(i)));
        indexDir[j] = FSDirectory.open(indexFile[j]);

        /* index path set */
        if (DirectoryReader.indexExists(indexDir[j])) {
            System.err.println("Index exists in " + indexFile[j].getAbsolutePath());
            boolIndexExists = true;
        } else {
            System.out.println("Will create the index in: " + indexFile[j].getName());
            boolIndexExists = false;
            /* Create a new index in the directory, removing any previous indexed documents */
            IndexWriterConfig[] iwcfg = new IndexWriterConfig[tweet_ends_from_date - tweet_starts_from_date
                    + 1];

            iwcfg[j] = new IndexWriterConfig(Version.LATEST, analyzer);
            iwcfg[j].setOpenMode(IndexWriterConfig.OpenMode.CREATE);

            /*  */
            indexWriter[j] = new IndexWriter(indexDir[j], iwcfg[j]);

        }
    }
    boolDumpIndex = Boolean.parseBoolean(prop.getProperty("dumpIndex", "false"));
    if (boolIndexExists == true && boolDumpIndex == true) {
        dumpPath = prop.getProperty("dumpPath");
    }
}