Permalink
Browse files

Merge pull request #146 from boneill42/virgil-rest-handler

Initial design / flow for REST handler.
  • Loading branch information...
2 parents 4ee1981 + 61f12fb commit 04481d29c1a19b7c34a94f9298dde4001ac4e604 @zznate committed Mar 6, 2013
Showing with 764 additions and 226 deletions.
  1. +3 −0 examples/json/create_schema.sh
  2. +3 −0 examples/rest/list_column_families.sh
  3. +3 −0 examples/rest/list_keyspaces.sh
  4. +3 −0 examples/rest/upsert_column.sh
  5. +3 −0 examples/say_hello.sh
  6. +2 −0 src/main/java/org/usergrid/vx/experimental/IntraOp.java
  7. +2 −0 src/main/java/org/usergrid/vx/experimental/IntraState.java
  8. +12 −5 src/main/java/org/usergrid/vx/experimental/Operations.java
  9. +31 −24 src/main/java/org/usergrid/vx/experimental/TypeHelper.java
  10. +16 −0 src/main/java/org/usergrid/vx/experimental/multiprocessor/FactoryProvider.java
  11. +1 −1 src/main/java/org/usergrid/vx/experimental/{ → multiprocessor}/MultiProcessor.java
  12. +6 −0 src/main/java/org/usergrid/vx/experimental/multiprocessor/MultiProcessorFactory.java
  13. +23 −0 src/main/java/org/usergrid/vx/experimental/multiprocessor/groovy/GroovyMultiProcessor.java
  14. +26 −0 src/main/java/org/usergrid/vx/experimental/multiprocessor/groovy/GroovyMultiProcessorFactory.java
  15. +19 −0 src/main/java/org/usergrid/vx/experimental/processor/FactoryProvider.java
  16. +1 −1 src/main/java/org/usergrid/vx/experimental/{ → processor}/Processor.java
  17. +5 −0 src/main/java/org/usergrid/vx/experimental/processor/ProcessorFactory.java
  18. +23 −0 src/main/java/org/usergrid/vx/experimental/processor/groovy/GroovyClProcessorFactory.java
  19. +23 −0 src/main/java/org/usergrid/vx/experimental/processor/groovy/GroovyProcessor.java
  20. +27 −0 src/main/java/org/usergrid/vx/experimental/processor/groovy/GroovyProcessorFactory.java
  21. +17 −4 src/main/java/org/usergrid/vx/handler/http/OperationsRequestHandler.java
  22. +39 −11 src/main/java/org/usergrid/vx/handler/rest/IntraHandlerRest.java
  23. +40 −18 src/main/java/org/usergrid/vx/handler/rest/IntravertRestUtils.java
  24. +38 −7 src/main/java/org/usergrid/vx/server/IntravertCassandraServer.java
  25. +7 −6 src/main/java/org/usergrid/vx/server/operations/AssumeHandler.java
  26. +45 −0 src/main/java/org/usergrid/vx/server/operations/BatchHandler.java
  27. +2 −2 src/main/java/org/usergrid/vx/server/operations/CounterHandler.java
  28. +76 −53 src/main/java/org/usergrid/vx/server/operations/CqlQueryHandler.java
  29. +7 −3 src/main/java/org/usergrid/vx/server/operations/CreateColumnFamilyHandler.java
  30. +53 −0 src/main/java/org/usergrid/vx/server/operations/CreateProcessorHandler.java
  31. +2 −2 src/main/java/org/usergrid/vx/server/operations/GetHandler.java
  32. +56 −20 src/main/java/org/usergrid/vx/server/operations/HandlerUtils.java
  33. +39 −0 src/main/java/org/usergrid/vx/server/operations/ProcessorHandler.java
  34. +3 −9 src/main/java/org/usergrid/vx/server/operations/SetHandler.java
  35. +1 −1 src/main/java/org/usergrid/vx/server/operations/SetKeyspaceHandler.java
  36. +1 −1 src/main/java/org/usergrid/vx/server/operations/SliceHandler.java
  37. +1 −1 src/main/resources/cassandra.yaml
  38. +104 −56 src/test/java/org/usergrid/vx/experimental/IntraServiceITest.java
  39. +1 −1 src/test/java/org/usergrid/vx/handler/rest/IntravertRestUtilsUnitTest.java
