package core.index;

import core.index.build.CountingPartitionWriter;
import core.index.build.InputReader;
import core.index.kdtree.KDDTree;
import core.index.key.CartilageIndexKey;
import core.utils.TreeUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;

import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * Created by qui on 3/30/15.
 */
public class CompareIndices {

    //int bucketSize = 64*1024*1024;
    int bucketSize = 1024*1024;
    CountingPartitionWriter writer;

    String inputFilename;
    CartilageIndexKey key;

    String localPartitionDir;
    int attributes;
    int maxBuckets;

    @Rule
    public TestName name = new TestName();

    @Before
    public void setUp(){
        inputFilename = Settings.tpchPath + "lineitem.tbl";

        localPartitionDir = Settings.localPartitionDir;

        key = new CartilageIndexKey('|');

        attributes = 16;
        writer = new CountingPartitionWriter(localPartitionDir);

        File f = new File(inputFilename);
        maxBuckets = (int) (f.length() / bucketSize) + 1;
        System.out.println("Max buckets: "+maxBuckets);
    }

    @After
    public void calculateStats() {
        Map<String, Integer> counts = writer.getCounts();
        int n = counts.size();

        // calculate mean
        double sum = 0.0;
        for (Integer count : counts.values()) {
            sum += count;
        }
        double mean = sum / n;

        // calculate st dev
        double sqErr = 0.0;
        for (Integer count : counts.values()) {
            double err = count - mean;
            sqErr += err * err;
        }
        double stdev = Math.sqrt(sqErr / n);

        System.out.println("Number of buckets: "+n);
        System.out.println("Mean size: "+mean);
        System.out.println("Size st dev: "+stdev);
        System.out.println(counts);
        try{
            TreeUtils.plot(counts, String.format("BarChart_%s.jpeg", name.getMethodName()));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @Test
    public void testBasicKDTree() {
        MDIndex index = new KDDTree();
        InputReader r = new InputReader(index, key);

        long startTime = System.nanoTime();
        index.initBuild(maxBuckets);
        r.scan(inputFilename);
        index.initProbe();
        double time1 = (double)(System.nanoTime()-startTime)/1E9;
        System.out.println("Index Build Time = "+time1+" sec");

        r.scan(inputFilename, writer);
    }
}

@@ -134,16 +134,55 @@
 DDTree;%0A
+import core.index.kdtree.KDMedianTree;%0A
 import c
@@ -214,16 +214,16 @@
 dexKey;%0A
-
 import c
@@ -722,16 +722,36 @@
 ributes;
+%0A%0A    long fileSize;
 %0A    int
@@ -1141,32 +1141,63 @@
 inputFilename);%0A
+        fileSize = f.length();%0A
         maxBucke
@@ -1209,25 +1209,23 @@
 (int) (f
-.length()
+ileSize
  / bucke
@@ -1990,44 +1990,8 @@
 v);%0A
-        System.out.println(counts);%0A
     
@@ -2259,24 +2259,24 @@
  KDDTree();%0A
-
         Inpu
@@ -2583,24 +2583,666 @@
 1+%22 sec%22);%0A%0A
+        r.scan(inputFilename, writer);%0A    %7D%0A%0A    @Test%0A    public void testKDMedianTree() %7B%0A        MDIndex index = new KDMedianTree(1);%0A        InputReader r = new InputReader(index, key);%0A%0A        Runtime runtime = Runtime.getRuntime();%0A        double samplingRate = runtime.freeMemory() / (2.0 * fileSize);%0A        System.out.println(%22Sampling rate: %22+samplingRate);%0A%0A        long startTime = System.nanoTime();%0A        index.initBuild(maxBuckets);%0A        r.scan(inputFilename);%0A        index.initProbe();%0A        double time1 = (double)(System.nanoTime()-startTime)/1E9;%0A        System.out.println(%22Index Build Time = %22+time1+%22 sec%22);%0A%0A
         r.sc

Add analysis for kd median tree