private void invokeCommittedFunction(String transactionId, String transactionBlockId) { BFunctionPointer fp = committedFuncRegistry.get(transactionBlockId); BValue[] args = {new BString(transactionId + ":" + transactionBlockId)}; if (fp != null) { BVMExecutor.executeFunction(fp.value().getPackageInfo().getProgramFile(), fp.value(), args); } }
/** * Invokes a callable function pointer. * * @param functionPointer function pointer * @return return values */ protected BValue[] invokeFunction(BFunctionPointer functionPointer) { List<BValue> lambdaFunctionArgs = new ArrayList<>(); for (BClosure closure : functionPointer.getClosureVars()) { lambdaFunctionArgs.add(closure.value()); } return BVMExecutor.executeFunction(functionPointer.value().getPackageInfo().getProgramFile(), functionPointer.value(), lambdaFunctionArgs.toArray(new BValue[0])); }
@Override public BValue copy(Map<BValue, BValue> refs) { return new BFunctionPointer(functionInfo); } }
functionInfo = ((BFunctionPointer) sf.refRegs[i]).value(); strand = invokeCallable(strand, (BFunctionPointer) sf.refRegs[i], funcCallCPEntry, functionInfo, sf, funcCallCPEntry.getFlags()); funcRefCPEntry = (FunctionRefCPEntry) sf.constPool[i]; typeEntry = (TypeRefCPEntry) sf.constPool[k]; BFunctionPointer functionPointer = new BFunctionPointer(funcRefCPEntry.getFunctionInfo(), typeEntry.getType()); sf.refRegs[j] = functionPointer; .get(funcRefCPEntry.getFunctionInfo().getName()); BFunctionPointer fPointer = new BFunctionPointer(attachedFuncInfo, typeEntry.getType()); sf.refRegs[j] = fPointer; findAndAddAdditionalVarRegIndexes(sf, operands, fPointer);
StreamSubscriber(String queueName, BFunctionPointer functionPointer) { this.queueName = queueName; this.functionPointer = functionPointer; for (BClosure closure : functionPointer.getClosureVars()) { closureArgs.add(closure.value()); } }
private static int expandLongRegs(StackFrame sf, BFunctionPointer fp) { int longIndex = 0; if (fp.getAdditionalIndexCount(BTypes.typeInt.getTag()) > 0) { if (sf.longRegs == null) { sf.longRegs = new long[0]; } long[] newLongRegs = new long[sf.longRegs.length + fp.getAdditionalIndexCount(BTypes.typeInt.getTag())]; System.arraycopy(sf.longRegs, 0, newLongRegs, 0, sf.longRegs.length); longIndex = sf.longRegs.length; sf.longRegs = newLongRegs; } return longIndex; }
switch (type) { case TypeTags.INT_TAG: { fp.addClosureVar(new BClosure(new BInteger(sf.longRegs[index]), BTypes.typeInt), TypeTags.INT_TAG); break; fp.addClosureVar(new BClosure(new BByte((byte) sf.intRegs[index]), BTypes.typeByte), TypeTags.BYTE_TAG); break; fp.addClosureVar(new BClosure(new BFloat(sf.doubleRegs[index]), BTypes.typeFloat), TypeTags.FLOAT_TAG); break; fp.addClosureVar(new BClosure(sf.refRegs[index], BTypes.typeDecimal), TypeTags.DECIMAL_TAG); break; fp.addClosureVar(new BClosure(new BBoolean(sf.intRegs[index] == 1), BTypes.typeBoolean), TypeTags.BOOLEAN_TAG); break; fp.addClosureVar(new BClosure(new BString(sf.stringRegs[index]), BTypes.typeString), TypeTags.STRING_TAG); break; fp.addClosureVar(new BClosure(sf.refRegs[index], BTypes.typeAny), TypeTags.ANY_TAG);
private static Strand invokeCallable(Strand ctx, BFunctionPointer fp, FunctionCallCPEntry funcCallCPEntry, FunctionInfo functionInfo, StackFrame sf, int flags) { List<BClosure> closureVars = fp.getClosureVars(); int[] argRegs = funcCallCPEntry.getArgRegs(); if (closureVars.isEmpty()) {
private static int expandDoubleRegs(StackFrame sf, BFunctionPointer fp) { int doubleIndex = 0; if (fp.getAdditionalIndexCount(BTypes.typeFloat.getTag()) > 0) { if (sf.doubleRegs == null) { sf.doubleRegs = new double[0]; } double[] newDoubleRegs = new double[sf.doubleRegs.length + fp.getAdditionalIndexCount(BTypes.typeFloat.getTag())]; System.arraycopy(sf.doubleRegs, 0, newDoubleRegs, 0, sf.doubleRegs.length); doubleIndex = sf.doubleRegs.length; sf.doubleRegs = newDoubleRegs; } return doubleIndex; }
/** * Method to register a subscription to the underlying topic representing the stream in the broker. * * @param functionPointer represents the function pointer reference for the function to be invoked on receiving * messages */ public void subscribe(BFunctionPointer functionPointer) { BType[] parameters = functionPointer.value().getParamTypes(); int lastArrayIndex = parameters.length - 1; if (!BVM.isAssignable(constraintType, parameters[lastArrayIndex], new ArrayList<>())) { throw new BallerinaException("incompatible function: subscription function needs to be a function" + " accepting:" + this.constraintType); } String queueName = String.valueOf(System.currentTimeMillis()) + UUID.randomUUID().toString(); brokerInstance.addSubscription(topicName, new StreamSubscriber(queueName, functionPointer)); }
public void addCallback(String streamId, BFunctionPointer functionPointer, SiddhiAppRuntime siddhiAppRuntime) { BType[] parameters = functionPointer.value().getParamTypes(); for (BClosure closure : functionPointer.getClosureVars()) { closureArgs.add(closure.value());
private static int expandRefRegs(StackFrame sf, BFunctionPointer fp) { int refIndex = 0; if (fp.getAdditionalIndexCount(BTypes.typeAny.getTag()) > 0) { if (sf.refRegs == null) { sf.refRegs = new BRefType[0]; } BRefType<?>[] newRefRegs = new BRefType[sf.refRegs.length + fp.getAdditionalIndexCount(BTypes.typeAny.getTag())]; System.arraycopy(sf.refRegs, 0, newRefRegs, 0, sf.refRegs.length); refIndex = sf.refRegs.length; sf.refRegs = newRefRegs; } return refIndex; }
private static void beginRemoteParticipant(Strand strand, int transactionBlockIdIndex, int committedFuncIndex, int abortedFuncIndex) { TransactionLocalContext localTransactionContext = strand.getLocalTransactionContext(); if (localTransactionContext == null) { // No transaction available to participate, // We have no business here. This is a no-op. return; } // Register committed function handler if exists. BFunctionPointer fpCommitted = null; if (committedFuncIndex != -1) { FunctionRefCPEntry funcRefCPEntry = (FunctionRefCPEntry) strand.currentFrame.constPool[committedFuncIndex]; fpCommitted = new BFunctionPointer(funcRefCPEntry.getFunctionInfo()); } // Register aborted function handler if exists. BFunctionPointer fpAborted = null; if (abortedFuncIndex != -1) { FunctionRefCPEntry funcRefCPEntry = (FunctionRefCPEntry) strand.currentFrame.constPool[abortedFuncIndex]; fpAborted = new BFunctionPointer(funcRefCPEntry.getFunctionInfo()); } String transactionBlockId = getTrxBlockIdFromCP(strand, transactionBlockIdIndex); localTransactionContext.setResourceParticipant(true); String globalTransactionId = localTransactionContext.getGlobalTransactionId(); localTransactionContext.beginTransactionBlock(transactionBlockId, -1); TransactionResourceManager.getInstance() .registerParticipation(globalTransactionId, transactionBlockId, fpCommitted, fpAborted, strand); strand.currentFrame.trxParticipant = StackFrame.TransactionParticipantType.REMOTE_PARTICIPANT; }
@Override protected void send(Message message) { try { BValue data = ((BallerinaBrokerByteBuf) (message.getContentChunks().get(0).getByteBuf()).unwrap()).getValue(); List<BValue> argsList = new ArrayList<>(closureArgs); argsList.add(data); BVMExecutor.executeFunction(functionPointer.value().getPackageInfo().getProgramFile(), functionPointer.value(), argsList.toArray(new BValue[0])); } catch (Exception e) { throw new BallerinaException("Error delivering event to subscriber: ", e); } }
private static int expandStringRegs(StackFrame sf, BFunctionPointer fp) { int stringIndex = 0; if (fp.getAdditionalIndexCount(BTypes.typeString.getTag()) > 0) { if (sf.stringRegs == null) { sf.stringRegs = new String[0]; } String[] newStringRegs = new String[sf.stringRegs.length + fp.getAdditionalIndexCount(BTypes.typeString.getTag())]; System.arraycopy(sf.stringRegs, 0, newStringRegs, 0, sf.stringRegs.length); stringIndex = sf.stringRegs.length; sf.stringRegs = newStringRegs; } return stringIndex; }
private static void beginTransactionLocalParticipant(Strand strand, int transactionBlockIdCpIndex, int committedFuncIndex, int abortedFuncIndex) { TransactionLocalContext transactionLocalContext = strand.getLocalTransactionContext(); if (transactionLocalContext == null) { // No transaction available to participate, // We have no business here. This is a no-op. return; } String transactionBlockId = getTrxBlockIdFromCP(strand, transactionBlockIdCpIndex); // Register committed function handler if exists. TransactionResourceManager transactionResourceManager = TransactionResourceManager.getInstance(); BFunctionPointer fpCommitted = null; if (committedFuncIndex != -1) { FunctionRefCPEntry funcRefCPEntry = (FunctionRefCPEntry) strand.currentFrame.constPool[committedFuncIndex]; fpCommitted = new BFunctionPointer(funcRefCPEntry.getFunctionInfo()); transactionResourceManager.registerCommittedFunction(transactionBlockId, fpCommitted); } // Register aborted function handler if exists. BFunctionPointer fpAborted = null; if (abortedFuncIndex != -1) { FunctionRefCPEntry funcRefCPEntry = (FunctionRefCPEntry) strand.currentFrame.constPool[abortedFuncIndex]; fpAborted = new BFunctionPointer(funcRefCPEntry.getFunctionInfo()); transactionResourceManager.registerAbortedFunction(transactionBlockId, fpAborted); } transactionLocalContext.beginTransactionBlock(transactionBlockId, 1); transactionResourceManager.registerParticipation(transactionLocalContext.getGlobalTransactionId(), transactionBlockId, fpCommitted, fpAborted, strand); // this call frame is a transaction participant. strand.currentFrame.trxParticipant = StackFrame.TransactionParticipantType.LOCAL_PARTICIPANT; }
@Override public void receive(Event[] events) { BValueArray outputArray = new BValueArray(new BMapType(structType)); int j = 0; for (Event event : events) { // Here it is assumed that an event data will contain all the fields // of the record. Otherwise, some fields will be missing from the record value. BMap<String, BValue> output = new BMap<String, BValue>(structType); Iterator<String> fieldNamesIterator = structType.getFields().keySet().iterator(); for (Object field : event.getData()) { if (field instanceof Long || field instanceof Integer) { output.put(fieldNamesIterator.next(), new BInteger(((Number) field).longValue())); } else if (field instanceof Double || field instanceof Float) { output.put(fieldNamesIterator.next(), new BFloat(((Number) field).doubleValue())); } else if (field instanceof Boolean) { output.put(fieldNamesIterator.next(), new BBoolean(((Boolean) field))); } else if (field instanceof String) { output.put(fieldNamesIterator.next(), new BString((String) field)); } } outputArray.add(j, output); j++; } List<BValue> argsList = new ArrayList<>(); argsList.addAll(closureArgs); argsList.add(outputArray); BVMExecutor.executeFunction(functionPointer.value().getPackageInfo().getProgramFile(), functionPointer.value(), argsList.toArray(new BValue[0])); } });
private static int expandIntRegs(StackFrame sf, BFunctionPointer fp) { int intIndex = 0; if (fp.getAdditionalIndexCount(BTypes.typeBoolean.getTag()) > 0 || fp.getAdditionalIndexCount(BTypes.typeByte.getTag()) > 0) { if (sf.intRegs == null) { sf.intRegs = new int[0]; } int[] newIntRegs = new int[sf.intRegs.length + fp.getAdditionalIndexCount(BTypes.typeBoolean.getTag()) + fp.getAdditionalIndexCount(BTypes.typeByte.getTag())]; System.arraycopy(sf.intRegs, 0, newIntRegs, 0, sf.intRegs.length); intIndex = sf.intRegs.length; sf.intRegs = newIntRegs; } return intIndex; }
for (int i = 0; i < functionPointerArray.size(); i++) { BFunctionPointer functionPointer = (BFunctionPointer) functionPointerArray.getRefValue(i); String functionName = functionPointer.value().getName(); String streamId = "stream" + functionName.replaceAll("\\$", "_"); StreamingRuntimeManager.getInstance().addCallback(streamId, functionPointer, siddhiAppRuntime);
BType functionInputType = lambdaFunction.value().getParamTypes()[0]; if (functionInputType != this.constraintType) { throw new BallerinaException("incompatible types: function with record type:" BValue[] returns = BVMExecutor.executeFunction(lambdaFunction.value().getPackageInfo() .getProgramFile(), lambdaFunction.value(), args); if (((BBoolean) returns[0]).booleanValue()) { ++deletedCount;