/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.ignite.examples.datagrid;

import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.IgniteException;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.CacheMetrics;
import org.apache.ignite.cache.CachePeekMode;
import org.apache.ignite.configuration.MemoryConfiguration;
import org.apache.ignite.configuration.MemoryPolicyConfiguration;
import org.apache.ignite.examples.ExampleNodeStartup;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * This example demonstrates how to tweak particular settings of Apache Ignite page memory using
 * {@link MemoryConfiguration} and set up several memory policies for different caches with
 * {@link MemoryPolicyConfiguration}.
 * <p>
 * Additional remote nodes can be started with special configuration file which
 * enables P2P class loading: {@code 'ignite.{sh|bat} example-memory-policies.xml'}.
 * <p>
 * Alternatively you can run {@link ExampleNodeStartup} in another JVM which passing
 * {@code examples/config/example-memory-policies.xml} configuration to it.
 */
public class MyMemoryPoliciesExample {

    /**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     * @throws IgniteException If example execution failed.
     */
    public static void main(String[] args) throws IgniteException {
        try (Ignite ignite = Ignition.start("examples/config/my-client-example-memory-policies.xml")) {
            //will store book as entry
            byte[] data = readFileToByteArray();
            System.out.println("Value size " + data.length + " bytes");

            System.out.println();
            System.out.println(">>> Memory policies example started.");
            IgniteCache cache = ignite.getOrCreateCache("MyCache1");
            int count = 0;
            int key = 1;
            while (true) {
                cache.put(key, new MyFile(data, key));
                Thread.sleep(1000l);//1 sec interval
                count++;
                key++;
                if (count == 10) {//every 10 sec
                    count = 0;
                    System.out.println("<----------> entries " + key + "; total size " + (data.length * key) / (1024l * 1024) + " MB");
                    printMetrics(cache);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static byte[] readFileToByteArray() {
        Path path = Paths.get("book.pdf");
        byte[] data = new byte[0];
        try {
            data = Files.readAllBytes(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }

    private static void printMetrics(IgniteCache cache) {
        CacheMetrics metrics = cache.metrics();
        System.out.println("ALL = " + cache.size(CachePeekMode.ALL));
        System.out.println("ONHEAP = " + cache.size(CachePeekMode.ONHEAP));
        System.out.println("OFHEAP = " + cache.size(CachePeekMode.OFFHEAP));
        System.out.println("PRIMARY = " + cache.size(CachePeekMode.PRIMARY));
        System.out.println("BACKUP = " + cache.size(CachePeekMode.BACKUP));
        System.out.println("NEAR = " + cache.size(CachePeekMode.NEAR));
        System.out.println("Evictions = " + metrics.getCacheEvictions());
    }


}

class MyFile {
    byte[] arr;
    int key;

    public MyFile(byte[] arr, int key) {
        this.arr = arr;
        this.key = key;
    }
}