Search in sources :

Example 41 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class SnapshotRequestsTests method testRestoreSnapshotRequestParsing.

public void testRestoreSnapshotRequestParsing() throws IOException {
    RestoreSnapshotRequest request = new RestoreSnapshotRequest("test-repo", "test-snap");
    XContentBuilder builder = jsonBuilder().startObject();
    if (randomBoolean()) {
        builder.field("indices", "foo,bar,baz");
    } else {
        builder.startArray("indices");
        builder.value("foo");
        builder.value("bar");
        builder.value("baz");
        builder.endArray();
    }
    IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
    if (indicesOptions.expandWildcardsClosed()) {
        if (indicesOptions.expandWildcardsOpen()) {
            builder.field("expand_wildcards", "all");
        } else {
            builder.field("expand_wildcards", "closed");
        }
    } else {
        if (indicesOptions.expandWildcardsOpen()) {
            builder.field("expand_wildcards", "open");
        } else {
            builder.field("expand_wildcards", "none");
        }
    }
    builder.field("allow_no_indices", indicesOptions.allowNoIndices());
    builder.field("rename_pattern", "rename-from");
    builder.field("rename_replacement", "rename-to");
    boolean partial = randomBoolean();
    builder.field("partial", partial);
    builder.startObject("settings").field("set1", "val1").endObject();
    builder.startObject("index_settings").field("set1", "val2").endObject();
    if (randomBoolean()) {
        builder.field("ignore_index_settings", "set2,set3");
    } else {
        builder.startArray("ignore_index_settings");
        builder.value("set2");
        builder.value("set3");
        builder.endArray();
    }
    BytesReference bytes = builder.endObject().bytes();
    request.source(XContentHelper.convertToMap(bytes, true, builder.contentType()).v2());
    assertEquals("test-repo", request.repository());
    assertEquals("test-snap", request.snapshot());
    assertArrayEquals(request.indices(), new String[] { "foo", "bar", "baz" });
    assertEquals("rename-from", request.renamePattern());
    assertEquals("rename-to", request.renameReplacement());
    assertEquals(partial, request.partial());
    assertEquals("val1", request.settings().get("set1"));
    assertArrayEquals(request.ignoreIndexSettings(), new String[] { "set2", "set3" });
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) RestoreSnapshotRequest(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotRequest) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 42 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class SnapshotRequestsTests method testCreateSnapshotRequestParsing.

