Example usage for io.netty.util CharsetUtil UTF_8

List of usage examples for io.netty.util CharsetUtil UTF_8

Introduction

In this page you can find the example usage for io.netty.util CharsetUtil UTF_8.

Prototype

Charset UTF_8

To view the source code for io.netty.util CharsetUtil UTF_8.

Click Source Link

Document

8-bit UTF (UCS Transformation Format)

Usage

From source file:com.couchbase.client.core.cluster.SubdocumentMessageTest.java

License:Apache License

@Test
public void shouldReturnPathNotExistOnCounterInIncompletePath() {
    String path = "counter.subCounter";
    long delta = 99L;

    SubCounterRequest request = new SubCounterRequest(testSubKey, path, delta, bucket());
    SimpleSubdocResponse response = cluster().<SimpleSubdocResponse>send(request).toBlocking().single();
    ReferenceCountUtil.releaseLater(response.content());
    String result = response.content().toString(CharsetUtil.UTF_8);

    assertEquals(0, result.length());/*w w w  . ja  va  2s  . c om*/
    assertEquals(ResponseStatus.SUBDOC_PATH_NOT_FOUND, response.status());
}

From source file:com.couchbase.client.core.cluster.SubdocumentMessageTest.java

License:Apache License

@Test
public void shouldReturnDeltaRangeOnCounterDeltaZero() {
    String path = "counter";
    long delta = 0L;

    SubCounterRequest request = new SubCounterRequest(testSubKey, path, delta, bucket());
    SimpleSubdocResponse response = cluster().<SimpleSubdocResponse>send(request).toBlocking().single();
    ReferenceCountUtil.releaseLater(response.content());
    String result = response.content().toString(CharsetUtil.UTF_8);

    assertEquals(0, result.length());/*from  w ww .  j  a v a 2 s  .  c om*/
    assertEquals(ResponseStatus.SUBDOC_DELTA_RANGE, response.status());
}

From source file:com.couchbase.client.core.cluster.SubdocumentMessageTest.java

License:Apache License

@Test
public void shouldReturnDeltaRangeOnCounterDeltaOverflow() {
    String path = "counter";
    long prepareOverFlow = 1L;
    long delta = Long.MAX_VALUE;

    //first request will bring the value to +1
    SubCounterRequest request = new SubCounterRequest(testSubKey, path, prepareOverFlow, bucket());
    SimpleSubdocResponse response = cluster().<SimpleSubdocResponse>send(request).toBlocking().single();
    ReferenceCountUtil.releaseLater(response.content());
    String result = response.content().toString(CharsetUtil.UTF_8);

    assertEquals("1", result);

    //second request will overflow
    request = new SubCounterRequest(testSubKey, path, delta, bucket());
    response = cluster().<SimpleSubdocResponse>send(request).toBlocking().single();
    ReferenceCountUtil.releaseLater(response.content());
    result = response.content().toString(CharsetUtil.UTF_8);

    assertEquals(result, 0, result.length());
    /*/*  w ww  . ja v  a2 s. co m*/
     * Was SUBDOC_DELTA_RANGE, but changed to VALUE_CANTINSERT between 4.5 dp and BETA.
     * See https://issues.couchbase.com/browse/JCBC-931, https://issues.couchbase.com/browse/MB-18169
     */
    assertEquals(ResponseStatus.SUBDOC_VALUE_CANTINSERT, response.status());
}

From source file:com.couchbase.client.core.cluster.SubdocumentMessageTest.java

License:Apache License

@Test
public void shouldReturnDeltaRangeOnCounterDeltaUnderflow() {
    String path = "counter";
    long prepareUnderflow = -1L;
    long delta = Long.MIN_VALUE;

    //first request will bring the value to -1
    SubCounterRequest request = new SubCounterRequest(testSubKey, path, prepareUnderflow, bucket());
    SimpleSubdocResponse response = cluster().<SimpleSubdocResponse>send(request).toBlocking().single();
    ReferenceCountUtil.releaseLater(response.content());
    String result = response.content().toString(CharsetUtil.UTF_8);

    assertEquals("-1", result);

    //second request will underflow
    request = new SubCounterRequest(testSubKey, path, delta, bucket());
    response = cluster().<SimpleSubdocResponse>send(request).toBlocking().single();
    ReferenceCountUtil.releaseLater(response.content());
    result = response.content().toString(CharsetUtil.UTF_8);

    assertEquals(result, 0, result.length());
    assertEquals(ResponseStatus.SUBDOC_DELTA_RANGE, response.status());
}

