package ch.ethz.globis.distindex.serializer;

import ch.ethz.globis.pht.PVIterator;
import ch.ethz.globis.pht.PhTreeV;
import ch.ethz.globis.pht.v5.PhTree5;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import org.objenesis.strategy.StdInstantiatorStrategy;

import java.io.*;

public class IterativeSerializer implements PhTreeSerializer {

    private ThreadLocal<Kryo> kryos = new ThreadLocal<Kryo>() {
        protected Kryo initialValue() {
            Kryo kryo = new Kryo();
            ((Kryo.DefaultInstantiatorStrategy) kryo.getInstantiatorStrategy()).
                    setFallbackInstantiatorStrategy(new StdInstantiatorStrategy());
            return kryo;
        };
    };

    @Override
    public <T> void export(PhTreeV<T> tree, String filename) throws FileNotFoundException {
        try (Output output = createOutput(filename)) {
            Kryo kryo = kryos.get();
            PVIterator<T> it = tree.queryExtent();
            while (it.hasNext()) {
                kryo.writeClassAndObject(output, it.nextEntry());
            }
            output.flush();
        }
    }

    private Output createOutput(String filename) throws FileNotFoundException {
        return new Output(new BufferedOutputStream(new FileOutputStream(filename)));
    }

    @Override
    public <T> PhTreeV<T> load(String filename) throws FileNotFoundException {
        try (Input input = new Input(new BufferedInputStream(new FileInputStream(filename)))){
            Kryo kryo = kryos.get();
            return (PhTreeV<T>) kryo.readClassAndObject(input);
        }
    }
}

@@ -39,16 +39,51 @@
 lizer;%0A%0A
+import ch.ethz.globis.pht.PVEntry;%0A
 import c
@@ -151,46 +151,8 @@
 eV;%0A
-import ch.ethz.globis.pht.v5.PhTree5;%0A
 impo
@@ -380,16 +380,19 @@
 rializer
+%3CT%3E
  impleme
@@ -415,16 +415,46 @@
 izer %7B%0A%0A
+    private PhTreeV%3CT%3E tree;%0A%0A
     priv
@@ -1399,20 +1399,16 @@
   public
- %3CT%3E
  PhTreeV
@@ -1456,32 +1456,32 @@
 oundException %7B%0A
+
         try (Inp
@@ -1612,69 +1612,359 @@
     
-return (PhTreeV%3CT%3E) kryo.readClassAndObject(input);%0A        %7D
+PVEntry%3CT%3E e;%0A            while (input.eof()) %7B%0A                e = (PVEntry%3CT%3E) kryo.readClassAndObject(input);%0A                tree.put(e.getKey(), e.getValue());%0A            %7D%0A            return tree;%0A        %7D%0A    %7D%0A%0A    public void setTree(PhTreeV%3CT%3E tree) %7B%0A        this.tree = tree;%0A    %7D%0A%0A    public PhTreeV%3CT%3E getTree() %7B%0A        return tree;
 %0A   

Index distribution Updated iterative serializer.