Permalink
Browse files

Merge pull request #186 from zznate/super_assume

Super assume
  • Loading branch information...
2 parents 4befa65 + 5e43115 commit 96aea24eafef077d86fc1b1c6624f6dcb7e481bd @zznate committed Apr 13, 2013
View
10 src/main/java/org/usergrid/vx/experimental/Operations.java
@@ -171,6 +171,16 @@ public static IntraOp assumeOp(String keyspace,String columnfamily,String type,
.set(CLAZZ, clazz );
}
+
+
+ public static IntraOp assumeColumnOp(String keyspace , String columnfamily, Object column, String clazz){
+ return new IntraOp(IntraOp.Type.ASSUME)
+ .set(KEYSPACE, keyspace)
+ .set(COLUMN_FAMILY, columnfamily)
+ .set(NAME, column) //should be column rowkey value
+ .set(CLAZZ, clazz );
+ }
+
public static IntraOp createProcessorOp(String name, String spec, String value){
return new IntraOp(IntraOp.Type.CREATEPROCESSOR)
.set(NAME,name)
View
63 src/main/java/org/usergrid/vx/server/operations/AssumeHandler.java
@@ -1,8 +1,23 @@
package org.usergrid.vx.server.operations;
+import java.nio.ByteBuffer;
+
+import org.apache.cassandra.utils.ByteBufferUtil;
import org.vertx.java.core.eventbus.Message;
import org.vertx.java.core.json.JsonObject;
+/*
+ keyspace, cf, type {"column"|"rowkey"|"value"} = class
+ default validator
+ default comparator
+ key comparator
+
+ keyspace, cf, columnname = class
+ column specific validator
+
+ keyspaec, cf, columnstart, column end, class
+ ranged validator
+ */
public class AssumeHandler extends AbstractIntravertHandler {
@Override
@@ -11,19 +26,45 @@ public void handleUser(Message<JsonObject> event) {
JsonObject params = event.body.getObject("op");
JsonObject state = event.body.getObject("state");
JsonObject meta = state.getObject("meta");
+ JsonObject metaColumn = state.getObject("metaColumn");
+ JsonObject metaRanged = state.getObject("metaranged");
if (meta == null) {
meta = new JsonObject();
}
- StringBuilder key = new StringBuilder();
- key.append(HandlerUtils.determineKs(params, state, null));
- key.append(' ');
- key.append(HandlerUtils.determineCf(params, state, null));
- key.append(' ');
- key.append(params.getString("type"));
- meta.putObject(key.toString(), new JsonObject()
- .putString("clazz", params.getString("clazz")));
- state.putObject("meta", meta);
- event.reply(new JsonObject().putString(id.toString(), "OK")
- .putObject("state", state));
+ if (metaColumn == null){
+ metaColumn = new JsonObject();
+ }
+ if (metaRanged == null){
+ metaRanged = new JsonObject();
+ }
+ if (params.getString("type") != null){
+ StringBuilder key = new StringBuilder();
+ key.append(HandlerUtils.determineKs(params, state, null));
+ key.append(' ');
+ key.append(HandlerUtils.determineCf(params, state, null));
+ key.append(' ');
+ key.append(params.getString("type"));
+ meta.putObject(key.toString(), new JsonObject()
+ .putString("clazz", params.getString("clazz")));
+ state.putObject("meta", meta);
+ event.reply(new JsonObject().putString(id.toString(), "OK")
+ .putObject("state", state));
+ } else if (params.getField("name") != null){
+ Object o = params.getField("name");
+ ByteBuffer bb = HandlerUtils.byteBufferForObject(o);
+ StringBuilder key = new StringBuilder();
+ key.append(HandlerUtils.determineKs(params, state, null));
+ key.append(' ');
+ key.append(HandlerUtils.determineCf(params, state, null));
+ key.append(' ');
+ key.append(ByteBufferUtil.bytesToHex(bb));
+ metaColumn.putObject(key.toString(), new JsonObject()
+ .putString("clazz", params.getString("clazz")));
+ state.putObject("metaColumn", metaColumn);
+ event.reply(new JsonObject().putString(id.toString(), "OK")
+ .putObject("state", state));
+ } else {
+ throw new RuntimeException("hit bottom this is bad ok");
+ }
}
}
View
34 src/main/java/org/usergrid/vx/server/operations/HandlerUtils.java
@@ -121,7 +121,7 @@ public static String determineKs(JsonObject params, JsonObject state, JsonObject
}
public static JsonArray readCf(ColumnFamily columnFamily, JsonObject state, JsonObject params) {
- System.out.println(state.toString());
+ System.out.println("state1"+ state);
JsonArray components = state.getArray("components");
JsonArray array = new JsonArray();
Iterator<IColumn> it = columnFamily.iterator();
@@ -147,7 +147,10 @@ public static JsonArray readCf(ColumnFamily columnFamily, JsonObject state, Json
if (ic instanceof CounterColumn) {
m.put("value", ((CounterColumn) ic).total());
} else {
- JsonObject valueMetadata = findMetaData(columnFamily, state, "value");
+ JsonObject valueMetadata = HandlerUtils.findColumnMetaData(columnFamily, state, ic.name().duplicate());
+ if (valueMetadata == null){
+ valueMetadata = findMetaData(columnFamily, state, "value");
+ }
if (valueMetadata == null) {
m.put("value", TypeHelper.getBytes(ic.value()));
} else {
@@ -187,10 +190,26 @@ public static JsonObject findMetaData(ColumnFamily cf, JsonObject state, String
return meta.getObject(key.toString());
}
}
-
+
+ public static JsonObject findColumnMetaData(ColumnFamily cf, JsonObject state, ByteBuffer name) {
+ StringBuilder key = new StringBuilder();
+ key.append(cf.metadata().ksName);
+ key.append(' ');
+ key.append(cf.metadata().cfName);
+ key.append(' ');
+ key.append(ByteBufferUtil.bytesToHex(name));
+ JsonObject meta = state.getObject("metaColumn");
+ if (meta!=null) {
+ return meta.getObject(key.toString());
+ } else {
+ return null;
+ }
+ }
+
public static void readCf(ColumnFamily columnFamily, JsonObject state, EventBus eb,
Handler<Message<JsonArray>> filterReplyHandler) {
JsonArray components = state.getArray("components");
+ System.out.println("state" + state);
JsonArray array = new JsonArray();
for (IColumn column : columnFamily) {
@@ -210,11 +229,14 @@ public static void readCf(ColumnFamily columnFamily, JsonObject state, EventBus
if (column instanceof CounterColumn) {
m.put("value", ((CounterColumn) column).total());
} else {
- JsonObject valueMetadata = findMetaData(columnFamily, state, "value");
- if (valueMetadata == null) {
+ JsonObject valueMetaData = HandlerUtils.findColumnMetaData(columnFamily, state, column.name().duplicate());
+ if (valueMetaData == null){
+ valueMetaData = findMetaData(columnFamily, state, "value");
+ }
+ if (valueMetaData == null) {
m.put("value", ByteBufferUtil.getArray(column.value()));
} else {
- String clazz = valueMetadata.getString("clazz");
+ String clazz = valueMetaData.getString("clazz");
m.put("value", TypeHelper.getTyped(clazz, column.value()));
}
}
View
4 src/main/java/org/usergrid/vx/server/operations/SetHandler.java
@@ -10,10 +10,10 @@
import org.vertx.java.core.eventbus.Message;
import org.vertx.java.core.json.JsonObject;
-public class SetHandler implements Handler<Message<JsonObject>> {
+public class SetHandler extends AbstractIntravertHandler{
@Override
- public void handle(Message<JsonObject> event) {
+ public void handleUser(Message<JsonObject> event) {
Integer id = event.body.getInteger("id");
JsonObject params = event.body.getObject("op");
JsonObject state = event.body.getObject("state");
View
87 src/test/java/org/usergrid/vx/experimental/AssumeITest.java
@@ -0,0 +1,87 @@
+package org.usergrid.vx.experimental;
+
+import java.util.List;
+import java.util.Map;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.usergrid.vx.client.IntraClient2;
+
+@RunWith(CassandraRunner.class)
+@RequiresKeyspace(ksName = "myks")
+@RequiresColumnFamily(ksName = "myks", cfName = "mycf")
+public class AssumeITest {
+
+ @Test
+ public void assumeTest() throws Exception {
+ IntraReq req = new IntraReq();
+ req.add(Operations.setKeyspaceOp("assks")); // 0
+ req.add(Operations.createKsOp("assks", 1)); // 1
+ req.add(Operations.createCfOp("asscf")); // 2
+ req.add(Operations.setColumnFamilyOp("asscf")); // 3
+ req.add(Operations.setAutotimestampOp(true)); // 4
+ req.add(Operations.assumeOp("assks", "asscf", "value", "UTF8Type"));// 5
+ req.add(Operations.setOp("rowa", "col1", "wow")); // 6
+ req.add(Operations.getOp("rowa", "col1")); // 7
+ IntraClient2 ic2 = new IntraClient2("localhost", 8080);
+ IntraRes res = ic2.sendBlocking(req);
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ List<Map> x = (List<Map>) res.getOpsRes().get(7);
+ System.out.println(res);
+ Assert.assertEquals("wow", x.get(0).get("value"));
+ }
+
+ @Test
+ public void assume2CfsTest() throws Exception {
+ IntraReq req = new IntraReq();
+ req.add(Operations.setKeyspaceOp("system"));
+ req.add(Operations.createKsOp("assks1", 1));
+ req.add(Operations.createKsOp("assks2", 1));
+
+ req.add(Operations.setKeyspaceOp("assks1"));
+ req.add(Operations.createCfOp("asscf1"));
+
+ req.add(Operations.setKeyspaceOp("assks2"));
+ req.add(Operations.createCfOp("asscf2"));
+ req.add(Operations.setAutotimestampOp(true));
+ req.add(Operations.assumeOp("assks1", "asscf1", "value", "UTF8Type"));
+ req.add(Operations.assumeOp("assks1", "asscf1", "column", "UTF8Type"));
+ req.add(Operations.assumeOp("assks2", "asscf2", "value", "Int32Type"));
+ req.add(Operations.assumeOp("assks2", "asscf2", "column", "Int32Type"));
+
+ req.add(Operations.setKeyspaceOp("assks1"));
+ req.add(Operations.setColumnFamilyOp("asscf1"));
+ req.add(Operations.setOp("rowa", "col1", "wow"));
+ req.add(Operations.getOp("rowa", "col1"));
+
+ req.add(Operations.setKeyspaceOp("assks2"));
+ req.add(Operations.setColumnFamilyOp("asscf2"));
+ req.add(Operations.setOp("rowa", 4, 3));
+ req.add(Operations.getOp("rowa", 4));
+
+ IntraClient2 ic2 = new IntraClient2("localhost", 8080);
+ IntraRes res = ic2.sendBlocking(req);
+ System.out.println(res);
+
+ }
+
+ @Test
+ @RequiresColumnFamily(ksName = "myks", cfName = "columnasscf")
+ public void columnAssumeTest() throws Exception{
+ IntraReq r = new IntraReq();
+ r.add( Operations.assumeColumnOp("myks", "columnasscf", "astring", "UTF8Type"))
+ .add( Operations.assumeColumnOp("myks", "columnasscf", "aint", "Int32Type"))
+ .add( Operations.setKeyspaceOp("myks"))
+ .add( Operations.setColumnFamilyOp("columnasscf"))
+ .add( Operations.setOp("arow", "astring", "wow") )
+ .add( Operations.setOp("arow", "aint", 5) )
+ .add( Operations.sliceOp("arow", "a", "b", 2));
+ IntraClient2 ic2 = new IntraClient2("localhost", 8080);
+ IntraRes res = ic2.sendBlocking(r);
+ System.out.println(res);
+ List<Map> x = (List<Map>) res.getOpsRes().get(6);
+ Assert.assertEquals(5, x.get(0).get("value"));
+ Assert.assertEquals("wow", x.get(1).get("value"));
+ }
+}
View
51 src/test/java/org/usergrid/vx/experimental/IntraServiceITest.java
@@ -119,57 +119,6 @@ public void exceptionHandleTest() throws Exception {
Assert.assertEquals(new Integer(1), res.getExceptionId());
}
- @Test
- public void assumeTest() throws Exception {
- IntraReq req = new IntraReq();
- req.add(Operations.setKeyspaceOp("assks")); // 0
- req.add(Operations.createKsOp("assks", 1)); // 1
- req.add(Operations.createCfOp("asscf")); // 2
- req.add(Operations.setColumnFamilyOp("asscf")); // 3
- req.add(Operations.setAutotimestampOp(true)); // 4
- req.add(Operations.assumeOp("assks", "asscf", "value", "UTF8Type"));// 5
- req.add(Operations.setOp("rowa", "col1", "wow")); // 6
- req.add(Operations.getOp("rowa", "col1")); // 7
- IntraClient2 ic2 = new IntraClient2("localhost", 8080);
- IntraRes res = ic2.sendBlocking(req);
- List<Map> x = (List<Map>) res.getOpsRes().get(7);
- System.out.println(res);
- Assert.assertEquals("wow", x.get(0).get("value"));
- }
-
- @Test
- public void assume2CfsTest() throws Exception {
- IntraReq req = new IntraReq();
- req.add(Operations.setKeyspaceOp("system"));
- req.add(Operations.createKsOp("assks1", 1));
- req.add(Operations.createKsOp("assks2", 1));
-
- req.add(Operations.setKeyspaceOp("assks1"));
- req.add(Operations.createCfOp("asscf1"));
-
- req.add(Operations.setKeyspaceOp("assks2"));
- req.add(Operations.createCfOp("asscf2"));
- req.add(Operations.setAutotimestampOp(true));
- req.add(Operations.assumeOp("assks1", "asscf1", "value", "UTF8Type"));
- req.add(Operations.assumeOp("assks1", "asscf1", "column", "UTF8Type"));
- req.add(Operations.assumeOp("assks2", "asscf2", "value", "Int32Type"));
- req.add(Operations.assumeOp("assks2", "asscf2", "column", "Int32Type"));
-
- req.add(Operations.setKeyspaceOp("assks1"));
- req.add(Operations.setColumnFamilyOp("asscf1"));
- req.add(Operations.setOp("rowa", "col1", "wow"));
- req.add(Operations.getOp("rowa", "col1"));
-
- req.add(Operations.setKeyspaceOp("assks2"));
- req.add(Operations.setColumnFamilyOp("asscf2"));
- req.add(Operations.setOp("rowa", 4, 3));
- req.add(Operations.getOp("rowa", 4));
-
- IntraClient2 ic2 = new IntraClient2("localhost", 8080);
- IntraRes res = ic2.sendBlocking(req);
- System.out.println(res);
-
- }
@Test
public void processorTest() throws Exception {

0 comments on commit 96aea24

Please sign in to comment.