Search in sources :

Example 1 with OutputStreamDataOutput

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

the class TestSuggestField method testTokenStream.

@Test
public void testTokenStream() throws Exception {
    Analyzer analyzer = new MockAnalyzer(random());
    SuggestField suggestField = new SuggestField("field", "input", 1);
    BytesRef surfaceForm = new BytesRef("input");
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try (OutputStreamDataOutput output = new OutputStreamDataOutput(byteArrayOutputStream)) {
        output.writeVInt(surfaceForm.length);
        output.writeBytes(surfaceForm.bytes, surfaceForm.offset, surfaceForm.length);
        output.writeVInt(1 + 1);
        output.writeByte(SuggestField.TYPE);
    }
    BytesRef payload = new BytesRef(byteArrayOutputStream.toByteArray());
    TokenStream stream = new CompletionTokenStreamTest.PayloadAttrToTypeAttrFilter(suggestField.tokenStream(analyzer, null));
    assertTokenStreamContents(stream, new String[] { "input" }, null, null, new String[] { payload.utf8ToString() }, new int[] { 1 }, null, null);
    CompletionAnalyzer completionAnalyzer = new CompletionAnalyzer(analyzer);
    stream = new CompletionTokenStreamTest.PayloadAttrToTypeAttrFilter(suggestField.tokenStream(completionAnalyzer, null));
    assertTokenStreamContents(stream, new String[] { "input" }, null, null, new String[] { payload.utf8ToString() }, new int[] { 1 }, null, null);
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) OutputStreamDataOutput(org.apache.lucene.store.OutputStreamDataOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test)

Example 2 with OutputStreamDataOutput

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

the class SuggestField method buildSuggestPayload.

