use of io.airlift.bytecode.FieldDefinition in project hetu-core by openlookeng.
the class PageFunctionCompiler method definePageProjectWorkClass.
private ClassDefinition definePageProjectWorkClass(RowExpression projection, CallSiteBinder callSiteBinder, Optional<String> classNameSuffix) {
ClassDefinition classDefinition = new ClassDefinition(a(PUBLIC, FINAL), generateProjectionWorkClassName(classNameSuffix), type(Object.class), type(Work.class));
FieldDefinition blockBuilderField = classDefinition.declareField(a(PRIVATE), "blockBuilder", BlockBuilder.class);
FieldDefinition sessionField = classDefinition.declareField(a(PRIVATE), "session", ConnectorSession.class);
FieldDefinition pageField = classDefinition.declareField(a(PRIVATE), "page", Page.class);
FieldDefinition selectedPositionsField = classDefinition.declareField(a(PRIVATE), "selectedPositions", SelectedPositions.class);
FieldDefinition nextIndexOrPositionField = classDefinition.declareField(a(PRIVATE), "nextIndexOrPosition", int.class);
FieldDefinition resultField = classDefinition.declareField(a(PRIVATE), "result", Block.class);
CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(classDefinition, callSiteBinder);
// process
generateProcessMethod(classDefinition, blockBuilderField, sessionField, pageField, selectedPositionsField, nextIndexOrPositionField, resultField);
// getResult
MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "getResult", type(Object.class), ImmutableList.of());
method.getBody().append(method.getThis().getField(resultField)).ret(Object.class);
// evaluate
Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap = generateMethodsForLambda(classDefinition, callSiteBinder, cachedInstanceBinder, projection);
generateEvaluateMethod(classDefinition, callSiteBinder, cachedInstanceBinder, compiledLambdaMap, projection, blockBuilderField);
// constructor
Parameter blockBuilder = arg("blockBuilder", BlockBuilder.class);
Parameter session = arg("session", ConnectorSession.class);
Parameter page = arg("page", Page.class);
Parameter selectedPositions = arg("selectedPositions", SelectedPositions.class);
MethodDefinition constructorDefinition = classDefinition.declareConstructor(a(PUBLIC), blockBuilder, session, page, selectedPositions);
BytecodeBlock body = constructorDefinition.getBody();
Variable thisVariable = constructorDefinition.getThis();
body.comment("super();").append(thisVariable).invokeConstructor(Object.class).append(thisVariable.setField(blockBuilderField, blockBuilder)).append(thisVariable.setField(sessionField, session)).append(thisVariable.setField(pageField, page)).append(thisVariable.setField(selectedPositionsField, selectedPositions)).append(thisVariable.setField(nextIndexOrPositionField, selectedPositions.invoke("getOffset", int.class))).append(thisVariable.setField(resultField, constantNull(Block.class)));
cachedInstanceBinder.generateInitializations(thisVariable, body);
body.ret();
return classDefinition;
}
use of io.airlift.bytecode.FieldDefinition in project hetu-core by openlookeng.
the class StateCompiler method generateSingleStateClass.
private static <T> Class<? extends T> generateSingleStateClass(Class<T> clazz, Map<String, Type> fieldTypes, DynamicClassLoader classLoader) {
ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName("Single" + clazz.getSimpleName()), type(Object.class), type(clazz), type(Restorable.class));
FieldDefinition instanceSize = generateInstanceSize(definition);
// Add getter for class size
definition.declareMethod(a(PUBLIC), "getEstimatedSize", type(long.class)).getBody().getStaticField(instanceSize).retLong();
// Generate constructor
MethodDefinition constructor = definition.declareConstructor(a(PUBLIC));
constructor.getBody().append(constructor.getThis()).invokeConstructor(Object.class);
// Generate fields
List<StateField> fields = enumerateFields(clazz, fieldTypes);
for (StateField field : fields) {
generateField(definition, constructor, field);
}
constructor.getBody().ret();
Parameter captureSerdeProvider = arg("serdeProvider", BlockEncodingSerdeProvider.class);
MethodDefinition capture = definition.declareMethod(a(PUBLIC), "capture", type(Object.class), captureSerdeProvider);
Variable myState = capture.getScope().declareVariable(List.class, "myState");
capture.getBody().append(myState.set(newInstance(ArrayList.class)));
Variable captureBlockEncodingSerde = capture.getScope().declareVariable(BlockEncodingSerde.class, "captureBlockEncodingSerde");
capture.getBody().append(captureBlockEncodingSerde.set(captureSerdeProvider.invoke("getBlockEncodingSerde", BlockEncodingSerde.class)));
Variable obj = capture.getScope().declareVariable(Object.class, "obj");
for (int i = 1; i < definition.getFields().size(); i++) {
capture.getBody().append(obj.set(invokeStatic(SnapshotUtils.class, "captureHelper", Object.class, capture.getThis().getField(definition.getFields().get(i)).cast(Object.class), captureSerdeProvider)));
capture.getBody().append(myState.invoke("add", boolean.class, obj));
}
capture.getBody().append(myState.ret());
Parameter state = arg("state", Object.class);
Parameter restoreSerdeProvider = arg("serdeProvider", BlockEncodingSerdeProvider.class);
MethodDefinition restore = definition.declareMethod(a(PUBLIC), "restore", type(void.class), state, restoreSerdeProvider);
Variable restoreBlockEncodingSerde = restore.getScope().declareVariable(BlockEncodingSerde.class, "restoreBlockEncodingSerde");
restore.getBody().append(restoreBlockEncodingSerde.set(restoreSerdeProvider.invoke("getBlockEncodingSerde", BlockEncodingSerde.class)));
Variable restoreState = restore.getScope().declareVariable(List.class, "restoreState");
restore.getBody().append(restoreState.set(state.cast(List.class)));
Variable value = restore.getScope().declareVariable(Object.class, "value");
for (int i = 0; i < fields.size(); i++) {
restore.getBody().append(value.set(restoreState.invoke("get", Object.class, constantInt(i))));
restore.getBody().append(value.set(invokeStatic(SnapshotUtils.class, "restoreHelper", Object.class, value, constantClass(fields.get(i).getType()), restoreSerdeProvider)));
restore.getBody().append(restore.getThis().invoke(fields.get(i).getSetterName(), void.class, value.cast(fields.get(i).getType())));
}
restore.getBody().ret();
return defineClass(definition, clazz, classLoader);
}
use of io.airlift.bytecode.FieldDefinition in project hetu-core by openlookeng.
the class StateCompiler method generateField.
private static void generateField(ClassDefinition definition, MethodDefinition constructor, StateField stateField) {
FieldDefinition field = definition.declareField(a(PRIVATE), UPPER_CAMEL.to(LOWER_CAMEL, stateField.getName()) + "Value", stateField.getType());
// Generate getter
MethodDefinition getter = definition.declareMethod(a(PUBLIC), stateField.getGetterName(), type(stateField.getType()));
getter.getBody().append(getter.getThis().getField(field).ret());
// Generate setter
Parameter value = arg("value", stateField.getType());
MethodDefinition setter = definition.declareMethod(a(PUBLIC), stateField.getSetterName(), type(void.class), value);
setter.getBody().append(setter.getThis().setField(field, value)).ret();
constructor.getBody().append(constructor.getThis().setField(field, stateField.initialValueExpression()));
}
use of io.airlift.bytecode.FieldDefinition in project trino by trinodb.
the class JoinCompiler method generateInstanceSize.
private static FieldDefinition generateInstanceSize(ClassDefinition definition) {
// Store instance size in static field
FieldDefinition instanceSize = definition.declareField(a(PRIVATE, STATIC, FINAL), "INSTANCE_SIZE", long.class);
definition.getClassInitializer().getBody().comment("INSTANCE_SIZE = ClassLayout.parseClass(%s.class).instanceSize()", definition.getName()).push(definition.getType()).invokeStatic(ClassLayout.class, "parseClass", ClassLayout.class, Class.class).invokeVirtual(ClassLayout.class, "instanceSize", int.class).intToLong().putStaticField(instanceSize);
return instanceSize;
}
use of io.airlift.bytecode.FieldDefinition in project trino by trinodb.
the class JoinCompiler method generateIsPositionNull.
private static void generateIsPositionNull(ClassDefinition classDefinition, List<FieldDefinition> joinChannelFields) {
Parameter blockIndex = arg("blockIndex", int.class);
Parameter blockPosition = arg("blockPosition", int.class);
MethodDefinition isPositionNullMethod = classDefinition.declareMethod(a(PUBLIC), "isPositionNull", type(boolean.class), blockIndex, blockPosition);
for (FieldDefinition joinChannelField : joinChannelFields) {
BytecodeExpression block = isPositionNullMethod.getThis().getField(joinChannelField).invoke("get", Object.class, blockIndex).cast(Block.class);
IfStatement ifStatement = new IfStatement();
ifStatement.condition(block.invoke("isNull", boolean.class, blockPosition));
ifStatement.ifTrue(constantTrue().ret());
isPositionNullMethod.getBody().append(ifStatement);
}
isPositionNullMethod.getBody().append(constantFalse().ret());
}
Aggregations