/*
 *      Copyright (C) 2015  higherfrequencytrading.com
 *
 *      This program is free software: you can redistribute it and/or modify
 *      it under the terms of the GNU Lesser General Public License as published by
 *      the Free Software Foundation, either version 3 of the License.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU Lesser General Public License for more details.
 *
 *      You should have received a copy of the GNU Lesser General Public License
 *      along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.openhft.chronicle.set;

import net.openhft.chronicle.hash.ChronicleHash;
import net.openhft.chronicle.hash.ChronicleHashBuilder;
import org.junit.Ignore;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.samskivert.util.CollectionUtil.selectRandomSubset;

public class Issue24ChronicleSetTest {

    public static <K, H extends ChronicleHash<K, ?, ?, ?>,  B extends ChronicleHashBuilder<K, H, B>>
    H init(B builder, int entrySize, int averageKeySize, String fileName) {

        String tmp = System.getProperty("java.io.tmpdir");
        String pathname = tmp + "/" + fileName;
        File file = new File(pathname);
        if (file.exists())
            file.delete();
        file.deleteOnExit();
        try {
            H result = builder.entries(entrySize)
                    .averageKeySize(averageKeySize).createPersistedTo(file);
            return result;
        } catch (IOException ioe) {
            throw new RuntimeException(ioe);
        }
    }

    public synchronized static <A> ChronicleSet<A> initSet(
            Class<A> entryClass, int entrySize, int averageKeySize, String fileName) {
        return init(ChronicleSetBuilder.of(entryClass), entrySize, averageKeySize, fileName);
    }

    @Test
    public void issue24ChronicleSetTest() {
        ChronicleSet<String> set = initSet(String.class, 1_000_000, 30, "stringSet.dat");
        ExecutorService executor = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 10; i++) {
            Runnable worker = new WorkerThread(set);
            executor.execute(worker);
        }
        executor.shutdown();
        while (!executor.isTerminated()) {

        }
        System.out.println("Finished all threads");


    }

    public static class WorkerThread implements Runnable {
        private final ChronicleSet<String> set;
        public WorkerThread(ChronicleSet<String> set){
            this.set = set;
        }

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName()+" Start. Command = " + set.size());
            processCommand();
            System.out.println(Thread.currentThread().getName()+" End.");
        }

        private void processCommand() {
            Set<String> nomenclatures = new HashSet<>();
            for(int i = 0; i < 10; i++) {
                String nomenclature = "#############################" + i;
                nomenclatures.add(nomenclature);

            }

            set.addAll(nomenclatures);


            Set<String> strings = new HashSet<>(
                    selectRandomSubset(nomenclatures, nomenclatures.size() / 2));

            set.addAll(strings);


            Set<String> toRemove = new HashSet<>();
            Random generator = new Random();
            for (int j = 0; j < 3; j++) {
                int i = generator.nextInt(10);
                String nomenclature = "#############################" + i;
                toRemove.add(nomenclature);
            }
            set.removeAll(toRemove);

            for (String s : set) {
                System.out.println(s);
            }

            strings = new HashSet<>(selectRandomSubset(
                    nomenclatures, nomenclatures.size() / 2));

            set.addAll(strings);

            for (String s : set) {
                System.out.println(s);
            }
        }

    }
}

@@ -874,33 +874,8 @@
 er;%0A
-import org.junit.Ignore;%0A
 impo
@@ -1371,228 +1371,91 @@
 Size
-, String fileName) %7B%0A%0A        String tmp = System.getProperty(%22java.io.tmpdir%22);%0A        String pathname = tmp + %22/%22 + fileName;%0A        File file = new File(pathname);%0A        if (file.exists())%0A            file.delete(
+) throws IOException %7B%0A%0A        File file = File.createTempFile(%22stringSet%22, %22.dat%22
 );%0A 
@@ -1878,26 +1878,40 @@
 Size
-, String fileName)
+)%0A            throws IOException
  %7B%0A 
@@ -1994,18 +1994,8 @@
 Size
-, fileName
 );%0A 
@@ -2051,16 +2051,35 @@
 etTest()
+ throws IOException
  %7B%0A     
@@ -2147,25 +2147,8 @@
 , 30
-, %22stringSet.dat%22
 );%0A 

Use temporary file in Issue24ChronicleSetTest