package org.vanilladb.core.latch.feature;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.vanilladb.core.server.VanillaDb;
import org.vanilladb.core.server.task.Task;

public class LatchFeatureCollector extends Task {
	private static final int FLUSH_TIMEOUT = 10;
	private static final long FLUSH_INTERVAL = 1000;
	private static Logger logger = Logger.getLogger(LatchFeatureCollector.class.getName());

	private String fileName;
	private BlockingQueue<LatchFeature> latchFeatures;

	public void startCollecting() {
		VanillaDb.taskMgr().runTask(this);
	}

	public LatchFeatureCollector(String fileName) {
		this.fileName = fileName;
		latchFeatures = new LinkedBlockingQueue<LatchFeature>();
	}

	public void addLatchFeature(LatchFeature latchFeature) {
		try {
			if (logger.isLoggable(Level.INFO)) {
				logger.info(Thread.currentThread().getName() + " adds a latchFeature");
			}
			latchFeatures.put(latchFeature);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		Thread.currentThread().setName("Latch Data Collector");
		try {
			// wait for first latch feature
			LatchFeature latchFeature = latchFeatures.take();
			if (logger.isLoggable(Level.INFO)) {
				logger.info("Latch feature collector starts to collect data");
			}

			try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {

				writer.append(LatchFeature.toHeader() + "\n");
				writer.append(latchFeature.toRow() + "\n");

				long flushCounter = 0;

				while ((latchFeature = latchFeatures.poll(FLUSH_TIMEOUT, TimeUnit.SECONDS)) != null) {
					writer.append(latchFeature.toRow() + "\n");

					flushCounter += 1;

					if (flushCounter % FLUSH_INTERVAL == 0) {
						if (logger.isLoggable(Level.FINE)) {
							logger.info("flushing latch features");
						}
						writer.flush();
					}
				}

				if (logger.isLoggable(Level.INFO)) {
					String log = String.format("%s is generated.", fileName);
					logger.info(log);
				}

			} catch (IOException e) {
				e.printStackTrace();
			}

		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

@@ -1006,129 +1006,8 @@
 y %7B%0A
-%09%09%09if (logger.isLoggable(Level.INFO)) %7B%0A%09%09%09%09logger.info(Thread.currentThread().getName() + %22 adds a latchFeature%22);%0A%09%09%09%7D%0A
 %09%09%09l
@@ -1790,24 +1790,26 @@
 ) + %22%5Cn%22);%0A%0A
+//
 %09%09%09%09%09flushCo
@@ -1820,17 +1820,21 @@
 r += 1;%0A
-%0A
+//%0A//
 %09%09%09%09%09if 
@@ -1868,24 +1868,26 @@
 VAL == 0) %7B%0A
+//
 %09%09%09%09%09%09if (lo
@@ -1913,24 +1913,26 @@
 el.FINE)) %7B%0A
+//
 %09%09%09%09%09%09%09logge
@@ -1970,16 +1970,18 @@
 %22);%0A
+//
 %09%09%09%09%09%09%7D%0A
 %09%09%09%09
@@ -1972,24 +1972,26 @@
 ;%0A//%09%09%09%09%09%09%7D%0A
+//
 %09%09%09%09%09%09writer
@@ -2000,16 +2000,18 @@
 lush();%0A
+//
 %09%09%09%09%09%7D%0A%09

Remove some debugging code