Search in sources :

Example 11 with RAMFile

use of org.apache.lucene.store.RAMFile in project lucene-solr by apache.

the class TestCodecUtil method testWriteTooLongSuffix.

public void testWriteTooLongSuffix() throws Exception {
    StringBuilder tooLong = new StringBuilder();
    for (int i = 0; i < 256; i++) {
        tooLong.append('a');
    }
    RAMFile file = new RAMFile();
    IndexOutput output = new RAMOutputStream(file, true);
    expectThrows(IllegalArgumentException.class, () -> {
        CodecUtil.writeIndexHeader(output, "foobar", 5, StringHelper.randomId(), tooLong.toString());
    });
}
Also used : RAMFile(org.apache.lucene.store.RAMFile) RAMOutputStream(org.apache.lucene.store.RAMOutputStream) IndexOutput(org.apache.lucene.store.IndexOutput)

Example 12 with RAMFile

use of org.apache.lucene.store.RAMFile in project lucene-solr by apache.

the class PrimaryNode method setCurrentInfos.

/** Steals incoming infos refCount; returns true if there were changes. */
private synchronized boolean setCurrentInfos(Set<String> completedMergeFiles) throws IOException {
    IndexSearcher searcher = null;
    SegmentInfos infos;
    try {
        searcher = mgr.acquire();
        infos = ((StandardDirectoryReader) searcher.getIndexReader()).getSegmentInfos();
        // TODO: this is test code specific!!
        message("setCurrentInfos: marker count: " + searcher.count(new TermQuery(new Term("marker", "marker"))) + " version=" + infos.getVersion() + " searcher=" + searcher);
    } finally {
        if (searcher != null) {
            mgr.release(searcher);
        }
    }
    if (curInfos != null && infos.getVersion() == curInfos.getVersion()) {
        // no change
        message("top: skip switch to infos: version=" + infos.getVersion() + " is unchanged: " + infos.toString());
        return false;
    }
    SegmentInfos oldInfos = curInfos;
    writer.incRefDeleter(infos);
    curInfos = infos;
    if (oldInfos != null) {
        writer.decRefDeleter(oldInfos);
    }
    message("top: switch to infos=" + infos.toString() + " version=" + infos.getVersion());
    // Serialize the SegmentInfos:
    RAMOutputStream out = new RAMOutputStream(new RAMFile(), true);
    infos.write(dir, out);
    byte[] infosBytes = new byte[(int) out.getFilePointer()];
    out.writeTo(infosBytes, 0);
    Map<String, FileMetaData> filesMetaData = new HashMap<String, FileMetaData>();
    for (SegmentCommitInfo info : infos) {
        for (String fileName : info.files()) {
            FileMetaData metaData = readLocalFileMetaData(fileName);
            // NOTE: we hold a refCount on this infos, so this file better exist:
            assert metaData != null;
            assert filesMetaData.containsKey(fileName) == false;
            filesMetaData.put(fileName, metaData);
        }
    }
    lastFileMetaData = Collections.unmodifiableMap(filesMetaData);
    message("top: set copyState primaryGen=" + primaryGen + " version=" + infos.getVersion() + " files=" + filesMetaData.keySet());
    copyState = new CopyState(lastFileMetaData, infos.getVersion(), infos.getGeneration(), infosBytes, completedMergeFiles, primaryGen, curInfos);
    return true;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) SegmentInfos(org.apache.lucene.index.SegmentInfos) SegmentCommitInfo(org.apache.lucene.index.SegmentCommitInfo) HashMap(java.util.HashMap) Term(org.apache.lucene.index.Term) RAMFile(org.apache.lucene.store.RAMFile) RAMOutputStream(org.apache.lucene.store.RAMOutputStream)

Example 13 with RAMFile

use of org.apache.lucene.store.RAMFile in project lucene-solr by apache.

the class TestCodecUtil method testWriteBogusCRC.

