<commit_before>/*
 *    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 org.gridgain.examples.datagrid.near;

import org.gridgain.grid.*;
import org.gridgain.grid.cache.*;

import java.util.concurrent.*;

/**
 *
 */
public class NearCacheExample {
    /** Cache name. */
    private static final String CACHE_NAME = "partitioned";

    /**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     * @throws GridException If example execution failed.
     */
    public static void main(String[] args) throws Exception {
        try (Grid g = GridGain.start("config/example-cache-near.xml")) {
            if (g.nodes().size() == 1) {
                System.err.println("Start remote nodes to run this example.");
            }
            else {
                int key = -1;

                final GridCache<Integer, Integer> cache = g.cache(CACHE_NAME);

                // Find key for which this node is neither primary or backup.
                // This will guarantee that the found key will end up in the near cache,
                // and not in the main partitioned cache.
                for (int i = 0; i < 1000; i++) {
                    GridCacheEntry<Integer, Integer> entry = cache.entry(i);

                    assert entry != null;

                    if (!entry.backup() && !entry.primary()) {
                        key = i;

                        break;
                    }
                }

                if (key == -1)
                    throw new Exception("Failed to map key to remote node.");

                // This will create near-cache entry.
                if (cache.get(key) != null)
                    throw new Exception("Key should not be in cache: " + key);

                System.out.println("Key belonging to remote node: " + key);

                final int key0 = key;

                // Update key on remote node to make sure that it won't
                // be present in the local near cache.
                g.forRemotes().compute().call(new Callable<Object>() {
                    @Override public Object call() throws Exception {
                        System.out.println("Putting sample key on node: " + g.localNode().id());

                        return cache.putx(key0, 10);
                    }
                }).get();

                if (cache.peek(key) != null)
                    throw new Exception("Key should not be in cache: " + key);

                // This will create near entry.
//                if (cache.get(key) != 10)
//                    throw new Exception("Unexpected value in cache: " + key);
//
//                if (cache.peek(key) != 10)
//                    throw new Exception("Unexpected value in cache: " + key);

                // Update key on remote node to make sure that it won't
                // be present in the local near cache.
                g.forRemotes().compute().call(new Callable<Object>() {
                    @Override public Object call() throws Exception {
                        System.out.println("Updating example key on node: " + g.localNode().id());

                        return cache.putx(key0, 15);
                    }
                }).get();

                // Peek into near cache.
                Object val = cache.peek(key);<commit_msg>Updated cache store example.<commit_after>/*
 *    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 org.gridgain.examples.datagrid.near;

import org.gridgain.grid.*;
import org.gridgain.grid.cache.*;

import java.util.concurrent.*;

/**
 *
 */
public class NearCacheExample {
    /** Cache name. */
    private static final String CACHE_NAME = "partitioned";

    /**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     * @throws GridException If example execution failed.
     */
    public static void main(String[] args) throws Exception {
        try (Grid g = GridGain.start("config/example-cache-near.xml")) {
            if (g.nodes().size() == 1) {
                System.err.println("Start remote nodes to run this example.");
            }
            else {
                int key = -1;

                final GridCache<Integer, Integer> cache = g.cache(CACHE_NAME);

                // Find key for which this node is neither primary or backup.
                // This will guarantee that the found key will end up in the near cache,
                // and not in the main partitioned cache.
                for (int i = 0; i < 1000; i++) {
                    GridCacheEntry<Integer, Integer> entry = cache.entry(i);

                    assert entry != null;

                    if (!entry.backup() && !entry.primary()) {
                        key = i;

                        break;
                    }
                }

                if (key == -1)
                    throw new Exception("Failed to map key to remote node.");

                // This will create near-cache entry.
                if (cache.get(key) != null)
                    throw new Exception("Key should not be in cache: " + key);

                System.out.println("Key belonging to remote node: " + key);

                final int key0 = key;

                // Update key on remote node to make sure that it won't
                // be present in the local near cache.
                g.forRemotes().compute().call(new Callable<Object>() {
                    @Override public Object call() throws Exception {
                        System.out.println("Putting sample key on node: " + g.localNode().id());

                        return cache.putx(key0, 10);
                    }
                }).get();

                if (cache.peek(key) != null)
                    throw new Exception("Key should not be in cache: " + key);

                // This will create near entry.
//                if (cache.get(key) != 10)
//                    throw new Exception("Unexpected value in cache: " + key);
//
//                if (cache.peek(key) != 10)
//                    throw new Exception("Unexpected value in cache: " + key);

                // Update key on remote node to make sure that it won't
                // be present in the local near cache.
                g.forRemotes().compute().call(new Callable<Object>() {
                    @Override public Object call() throws Exception {
                        System.out.println("Updating sample key on node: " + g.localNode().id());

                        return cache.putx(key0, 15);
                    }
                }).get();

                // Peek into near cache.
                Object val = cache.peek(key);
{'fraction_non_alphanumeric': 0.10969812217732351, 'fraction_numerical': 0.005467078678393154, 'mean_word_length': 1.4426705370101596, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 4, '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': '2371856', 'n_tokens_mistral': 2308, 'n_tokens_neox': 2135, 'n_words': 702}