// Path: reference/src/main/java/me/doubledutch/stroom/client/StreamConnection.java
// public interface StreamConnection{
// 	public final static int LINEAR=0;
// 	public final static int SYNC=1;
// 	public final static int FLUSH=2;
// 	public final static int NONE=3;
// 
// 	public List<String> get(long index,long endIndex) throws IOException;
// 	public String get(long index) throws IOException;
// 	public String getLast() throws IOException;
// 	public long getCount() throws IOException;
// 	
// 	public long append(JSONObject data) throws IOException,JSONException;
// 	public long append(String data) throws IOException,JSONException;
// 	public List<Long> append(List<String> data) throws IOException,JSONException;
// 	
// 	public long append(JSONObject data,int hint) throws IOException,JSONException;
// 	public long append(String data,int hint) throws IOException,JSONException;
// 	public List<Long> append(List<String> data,int hint) throws IOException,JSONException;
// 
// 	public void truncate(long index) throws IOException;
// }


import org.apache.log4j.Logger;
import me.doubledutch.stroom.perf.*;
import me.doubledutch.stroom.streams.*;
import me.doubledutch.stroom.*;
import me.doubledutch.lazyjson.*;
import me.doubledutch.stroom.client.StreamConnection;
import java.util.*;
import org.json.*;
import java.net.*;
import javax.script.*;
package me.doubledutch.stroom.filters;



public class FilterService extends Service{
	private final Logger log = Logger.getLogger("Filter");

	private long outputIndex=-1;
	private double sampleRate=1.0;
	private BatchMetric metric=null;
	private List<String> buffer=null;
	private int bufferSize=0;
	private long lastFlush=0;

	public FilterService(StreamHandler handler,JSONObject obj) throws Exception{
		super(handler,obj);
		
		String strType=obj.getString("type");
		if(strType.equals("sample")){
			type=SAMPLE;
			sampleRate=obj.getDouble("sample_rate");
		}
	}

	private synchronized void loadState() throws Exception{
		if(getStream("state").getCount()>0){
			JSONObject obj=new JSONObject(getStream("state").getLast());
			index=obj.getLong("i");
			outputIndex=obj.getLong("o");
		}
	}

	private synchronized void saveState() throws Exception{
		if(index>-1){
			JSONObject obj=new JSONObject();
			obj.put("i",index);
			obj.put("o",outputIndex);
// Path: reference/src/main/java/me/doubledutch/stroom/client/StreamConnection.java
// public interface StreamConnection{
// 	public final static int LINEAR=0;
// 	public final static int SYNC=1;
// 	public final static int FLUSH=2;
// 	public final static int NONE=3;
// 
// 	public List<String> get(long index,long endIndex) throws IOException;
// 	public String get(long index) throws IOException;
// 	public String getLast() throws IOException;
// 	public long getCount() throws IOException;
// 	
// 	public long append(JSONObject data) throws IOException,JSONException;
// 	public long append(String data) throws IOException,JSONException;
// 	public List<Long> append(List<String> data) throws IOException,JSONException;
// 	
// 	public long append(JSONObject data,int hint) throws IOException,JSONException;
// 	public long append(String data,int hint) throws IOException,JSONException;
// 	public List<Long> append(List<String> data,int hint) throws IOException,JSONException;
// 
// 	public void truncate(long index) throws IOException;
// }


// Path: reference/src/main/java/me/doubledutch/stroom/filters/FilterService.java
import org.apache.log4j.Logger;
import me.doubledutch.stroom.perf.*;
import me.doubledutch.stroom.streams.*;
import me.doubledutch.stroom.*;
import me.doubledutch.lazyjson.*;
import me.doubledutch.stroom.client.StreamConnection;
import java.util.*;
import org.json.*;
import java.net.*;
import javax.script.*;

package me.doubledutch.stroom.filters;



public class FilterService extends Service{
	private final Logger log = Logger.getLogger("Filter");

	private long outputIndex=-1;
	private double sampleRate=1.0;
	private BatchMetric metric=null;
	private List<String> buffer=null;
	private int bufferSize=0;
	private long lastFlush=0;

	public FilterService(StreamHandler handler,JSONObject obj) throws Exception{
		super(handler,obj);
		
		String strType=obj.getString("type");
		if(strType.equals("sample")){
			type=SAMPLE;
			sampleRate=obj.getDouble("sample_rate");
		}
	}

	private synchronized void loadState() throws Exception{
		if(getStream("state").getCount()>0){
			JSONObject obj=new JSONObject(getStream("state").getLast());
			index=obj.getLong("i");
			outputIndex=obj.getLong("o");
		}
	}

	private synchronized void saveState() throws Exception{
		if(index>-1){
			JSONObject obj=new JSONObject();
			obj.put("i",index);
			obj.put("o",outputIndex);
			getStream("state").append(obj,StreamConnection.FLUSH);