Search in sources :

Example 1 with ByteBufferStreamInput

use of org.opensearch.common.io.stream.ByteBufferStreamInput in project OpenSearch by opensearch-project.

the class PrimaryReplicaSyncerTests method testStatusSerialization.

public void testStatusSerialization() throws IOException {
    PrimaryReplicaSyncer.ResyncTask.Status status = new PrimaryReplicaSyncer.ResyncTask.Status(randomAlphaOfLength(10), randomIntBetween(0, 1000), randomIntBetween(0, 1000), randomIntBetween(0, 1000));
    final BytesStreamOutput out = new BytesStreamOutput();
    status.writeTo(out);
    final ByteBufferStreamInput in = new ByteBufferStreamInput(ByteBuffer.wrap(out.bytes().toBytesRef().bytes));
    PrimaryReplicaSyncer.ResyncTask.Status serializedStatus = new PrimaryReplicaSyncer.ResyncTask.Status(in);
    assertEquals(status, serializedStatus);
}
Also used : ByteBufferStreamInput(org.opensearch.common.io.stream.ByteBufferStreamInput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 2 with ByteBufferStreamInput

use of org.opensearch.common.io.stream.ByteBufferStreamInput in project OpenSearch by opensearch-project.

the class ActiveShardCountTests method doWriteRead.

private void doWriteRead(ActiveShardCount activeShardCount) throws IOException {
    final BytesStreamOutput out = new BytesStreamOutput();
    activeShardCount.writeTo(out);
    final ByteBufferStreamInput in = new ByteBufferStreamInput(ByteBuffer.wrap(out.bytes().toBytesRef().bytes));
    ActiveShardCount readActiveShardCount = ActiveShardCount.readFrom(in);
    if (activeShardCount == ActiveShardCount.DEFAULT || activeShardCount == ActiveShardCount.ALL || activeShardCount == ActiveShardCount.NONE) {
        assertSame(activeShardCount, readActiveShardCount);
    } else {
        assertEquals(activeShardCount, readActiveShardCount);
    }
}
Also used : ByteBufferStreamInput(org.opensearch.common.io.stream.ByteBufferStreamInput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 3 with ByteBufferStreamInput

use of org.opensearch.common.io.stream.ByteBufferStreamInput in project OpenSearch by opensearch-project.

the class SearchContextId method decode.

public static SearchContextId decode(NamedWriteableRegistry namedWriteableRegistry, String id) {
    final ByteBuffer byteBuffer;
    try {
        byteBuffer = ByteBuffer.wrap(Base64.getUrlDecoder().decode(id));
    } catch (Exception e) {
        throw new IllegalArgumentException("invalid id: [" + id + "]", e);
    }
    try (StreamInput in = new NamedWriteableAwareStreamInput(new ByteBufferStreamInput(byteBuffer), namedWriteableRegistry)) {
        final Version version = Version.readVersion(in);
        in.setVersion(version);
        final Map<ShardId, SearchContextIdForNode> shards = in.readMap(ShardId::new, SearchContextIdForNode::new);
        final Map<String, AliasFilter> aliasFilters = in.readMap(StreamInput::readString, AliasFilter::new);
        if (in.available() > 0) {
            throw new IllegalArgumentException("Not all bytes were read");
        }
        return new SearchContextId(Collections.unmodifiableMap(shards), Collections.unmodifiableMap(aliasFilters));
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : AliasFilter(org.opensearch.search.internal.AliasFilter) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) ShardId(org.opensearch.index.shard.ShardId) ByteBufferStreamInput(org.opensearch.common.io.stream.ByteBufferStreamInput) Version(org.opensearch.Version) StreamInput(org.opensearch.common.io.stream.StreamInput) ByteBufferStreamInput(org.opensearch.common.io.stream.ByteBufferStreamInput) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput)

Example 4 with ByteBufferStreamInput

use of org.opensearch.common.io.stream.ByteBufferStreamInput in project OpenSearch by opensearch-project.

the class UnassignedInfoTests method testAllocationStatusSerialization.

public void testAllocationStatusSerialization() throws IOException {
    for (AllocationStatus allocationStatus : AllocationStatus.values()) {
        BytesStreamOutput out = new BytesStreamOutput();
        allocationStatus.writeTo(out);
        ByteBufferStreamInput in = new ByteBufferStreamInput(ByteBuffer.wrap(out.bytes().toBytesRef().bytes));
        AllocationStatus readStatus = AllocationStatus.readFrom(in);
        assertThat(readStatus, equalTo(allocationStatus));
    }
}
Also used : ByteBufferStreamInput(org.opensearch.common.io.stream.ByteBufferStreamInput) AllocationStatus(org.opensearch.cluster.routing.UnassignedInfo.AllocationStatus) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 5 with ByteBufferStreamInput

use of org.opensearch.common.io.stream.ByteBufferStreamInput in project OpenSearch by opensearch-project.

the class BaseTranslogReader method checksummedStream.

/**
 * reads an operation at the given position and returns it. The buffer length is equal to the number
 * of bytes reads.
 */
protected final BufferedChecksumStreamInput checksummedStream(ByteBuffer reusableBuffer, long position, int opSize, BufferedChecksumStreamInput reuse) throws IOException {
    final ByteBuffer buffer;
    if (reusableBuffer.capacity() >= opSize) {
        buffer = reusableBuffer;
    } else {
        buffer = ByteBuffer.allocate(opSize);
    }
    buffer.clear();
    buffer.limit(opSize);
    readBytes(buffer, position);
    buffer.flip();
    return new BufferedChecksumStreamInput(new ByteBufferStreamInput(buffer), path.toString(), reuse);
}
Also used : ByteBufferStreamInput(org.opensearch.common.io.stream.ByteBufferStreamInput) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBufferStreamInput (org.opensearch.common.io.stream.ByteBufferStreamInput)6 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)4 ByteBuffer (java.nio.ByteBuffer)3 IOException (java.io.IOException)1 Version (org.opensearch.Version)1 AllocationStatus (org.opensearch.cluster.routing.UnassignedInfo.AllocationStatus)1 NamedWriteableAwareStreamInput (org.opensearch.common.io.stream.NamedWriteableAwareStreamInput)1 StreamInput (org.opensearch.common.io.stream.StreamInput)1 ShardId (org.opensearch.index.shard.ShardId)1 AliasFilter (org.opensearch.search.internal.AliasFilter)1