Skip to content
This repository

Clearverb #37

Merged
merged 2 commits into from over 1 year ago

2 participants

edwardcapriolo Nate McCall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Dec 31, 2012
edwardcapriolo edwardcapriolo Almost 3152591
Jan 01, 2013
edwardcapriolo edwardcapriolo Use clear to clear unneeded result sets from response. 87196c1
This page is out of date. Refresh to see the latest.
19 src/main/java/org/usergrid/vx/experimental/IntraOp.java
@@ -191,7 +191,20 @@ public static IntraOp filterModeOp(String name, boolean on) {
191 191 i.set("on", on);
192 192 return i;
193 193 }
  194 +
  195 + public static IntraOp cqlQuery(String query, String version){
  196 + IntraOp i = new IntraOp(Type.CQLQUERY);
  197 + i.set("query", query);
  198 + i.set("version", version );
  199 + return i;
  200 + }
194 201
  202 + public static IntraOp clear(int resultId){
  203 + IntraOp i = new IntraOp(Type.CLEAR);
  204 + i.set("id", resultId);
  205 + return i;
  206 + }
  207 +
195 208 public Type getType() {
196 209 return type;
197 210 }
@@ -200,6 +213,8 @@ private static void checkForBlankStr(String arg, String msg, Type type) {
200 213 Preconditions.checkArgument(arg != null && arg.length() > 0,
201 214 "A non-blank '{}' is required for {}", new Object[]{msg,type});
202 215 }
  216 +
  217 +