From source file:com.couchbase.client.core.cluster.SubdocumentMessageTest.java

License:Apache License

@Test
public void shouldApplyAllMultiMutationsAndReleaseCommandFragments() {
    ByteBuf counterFragment = Unpooled.copiedBuffer("-404", CharsetUtil.UTF_8);
    counterFragment.retain(2);//from  w  w w  . ja v a2 s.c om

    ByteBuf stringFragment = Unpooled.copiedBuffer("\"mutated\"", CharsetUtil.UTF_8);
    stringFragment.retain(2);

    ByteBuf arrayInsertedFragment = Unpooled.copiedBuffer("\"inserted\"", CharsetUtil.UTF_8);
    ByteBuf arrayFirstFragment = Unpooled.copiedBuffer("\"first\"", CharsetUtil.UTF_8);
    ByteBuf arrayLastFragment = Unpooled.copiedBuffer("\"last\"", CharsetUtil.UTF_8);
    ByteBuf uniqueFragment = Unpooled.copiedBuffer("\"unique\"", CharsetUtil.UTF_8);

    MutationCommand[] commands = new MutationCommand[] {
            new MutationCommand(Mutation.COUNTER, "counter", counterFragment, false),
            new MutationCommand(Mutation.COUNTER, "another.counter", counterFragment, true),
            new MutationCommand(Mutation.COUNTER, "another.counter", counterFragment, false),
            new MutationCommand(Mutation.DICT_ADD, "sub.value2", stringFragment),
            new MutationCommand(Mutation.DICT_UPSERT, "sub.value3", stringFragment),
            new MutationCommand(Mutation.REPLACE, "value", stringFragment),
            new MutationCommand(Mutation.ARRAY_INSERT, "sub.array[1]", arrayInsertedFragment),
            new MutationCommand(Mutation.ARRAY_PUSH_FIRST, "sub.array", arrayFirstFragment),
            new MutationCommand(Mutation.ARRAY_PUSH_LAST, "sub.array", arrayLastFragment),
            new MutationCommand(Mutation.ARRAY_ADD_UNIQUE, "sub.array", uniqueFragment),
            new MutationCommand(Mutation.DELETE, "sub.value") };

    SubMultiMutationRequest request = new SubMultiMutationRequest(testSubKey, bucket(), commands);
    MultiMutationResponse response = cluster().<MultiMutationResponse>send(request).toBlocking().single();
    assertEquals(ResponseStatus.SUCCESS, response.status());
    assertEquals(Unpooled.EMPTY_BUFFER, response.content());
    assertEquals(-1, response.firstErrorIndex());
    assertEquals(ResponseStatus.SUCCESS, response.firstErrorStatus());

    assertEquals(0, stringFragment.refCnt());
    assertEquals(0, counterFragment.refCnt());
    assertEquals(0, arrayInsertedFragment.refCnt());
    assertEquals(0, arrayFirstFragment.refCnt());
    assertEquals(0, arrayLastFragment.refCnt());
    assertEquals(0, uniqueFragment.refCnt());

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < commands.length; i++) {
        MutationCommand command = commands[i];
        MultiResult result = response.responses().get(i);
        assertEquals(command.path(), result.path());
        assertEquals(command.mutation(), result.operation());

        sb.append('\n').append(result);
        ReferenceCountUtil.releaseLater(result.value());
    }
    if (sb.length() > 1)
        sb.deleteCharAt(0);

    String expectedResponse = "COUNTER(counter): SUCCESS = -404" + "\nCOUNTER(another.counter): SUCCESS = -404"
            + "\nCOUNTER(another.counter): SUCCESS = -808" +
            //values below have no content
            "\nDICT_ADD(sub.value2): SUCCESS" + "\nDICT_UPSERT(sub.value3): SUCCESS"
            + "\nREPLACE(value): SUCCESS" + "\nARRAY_INSERT(sub.array[1]): SUCCESS"
            + "\nARRAY_PUSH_FIRST(sub.array): SUCCESS" + "\nARRAY_PUSH_LAST(sub.array): SUCCESS"
            + "\nARRAY_ADD_UNIQUE(sub.array): SUCCESS" + "\nDELETE(sub.value): SUCCESS";
    assertEquals(expectedResponse, sb.toString());

    String expected = "{\"value\":\"mutated\"," + "\"sub\":{" +
    //                "\"value\":\"subStringValue\"," + //DELETED
            "\"array\":[\"first\",\"array1\",\"inserted\",2,true,\"last\",\"unique\"]"
            + ",\"value2\":\"mutated\"" + ",\"value3\":\"mutated\"}," + "\"counter\":-404,"
            + "\"another\":{\"counter\":-808}}";
    assertMutation(testSubKey, expected);
}

