package net.openhft.chronicle.map;

import net.openhft.chronicle.core.OS;
import net.openhft.chronicle.core.io.IOTools;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeTrue;

public class HugeSparseMapTest {

    private static ChronicleMap<CharSequence, CharSequence> createMap(boolean sparseFile) throws IOException {
        File file = IOTools.createTempFile("huge-map");
        try {
            file.deleteOnExit();
            ChronicleMapBuilder<CharSequence, CharSequence> builder = ChronicleMapBuilder
                    .of(CharSequence.class, CharSequence.class)
                    .averageKeySize(64)
                    // .entries(10_000_000_000L).averageValueSize(640) // 16 TB
                    .entries(3_000_000_000L).averageValueSize(200) // 2 TB
                    .sparseFile(sparseFile); // ~16 TB.
            ChronicleMap<CharSequence, CharSequence> map = builder.createPersistedTo(file);
            return map;
        } catch (Throwable t) {
            file.delete();
            throw t;
        }
    }

    @Test
    public void hugeSparseMap() throws IOException {
        assumeTrue(OS.isLinux());
        try (ChronicleMap<CharSequence, CharSequence> map = createMap(true)) {
            map.put("hi", "there");
            assertEquals("there", map.get("hi").toString());
        }
    }

    @Test(expected = IOException.class)
    public void hugeAllocatedMap() throws IOException {
        assumeTrue(OS.isLinux());
        try (ChronicleMap<CharSequence, CharSequence> map = createMap(false)) {
            map.put("hi", "there");
            assertEquals("there", map.get("hi").toString());
        }
    }
}

@@ -714,18 +714,17 @@
 KeySize(
-64
+8
 )%0A      
@@ -793,24 +793,26 @@
 0) // 16 TB%0A
+//
             
@@ -874,16 +874,92 @@
 // 2 TB%0A
+                    .entries(3_000_000_000L).averageValueSize(16) // 216 GB%0A
         
@@ -1484,32 +1484,62 @@
 %22).toString());%0A
+            System.in.read();%0A
         %7D%0A    %7D%0A

Add direct support for sparse files under Linux (with some size tuning) #313