203 218
204 219 public enum Type {
205 220 LISTCOLUMNFAMILY,
@@ -221,7 +236,9 @@ private static void checkForBlankStr(String arg, String msg, Type type) {
221 236 PROCESS,
222 237 DROPKEYSPACE,
223 238 CREATEFILTER,
224   - FILTERMODE;
  239 + FILTERMODE,
  240 + CQLQUERY,
  241 + CLEAR
225 242
226 243 }
227 244
64 src/main/java/org/usergrid/vx/experimental/IntraService.java
@@ -15,6 +15,7 @@
15 15 import org.apache.cassandra.config.CFMetaData;
16 16 import org.apache.cassandra.config.KSMetaData;
17 17 import org.apache.cassandra.config.Schema;
  18 +import org.apache.cassandra.cql3.QueryProcessor;
18 19 import org.apache.cassandra.db.ColumnFamily;
19 20 import org.apache.cassandra.db.ConsistencyLevel;
20 21 import org.apache.cassandra.db.DecoratedKey;
@@ -27,21 +28,32 @@
27 28 import org.apache.cassandra.db.SliceFromReadCommand;
28 29 import org.apache.cassandra.db.filter.ColumnSlice;
29 30 import org.apache.cassandra.db.filter.QueryPath;
  31 +import org.apache.cassandra.db.marshal.Int32Type;
  32 +import org.apache.cassandra.db.marshal.IntegerType;
30 33 import org.apache.cassandra.dht.IPartitioner;
31 34 import org.apache.cassandra.exceptions.ConfigurationException;
  35 +import org.apache.cassandra.exceptions.InvalidRequestException;
32 36 import org.apache.cassandra.exceptions.IsBootstrappingException;
33 37 import org.apache.cassandra.exceptions.OverloadedException;
34 38 import org.apache.cassandra.exceptions.ReadTimeoutException;
  39 +import org.apache.cassandra.exceptions.RequestExecutionException;
  40 +import org.apache.cassandra.exceptions.RequestValidationException;
35 41 import org.apache.cassandra.exceptions.UnavailableException;
36 42 import org.apache.cassandra.exceptions.WriteTimeoutException;
  43 +import org.apache.cassandra.service.ClientState;
37 44 import org.apache.cassandra.service.MigrationManager;
  45 +import org.apache.cassandra.service.QueryState;
38 46 import org.apache.cassandra.service.StorageProxy;
39 47 import org.apache.cassandra.service.StorageService;
40 48 import org.apache.cassandra.thrift.CassandraServer;
41 49 import org.apache.cassandra.thrift.CfDef;
  50 +import org.apache.cassandra.thrift.Column;
42 51 import org.apache.cassandra.thrift.ColumnParent;
43 52 import org.apache.cassandra.thrift.ColumnPath;
  53 +import org.apache.cassandra.thrift.CqlResult;
  54 +import org.apache.cassandra.thrift.CqlRow;
44 55 import org.apache.cassandra.thrift.KsDef;
  56 +import org.apache.cassandra.transport.messages.ResultMessage;
45 57 import org.apache.cassandra.utils.ByteBufferUtil;
46 58 import org.vertx.java.core.Handler;
47 59 import org.vertx.java.core.Vertx;
@@ -106,6 +118,10 @@ protected boolean executeReq(IntraReq req, IntraRes res, IntraState state, Vertx
106 118 createFilter(req,res,state,i,vertx);
107 119 } else if (op.getType().equals(IntraOp.Type.FILTERMODE)){
108 120 filterMode(req,res,state,i,vertx);
  121 + } else if (op.getType().equals(IntraOp.Type.CQLQUERY)){
  122 + cqlQuery(req,res,state,i,vertx);
  123 + } else if (op.getType().equals(IntraOp.Type.CLEAR)){
  124 + clear(req,res,state,i,vertx);
109 125 }
110 126 } catch (Exception ex){
111 127 res.setExceptionAndId(ex,i);
@@ -154,7 +170,8 @@ ByteBuffer byteBufferForObject(Object o){
154 170 byte [] entireComp = CompositeTool.makeComposite(b, sep);
155 171 return ByteBuffer.wrap(entireComp);
156 172 } else if (o instanceof Integer){
157   - return ByteBufferUtil.bytes( ((Integer) o).intValue());
  173 + return Int32Type.instance.decompose( (Integer)o);
  174 + //return ByteBufferUtil.bytes( ((Integer) o).intValue());
158 175 } else if (o instanceof String){
159 176 return ByteBufferUtil.bytes((String) o);
160 177 } else if (o instanceof byte[] ) {
@@ -460,4 +477,49 @@ private void process(IntraReq req, IntraRes res, IntraState state, int i,Vertx v
460 477 List<Map> results = p.process(toProcess);
461 478 res.getOpsRes().put(i, results);
462 479 }
  480 +
  481 + private void cqlQuery(IntraReq req, IntraRes res, IntraState state, int i,Vertx vertx) {
  482 + IntraOp op = req.getE().get(i);
  483 + ClientState clientState = new ClientState();
  484 + try {
  485 + clientState.setCQLVersion((String) op.getOp().get("version"));
  486 + clientState.setKeyspace(state.currentKeyspace);
  487 + } catch (InvalidRequestException e) {
  488 + res.setExceptionAndId(e, i);
  489 + return;
  490 + }
  491 + QueryState queryState = new QueryState(clientState);
  492 + ResultMessage rm = null;
  493 + try {
  494 + rm = QueryProcessor.process((String) op.getOp().get("query"), state.consistency, queryState);
  495 + } catch (RequestExecutionException e) {
  496 + res.setExceptionAndId(e, i);
  497 + return;
  498 + } catch (RequestValidationException e) {
  499 + res.setExceptionAndId(e, i);
  500 + return;
  501 + }
  502 + //ToDo maybe processInternal
  503 + CqlResult result = rm.toThriftResult();
  504 +
  505 + List<CqlRow> rows = result.getRows();
  506 + List<HashMap> returnRows = new ArrayList<HashMap>();
  507 + for (CqlRow row: rows){
  508 + List<Column> columns = row.getColumns();
  509 + for (Column c: columns){
  510 + HashMap m = new HashMap();
  511 + m.put("value", c.value);
  512 + m.put("name", c.name);
  513 + returnRows.add(m);
  514 + }
  515 + }
  516 + res.getOpsRes().put(i,returnRows);
  517 + }
  518 +
  519 + private void clear(IntraReq req, IntraRes res, IntraState state, int i,Vertx vertx) {
  520 + IntraOp op = req.getE().get(i);
  521 + int id = (Integer) op.getOp().get("id");
  522 + res.getOpsRes().put(id, new ArrayList<HashMap>());
  523 +
  524 + }
463 525 }
52 src/test/java/org/usergrid/vx/experimental/IntraServiceTest.java
@@ -12,10 +12,12 @@
12 12 import java.util.Map;
13 13 import java.util.Set;
14 14
  15 +import org.apache.cassandra.db.marshal.IntegerType;
15 16 import org.apache.cassandra.service.CassandraDaemon;
16 17 import org.apache.cassandra.utils.ByteBufferUtil;
17 18 import org.junit.Assert;
18 19 import org.junit.BeforeClass;
  20 +import org.junit.Ignore;
19 21 import org.junit.Test;
20 22 import org.junit.runner.RunWith;
21 23 import org.usergrid.vx.server.IntravertCassandraServer;
@@ -191,6 +193,7 @@ public void intTest() throws CharacterCodingException{
191 193 IntraRes res = new IntraRes();
192 194 is.handleIntraReq(req, res, x);
193 195 List<Map> x = (List<Map>) res.getOpsRes().get(8);
  196 +
194 197 Assert.assertEquals( "wow", x.get(0).get("value") );
195 198 Assert.assertEquals( 1, x.get(0).get("name") );
196 199 }
@@ -216,5 +219,54 @@ public void compositeTest() throws CharacterCodingException{
216 219 Assert.assertEquals( 2, ((Object [])x.get(0).get("value"))[1] );
217 220 }
218 221
  222 +
  223 + @Ignore
  224 + public void CqlTest() throws CharacterCodingException{
  225 + IntraReq req = new IntraReq();
  226 + req.add( IntraOp.setKeyspaceOp("cqlks") ); //0
  227 + req.add( IntraOp.createKsOp("cqlks", 1)); //1
  228 + req.add( IntraOp.createCfOp("cqlcf")); //2
  229 + req.add( IntraOp.setColumnFamilyOp("cqlcf") ); //3
  230 + req.add( IntraOp.setAutotimestampOp() ); //4
  231 + req.add( IntraOp.assumeOp("cqlks", "cqlcf", "value", "int32"));//5
  232 + req.add( IntraOp.assumeOp("cqlks", "cqlcf", "column", "int32"));//6
  233 + req.add( IntraOp.setOp("rowa", 1, 2)); //7
  234 + req.add( IntraOp.getOp("rowa", 1)); //8
  235 + req.add( IntraOp.cqlQuery("select * from cqlcf", "3.0.0"));//9
  236 +
  237 + IntraRes res = new IntraRes();
  238 + is.handleIntraReq(req, res, x);
  239 + List<Map> x = (List<Map>) res.getOpsRes().get(8);
  240 + Assert.assertEquals( 1, x.get(0).get("name") );
  241 + Assert.assertEquals( 2, x.get(0).get("value") );
  242 + x = (List<Map>) res.getOpsRes().get(9);
  243 + Assert.assertEquals( 2, IntegerType.instance.compose((ByteBuffer)x.get(0).get("value")) );
  244 +
  245 + }
219 246
  247 +
  248 + @Test
  249 + public void clearTest() throws CharacterCodingException{
  250 + IntraReq req = new IntraReq();
  251 + req.add( IntraOp.setKeyspaceOp("clearks") ); //0
  252 + req.add( IntraOp.createKsOp("clearks", 1)); //1
  253 + req.add( IntraOp.createCfOp("clearcf")); //2
  254 + req.add( IntraOp.setColumnFamilyOp("clearcf") ); //3
  255 + req.add( IntraOp.setAutotimestampOp() ); //4
  256 + req.add( IntraOp.assumeOp("clearks", "clearcf", "value", "UTF-8")); //5
  257 + req.add( IntraOp.setOp("rowa", 1, "wow")); //6
  258 + req.add( IntraOp.getOp("rowa", 1)); //7
  259 + req.add( IntraOp.getOp("rowa", 1)); //8
  260 + req.add( IntraOp.clear(8)); //9
  261 +
  262 + IntraRes res = new IntraRes();
  263 + is.handleIntraReq(req, res, x);
  264 +
  265 + List<Map> x = (List<Map>) res.getOpsRes().get(7);
  266 + Assert.assertEquals( "wow", x.get(0).get("value") );
  267 +
  268 + x = (List<Map>) res.getOpsRes().get(8);
  269 + Assert.assertEquals(0, x.size());
  270 + }
  271 +
220 272 }

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.