package performance.nameByPrefix;

import java.net.URL;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import us.kbase.common.service.ServerException;
import us.kbase.common.service.UObject;
import us.kbase.workspace.CloneWorkspaceParams;
import us.kbase.workspace.CreateWorkspaceParams;
import us.kbase.workspace.ObjectSaveData;
import us.kbase.workspace.SaveObjectsParams;
import us.kbase.workspace.WorkspaceClient;
import us.kbase.workspace.WorkspaceIdentity;

/** Set up objects to test speed of the name_by_prefix function. Assumes
 *  Empty.AType-0.1 (which has no restrictions on the save object, e.g. 1
 *  optional field) exists in the target workspace.
 * @author gaprice@lbl.gov
 *
 */
public class NamebyPrefix {
	
	public static final String TYPE = "Empty.AType-0.1";
	public static final String wsURL = "http://localhost:7058";
	public static final String ORIGINAL_WORKSPACE_NAME =
			"getnamesbyprefix_original";
	public static final int CLONE_COUNT = 9;
	public static final int READ_TIME_OUT = 60 * 60 * 1000; // 1hr in ms
	
	public static String PASSWORD;
	
	public static void main(String[] args) throws Exception {
		PASSWORD = args[0];
		try {
			loadOneMillion();
		} catch (ServerException se) {
			System.out.println(se.getData());
			throw se;
		}
	}
	
	private static String intToName(int in) {
		final char[] seq = new char[4];
		final int start = (int) 'a';
		final int abc = 26;
		for (int i = seq.length - 1; i >= 0; i--) {
			seq[i] = (char)(start + in % abc);
			in = in / abc;
		}
		return new String(seq);
	}
	
	private static void loadOneMillion() throws Exception {
		WorkspaceClient cli = new WorkspaceClient(new URL(wsURL), "kbasetest",
				PASSWORD);
		cli.setIsInsecureHttpConnectionAllowed(true);
		cli.setConnectionReadTimeOut(READ_TIME_OUT);
		cli.createWorkspace(new CreateWorkspaceParams().withGlobalread("r")
				.withWorkspace(ORIGINAL_WORKSPACE_NAME));
		List<ObjectSaveData> o = new LinkedList<ObjectSaveData>();
		for (int i = 0; i < 100000; i++) {
			o.add(new ObjectSaveData()
					.withData(new UObject(new HashMap<String, String>()))
					.withName(intToName(i))
					.withType(TYPE));
		}
		System.out.println("saving 100000 objects");
		cli.saveObjects(new SaveObjectsParams()
				.withWorkspace(ORIGINAL_WORKSPACE_NAME)
				.withObjects(o));
		
		
		for (int i = 1; i <= CLONE_COUNT; i++) {
			System.out.println(String.format(
					"Clone %s of %s.", i, CLONE_COUNT));
			cli.cloneWorkspace(new CloneWorkspaceParams().withGlobalread("r")
					.withWsi(new WorkspaceIdentity()
							.withWorkspace(ORIGINAL_WORKSPACE_NAME))
					.withWorkspace(ORIGINAL_WORKSPACE_NAME + (i + 1)));
			}
		System.out.println("Done");

	}

}

@@ -2175,55 +2175,149 @@
 %7D%0A%09%09
-System.out.println(%22saving 100000 objects%22
+for (int i = 0; i %3C 10; i++) %7B%0A%09%09%09System.out.println(String.format(%22saving objects from %25s to %25s%22,%0A%09%09%09%09%09i * 10000, (i + 1) * 10000 - 1)
 );%0A%09%09
+%09
 cli.
@@ -2352,16 +2352,17 @@
 arams()%0A
+%09
 %09%09%09%09.wit
@@ -2405,28 +2405,108 @@
 %09%09%09%09
-.withObjects(o
+%09// sublist is inclusive to exclusive%0A%09%09%09%09%09.withObjects(o.subList(i * 10000, (i + 1) * 10000)
 ));%0A%09%09
+%7D
 %0A%09%09%0A

Save objects in 10K vs 100K batches.