use of com.sun.codemodel.JExpression in project drill by apache.
the class PartitionSenderRootExec method createClassInstances.
private List<Partitioner> createClassInstances(int actualPartitions) {
// set up partitioning function
final LogicalExpression expr = operator.getExpr();
final ErrorCollector collector = new ErrorCollectorImpl();
final ClassGenerator<Partitioner> cg;
cg = CodeGenerator.getRoot(Partitioner.TEMPLATE_DEFINITION, context.getOptions());
cg.getCodeGenerator().plainJavaCapable(true);
// Uncomment out this line to debug the generated code.
// cg.getCodeGenerator().saveCodeForDebugging(true);
ClassGenerator<Partitioner> cgInner = cg.getInnerGenerator("OutgoingRecordBatch");
final LogicalExpression materializedExpr = ExpressionTreeMaterializer.materialize(expr, incoming, collector, context.getFunctionRegistry());
collector.reportErrors(logger);
// generate code to copy from an incoming value vector to the destination partition's outgoing value vector
JExpression bucket = JExpr.direct("bucket");
// generate evaluate expression to determine the hash
ClassGenerator.HoldingContainer exprHolder = cg.addExpr(materializedExpr);
cg.getEvalBlock().decl(JType.parse(cg.getModel(), "int"), "bucket", exprHolder.getValue().mod(JExpr.lit(outGoingBatchCount)));
cg.getEvalBlock()._return(cg.getModel().ref(Math.class).staticInvoke("abs").arg(bucket));
CopyUtil.generateCopies(cgInner, incoming, incoming.getSchema().getSelectionVectorMode() == SelectionVectorMode.FOUR_BYTE);
// compile and setup generated code
List<Partitioner> subPartitioners = context.getImplementationClass(cg, actualPartitions);
final int divisor = Math.max(1, outGoingBatchCount / actualPartitions);
final int longTail = outGoingBatchCount % actualPartitions;
int startIndex = 0;
int endIndex = 0;
boolean success = false;
try {
for (int i = 0; i < actualPartitions; i++) {
startIndex = endIndex;
endIndex = (i < actualPartitions - 1) ? startIndex + divisor : outGoingBatchCount;
if (i < longTail) {
endIndex++;
}
final OperatorStats partitionStats = new OperatorStats(stats, true);
subPartitioners.get(i).setup(context, incoming, popConfig, partitionStats, oContext, cgInner, startIndex, endIndex);
}
partitioner = new PartitionerDecorator(subPartitioners, stats, context);
for (int index = 0; index < terminations.size(); index++) {
partitioner.getOutgoingBatches(terminations.buffer[index]).terminate();
}
terminations.clear();
success = true;
} catch (SchemaChangeException e) {
throw AbstractRecordBatch.schemaChangeException(e, "Partition Sender", logger);
} finally {
if (!success) {
for (Partitioner p : subPartitioners) {
p.clear();
}
}
}
return subPartitioners;
}
use of com.sun.codemodel.JExpression in project raml-module-builder by folio-org.
the class ClientGenerator method formatDateParameter.
private void formatDateParameter(JBlock b, ParameterDetails details) {
JExpression expr = jcodeModel.ref(java.time.format.DateTimeFormatter.class).staticRef("ISO_LOCAL_DATE_TIME").invoke("format").arg(jcodeModel.ref(java.time.ZonedDateTime.class).staticInvoke("ofInstant").arg(JExpr.ref(details.valueName).invoke("toInstant")).arg(jcodeModel.ref(java.time.ZoneId.class).staticInvoke("of").arg("UTC")));
b.invoke(details.queryParams, APPEND).arg(expr);
}
use of com.sun.codemodel.JExpression in project raml-module-builder by folio-org.
the class ClientGenerator method addConstructorOkapi6Args.
private void addConstructorOkapi6Args() {
JMethod constructor = constructor();
JVar okapiUrlVar = constructor.param(String.class, OKAPI_URL);
JVar tenantIdVar = constructor.param(String.class, TENANT_ID);
JVar tokenVar = constructor.param(String.class, TOKEN);
JVar keepAlive = constructor.param(boolean.class, KEEP_ALIVE);
JVar connTimeout = constructor.param(int.class, "connTO");
JVar idleTimeout = constructor.param(int.class, "idleTO");
/* populate constructor */
JBlock conBody = constructor.body();
conBody.assign(JExpr._this().ref(tenantId), tenantIdVar);
conBody.assign(JExpr._this().ref(token), tokenVar);
conBody.assign(JExpr._this().ref(okapiUrl), okapiUrlVar);
JVar options = conBody.decl(jcodeModel._ref(WebClientOptions.class), "options", JExpr._new(jcodeModel.ref(WebClientOptions.class)));
conBody.invoke(options, "setLogActivity").arg(JExpr.TRUE);
conBody.invoke(options, "setKeepAlive").arg(keepAlive);
conBody.invoke(options, "setConnectTimeout").arg(connTimeout);
conBody.invoke(options, "setIdleTimeout").arg(idleTimeout);
JExpression client = jcodeModel.ref("io.vertx.ext.web.client.WebClient").staticInvoke("create").arg(jcodeModel.ref("org.folio.rest.tools.utils.VertxUtils").staticInvoke("getVertxFromContextOrNew")).arg(options);
conBody.assign(webClient, client);
deprecate(constructor);
}
use of com.sun.codemodel.JExpression in project raml-module-builder by folio-org.
the class ClientGenerator method handleParams.
/**
* @param paramType
* @param valueType
*/
private boolean handleParams(JMethod method, JVar queryParams, String paramType, String valueType, String valueName) {
JBlock methodBody = method.body();
if (AnnotationGrabber.NON_ANNOTATED_PARAM.equals(paramType)) /*&& !FILE_UPLOAD_PARAM.equals(valueType)*/
{
try {
// this will also validate the json against the pojo created from the schema
Class<?> entityClazz = Class.forName(valueType);
if (!valueType.equals("io.vertx.core.Handler") && !valueType.equals("io.vertx.core.Context") && !valueType.equals("java.util.Map") && !valueType.equals("io.vertx.ext.web.RoutingContext")) {
/* this is a post or put since our only options here are receiving a reader (data in body) or
* entity - which is also data in body - but we can only have one since a multi part body
* should be indicated by a multipart objector input stream in the body */
JExpression jexpr = jcodeModel.ref(io.vertx.core.buffer.Buffer.class).staticInvoke("buffer");
JVar buffer = methodBody.decl(jcodeModel.ref(io.vertx.core.buffer.Buffer.class), "buffer", jexpr);
if ("java.io.Reader".equals(valueType)) {
JVar reader = method.param(Reader.class, "reader");
method._throws(Exception.class);
JConditional ifClause = methodBody._if(reader.ne(JExpr._null()));
ifClause._then().directStatement("buffer.appendString(org.apache.commons.io.IOUtils.toString(reader));");
} else if ("java.io.InputStream".equals(valueType)) {
JVar inputStream = method.param(InputStream.class, "inputStream");
JVar result = methodBody.decl(jcodeModel.ref(ByteArrayOutputStream.class), "result", JExpr._new(jcodeModel.ref(ByteArrayOutputStream.class)));
JVar byteA = methodBody.decl(jcodeModel.BYTE.array(), "buffer1", JExpr.newArray(jcodeModel.BYTE, 1024));
JVar length = methodBody.decl(jcodeModel.INT, "length");
// http://stackoverflow.com/questions/26037015/how-do-i-force-enclose-a-codemodel-expression-in-brackets
JWhileLoop whileClause = methodBody._while(JExpr.TRUE);
whileClause.body().assign(length, inputStream.invoke("read").arg(byteA));
whileClause.body()._if(length.eq(JExpr.lit(-1)))._then()._break();
whileClause.body().add(result.invoke("write").arg(byteA).arg(JExpr.lit(0)).arg(length));
methodBody.add(buffer.invoke("appendBytes").arg(result.invoke("toByteArray")));
method._throws(IOException.class);
} else if ("javax.mail.internet.MimeMultipart".equals(valueType)) {
JVar mimeMultiPart = method.param(MimeMultipart.class, "mimeMultipart");
method._throws(MessagingException.class);
method._throws(IOException.class);
JBlock b1 = methodBody._if(mimeMultiPart.ne(JExpr._null()))._then();
JVar parts = b1.decl(jcodeModel.INT, "parts", mimeMultiPart.invoke("getCount"));
JVar sb = b1.decl(jcodeModel._ref(StringBuilder.class), "sb", JExpr._new(jcodeModel.ref(StringBuilder.class)));
JForLoop forClause = b1._for();
JVar iVar = forClause.init(jcodeModel._ref(int.class), "i", JExpr.lit(0));
forClause.test(iVar.lt(parts));
forClause.update(iVar.incr());
JBlock fBody = forClause.body();
JVar bp = fBody.decl(jcodeModel.ref(javax.mail.BodyPart.class), "bp", mimeMultiPart.invoke("getBodyPart").arg(iVar));
fBody.add(sb.invoke(APPEND).arg("----BOUNDARY\r\n"));
fBody.add(sb.invoke(APPEND).arg("Content-Disposition: \""));
fBody.add(sb.invoke(APPEND).arg(bp.invoke("getDisposition")));
fBody.add(sb.invoke(APPEND).arg("\"; name=\""));
fBody.add(sb.invoke(APPEND).arg(bp.invoke("getFileName")));
fBody.add(sb.invoke(APPEND).arg("\"; filename=\")"));
fBody.add(sb.invoke(APPEND).arg(bp.invoke("getFileName")));
fBody.add(sb.invoke(APPEND).arg("\"\r\n"));
fBody.add(sb.invoke(APPEND).arg("Content-Type: application/octet-stream\r\n"));
fBody.add(sb.invoke(APPEND).arg("Content-Transfer-Encoding: binary\r\n"));
b1.add(sb.invoke(APPEND).arg("----BOUNDARY\r\n"));
b1.add(buffer.invoke("appendString").arg(sb.invoke("toString")));
} else {
String objParamName = entityClazz.getSimpleName();
JConditional ifClause = methodBody._if(JExpr.ref(objParamName).ne(JExpr._null()));
JBlock b = ifClause._then();
if (mappingType.equals("postgres")) {
b.directStatement("buffer.appendString(" + "org.folio.rest.tools.ClientHelpers.pojo2json(" + objParamName + "));");
} else {
b.directStatement("buffer.appendString(" + "org.folio.rest.tools.utils.JsonUtils.entity2Json(" + objParamName + ").encode());");
}
method.param(entityClazz, entityClazz.getSimpleName());
}
return true;
}
} catch (Exception e) {
log.log(Level.SEVERE, e.getMessage(), e);
}
} else if (AnnotationGrabber.PATH_PARAM.equals(paramType)) {
method.param(String.class, valueName);
} else if (AnnotationGrabber.HEADER_PARAM.equals(paramType)) {
method.param(String.class, valueName);
functionSpecificHeaderParams.add("request.putHeader(\"" + valueName + "\", " + valueName + ");");
} else if (AnnotationGrabber.QUERY_PARAM.equals(paramType)) {
// support date, enum, numbers or strings as query parameters
try {
if (valueType.contains("String")) {
method.param(String.class, valueName);
addParameter(new ParameterDetails(methodBody, queryParams, valueName).withOp(ParameterOp.ENCODE));
} else if (valueType.contains("Date")) {
method.param(Date.class, valueName);
addParameter(new ParameterDetails(methodBody, queryParams, valueName).withOp(ParameterOp.FORMAT_DATE));
} else if (valueType.contains("int")) {
method.param(int.class, valueName);
addParameter(new ParameterDetails(methodBody, queryParams, valueName).nullCheck(false));
} else if (valueType.contains("boolean")) {
method.param(boolean.class, valueName);
addParameter(new ParameterDetails(methodBody, queryParams, valueName).nullCheck(false));
} else if (valueType.contains("BigDecimal")) {
method.param(BigDecimal.class, valueName);
addParameter(new ParameterDetails(methodBody, queryParams, valueName));
} else if (valueType.contains("Number")) {
method.param(Number.class, valueName);
addParameter(new ParameterDetails(methodBody, queryParams, valueName));
} else if (valueType.contains("Integer")) {
method.param(Integer.class, valueName);
addParameter(new ParameterDetails(methodBody, queryParams, valueName));
} else if (valueType.contains("Boolean")) {
method.param(Boolean.class, valueName);
addParameter(new ParameterDetails(methodBody, queryParams, valueName));
} else if (valueType.contains("List")) {
method.param(String[].class, valueName);
addParameter(new ParameterDetails(methodBody, queryParams, valueName).withOp(ParameterOp.PROCESS_LIST));
} else {
// enum object type
Class<?> enumClazz = classForName(valueType);
if (enumClazz.isEnum()) {
method.param(enumClazz, valueName);
addParameter(new ParameterDetails(methodBody, queryParams, valueName));
}
}
} catch (Exception e) {
log.log(Level.SEVERE, e.getMessage(), e);
}
}
return false;
}
Aggregations