Permalink
Browse files

initial replace of some common operations with HandlerUtils. Closes #112

  • Loading branch information...
1 parent 7b699ce commit ae7039745965e9f740a5ec18de79ef3423ea5a7a @zznate committed Feb 25, 2013
@@ -17,8 +17,8 @@ public void handle(Message<JsonObject> event) {
meta = new JsonObject();
}
meta.putObject(params.getString("type"), new JsonObject()
- .putString("keyspace", params.getString("keyspace"))
- .putString("columnfamily", params.getString("columnfamily"))
+ .putString("keyspace", HandlerUtils.determineKs(params, state))
+ .putString("columnfamily", HandlerUtils.determineCf(params, state))
.putString("type", params.getString("type"))
.putString("clazz", params.getString("clazz")));
state.putObject("meta", meta);
@@ -31,7 +31,7 @@ public void handle(Message<JsonObject> event) {
ClientState clientState = new ClientState();
try {
clientState.setCQLVersion(params.getString("version"));
- clientState.setKeyspace(state.getString("currentKeyspace"));
+ clientState.setKeyspace(HandlerUtils.determineKs(params,state));
} catch (InvalidRequestException e) {
event.reply(new JsonObject().putString(id.toString(), e.getMessage()));
return;
@@ -44,9 +44,9 @@ public void handle(Message<JsonObject> event) {
id));
ByteBuffer column = IntraService.byteBufferForObject(IntraService.resolveObject(nameParam, null, null, null,
id));
- QueryPath path = new QueryPath(determineCf(params, state), null);
+ QueryPath path = new QueryPath(HandlerUtils.determineCf(params, state), null);
List<ByteBuffer> nameAsList = Arrays.asList(column);
- ReadCommand command = new SliceByNamesReadCommand(determineKs(params, state), rowkey, path, nameAsList);
+ ReadCommand command = new SliceByNamesReadCommand(HandlerUtils.determineKs(params, state), rowkey, path, nameAsList);
List<Row> rows = null;
try {
@@ -56,7 +56,7 @@ public void handle(Message<JsonObject> event) {
ColumnFamily cf1 = rows.get(0).cf;
if (cf1 == null) { // cf= null is no data
} else {
- readCf(cf1, finalResults, state, params);
+ HandlerUtils.readCf(cf1, finalResults, state, params);
}
JsonObject response = new JsonObject();
@@ -71,58 +71,6 @@ public void handle(Message<JsonObject> event) {
}
}
- private String determineKs(JsonObject params, JsonObject state) {
- String ks = null;
- if (params.getString("keyspace") != null) {
- ks = params.getString("keyspace");
- } else {
- ks = state.getString("currentKeyspace");
- }
- return ks;
- }
-
- private String determineCf(JsonObject params, JsonObject state) {
- String cf = null;
- if (params.getString("columnfamily") != null) {
- cf = params.getString("columnfamily");
- } else {
- cf = state.getString("currentColumnFamily");
- }
- return cf;
- }
- private void readCf(ColumnFamily columnFamily, List<Map> finalResults, JsonObject state, JsonObject params) {
- Iterator<IColumn> it = columnFamily.iterator();
- while (it.hasNext()) {
- IColumn ic = it.next();
- if (ic.isLive()) {
- HashMap m = new HashMap();
- JsonArray components = state.getArray("components");
- if (components.contains("name")) {
- String clazz = state.getObject("meta").getObject("column").getString("clazz");
- m.put("name", TypeHelper.getTyped(clazz, ic.name()));
- }
- if (components.contains("value")) {
- String clazz = state.getObject("meta").getObject("value").getString("clazz");
- m.put("value", TypeHelper.getTyped(clazz, ic.value()));
- }
- if (components.contains("timestamp")) {
- m.put("timestamp", ic.timestamp());
- }
- if (components.contains("markeddelete")) {
- m.put("markeddelete", ic.getMarkedForDeleteAt());
- }
-// if (state.currentFilter != null) {
-// Map newMap = (Map) state.currentFilter.filter(m);
-// if (newMap != null) {
-// finalResults.add(newMap);
-// }
-// } else {
-// finalResults.add(m);
-// }
- finalResults.add(m);
- }
- }
- }
}
@@ -0,0 +1,67 @@
+package org.usergrid.vx.server.operations;
+
+import org.apache.cassandra.db.ColumnFamily;
+import org.apache.cassandra.db.IColumn;
+import org.usergrid.vx.experimental.TypeHelper;
+import org.vertx.java.core.json.JsonArray;
+import org.vertx.java.core.json.JsonObject;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author zznate
+ */
+public class HandlerUtils {
+
+ public static String determineCf(JsonObject params, JsonObject state) {
+ String cf = null;
+ if (params.getString("columnfamily") != null) {
+ cf = params.getString("columnfamily");
+ } else {
+ cf = state.getString("currentColumnFamily");
+ }
+ return cf;
+ }
+
+ public static String determineKs(JsonObject params, JsonObject state) {
+ String ks = null;
+ if (params.getString("keyspace") != null) {
+ ks = params.getString("keyspace");
+ } else {
+ ks = state.getString("currentKeyspace");
+ }
+ return ks;
+ }
+
+ public static void readCf(ColumnFamily columnFamily, List<Map> finalResults, JsonObject state, JsonObject params) {
+ Iterator<IColumn> it = columnFamily.iterator();
+ while (it.hasNext()) {
+ IColumn ic = it.next();
+ if (ic.isLive()) {
+ HashMap m = new HashMap();
+ JsonArray components = state.getArray("components");
+
+ if (components.contains("name")) {
+ String clazz = state.getObject("meta").getObject("column").getString("clazz");
+ m.put("name", TypeHelper.getTyped(clazz, ic.name()));
+ }
+ if (components.contains("value")) {
+ String clazz = state.getObject("meta").getObject("value").getString("clazz");
+ m.put("value", TypeHelper.getTyped(clazz, ic.value()));
+ }
+ if (components.contains("timestamp")) {
+ m.put("timestamp", ic.timestamp());
+ }
+ if (components.contains("markeddelete")) {
+ m.put("markeddelete", ic.getMarkedForDeleteAt());
+ }
+
+ finalResults.add(m);
+ }
+ }
+ }
+
+}
@@ -22,22 +22,12 @@ public void handle(Message<JsonObject> event) {
JsonObject params = event.body.getObject("op");
JsonObject state = event.body.getObject("state");
- RowMutation rm = null;
- String ks = null;
- String cf = null;
+ RowMutation rm = new RowMutation(HandlerUtils.determineKs(params,state),
+ IntraService.byteBufferForObject(params.getString("rowkey")));
+ QueryPath qp = new QueryPath(HandlerUtils.determineCf(params,state),
+ null,
+ IntraService.byteBufferForObject(params.getString("name")));
- ks = params.getString("keyspace");
- if (ks == null) {
- ks = state.getString("currentKeyspace");
- }
-
- cf = params.getString("columnfamily");
- if (cf == null) {
- cf = state.getString("currentColumnFamily");
- }
-
- rm = new RowMutation(ks, IntraService.byteBufferForObject(params.getString("rowkey")));
- QueryPath qp = new QueryPath(cf, null, IntraService.byteBufferForObject(params.getString("name")));
Object val = params.toMap().get("value");
Integer ttl = params.getInteger("ttl");
@@ -11,7 +11,7 @@ public void handle(Message<JsonObject> event) {
Integer id = event.body.getInteger("id");
JsonObject params = event.body.getObject("op");
JsonObject state = event.body.getObject("state");
- state.putString("currentKeyspace", params.getString("keyspace"));
+ state.putString("currentKeyspace", HandlerUtils.determineKs(params,state));
event.reply(new JsonObject()
.putString(id.toString(), "OK")
@@ -47,10 +47,10 @@ public void handle(Message<JsonObject> event) {
ByteBuffer end = IntraService.byteBufferForObject(IntraService.resolveObject(endParam, null, null, null, id));
List<ReadCommand> commands = new ArrayList<ReadCommand>(1);
- ColumnPath cp = new ColumnPath();
- cp.setColumn_family(state.getString("currentColumnFamily"));
- QueryPath qp = new QueryPath(cp);
- SliceFromReadCommand sr = new SliceFromReadCommand(state.getString("currentKeyspace"), rowkey, qp, start, end,
+
+ QueryPath path = new QueryPath(HandlerUtils.determineCf(params, state), null);
+
+ SliceFromReadCommand sr = new SliceFromReadCommand(state.getString("currentKeyspace"), rowkey, path, start, end,
false, 100);
commands.add(sr);
@@ -62,7 +62,7 @@ public void handle(Message<JsonObject> event) {
ColumnFamily cf = results.get(0).cf;
if (cf == null){ //cf= null is no data
} else {
- readCf(cf, finalResults, state, params);
+ HandlerUtils.readCf(cf, finalResults, state, params);
}
JsonObject response = new JsonObject();
@@ -77,38 +77,5 @@ public void handle(Message<JsonObject> event) {
}
}
- private void readCf(ColumnFamily columnFamily, List<Map> finalResults, JsonObject state, JsonObject params) {
- Iterator<IColumn> it = columnFamily.iterator();
- while (it.hasNext()) {
- IColumn ic = it.next();
- if (ic.isLive()) {
- HashMap m = new HashMap();
- JsonArray components = state.getArray("components");
- if (components.contains("name")) {
- String clazz = state.getObject("meta").getObject("column").getString("clazz");
- m.put("name", TypeHelper.getTyped(clazz, ic.name()));
- }
- if (components.contains("value")) {
- String clazz = state.getObject("meta").getObject("value").getString("clazz");
- m.put("value", TypeHelper.getTyped(clazz, ic.value()));
- }
- if (components.contains("timestamp")) {
- m.put("timestamp", ic.timestamp());
- }
- if (components.contains("markeddelete")) {
- m.put("markeddelete", ic.getMarkedForDeleteAt());
- }
-// if (state.currentFilter != null) {
-// Map newMap = (Map) state.currentFilter.filter(m);
-// if (newMap != null) {
-// finalResults.add(newMap);
-// }
-// } else {
-// finalResults.add(m);
-// }
- finalResults.add(m);
- }
- }
- }
}

0 comments on commit ae70397

Please sign in to comment.