Example usage for org.apache.lucene.store RAMDirectory sizeInBytes

List of usage examples for org.apache.lucene.store RAMDirectory sizeInBytes

Introduction

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

Prototype

AtomicLong sizeInBytes

To view the source code for org.apache.lucene.store RAMDirectory sizeInBytes.

Click Source Link

Usage

From source file:org.xcmis.search.lucene.index.InMemoryIndexDataKeeperFactory.java

License:Open Source License

/**
 * {@inheritDoc}//from ww w .  j  a v  a 2 s  .  co m
 */

public LuceneIndexDataManager merge(final Collection<LuceneIndexDataManager> chains) throws IndexException {
    final List<TransactionLog> transactionsLogs = new ArrayList<TransactionLog>();
    final List<Directory> mergeDirectorys = new ArrayList<Directory>();
    final Map<String, Document> documentsBuffer = new HashMap<String, Document>();
    final Map<String, Document> pendingBuffer = new HashMap<String, Document>();

    for (final IndexDataKeeper<Document> indexDataKeeper : chains) {
        final ReducibleInMemoryIndexDataKeeper reducibleInMemoryIndexDataKeeper = (ReducibleInMemoryIndexDataKeeper) indexDataKeeper;

        if (reducibleInMemoryIndexDataKeeper.getDocumentCount() > 0) {

            final RAMDirectory directory = (RAMDirectory) reducibleInMemoryIndexDataKeeper.getDirectory();
            if (directory.sizeInBytes() > 0) {
                mergeDirectorys.add(directory);
            }
            pendingBuffer.putAll(reducibleInMemoryIndexDataKeeper.getPendingDocumentsBuffer());
            documentsBuffer.putAll(reducibleInMemoryIndexDataKeeper.getDocumentsBuffer());
            transactionsLogs.add(reducibleInMemoryIndexDataKeeper.getTransactionLog());
        }
    }
    LuceneIndexDataManager reducibleInMemoryIndexDataKeeper = null;
    try {
        RAMDirectory newDirectory = null;

        if (mergeDirectorys.size() > 0) {

            newDirectory = new RAMDirectory();
            final IndexWriter newWriter = new IndexWriter(newDirectory, new StandardAnalyzer(),
                    MaxFieldLength.UNLIMITED);
            final Directory[] dirsToMerge = new Directory[mergeDirectorys.size()];
            newWriter.addIndexesNoOptimize(mergeDirectorys.toArray(dirsToMerge));
            newWriter.optimize();
            newWriter.close();

            //
        } else {
            newDirectory = new RAMDirectory();
        }
        reducibleInMemoryIndexDataKeeper = new ReducibleInMemoryIndexDataKeeper(newDirectory, documentsBuffer,
                pendingBuffer, new CompositeTransactionLog(transactionsLogs));

    } catch (final IOException e) {
        throw new IndexException(e.getLocalizedMessage(), e);
    } catch (final TransactionLogException e) {
        throw new IndexException(e.getLocalizedMessage(), e);
    }

    return reducibleInMemoryIndexDataKeeper;
}