Search in sources :

Example 6 with RAMOutputStream

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

the class TestCodecUtil method testWriteNonAsciiSuffix.

public void testWriteNonAsciiSuffix() throws Exception {
    RAMFile file = new RAMFile();
    IndexOutput output = new RAMOutputStream(file, true);
    expectThrows(IllegalArgumentException.class, () -> {
        CodecUtil.writeIndexHeader(output, "foobar", 5, StringHelper.randomId(), "ሴ");
    });
}
Also used : RAMFile(org.apache.lucene.store.RAMFile) RAMOutputStream(org.apache.lucene.store.RAMOutputStream) IndexOutput(org.apache.lucene.store.IndexOutput)

Example 7 with RAMOutputStream

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

the class TestCodecUtil method testWriteVeryLongSuffix.

public void testWriteVeryLongSuffix() throws Exception {
    StringBuilder justLongEnough = new StringBuilder();
    for (int i = 0; i < 255; i++) {
        justLongEnough.append('a');
    }
    RAMFile file = new RAMFile();
    IndexOutput output = new RAMOutputStream(file, true);
    byte[] id = StringHelper.randomId();
    CodecUtil.writeIndexHeader(output, "foobar", 5, id, justLongEnough.toString());
    output.close();
    IndexInput input = new RAMInputStream("file", file);
    CodecUtil.checkIndexHeader(input, "foobar", 5, 5, id, justLongEnough.toString());
    assertEquals(input.getFilePointer(), input.length());
    assertEquals(input.getFilePointer(), CodecUtil.indexHeaderLength("foobar", justLongEnough.toString()));
    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 8 with RAMOutputStream

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

the class TestCodecUtil method testReadBogusCRC.

public void testReadBogusCRC() throws Exception {
    RAMFile file = new RAMFile();
    IndexOutput output = new RAMOutputStream(file, false);
    // bad
    output.writeLong(-1L);
    // bad
    output.writeLong(1L << 32);
    // bad
    output.writeLong(-(1L << 32));
    // ok
    output.writeLong((1L << 32) - 1);
    output.close();
    IndexInput input = new RAMInputStream("file", file);
    // read 3 bogus values
    for (int i = 0; i < 3; i++) {
        expectThrows(CorruptIndexException.class, () -> {
            CodecUtil.readCRC(input);
        });
    }
    // good value
    CodecUtil.readCRC(input);
}
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 9 with RAMOutputStream

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

the class TestCodecUtil method testCheckFooterValidPastFooter.

public void testCheckFooterValidPastFooter() throws Exception {
    RAMFile file = new RAMFile();
    IndexOutput output = new RAMOutputStream(file, true);
    CodecUtil.writeHeader(output, "FooBar", 5);
    output.writeString("this is the data");
    CodecUtil.writeFooter(output);
    output.close();
    ChecksumIndexInput input = new BufferedChecksumIndexInput(new RAMInputStream("file", file));
    CodecUtil.checkHeader(input, "FooBar", 5, 5);
    assertEquals("this is the data", input.readString());
    // bogusly read a byte too far (can happen)
    input.readByte();
    Exception mine = new RuntimeException("fake exception");
    RuntimeException expected = expectThrows(RuntimeException.class, () -> {
        CodecUtil.checkFooter(input, mine);
    });
    assertEquals("fake exception", expected.getMessage());
    Throwable[] suppressed = expected.getSuppressed();
    assertEquals(1, suppressed.length);
    assertTrue(suppressed[0].getMessage().contains("checksum status indeterminate"));
    input.close();
}
Also used : RAMFile(org.apache.lucene.store.RAMFile) ChecksumIndexInput(org.apache.lucene.store.ChecksumIndexInput) BufferedChecksumIndexInput(org.apache.lucene.store.BufferedChecksumIndexInput) RAMInputStream(org.apache.lucene.store.RAMInputStream) BufferedChecksumIndexInput(org.apache.lucene.store.BufferedChecksumIndexInput) RAMOutputStream(org.apache.lucene.store.RAMOutputStream) IndexOutput(org.apache.lucene.store.IndexOutput) IOException(java.io.IOException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException)

Example 10 with RAMOutputStream

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

the class FST method save.

public void save(DataOutput out) throws IOException {
    if (startNode == -1) {
        throw new IllegalStateException("call finish first");
    }
    CodecUtil.writeHeader(out, FILE_FORMAT_NAME, VERSION_CURRENT);
    // to the root node, instead of special casing here:
    if (emptyOutput != null) {
        // Accepts empty string
        out.writeByte((byte) 1);
        // Serialize empty-string output:
        RAMOutputStream ros = new RAMOutputStream();
        outputs.writeFinalOutput(emptyOutput, ros);
        byte[] emptyOutputBytes = new byte[(int) ros.getFilePointer()];
        ros.writeTo(emptyOutputBytes, 0);
        // reverse
        final int stopAt = emptyOutputBytes.length / 2;
        int upto = 0;
        while (upto < stopAt) {
            final byte b = emptyOutputBytes[upto];
            emptyOutputBytes[upto] = emptyOutputBytes[emptyOutputBytes.length - upto - 1];
            emptyOutputBytes[emptyOutputBytes.length - upto - 1] = b;
            upto++;
        }
        out.writeVInt(emptyOutputBytes.length);
        out.writeBytes(emptyOutputBytes, 0, emptyOutputBytes.length);
    } else {
        out.writeByte((byte) 0);
    }
    final byte t;
    if (inputType == INPUT_TYPE.BYTE1) {
        t = 0;
    } else if (inputType == INPUT_TYPE.BYTE2) {
        t = 1;
    } else {
        t = 2;
    }
    out.writeByte(t);
    out.writeVLong(startNode);
    if (bytes != null) {
        long numBytes = bytes.getPosition();
        out.writeVLong(numBytes);
        bytes.writeTo(out);
    } else {
        assert bytesArray != null;
        out.writeVLong(bytesArray.length);
        out.writeBytes(bytesArray, 0, bytesArray.length);
    }
}
Also used : RAMOutputStream(org.apache.lucene.store.RAMOutputStream)

Aggregations

RAMOutputStream (org.apache.lucene.store.RAMOutputStream)27 RAMFile (org.apache.lucene.store.RAMFile)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 BytesRef (org.apache.lucene.util.BytesRef)5 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)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