Search in sources :

Example 1 with SegmentReader

use of org.apache.lucene.index.SegmentReader in project elasticsearch by elastic.

the class CodecTests method assertCompressionEquals.

// write some docs with it, inspect .si to see this was the used compression
private void assertCompressionEquals(Mode expected, Codec actual) throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(null);
    iwc.setCodec(actual);
    IndexWriter iw = new IndexWriter(dir, iwc);
    iw.addDocument(new Document());
    iw.commit();
    iw.close();
    DirectoryReader ir = DirectoryReader.open(dir);
    SegmentReader sr = (SegmentReader) ir.leaves().get(0).reader();
    String v = sr.getSegmentInfo().info.getAttribute(Lucene50StoredFieldsFormat.MODE_KEY);
    assertNotNull(v);
    assertEquals(expected, Mode.valueOf(v));
    ir.close();
    dir.close();
}
Also used : IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) Document(org.apache.lucene.document.Document) SegmentReader(org.apache.lucene.index.SegmentReader) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 2 with SegmentReader

use of org.apache.lucene.index.SegmentReader in project elasticsearch by elastic.

the class Engine method getSegmentInfo.

protected Segment[] getSegmentInfo(SegmentInfos lastCommittedSegmentInfos, boolean verbose) {
    ensureOpen();
    Map<String, Segment> segments = new HashMap<>();
    // first, go over and compute the search ones...
    Searcher searcher = acquireSearcher("segments");
    try {
        for (LeafReaderContext reader : searcher.reader().leaves()) {
            SegmentCommitInfo info = segmentReader(reader.reader()).getSegmentInfo();
            assert !segments.containsKey(info.info.name);
            Segment segment = new Segment(info.info.name);
            segment.search = true;
            segment.docCount = reader.reader().numDocs();
            segment.delDocCount = reader.reader().numDeletedDocs();
            segment.version = info.info.getVersion();
            segment.compound = info.info.getUseCompoundFile();
            try {
                segment.sizeInBytes = info.sizeInBytes();
            } catch (IOException e) {
                logger.trace((Supplier<?>) () -> new ParameterizedMessage("failed to get size for [{}]", info.info.name), e);
            }
            final SegmentReader segmentReader = segmentReader(reader.reader());
            segment.memoryInBytes = segmentReader.ramBytesUsed();
            if (verbose) {
                segment.ramTree = Accountables.namedAccountable("root", segmentReader);
            }
            // TODO: add more fine grained mem stats values to per segment info here
            segments.put(info.info.name, segment);
        }
    } finally {
        searcher.close();
    }
    // now, correlate or add the committed ones...
    if (lastCommittedSegmentInfos != null) {
        SegmentInfos infos = lastCommittedSegmentInfos;
        for (SegmentCommitInfo info : infos) {
            Segment segment = segments.get(info.info.name);
            if (segment == null) {
                segment = new Segment(info.info.name);
                segment.search = false;
                segment.committed = true;
                segment.docCount = info.info.maxDoc();
                segment.delDocCount = info.getDelCount();
                segment.version = info.info.getVersion();
                segment.compound = info.info.getUseCompoundFile();
                try {
                    segment.sizeInBytes = info.sizeInBytes();
                } catch (IOException e) {
                    logger.trace((Supplier<?>) () -> new ParameterizedMessage("failed to get size for [{}]", info.info.name), e);
                }
                segments.put(info.info.name, segment);
            } else {
                segment.committed = true;
            }
        }
    }
    Segment[] segmentsArr = segments.values().toArray(new Segment[segments.values().size()]);
    Arrays.sort(segmentsArr, new Comparator<Segment>() {

        @Override
        public int compare(Segment o1, Segment o2) {
            return (int) (o1.getGeneration() - o2.getGeneration());
        }
    });
    return segmentsArr;
}
Also used : SegmentInfos(org.apache.lucene.index.SegmentInfos) SegmentCommitInfo(org.apache.lucene.index.SegmentCommitInfo) HashMap(java.util.HashMap) IndexSearcher(org.apache.lucene.search.IndexSearcher) IOException(java.io.IOException) SegmentReader(org.apache.lucene.index.SegmentReader) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

Example 3 with SegmentReader

use of org.apache.lucene.index.SegmentReader in project lucene-solr by apache.

the class AnalyzingInfixSuggester method ramBytesUsed.