From source file:com.couchbase.client.core.cluster.SubdocumentMessageTest.java

License:Apache License

@Test
public void shouldFailSomeMultiMutationsAndReleaseCommandFragments() {
    ByteBuf counterFragment = Unpooled.copiedBuffer("-404", CharsetUtil.UTF_8);
    counterFragment.retain();/*ww  w .  j  a va 2s .  c  o  m*/

    ByteBuf stringFragment = Unpooled.copiedBuffer("\"mutated\"", CharsetUtil.UTF_8);
    stringFragment.retain(2);

    ByteBuf arrayInsertedFragment = Unpooled.copiedBuffer("\"inserted\"", CharsetUtil.UTF_8);
    ByteBuf arrayFirstFragment = Unpooled.copiedBuffer("\"first\"", CharsetUtil.UTF_8);
    ByteBuf arrayLastFragment = Unpooled.copiedBuffer("\"last\"", CharsetUtil.UTF_8);
    ByteBuf uniqueFragment = Unpooled.copiedBuffer("\"unique\"", CharsetUtil.UTF_8);

    SubMultiMutationRequest request = new SubMultiMutationRequest(testSubKey, bucket(),
            new MutationCommand(Mutation.COUNTER, "counter", counterFragment, false),
            new MutationCommand(Mutation.COUNTER, "another.counter", counterFragment, true),
            new MutationCommand(Mutation.DICT_ADD, "sub.value2", stringFragment),
            new MutationCommand(Mutation.DICT_UPSERT, "sub.value3", stringFragment),
            new MutationCommand(Mutation.REPLACE, "value", stringFragment),
            //this one fails
            new MutationCommand(Mutation.ARRAY_INSERT, "sub.array[5]", arrayInsertedFragment),
            new MutationCommand(Mutation.ARRAY_PUSH_FIRST, "sub.array", arrayFirstFragment),
            new MutationCommand(Mutation.ARRAY_PUSH_LAST, "sub.array", arrayLastFragment),
            new MutationCommand(Mutation.ARRAY_ADD_UNIQUE, "sub.array", uniqueFragment),
            //this one would also fail, but server stops at first failure
            new MutationCommand(Mutation.DELETE, "path.not.found"));
    MultiMutationResponse response = cluster().<MultiMutationResponse>send(request).toBlocking().single();
    assertEquals(ResponseStatus.SUBDOC_MULTI_PATH_FAILURE, response.status());
    assertEquals(Unpooled.EMPTY_BUFFER, response.content());
    assertEquals(5, response.firstErrorIndex());
    assertEquals(ResponseStatus.SUBDOC_PATH_NOT_FOUND, response.firstErrorStatus());
    assertEquals(0, response.responses().size());

    assertEquals(0, stringFragment.refCnt());
    assertEquals(0, counterFragment.refCnt());
    assertEquals(0, arrayInsertedFragment.refCnt());
    assertEquals(0, arrayFirstFragment.refCnt());
    assertEquals(0, arrayLastFragment.refCnt());
    assertEquals(0, uniqueFragment.refCnt());

    //no mutation happened
    String expected = jsonContent;
    assertMutation(testSubKey, expected);
}

From source file:com.couchbase.client.core.cluster.SubdocumentMessageTest.java

License:Apache License

