Commits

Anonymous committed 5fae07f

walk() for java client, plus some tests

Comments (0)

Files changed (2)

client_lib/java/src/com/basho/riak/JiakClient.java

 import java.util.List;
 import java.util.Map;
 
+import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
 		return requestConn;
 	}
 
+	/**
+	 * Perform a map/reduce link walking operation and return a list of lists of
+	 * JiakObjects for which the "accumulate" flag is true. (see documentation
+	 * on walkSpec below).
+	 * 
+	 * @param bucket
+	 *            The bucket of the "starting object"
+	 * @param key
+	 *            The key of the "starting object"
+	 * @param walkSpec
+	 *            A URL-path (omit beginning /) of the form
+	 *            <code>bucket,tag-spec,accumulateFlag</code> The
+	 *            <code>tag-spec "_"</code> matches all tags.
+	 *            <code>accumulateFlag</code> is either the String "1" or "0".
+	 * @return An <code>ArrayList</code> of <code>ArrayLists</code>, where each
+	 *         sub-list corresponds to a <code>walkSpec</code> element that had
+	 *         <code>accumulateFlag</code> equal to 1.
+	 * @throws IOException
+	 *             If an error occurs communicating with the Riak server.
+	 * @throws JSONException
+	 *             If an error occurs unparsing JSON requests or responses.
+	 * @throws JiakException
+	 *             If the Riak server returns an error or unexpected response
+	 *             code.
+	 */
+	public ArrayList<ArrayList<JiakObject>> walk(final String bucket,
+			final String key, final String walkSpec) throws IOException,
+			JSONException, JiakException {
+		final ArrayList<ArrayList<JiakObject>> results = new ArrayList<ArrayList<JiakObject>>();
+		final String reqURI = makeURI(bucket + "/" + key + "/" + walkSpec);
+		final Map<String, String> reqHeaders = new HashMap<String, String>();
+		reqHeaders.put("Accept", "application/json");
+		final HttpURLConnection requestConn = doRequest("GET", reqURI, null,
+				reqHeaders);
+		final int responseCode = requestConn.getResponseCode();
+		if (responseCode == 404)
+			return null;
+		final JSONArray jsonResults = expect(200, requestConn).getJSONArray(
+				"results");
+		for (int i = 0; i < jsonResults.length(); ++i) {
+			final ArrayList<JiakObject> ra = new ArrayList<JiakObject>();
+			final JSONArray ja = jsonResults.getJSONArray(i);
+			for (int j = 0; j < ja.length(); ++j) {
+				final JSONObject json = ja.getJSONObject(j);
+				final JiakObject jo = new JiakObject(json.getString("bucket"),
+						json.getString("key"));
+				jo.update(json);
+				ra.add(jo);
+			}
+			results.add(ra);
+		}
+		return results;
+	}
+
 	protected JSONObject expect(final int responseCode,
 			final HttpURLConnection connection) throws JSONException,
 			JiakException, IOException {

client_lib/java/src/com/basho/riak/JiakTest.java

+package com.basho.riak;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.json.JSONArray;
+
+public class JiakTest {
+
+	public static void main(String[] args) throws Exception {
+		JiakClient client = new JiakClient("127.0.0.1", "8098");
+		ArrayList<String> allKeys = new ArrayList<String>();
+		allKeys.add("testkey");
+		allKeys.add("jroot");
+		allKeys.add("jleaf1");
+		allKeys.add("jleaf2");
+		allKeys.add("jleaf3");
+		for (String k : allKeys) {
+			try {
+				client.delete("jiak_example", k);
+			}
+			catch (Exception e) {}
+		}
+		JiakObject jo = new JiakObject("jiak_example", "testkey");
+		jo.set("foo", 2);
+		client.store(jo);
+		jo = client.fetch("jiak_example", "testkey");
+		assert(jo.get("foo").equals(2));
+		JiakObject jRoot = new JiakObject("jiak_example", "jroot");
+		jRoot.set("foo", 0);
+		JiakObject jLeaf1 = new JiakObject("jiak_example", "jleaf1");
+		jLeaf1.set("foo", "in results");
+		JiakObject jLeaf2 = new JiakObject("jiak_example", "jleaf2");
+		jLeaf2.set("foo", "in results");
+		JiakObject jLeaf3 = new JiakObject("jiak_example", "jleaf3");
+		jLeaf3.set("foo", "not in results");
+		JSONArray links = new JSONArray();
+		links.put(new String[]{"jiak_example", "jleaf1", "tag_one"});
+		links.put(new String[]{"jiak_example", "jleaf2", "tag_one"});		
+		links.put(new String[]{"jiak_example", "jleaf3", "tag_other"});				
+		jRoot.setLinks(links);
+		client.store(jRoot);
+		client.store(jLeaf1);
+		client.store(jLeaf2);
+		client.store(jLeaf3);
+		ArrayList<ArrayList<JiakObject>> res = client.walk("jiak_example", "jroot", "jiak_example,tag_one,1");
+		for (ArrayList<JiakObject> i : res) {
+			for (JiakObject j: i) {
+				assert(j.get("foo").equals("in results"));
+			}
+		}
+		for (String k : allKeys) {
+			try {
+				client.delete("jiak_example", k);
+			}
+			catch (Exception e) {}
+		}
+		System.out.println("all tests passed");
+	}
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.