private BytesRef buildSuggestPayload() {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try (OutputStreamDataOutput output = new OutputStreamDataOutput(byteArrayOutputStream)) {
        output.writeVInt(surfaceForm.length);
        output.writeBytes(surfaceForm.bytes, surfaceForm.offset, surfaceForm.length);
        output.writeVInt(weight + 1);
        output.writeByte(type());
    } catch (IOException e) {
        // not possible, it's a ByteArrayOutputStream!
        throw new RuntimeException(e);
    }
    return new BytesRef(byteArrayOutputStream.toByteArray());
}
Also used : OutputStreamDataOutput(org.apache.lucene.store.OutputStreamDataOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BytesRef(org.apache.lucene.util.BytesRef)

Example 3 with OutputStreamDataOutput

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

the class BinaryDictionaryWriter method writeTargetMap.

// TODO: maybe this int[] should instead be the output to the FST...
protected void writeTargetMap(String filename) throws IOException {
    new File(filename).getParentFile().mkdirs();
    OutputStream os = new FileOutputStream(filename);
    try {
        os = new BufferedOutputStream(os);
        final DataOutput out = new OutputStreamDataOutput(os);
        CodecUtil.writeHeader(out, BinaryDictionary.TARGETMAP_HEADER, BinaryDictionary.VERSION);
        final int numSourceIds = lastSourceId + 1;
        // <-- size of main array
        out.writeVInt(targetMapEndOffset);
        // <-- size of offset array (+ 1 more entry)
        out.writeVInt(numSourceIds + 1);
        int prev = 0, sourceId = 0;
        for (int ofs = 0; ofs < targetMapEndOffset; ofs++) {
            final int val = targetMap[ofs], delta = val - prev;
            assert delta >= 0;
            if (ofs == targetMapOffsets[sourceId]) {
                out.writeVInt((delta << 1) | 0x01);
                sourceId++;
            } else {
                out.writeVInt((delta << 1));
            }
            prev += delta;
        }
        assert sourceId == numSourceIds : "sourceId:" + sourceId + " != numSourceIds:" + numSourceIds;
    } finally {
        os.close();
    }
}
Also used : DataOutput(org.apache.lucene.store.DataOutput) OutputStreamDataOutput(org.apache.lucene.store.OutputStreamDataOutput) OutputStreamDataOutput(org.apache.lucene.store.OutputStreamDataOutput) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 4 with OutputStreamDataOutput

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

the class CharacterDefinitionWriter method write.

public void write(String baseDir) throws IOException {
    String filename = baseDir + File.separator + CharacterDefinition.class.getName().replace('.', File.separatorChar) + CharacterDefinition.FILENAME_SUFFIX;
    new File(filename).getParentFile().mkdirs();
    OutputStream os = new FileOutputStream(filename);
    try {
        os = new BufferedOutputStream(os);
        final DataOutput out = new OutputStreamDataOutput(os);
        CodecUtil.writeHeader(out, CharacterDefinition.HEADER, CharacterDefinition.VERSION);
        out.writeBytes(characterCategoryMap, 0, characterCategoryMap.length);
        for (int i = 0; i < CharacterDefinition.CLASS_COUNT; i++) {
            final byte b = (byte) ((invokeMap[i] ? 0x01 : 0x00) | (groupMap[i] ? 0x02 : 0x00));
            out.writeByte(b);
        }
    } finally {
        os.close();
    }
}
Also used : CharacterDefinition(org.apache.lucene.analysis.ja.dict.CharacterDefinition) OutputStreamDataOutput(org.apache.lucene.store.OutputStreamDataOutput) DataOutput(org.apache.lucene.store.DataOutput) OutputStreamDataOutput(org.apache.lucene.store.OutputStreamDataOutput) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 5 with OutputStreamDataOutput

use of org.apache.lucene.store.OutputStreamDataOutput in project crate by crate.

the class TranslogHeader method write.

/**
 * Writes this header with the latest format into the file channel
 */
void write(final FileChannel channel) throws IOException {
    // This output is intentionally not closed because closing it will close the FileChannel.
    @SuppressWarnings({ "IOResourceOpenedButNotSafelyClosed", "resource" }) final BufferedChecksumStreamOutput out = new BufferedChecksumStreamOutput(new OutputStreamStreamOutput(java.nio.channels.Channels.newOutputStream(channel)));
    CodecUtil.writeHeader(new OutputStreamDataOutput(out), TRANSLOG_CODEC, CURRENT_VERSION);
    // Write uuid
    final BytesRef uuid = new BytesRef(translogUUID);
    out.writeInt(uuid.length);
    out.writeBytes(uuid.bytes, uuid.offset, uuid.length);
    // Write primary term
    out.writeLong(primaryTerm);
    // Checksum header
    out.writeInt((int) out.getChecksum());
    out.flush();
    channel.force(true);
    assert channel.position() == headerSizeInBytes : "Header is not fully written; header size [" + headerSizeInBytes + "], channel position [" + channel.position() + "]";
}
Also used : OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) OutputStreamDataOutput(org.apache.lucene.store.OutputStreamDataOutput) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

OutputStreamDataOutput (org.apache.lucene.store.OutputStreamDataOutput)12 DataOutput (org.apache.lucene.store.DataOutput)6 BytesRef (org.apache.lucene.util.BytesRef)6 File (java.io.File)5 FileOutputStream (java.io.FileOutputStream)5 BufferedOutputStream (java.io.BufferedOutputStream)4 OutputStream (java.io.OutputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IOException (java.io.IOException)2 FileChannel (java.nio.channels.FileChannel)2 Analyzer (org.apache.lucene.analysis.Analyzer)2 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)2 TokenStream (org.apache.lucene.analysis.TokenStream)2 Test (org.junit.Test)2 WritableByteChannel (java.nio.channels.WritableByteChannel)1 CharacterDefinition (org.apache.lucene.analysis.ja.dict.CharacterDefinition)1 ConnectionCosts (org.apache.lucene.analysis.ja.dict.ConnectionCosts)1 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)1 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)1 ByteArrayDataOutput (org.apache.lucene.store.ByteArrayDataOutput)1