@@ -0,0 +1,3 @@
+echo "Creating keyspace 'myks' and column family 'mycf'"
+curl -vX POST http://localhost:8080/myapp/intrareq-json -d "{\"e\":[ {\"type\":\"CREATEKEYSPACE\",\"op\":{\"name\":\"myks\",\"replication\":1}}, {\"type\":\"CREATECOLUMNFAMILY\",\"op\":{\"name\":\"mycf\"}} ]}"
+echo
@@ -0,0 +1,3 @@
+echo "Listing column families."
+curl -X GET http://localhost:8080/myapp/intrareq-rest/myks/
+echo
@@ -0,0 +1,3 @@
+echo "Listing keyspaces."
+curl -X GET http://localhost:8080/myapp/intrareq-rest/
+echo
@@ -0,0 +1,3 @@
+echo "Creating keyspace 'myks' and column family 'mycf'"
+curl -vX POST http://localhost:8080/myapp/intrareq-rest/myks/mycf/myrow/mycol
+echo
@@ -0,0 +1,3 @@
+echo "Saying hello to Intravert, should return ok"
+curl http://localhost:8080/myapp/hello
+echo
@@ -57,6 +57,8 @@
import org.usergrid.vx.experimental.filter.FactoryProvider;
import org.usergrid.vx.experimental.filter.Filter;
import org.usergrid.vx.experimental.filter.FilterFactory;
+import org.usergrid.vx.experimental.multiprocessor.MultiProcessor;
+import org.usergrid.vx.experimental.processor.Processor;
import org.usergrid.vx.experimental.scan.ScanFilter;
import org.vertx.java.core.Vertx;
@@ -17,6 +17,8 @@
import org.apache.cassandra.db.ConsistencyLevel;
import org.usergrid.vx.experimental.filter.Filter;
+import org.usergrid.vx.experimental.multiprocessor.MultiProcessor;
+import org.usergrid.vx.experimental.processor.Processor;
import org.usergrid.vx.experimental.scan.ScanContext;
import org.usergrid.vx.experimental.scan.ScanFilter;
@@ -15,16 +15,15 @@
*/
package org.usergrid.vx.experimental;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableMap;
+import org.apache.cassandra.db.ConsistencyLevel;
+
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableMap;
-
-import org.apache.cassandra.db.ConsistencyLevel;
-
/**
* Factory class for building IntraOp objects
* @author zznate
@@ -55,6 +54,7 @@
private static final String ON = "on";
private static final String QUERY = "query";
private static final String VERSION = "version";
+ private static final String TRANSPOSE = "transpose";
private static final String ID = "id";
private static final String MODE = "mode";
private static final String SAVE = "save";
@@ -210,6 +210,13 @@ public static IntraOp cqlQuery(String query, String version){
.set(VERSION, version);
}
+ public static IntraOp cqlQuery(String query, String version, boolean transpose) {
+ return new IntraOp(IntraOp.Type.CQLQUERY)
+ .set(QUERY, query)
+ .set(VERSION, version)
+ .set(TRANSPOSE, transpose);
+ }
+
public static IntraOp clear(int resultId){
return new IntraOp(IntraOp.Type.CLEAR)
.set(ID, resultId);
@@ -15,41 +15,48 @@
*/
package org.usergrid.vx.experimental;
-import java.nio.ByteBuffer;
-
import org.apache.cassandra.db.marshal.AbstractType;
import org.apache.cassandra.db.marshal.TypeParser;
import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.cassandra.exceptions.SyntaxException;
+import org.apache.cassandra.utils.ByteBufferUtil;
+
+import java.nio.ByteBuffer;
public class TypeHelper {
- public static Object getTypedIfPossible(IntraState state, String type, ByteBuffer bb, IntraOp op){
-
- IntraMetaData imd = new IntraMetaData(IntraService.determineKs(null ,op, state),IntraService.determineCf(null, op, state),type);
+ public static Object getTypedIfPossible(IntraState state, String type, ByteBuffer bb, IntraOp op) {
+
+ IntraMetaData imd = new IntraMetaData(IntraService.determineKs(null, op, state), IntraService.determineCf(null, op, state), type);
String s = state.meta.get(imd);
- if (s == null) {
- return bb;
- }
- return compose(bb, s);
+ if (s == null) {
+ return bb;
+ }
+ return compose(bb, s);
}
- public static Object getTyped(String type, ByteBuffer bb) {
- return compose(bb, type);
- }
+ public static Object getTyped(String type, ByteBuffer bb) {
+ return compose(bb, type);
+ }
- public static Object getCqlTyped(String type, ByteBuffer bb) {
- if (bb == null) {
- return null;
- }
- return compose(bb, type);
+ public static Object getCqlTyped(String type, ByteBuffer bb) {
+ if (bb == null) {
+ return null;
}
+ return compose(bb, type);
+ }
- private static Object compose(ByteBuffer bb, String s) {
- try {
- AbstractType<?> abstractType = TypeParser.parse(s);
- return abstractType.compose(bb);
- } catch (SyntaxException | ConfigurationException e) {
- throw new RuntimeException("Failed to parse type [" + s + "]", e);
- }
+ private static Object compose(ByteBuffer bb, String s) {
+ try {
+ AbstractType<?> abstractType = TypeParser.parse(s);
+ return abstractType.compose(bb);
+ } catch (SyntaxException | ConfigurationException e) {
+ throw new RuntimeException("Failed to parse type [" + s + "]", e);
}
+ }
+
+ public static byte[] getBytes(ByteBuffer buffer) {
+ // TODO figure out more efficient way to pull bytes out of buffer
+ return ByteBufferUtil.getArray(buffer);
+ }
+
}
@@ -0,0 +1,16 @@
+package org.usergrid.vx.experimental.multiprocessor;
+
+import org.usergrid.vx.experimental.multiprocessor.groovy.GroovyMultiProcessorFactory;
+
+public class FactoryProvider {
+
+ public MultiProcessorFactory getFilterFactory(String spec) {
+ switch (spec) {
+ case "groovyclassloader":
+ return new GroovyMultiProcessorFactory();
+ default:
+ throw new IllegalArgumentException(spec + " is not yet supported for filters");
+ }
+ }
+
+}
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.usergrid.vx.experimental;
+package org.usergrid.vx.experimental.multiprocessor;
import java.util.List;
import java.util.Map;
@@ -0,0 +1,6 @@
+package org.usergrid.vx.experimental.multiprocessor;
+
+
+public interface MultiProcessorFactory {
+ public MultiProcessor createMultiProcessor(String script);
+}
@@ -0,0 +1,23 @@
+package org.usergrid.vx.experimental.multiprocessor.groovy;
+
+import groovy.lang.Closure;
+
+import java.util.List;
+import java.util.Map;
+
+import org.usergrid.vx.experimental.multiprocessor.MultiProcessor;
+
+public class GroovyMultiProcessor implements MultiProcessor {
+
+ private Closure closure;
+
+ public GroovyMultiProcessor(Closure closure) {
+ this.closure = closure;
+ }
+
+ @Override
+ public List<Map> multiProcess(Map<Integer, Object> results, Map params) {
+ return (List<Map>) closure.call(results, params);
+ }
+
+}
@@ -0,0 +1,26 @@
+package org.usergrid.vx.experimental.multiprocessor.groovy;
+
+import org.usergrid.vx.experimental.multiprocessor.MultiProcessor;
+import org.usergrid.vx.experimental.multiprocessor.MultiProcessorFactory;
+
+import groovy.lang.Closure;
+import groovy.lang.GroovyShell;
+
+public class GroovyMultiProcessorFactory implements MultiProcessorFactory {
+
+ @Override
+ public MultiProcessor createMultiProcessor(String script) {
+ GroovyShell shell = new GroovyShell();
+ Object result = shell.evaluate(script);
+ if (result instanceof MultiProcessor) {
+ return (MultiProcessor) result;
+ } else if (result instanceof Closure) {
+ return new GroovyMultiProcessor((Closure) result);
+ } else {
+ throw new RuntimeException(
+ "Cannot create processor. Script must return either a closure or an instace " + "of "
+ + MultiProcessor.class.getName());
+ }
+ }
+
+}
@@ -0,0 +1,19 @@
+package org.usergrid.vx.experimental.processor;
+
+import org.usergrid.vx.experimental.processor.groovy.GroovyClProcessorFactory;
+import org.usergrid.vx.experimental.processor.groovy.GroovyProcessorFactory;
+
+public class FactoryProvider {
+
+ public ProcessorFactory getFilterFactory(String spec) {
+ switch (spec) {
+ case "groovyscript":
+ return new GroovyProcessorFactory();
+ case "groovyclassloader":
+ return new GroovyClProcessorFactory();
+ default:
+ throw new IllegalArgumentException(spec + " is not yet supported for filters");
+ }
+ }
+
+}
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.usergrid.vx.experimental;
+package org.usergrid.vx.experimental.processor;
import java.util.List;
import java.util.Map;
@@ -0,0 +1,5 @@
+package org.usergrid.vx.experimental.processor;
+
+public interface ProcessorFactory {
+ public Processor createProcessor(String script);
+}
@@ -0,0 +1,23 @@
+package org.usergrid.vx.experimental.processor.groovy;
+
+import groovy.lang.GroovyClassLoader;
+
+import org.usergrid.vx.experimental.processor.Processor;
+import org.usergrid.vx.experimental.processor.ProcessorFactory;
+
+public class GroovyClProcessorFactory implements ProcessorFactory {
+
+ @Override
+ public Processor createProcessor(String script) {
+ GroovyClassLoader gc = new GroovyClassLoader();
+ Class<?> c = gc.parseClass( script) ;
+ Processor p = null;
+ try {
+ p = (Processor) c.newInstance();
+ } catch (InstantiationException | IllegalAccessException e) {
+ throw new RuntimeException (e);
+ }
+ return p;
+ }
+
+}
@@ -0,0 +1,23 @@
+package org.usergrid.vx.experimental.processor.groovy;
+
+import groovy.lang.Closure;
+
+import java.util.List;
+import java.util.Map;
+
+import org.usergrid.vx.experimental.processor.Processor;
+
+public class GroovyProcessor implements Processor {
+
+ private Closure closure;
+
+ public GroovyProcessor (Closure closure){
+ this.closure=closure;
+ }
+
+ @Override
+ public List<Map> process(List<Map> input) {
+ return (List<Map>) closure.call(input);
+ }
+
+}
@@ -0,0 +1,27 @@
+package org.usergrid.vx.experimental.processor.groovy;
+
+import org.usergrid.vx.experimental.filter.Filter;
+import org.usergrid.vx.experimental.filter.FilterFactory;
+import org.usergrid.vx.experimental.processor.Processor;
+import org.usergrid.vx.experimental.processor.ProcessorFactory;
+
+import groovy.lang.Closure;
+import groovy.lang.GroovyShell;
+
+public class GroovyProcessorFactory implements ProcessorFactory {
+
+ public Processor createProcessor(String script) {
+ GroovyShell shell = new GroovyShell();
+ Object result = shell.evaluate(script);
+ if (result instanceof Processor) {
+ return (Processor) result;
+ } else if (result instanceof Closure) {
+ return new GroovyProcessor((Closure) result);
+ } else {
+ throw new RuntimeException(
+ "Cannot create processor. Script must return either a closure or an instace " + "of "
+ + Processor.class.getName());
+ }
+ }
+
+}
Oops, something went wrong.

0 comments on commit 04481d2

Please sign in to comment.