package com.github.davidmoten.logan;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import java.io.File;
import java.util.Iterator;
import java.util.TreeSet;

import org.junit.Test;

import com.github.davidmoten.bplustree.BPlusTree;

public class DataPersisted2Test {

    @Test
    public void test() {
        BPlusTree<IntWithTimestamp, PropertyWithTimestamp> tree = create();
        tree.insert(new IntWithTimestamp(123, 500L),
                new PropertyWithTimestamp("hello", 1.1, 500L));
        tree.insert(new IntWithTimestamp(234, 200L),
                new PropertyWithTimestamp("there", 1.2, 200L));
        tree.insert(new IntWithTimestamp(124, 300L),
                new PropertyWithTimestamp("hello2", 1.3, 300L));
        tree.print();

        Iterator<PropertyWithTimestamp> it = tree.find( //
                new IntWithTimestamp(123, 0), //
                new IntWithTimestamp(123, 1000)) //
                .iterator();
        assertEquals(500L, it.next().time);
        assertFalse(it.hasNext());
    }

    @Test
    public void testTreeSet() {
        TreeSet<IntWithTimestamp> set = new TreeSet<>();
        set.add(new IntWithTimestamp("hello".hashCode(), 100L));
        set.add(new IntWithTimestamp("there".hashCode(), 200L));
        set.add(new IntWithTimestamp("hello2".hashCode(), 300L));
        set.stream().forEach(System.out::println);
    }

    private BPlusTree<IntWithTimestamp, PropertyWithTimestamp> create() {
        File dir = new File("target/testbp");
        dir.mkdirs();
        for (File f : dir.listFiles()) {
            f.delete();
        }

        return BPlusTree.file() //
                .directory(dir) //
                .maxKeys(12) //
                .segmentSizeMB(1) //
                .keySerializer(IntWithTimestamp.SERIALIZER) //
                .valueSerializer(PropertyWithTimestamp.SERIALIZER) //
                .naturalOrder();
    }

}
@@ -142,16 +142,41 @@
 o.File;%0A
+import java.util.Arrays;%0A
 import j
@@ -211,23 +211,82 @@
 va.util.
-TreeSet
+List;%0Aimport java.util.TreeSet;%0Aimport java.util.stream.Collectors
 ;%0A%0Aimpor
@@ -553,32 +553,16 @@
 , 500L),
-%0A               
  new Pro
@@ -654,32 +654,16 @@
 , 200L),
-%0A               
  new Pro
@@ -755,32 +755,16 @@
 , 300L),
-%0A               
  new Pro
@@ -1243,26 +1243,11 @@
 amp(
-%22hello%22.hashCode()
+123
 , 10
@@ -1293,26 +1293,11 @@
 amp(
-%22there%22.hashCode()
+234
 , 20
@@ -1343,85 +1343,165 @@
 amp(
-%22hello2%22.hashCode(), 300L));%0A        set.stream().forEach(System.out::println
+124, 300L));%0A        List%3CLong%3E list = set.stream().map(x -%3E x.time).collect(Collectors.toList());%0A        assertEquals(Arrays.asList(100L, 300L, 200L), list
 );%0A 

fix tests