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("hello".hashCode(), 100L),
                new PropertyWithTimestamp("hello", 1.1, 100L));
        tree.insert(new IntWithTimestamp("there".hashCode(), 200L),
                new PropertyWithTimestamp("there", 1.2, 200L));
        tree.insert(new IntWithTimestamp("hello2".hashCode(), 300L),
                new PropertyWithTimestamp("hello2", 1.3, 300L));
        tree.print();
        Iterator<PropertyWithTimestamp> it = tree.find( //
                new IntWithTimestamp("hello".hashCode(), 0), //
                new IntWithTimestamp("hello".hashCode(), 1000)) //
                .iterator();
        assertEquals(100L, it.next().time);
        assertEquals(200L, it.next().time);
        assertEquals(300L, 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() {
        new File("target/testbp").mkdirs();
        return BPlusTree.file() //
                .directory("target/testbp") //
                .maxKeys(12) //
                .segmentSizeMB(1) //
                .keySerializer(IntWithTimestamp.SERIALIZER) //
                .valueSerializer(PropertyWithTimestamp.SERIALIZER) //
                .naturalOrder();
    }

}
@@ -474,25 +474,25 @@
 hashCode(), 
-1
+5
 00L),%0A      
@@ -541,17 +541,17 @@
 %22, 1.1, 
-1
+5
 00L));%0A 
@@ -833,24 +833,25 @@
 ee.print();%0A
+%0A
         Iter
@@ -1078,17 +1078,17 @@
 tEquals(
-1
+5
 00L, it.
@@ -1661,24 +1661,35 @@
 () %7B%0A       
+ File dir =
  new File(%22t
@@ -1702,16 +1702,29 @@
 testbp%22)
+;%0A        dir
 .mkdirs(
@@ -1722,24 +1722,100 @@
 r.mkdirs();%0A
+        for (File f : dir.listFiles()) %7B%0A            f.delete();%0A        %7D%0A%0A
         retu
@@ -1864,31 +1864,19 @@
 rectory(
-%22target/testbp%22
+dir
 ) //%0A   

test still broken