public void testCreateSnapshotRequestParsing() throws IOException {
    CreateSnapshotRequest request = new CreateSnapshotRequest("test-repo", "test-snap");
    XContentBuilder builder = jsonBuilder().startObject();
    if (randomBoolean()) {
        builder.field("indices", "foo,bar,baz");
    } else {
        builder.startArray("indices");
        builder.value("foo");
        builder.value("bar");
        builder.value("baz");
        builder.endArray();
    }
    IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
    if (indicesOptions.expandWildcardsClosed()) {
        if (indicesOptions.expandWildcardsOpen()) {
            builder.field("expand_wildcards", "all");
        } else {
            builder.field("expand_wildcards", "closed");
        }
    } else {
        if (indicesOptions.expandWildcardsOpen()) {
            builder.field("expand_wildcards", "open");
        } else {
            builder.field("expand_wildcards", "none");
        }
    }
    builder.field("allow_no_indices", indicesOptions.allowNoIndices());
    boolean partial = randomBoolean();
    builder.field("partial", partial);
    builder.startObject("settings").field("set1", "val1").endObject();
    builder.startObject("index_settings").field("set1", "val2").endObject();
    if (randomBoolean()) {
        builder.field("ignore_index_settings", "set2,set3");
    } else {
        builder.startArray("ignore_index_settings");
        builder.value("set2");
        builder.value("set3");
        builder.endArray();
    }
    BytesReference bytes = builder.endObject().bytes();
    request.source(XContentHelper.convertToMap(bytes, true, builder.contentType()).v2());
    assertEquals("test-repo", request.repository());
    assertEquals("test-snap", request.snapshot());
    assertArrayEquals(request.indices(), new String[] { "foo", "bar", "baz" });
    assertEquals(partial, request.partial());
    assertEquals("val1", request.settings().get("set1"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CreateSnapshotRequest(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 43 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class TaskIdTests method roundTrip.

private TaskId roundTrip(TaskId taskId, int expectedSize) throws IOException {
    try (BytesStreamOutput out = new BytesStreamOutput()) {
        taskId.writeTo(out);
        BytesReference bytes = out.bytes();
        assertEquals(expectedSize, bytes.length());
        try (StreamInput in = bytes.streamInput()) {
            return TaskId.readFromStream(in);
        }
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 44 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class TCPTransportTests method testCompressRequest.

public void testCompressRequest() throws IOException {
    final boolean compressed = randomBoolean();
    final AtomicBoolean called = new AtomicBoolean(false);
    Req request = new Req(randomRealisticUnicodeOfLengthBetween(10, 100));
    ThreadPool threadPool = new TestThreadPool(TCPTransportTests.class.getName());
    try {
        TcpTransport transport = new TcpTransport("test", Settings.builder().put("transport.tcp.compress", compressed).build(), threadPool, new BigArrays(Settings.EMPTY, null), null, null, null) {

            @Override
            protected InetSocketAddress getLocalAddress(Object o) {
                return null;
            }

            @Override
            protected Object bind(String name, InetSocketAddress address) throws IOException {
                return null;
            }

            @Override
            protected void closeChannels(List channel) throws IOException {
            }

            @Override
            protected void sendMessage(Object o, BytesReference reference, Runnable sendListener) throws IOException {
                StreamInput streamIn = reference.streamInput();
                streamIn.skip(TcpHeader.MARKER_BYTES_SIZE);
                int len = streamIn.readInt();
                long requestId = streamIn.readLong();
                assertEquals(42, requestId);
                byte status = streamIn.readByte();
                Version version = Version.fromId(streamIn.readInt());
                assertEquals(Version.CURRENT, version);
                assertEquals(compressed, TransportStatus.isCompress(status));
                called.compareAndSet(false, true);
                if (compressed) {
                    final int bytesConsumed = TcpHeader.HEADER_SIZE;
                    streamIn = CompressorFactory.compressor(reference.slice(bytesConsumed, reference.length() - bytesConsumed)).streamInput(streamIn);
                }
                threadPool.getThreadContext().readHeaders(streamIn);
                assertEquals("foobar", streamIn.readString());
                Req readReq = new Req("");
                readReq.readFrom(streamIn);
                assertEquals(request.value, readReq.value);
            }

            @Override
            protected NodeChannels connectToChannels(DiscoveryNode node, ConnectionProfile profile) throws IOException {
                return new NodeChannels(node, new Object[profile.getNumConnections()], profile);
            }

            @Override
            protected boolean isOpen(Object o) {
                return false;
            }

            @Override
            public long serverOpen() {
                return 0;
            }

            @Override
            public NodeChannels getConnection(DiscoveryNode node) {
                return new NodeChannels(node, new Object[MockTcpTransport.LIGHT_PROFILE.getNumConnections()], MockTcpTransport.LIGHT_PROFILE);
            }
        };
        DiscoveryNode node = new DiscoveryNode("foo", buildNewFakeTransportAddress(), Version.CURRENT);
        Transport.Connection connection = transport.getConnection(node);
        connection.sendRequest(42, "foobar", request, TransportRequestOptions.EMPTY);
        assertTrue(called.get());
    } finally {
        ThreadPool.terminate(threadPool, 10, TimeUnit.SECONDS);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) InetSocketAddress(java.net.InetSocketAddress) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BigArrays(org.elasticsearch.common.util.BigArrays) Version(org.elasticsearch.Version) StreamInput(org.elasticsearch.common.io.stream.StreamInput) List(java.util.List)

Example 45 with BytesReference

use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.

the class MustacheTests method testSizeAccessForCollectionsAndArrays.

public void testSizeAccessForCollectionsAndArrays() throws Exception {
    String[] randomArrayValues = generateRandomStringArray(10, 20, false);
    List<String> randomList = Arrays.asList(generateRandomStringArray(10, 20, false));
    String template = "{{data.array.size}} {{data.list.size}}";
    CompiledScript mustache = new CompiledScript(INLINE, "inline", "mustache", engine.compile(null, template, Collections.emptyMap()));
    Map<String, Object> data = new HashMap<>();
    data.put("array", randomArrayValues);
    data.put("list", randomList);
    Map<String, Object> vars = new HashMap<>();
    vars.put("data", data);
    Object output = engine.executable(mustache, vars).run();
    assertThat(output, notNullValue());
    assertThat(output, instanceOf(BytesReference.class));
    BytesReference bytes = (BytesReference) output;
    String expectedString = String.format(Locale.ROOT, "%s %s", randomArrayValues.length, randomList.size());
    assertThat(bytes.utf8ToString(), equalTo(expectedString));
}
Also used : CompiledScript(org.elasticsearch.script.CompiledScript) BytesReference(org.elasticsearch.common.bytes.BytesReference) HashMap(java.util.HashMap) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString)

Aggregations

BytesReference (org.elasticsearch.common.bytes.BytesReference)318 Matchers.containsString (org.hamcrest.Matchers.containsString)72 XContentParser (org.elasticsearch.common.xcontent.XContentParser)63 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)61 IOException (java.io.IOException)58 XContentType (org.elasticsearch.common.xcontent.XContentType)50 BytesArray (org.elasticsearch.common.bytes.BytesArray)47 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)37 ArrayList (java.util.ArrayList)30 HashMap (java.util.HashMap)30 Map (java.util.Map)26 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)26 Test (org.junit.Test)25 List (java.util.List)24 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)24 Version (org.elasticsearch.Version)22 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)20 ReleasableBytesReference (org.elasticsearch.common.bytes.ReleasableBytesReference)19 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)18 BytesRef (org.apache.lucene.util.BytesRef)18