// 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/client/KVStoreConnection.java
// public interface KVStoreConnection{
// 	public String get(String key) throws IOException;
// 	public List<String> list() throws IOException;
// 	public List<String> search(String pattern) throws IOException;
// }


import org.apache.log4j.Logger;
import me.doubledutch.stroom.perf.*;
import me.doubledutch.stroom.streams.*;
import me.doubledutch.stroom.*;
import me.doubledutch.stroom.client.StreamConnection;
import me.doubledutch.stroom.client.KVStoreConnection;
import java.util.*;
import java.util.concurrent.*;
import org.json.*;
import java.net.*;
import javax.script.*;
import java.io.*;
				JSONObject obj=new JSONObject(str);
				index=obj.getLong("i");
				if(obj.has("o")){
					JSONObject objSt=obj.getJSONObject("o");
					Iterator<String> keyIt=objSt.keys();

					while(keyIt.hasNext()){
						String key=keyIt.next();
						if(objSt.isNull(key)){
							keyMap.remove(key);
						}else{
							keyMap.put(key,objSt.getLong(key));
						}
					}
				}
			}

			loc+=batch.size();
			batch=getStream("state").get(loc,loc+500);
		}

	}

	private void saveState() throws Exception{
		JSONObject obj=new JSONObject();
		obj.put("i",index);
		if(stateMap!=null){
			// obj.put("o",outputIndex);
			obj.put("o",stateMap);
		}
// 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/client/KVStoreConnection.java
// public interface KVStoreConnection{
// 	public String get(String key) throws IOException;
// 	public List<String> list() throws IOException;
// 	public List<String> search(String pattern) throws IOException;
// }


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

				JSONObject obj=new JSONObject(str);
				index=obj.getLong("i");
				if(obj.has("o")){
					JSONObject objSt=obj.getJSONObject("o");
					Iterator<String> keyIt=objSt.keys();

					while(keyIt.hasNext()){
						String key=keyIt.next();
						if(objSt.isNull(key)){
							keyMap.remove(key);
						}else{
							keyMap.put(key,objSt.getLong(key));
						}
					}
				}
			}

			loc+=batch.size();
			batch=getStream("state").get(loc,loc+500);
		}

	}

	private void saveState() throws Exception{
		JSONObject obj=new JSONObject();
		obj.put("i",index);
		if(stateMap!=null){
			// obj.put("o",outputIndex);
			obj.put("o",stateMap);
		}
		getStream("state").append(obj,StreamConnection.FLUSH);