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(8)
                    // .entries(10_000_000_000L).averageValueSize(640) // 16 TB
//                    .entries(3_000_000_000L).averageValueSize(200) // 2 TB
                    .entries(3_000_000_000L).averageValueSize(16) // 216 GB
                    .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());
        }
    }
}

@@ -716,16 +716,17 @@
 ySize(8)
+;
 %0A       
@@ -734,19 +734,47 @@
     
-        // 
+if (sparseFile)%0A                builder
 .ent
@@ -782,10 +782,9 @@
 ies(
-10
+3
 _000
@@ -815,24 +815,23 @@
 ize(
-640)
+16);
  // 
+2
 16 
-T
+G
 B%0A
-//
     
@@ -829,32 +829,38 @@
 6 GB%0A           
+ else%0A
          .entrie
@@ -844,32 +844,46 @@
   else%0A         
+       builder
 .entries(3_000_0
@@ -867,33 +867,34 @@
 builder.entries(
-3
+10
 _000_000_000L).a
@@ -913,21 +913,25 @@
 ize(
-20
+64
 0)
+;
  // 
-2
+16
  TB%0A
+//
     
@@ -992,25 +992,21 @@
 ize(
-16
+200
 ) // 2
-16 GB%0A   
+ TB%0A
     
@@ -1013,21 +1013,23 @@
         
-     
+builder
 .sparseF

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