@Override
public long ramBytesUsed() {
    long mem = RamUsageEstimator.shallowSizeOf(this);
    try {
        if (searcherMgr != null) {
            SearcherManager mgr;
            IndexSearcher searcher;
            synchronized (searcherMgrLock) {
                // acquire & release on same SearcherManager, via local reference
                mgr = searcherMgr;
                searcher = mgr.acquire();
            }
            try {
                for (LeafReaderContext context : searcher.getIndexReader().leaves()) {
                    LeafReader reader = FilterLeafReader.unwrap(context.reader());
                    if (reader instanceof SegmentReader) {
                        mem += ((SegmentReader) context.reader()).ramBytesUsed();
                    }
                }
            } finally {
                mgr.release(searcher);
            }
        }
        return mem;
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) LeafReader(org.apache.lucene.index.LeafReader) FilterLeafReader(org.apache.lucene.index.FilterLeafReader) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) SearcherManager(org.apache.lucene.search.SearcherManager) IOException(java.io.IOException) SegmentReader(org.apache.lucene.index.SegmentReader)

Example 4 with SegmentReader

use of org.apache.lucene.index.SegmentReader in project lucene-solr by apache.

the class AnalyzingInfixSuggester method getChildResources.

@Override
public Collection<Accountable> getChildResources() {
    List<Accountable> resources = new ArrayList<>();
    try {
        if (searcherMgr != null) {
            SearcherManager mgr;
            IndexSearcher searcher;
            synchronized (searcherMgrLock) {
                // acquire & release on same SearcherManager, via local reference
                mgr = searcherMgr;
                searcher = mgr.acquire();
            }
            try {
                for (LeafReaderContext context : searcher.getIndexReader().leaves()) {
                    LeafReader reader = FilterLeafReader.unwrap(context.reader());
                    if (reader instanceof SegmentReader) {
                        resources.add(Accountables.namedAccountable("segment", (SegmentReader) reader));
                    }
                }
            } finally {
                mgr.release(searcher);
            }
        }
        return Collections.unmodifiableList(resources);
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) LeafReader(org.apache.lucene.index.LeafReader) FilterLeafReader(org.apache.lucene.index.FilterLeafReader) ArrayList(java.util.ArrayList) Accountable(org.apache.lucene.util.Accountable) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) SearcherManager(org.apache.lucene.search.SearcherManager) IOException(java.io.IOException) SegmentReader(org.apache.lucene.index.SegmentReader)

Example 5 with SegmentReader

use of org.apache.lucene.index.SegmentReader in project lucene-solr by apache.

the class PreCopyMergedSegmentWarmer method warm.

@Override
public void warm(LeafReader reader) throws IOException {
    long startNS = System.nanoTime();
    final SegmentCommitInfo info = ((SegmentReader) reader).getSegmentInfo();
    //System.out.println("TEST: warm merged segment files " + info);
    Map<String, FileMetaData> filesMetaData = new HashMap<>();
    for (String fileName : info.files()) {
        FileMetaData metaData = primary.readLocalFileMetaData(fileName);
        assert metaData != null;
        assert filesMetaData.containsKey(fileName) == false;
        filesMetaData.put(fileName, metaData);
    }
    primary.preCopyMergedSegmentFiles(info, filesMetaData);
    primary.message(String.format(Locale.ROOT, "top: done warm merge " + info + ": took %.3f sec, %.1f MB", (System.nanoTime() - startNS) / 1000000000., info.sizeInBytes() / 1024 / 1024.));
    primary.finishedMergedFiles.addAll(filesMetaData.keySet());
}
Also used : SegmentCommitInfo(org.apache.lucene.index.SegmentCommitInfo) HashMap(java.util.HashMap) SegmentReader(org.apache.lucene.index.SegmentReader)

Aggregations

SegmentReader (org.apache.lucene.index.SegmentReader)8 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)5 IndexSearcher (org.apache.lucene.search.IndexSearcher)4 IOException (java.io.IOException)3 FilterLeafReader (org.apache.lucene.index.FilterLeafReader)3 LeafReader (org.apache.lucene.index.LeafReader)3 HashMap (java.util.HashMap)2 SegmentCommitInfo (org.apache.lucene.index.SegmentCommitInfo)2 SearcherManager (org.apache.lucene.search.SearcherManager)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 Supplier (org.apache.logging.log4j.util.Supplier)1 Document (org.apache.lucene.document.Document)1 CodecReader (org.apache.lucene.index.CodecReader)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 SegmentInfos (org.apache.lucene.index.SegmentInfos)1