package simpledb;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;

public class ImputedPlanCacheDotted extends ImputedPlanCache {
	private final File outDir;
	
	public ImputedPlanCacheDotted(File outDir) throws FileNotFoundException {
		super();
		if (!outDir.exists()) {
			outDir.mkdir();
		} else if (!outDir.isDirectory()) {
			throw new IllegalArgumentException();
		}
		
		this.outDir = outDir;
	}
	
	private void writePlan(Set<QualifiedName> dirtySet, ImputedPlan plan, double lossWeight) {
		StringBuilder prefix = new StringBuilder();
		prefix.append(lossWeight);
		prefix.append(" ");
		for (QualifiedName name : new TreeSet<QualifiedName>(dirtySet)) {
			prefix.append(name.toString());
			prefix.append(",");
		}
		
		try {
			File outFile = File.createTempFile(prefix.toString(), ".dot", outDir);
			QueryPlanDotter.print(plan.getPlan(), outFile.getAbsolutePath());
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	void addJoinPlan(Set<String> ts, Set<QualifiedName> dirtySet, Set<LogicalJoinNode> joins, ImputedPlan newPlan, double lossWeight) {
		super.addJoinPlan(ts, dirtySet, joins, newPlan, lossWeight);
		writePlan(dirtySet, newPlan, lossWeight);
	}
	
	@Override
	void addPlan(Set<String> ts, Set<QualifiedName> dirtySet, ImputedPlan newPlan, double lossWeight) {
		super.addPlan(ts, dirtySet, newPlan, lossWeight);
		writePlan(dirtySet, newPlan, lossWeight);
	}
}

@@ -641,16 +641,26 @@
 .append(
+plan.cost(
 lossWeig
@@ -658,24 +658,25 @@
 (lossWeight)
+)
 ;%0A%09%09prefix.a

correctly print out cost