@Test
public void shouldFailAllMultiMutationsAndReleaseCommandFragments() {
    ByteBuf counterFragment = Unpooled.copiedBuffer("-404", CharsetUtil.UTF_8);
    ByteBuf stringFragment = Unpooled.copiedBuffer("\"mutated\"", CharsetUtil.UTF_8);
    ByteBuf arrayFirstFragment = Unpooled.copiedBuffer("\"first\"", CharsetUtil.UTF_8);

    SubMultiMutationRequest request = new SubMultiMutationRequest(testInsertionSubKey, bucket(),
            new MutationCommand(Mutation.COUNTER, "counter", counterFragment, false),
            new MutationCommand(Mutation.DICT_UPSERT, "sub.value3", stringFragment),
            new MutationCommand(Mutation.ARRAY_PUSH_FIRST, "sub.array", arrayFirstFragment),
            new MutationCommand(Mutation.DELETE, "some.paht"));
    MultiMutationResponse response = cluster().<MultiMutationResponse>send(request).toBlocking().single();
    assertEquals(ResponseStatus.NOT_EXISTS, response.status());
    assertEquals(Unpooled.EMPTY_BUFFER, response.content());
    assertEquals(-1, response.firstErrorIndex());
    assertEquals(ResponseStatus.FAILURE, response.firstErrorStatus());
    assertEquals(0, response.responses().size());

    assertEquals(0, stringFragment.refCnt());
    assertEquals(0, counterFragment.refCnt());
    assertEquals(0, arrayFirstFragment.refCnt());
}

From source file:com.couchbase.client.core.cluster.SubdocumentMessageTest.java

License:Apache License

/**
 * Helper method to get a whole document and assert its content against a subdoc mutation.
 * @param key the document ID./*w  w w  . j a va 2s .  c  om*/
 * @param expected the JSON content that is expected.
 */
private void assertMutation(String key, String expected) {
    //assert the mutation
    GetResponse finalState = cluster().<GetResponse>send(new GetRequest(key, bucket())).toBlocking().single();
    ReferenceCountUtil.releaseLater(finalState.content());
    String actual = finalState.content().toString(CharsetUtil.UTF_8);
    //work around MB-17143 and generally fact that no order/space guarantees
    // are made for JSON production by the subdoc API.
    try {
        Map actualMap = JSON.readValue(actual, Map.class);
        Map expectedMap = JSON.readValue(expected, Map.class);
        assertEquals("Expected mutation on " + key + " was not observed", expectedMap, actualMap);
    } catch (IOException e) {
        fail("Failure to compare JSON contents: " + e.toString());
    }
}

From source file:com.couchbase.client.core.config.DefaultMemcachedBucketConfig.java

License:Apache License

private void populateKetamaNodes() {
    for (NodeInfo node : nodes()) {
        if (!node.services().containsKey(ServiceType.BINARY)) {
            continue;
        }//from   w w w.  j a v  a2  s  . c  o  m

        for (int i = 0; i < 40; i++) {
            MessageDigest md5;
            try {
                md5 = MessageDigest.getInstance("MD5");
                md5.update(keyForNode(node.hostname(), i).getBytes(CharsetUtil.UTF_8));
                byte[] digest = md5.digest();
                for (int j = 0; j < 4; j++) {
                    Long key = ((long) (digest[3 + j * 4] & 0xFF) << 24)
                            | ((long) (digest[2 + j * 4] & 0xFF) << 16)
                            | ((long) (digest[1 + j * 4] & 0xFF) << 8) | (digest[j * 4] & 0xFF);
                    ketamaNodes.put(key, node);
                }
            } catch (NoSuchAlgorithmException e) {
                throw new IllegalStateException("Could not populate ketama nodes.", e);
            }
        }
    }
}

From source file:com.couchbase.client.core.config.loader.CarrierLoader.java

License:Apache License

@Override
protected Observable<String> discoverConfig(final String bucket, final String password,
        final InetAddress hostname) {
    if (!env().bootstrapCarrierEnabled()) {
        LOGGER.info("Carrier Bootstrap manually disabled.");
        return Observable
                .error(new ConfigurationException("Carrier Bootstrap disabled through configuration."));
    }/*from  ww  w. ja v  a  2  s .  c o m*/
    LOGGER.debug("Starting to discover config through Carrier Bootstrap");

    return Buffers
            .wrapColdWithAutoRelease(
                    cluster().<GetBucketConfigResponse>send(new GetBucketConfigRequest(bucket, hostname)))
            .map(new Func1<GetBucketConfigResponse, String>() {
                @Override
                public String call(GetBucketConfigResponse response) {
                    if (!response.status().isSuccess()) {
                        response.content().release();
                        throw new IllegalStateException("Bucket config response did not return with success.");
                    }

                    LOGGER.debug("Successfully loaded config through carrier.");
                    String content = response.content().toString(CharsetUtil.UTF_8);
                    response.content().release();
                    return replaceHostWildcard(content, hostname);
                }
            });
}