public void testWriteBogusCRC() throws Exception {
    RAMFile file = new RAMFile();
    final IndexOutput output = new RAMOutputStream(file, false);
    AtomicLong fakeChecksum = new AtomicLong();
    // wrap the index input where we control the checksum for mocking
    IndexOutput fakeOutput = new IndexOutput("fake", "fake") {

        @Override
        public void close() throws IOException {
            output.close();
        }

        @Override
        public long getFilePointer() {
            return output.getFilePointer();
        }

        @Override
        public long getChecksum() throws IOException {
            return fakeChecksum.get();
        }

        @Override
        public void writeByte(byte b) throws IOException {
            output.writeByte(b);
        }

        @Override
        public void writeBytes(byte[] b, int offset, int length) throws IOException {
            output.writeBytes(b, offset, length);
        }
    };
    // bad
    fakeChecksum.set(-1L);
    expectThrows(IllegalStateException.class, () -> {
        CodecUtil.writeCRC(fakeOutput);
    });
    // bad
    fakeChecksum.set(1L << 32);
    expectThrows(IllegalStateException.class, () -> {
        CodecUtil.writeCRC(fakeOutput);
    });
    // bad
    fakeChecksum.set(-(1L << 32));
    expectThrows(IllegalStateException.class, () -> {
        CodecUtil.writeCRC(fakeOutput);
    });
    // ok
    fakeChecksum.set((1L << 32) - 1);
    CodecUtil.writeCRC(fakeOutput);
}
Also used : RAMFile(org.apache.lucene.store.RAMFile) AtomicLong(java.util.concurrent.atomic.AtomicLong) RAMOutputStream(org.apache.lucene.store.RAMOutputStream) IndexOutput(org.apache.lucene.store.IndexOutput)

Example 14 with RAMFile

use of org.apache.lucene.store.RAMFile in project lucene-solr by apache.

the class TestCodecUtil method testHeaderLength.

public void testHeaderLength() throws Exception {
    RAMFile file = new RAMFile();
    IndexOutput output = new RAMOutputStream(file, true);
    CodecUtil.writeHeader(output, "FooBar", 5);
    output.writeString("this is the data");
    output.close();
    IndexInput input = new RAMInputStream("file", file);
    input.seek(CodecUtil.headerLength("FooBar"));
    assertEquals("this is the data", input.readString());
    input.close();
}
Also used : RAMFile(org.apache.lucene.store.RAMFile) RAMInputStream(org.apache.lucene.store.RAMInputStream) RAMOutputStream(org.apache.lucene.store.RAMOutputStream) IndexInput(org.apache.lucene.store.IndexInput) ChecksumIndexInput(org.apache.lucene.store.ChecksumIndexInput) BufferedChecksumIndexInput(org.apache.lucene.store.BufferedChecksumIndexInput) IndexOutput(org.apache.lucene.store.IndexOutput)

Example 15 with RAMFile

use of org.apache.lucene.store.RAMFile in project lucene-solr by apache.

the class TestCodecUtil method testTruncatedFileThrowsCorruptIndexException.

public void testTruncatedFileThrowsCorruptIndexException() throws IOException {
    RAMFile file = new RAMFile();
    IndexOutput output = new RAMOutputStream(file, false);
    output.close();
    IndexInput input = new RAMInputStream("file", file);
    CorruptIndexException e = expectThrows(CorruptIndexException.class, () -> CodecUtil.checksumEntireFile(input));
    assertEquals("misplaced codec footer (file truncated?): length=0 but footerLength==16 (resource=RAMInputStream(name=file))", e.getMessage());
    e = expectThrows(CorruptIndexException.class, () -> CodecUtil.retrieveChecksum(input));
    assertEquals("misplaced codec footer (file truncated?): length=0 but footerLength==16 (resource=RAMInputStream(name=file))", e.getMessage());
}
Also used : RAMFile(org.apache.lucene.store.RAMFile) RAMInputStream(org.apache.lucene.store.RAMInputStream) RAMOutputStream(org.apache.lucene.store.RAMOutputStream) IndexInput(org.apache.lucene.store.IndexInput) ChecksumIndexInput(org.apache.lucene.store.ChecksumIndexInput) BufferedChecksumIndexInput(org.apache.lucene.store.BufferedChecksumIndexInput) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) IndexOutput(org.apache.lucene.store.IndexOutput)

Aggregations

RAMFile (org.apache.lucene.store.RAMFile)21 RAMOutputStream (org.apache.lucene.store.RAMOutputStream)21 IndexOutput (org.apache.lucene.store.IndexOutput)16 RAMInputStream (org.apache.lucene.store.RAMInputStream)15 BufferedChecksumIndexInput (org.apache.lucene.store.BufferedChecksumIndexInput)11 ChecksumIndexInput (org.apache.lucene.store.ChecksumIndexInput)11 IndexInput (org.apache.lucene.store.IndexInput)7 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)5 IOException (java.io.IOException)4 Document (org.apache.lucene.document.Document)4 DirectoryReader (org.apache.lucene.index.DirectoryReader)4 IndexWriter (org.apache.lucene.index.IndexWriter)4 LeafReader (org.apache.lucene.index.LeafReader)4 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)4 Directory (org.apache.lucene.store.Directory)4 TreeSet (java.util.TreeSet)2 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)2 SortedSetDocValuesField (org.apache.lucene.document.SortedSetDocValuesField)2 SortedNumericDocValues (org.apache.lucene.index.SortedNumericDocValues)2 SortedSetDocValues (org.apache.lucene.index.SortedSetDocValues)2