<commit_before>/*
 * Copyright 2013 Basho Technologies Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.basho.riak.client.core.operations.itest;

import com.basho.riak.client.core.operations.DtFetchOperation;
import com.basho.riak.client.core.operations.DtUpdateOperation;
import com.basho.riak.client.operations.crdt.MapMutation;
import com.basho.riak.client.query.crdt.ops.MapOp;
import com.basho.riak.client.query.crdt.types.*;
import com.basho.riak.client.util.ByteArrayWrapper;
import org.junit.Test;

import java.nio.ByteBuffer;
import java.util.concurrent.ExecutionException;

import static com.basho.riak.client.operations.crdt.CounterMutation.increment;
import static com.basho.riak.client.operations.crdt.CrdtMutation.forMap;
import static com.basho.riak.client.operations.crdt.FlagMutation.enabled;
import static com.basho.riak.client.operations.crdt.RegisterMutation.registerValue;
import static junit.framework.Assert.*;

public class ITestCrdtApi extends ITestBase
{

    @Test
    public void simpleTest() throws ExecutionException, InterruptedException
    {

        /**
         * Update some info about a user in a table of users
         */

        resetAndEmptyBucket(bucketName, mapBucketType);

        // ByteArrayWrappers make it look messy, so define them all here.
        ByteArrayWrapper numLogins = ByteArrayWrapper.create("logins");
        ByteArrayWrapper lastLoginTime = ByteArrayWrapper.create("last-login");
        ByteBuffer nowBinary = ByteBuffer.allocate(8).putLong(System.currentTimeMillis());
        ByteArrayWrapper now = ByteArrayWrapper.create(nowBinary.array());
        ByteArrayWrapper username = ByteArrayWrapper.create("username");
        ByteArrayWrapper loggedIn = ByteArrayWrapper.create("logged-in");

        // Create a builder for the user and update the values
        MapMutation userMapUpdate = forMap()
            .update(numLogins, increment())
            .update(lastLoginTime, registerValue(now))
            .update(loggedIn, enabled());

        // Now create an update for the user's entry
        MapMutation userEntryUpdate = forMap()
            .update(username, userMapUpdate);

        ByteArrayWrapper key = ByteArrayWrapper.create("user-info");

        DtUpdateOperation update =
            new DtUpdateOperation(bucketName, mapBucketType)
                .withOp(userEntryUpdate.getOp())
                .withKey(key);

        cluster.execute(update);
        update.get();

        DtFetchOperation fetch = new DtFetchOperation(bucketName, key).withBucketType(mapBucketType);
        cluster.execute(fetch);

        // enclosing map
        CrdtElement element = fetch.get();
        assertNotNull(element);
        assertTrue(element.isMap());
        CrdtMap usersMap = element.getAsMap();

        // username
        CrdtElement usernameElement = usersMap.get(username);
        assertNotNull(usernameElement);
        assertTrue(usernameElement.isMap());
        CrdtMap usernameMap = usernameElement.getAsMap();

        // logins
        CrdtElement numLoginsElement = usernameMap.get(numLogins);
        assertNotNull(numLoginsElement);
        assertTrue(numLoginsElement.isCounter());
        CrdtCounter numLoginsCounter = numLoginsElement.getAsCounter();
        assertEquals(1, numLoginsCounter.getValue());

        // last-login
        CrdtElement lastLoginTimeElement = usernameMap.get(lastLoginTime);
        assertNotNull(lastLoginTimeElement);
        assertTrue(lastLoginTimeElement.isRegister());
        CrdtRegister lastLoginTimeRegister = lastLoginTimeElement.getAsRegister();
        assertEquals(now, lastLoginTimeRegister.getValue());

        // logged-in
        CrdtElement loggedInElement = usernameMap.get(loggedIn);
        assertNotNull(loggedInElement);
        assertTrue(loggedInElement.isFlag());
        CrdtFlag loggedInFlag = loggedInElement.getAsFlag();
        assertEquals(true, loggedInFlag.getEnabled());

    }

}
<commit_msg>add set to example, in addition to some documentation<commit_after>/*
 * Copyright 2013 Basho Technologies Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.basho.riak.client.core.operations.itest;

import com.basho.riak.client.core.operations.DtFetchOperation;
import com.basho.riak.client.core.operations.DtUpdateOperation;
import com.basho.riak.client.operations.crdt.MapMutation;
import com.basho.riak.client.operations.crdt.SetMutation;
import com.basho.riak.client.query.crdt.ops.MapOp;
import com.basho.riak.client.query.crdt.types.*;
import com.basho.riak.client.util.ByteArrayWrapper;
import org.junit.Test;

import java.nio.ByteBuffer;
import java.util.concurrent.ExecutionException;

import static com.basho.riak.client.operations.crdt.CounterMutation.increment;
import static com.basho.riak.client.operations.crdt.CrdtMutation.forMap;
import static com.basho.riak.client.operations.crdt.CrdtMutation.forSet;
import static com.basho.riak.client.operations.crdt.FlagMutation.enabled;
import static com.basho.riak.client.operations.crdt.RegisterMutation.registerValue;
import static junit.framework.Assert.*;

public class ITestCrdtApi extends ITestBase
{

    @Test
    public void simpleTest() throws ExecutionException, InterruptedException
    {

        /**
         * Update some info about a user in a table of users
         */

        resetAndEmptyBucket(bucketName, mapBucketType);

        // ByteArrayWrappers make it look messy, so define them all here.
        ByteArrayWrapper numLogins = ByteArrayWrapper.create("logins");
        ByteArrayWrapper lastLoginTime = ByteArrayWrapper.create("last-login");
        ByteBuffer nowBinary = ByteBuffer.allocate(8).putLong(System.currentTimeMillis());
        ByteArrayWrapper now = ByteArrayWrapper.create(nowBinary.array());
        ByteArrayWrapper username = ByteArrayWrapper.create("username");
        ByteArrayWrapper loggedIn = ByteArrayWrapper.create("logged-in");
        ByteArrayWrapper favoriteThings = ByteArrayWrapper.create("favs");


        /*

            Data structure:

            bucket-type == map
            -> "username" : map
              -> "logins"     : counter
              -> "last-login" : register
              -> "logged-in"  : flag
              -> "favs"       : set

         */

        // Build a set of things
        ByteBuffer buffer = ByteBuffer.allocate(4);
        SetMutation favorites = forSet();
        for (int i = 0; i < 10; ++i)
        {
            buffer.putInt(i);
            favorites.add(ByteArrayWrapper.create(buffer.array()));
            buffer.rewind();
        }

        // Create an update for the user their values
        MapMutation userMapUpdate = forMap()
            .update(numLogins, increment())                // counter
            .update(lastLoginTime, registerValue(now))     // register
            .update(loggedIn, enabled())                   // flag
            .update(favoriteThings, favorites);            // set

        // Now create an update for the user's entry
        MapMutation userEntryUpdate = forMap()
            .update(username, userMapUpdate);

        ByteArrayWrapper key = ByteArrayWrapper.create("user-info");

        DtUpdateOperation update =
            new DtUpdateOperation(bucketName, mapBucketType)
                .withOp(userEntryUpdate.getOp())
                .withKey(key);

        cluster.execute(update);
        update.get();

        DtFetchOperation fetch = new DtFetchOperation(bucketName, key).withBucketType(mapBucketType);
        cluster.execute(fetch);

        // users
        CrdtElement element = fetch.get();
        assertNotNull(element);
        assertTrue(element.isMap());
        CrdtMap usersMap = element.getAsMap();

        // username
        CrdtElement usernameElement = usersMap.get(username);
        assertNotNull(usernameElement);
        assertTrue(usernameElement.isMap());
        CrdtMap usernameMap = usernameElement.getAsMap();

        // logins - counter
        CrdtElement numLoginsElement = usernameMap.get(numLogins);
        assertNotNull(numLoginsElement);
        assertTrue(numLoginsElement.isCounter());
        CrdtCounter numLoginsCounter = numLoginsElement.getAsCounter();
        assertEquals(1, numLoginsCounter.getValue());

        // last-login - register
        CrdtElement lastLoginTimeElement = usernameMap.get(lastLoginTime);
        assertNotNull(lastLoginTimeElement);
        assertTrue(lastLoginTimeElement.isRegister());
        CrdtRegister lastLoginTimeRegister = lastLoginTimeElement.getAsRegister();
        assertEquals(now, lastLoginTimeRegister.getValue());

        // logged-in - flag
        CrdtElement loggedInElement = usernameMap.get(loggedIn);
        assertNotNull(loggedInElement);
        assertTrue(loggedInElement.isFlag());
        CrdtFlag loggedInFlag = loggedInElement.getAsFlag();
        assertEquals(true, loggedInFlag.getEnabled());

    }

}

{'fraction_non_alphanumeric': 0.09522852837769964, 'fraction_numerical': 0.002410848819688599, 'mean_word_length': 3.053745928338762, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7490014', 'n_tokens_mistral': 2710, 'n_tokens_neox': 2664, 'n_words': 605}